Skip to content

Commit

Permalink
Clang has limited constexpr support
Browse files Browse the repository at this point in the history
e.g. cannot use `static_cast` so FlashString needs some fudging to get it to compile.
Also clang doesn't support constexpr math (yet) so workaround that.
  • Loading branch information
mikee47 committed Apr 19, 2024
1 parent 66a1dc3 commit 758751a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions Sming/Core/NanoTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@

namespace NanoTime
{
constexpr uint64_t round_ce(double v)
{
#ifdef __clang__
return uint64_t(v + 0.5);
#else
return round(v);
#endif
}

/**
* @brief Identify units for a scalar quantity of time
* @note Ordered in increasing unit size, e.g. days > seconds
Expand Down Expand Up @@ -242,7 +251,7 @@ template <uint64_t time, Unit unitsFrom, Unit unitsTo,
typename R = std::ratio_divide<UnitTickRatio<unitsTo>, UnitTickRatio<unitsFrom>>>
constexpr uint64_t convert()
{
return ({ round(double(time) * R::num / R::den); });
return ({ round_ce(double(time) * R::num / R::den); });
}

/**
Expand Down Expand Up @@ -504,7 +513,7 @@ template <class Clock_, Unit unit_, uint64_t time_> struct TimeConst {
*/
static constexpr uint64_t ticks()
{
return round(double(time_) * TicksPerUnit::num / TicksPerUnit::den);
return round_ce(double(time_) * TicksPerUnit::num / TicksPerUnit::den);
}

/**
Expand All @@ -522,7 +531,7 @@ template <class Clock_, Unit unit_, uint64_t time_> struct TimeConst {
*/
static constexpr uint64_t clockTime()
{
return round(double(ticks()) * TicksPerUnit::den / TicksPerUnit::num);
return round_ce(double(ticks()) * TicksPerUnit::den / TicksPerUnit::num);
}

/**
Expand Down Expand Up @@ -583,8 +592,8 @@ template <class Clock_, uint64_t ticks_> struct TicksConst {

template <Unit unit>
using TimeConst = TimeConst<Clock, unit,
TimeType(round(double(ticks_) * Clock::template TicksPerUnit<unit>::den /
Clock::template TicksPerUnit<unit>::num))>;
TimeType(round_ce(double(ticks_) * Clock::template TicksPerUnit<unit>::den /
Clock::template TicksPerUnit<unit>::num))>;

/**
* @brief Get the time for the tick count in a specific time unit
Expand Down

0 comments on commit 758751a

Please sign in to comment.