Skip to content

Commit

Permalink
[platform] Fix use of fabs in constexpr context
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Dec 19, 2020
1 parent 2a2d2e7 commit 6578b24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/modm/math/utils/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstddef>
#include <cmath>
#include <stdint.h>
#include <type_traits>

#include <modm/architecture/utils.hpp>

Expand Down Expand Up @@ -161,6 +162,20 @@ max(const T& a, const T& b, Compare compare)
return a;
}

/**
* @brief constexpr implementation of fabs
*/
template <typename Float>
requires std::is_floating_point_v<Float>
constexpr Float constexpr_fabs(Float number)
{
if (number >= 0) {
return number;
} else
return -number;
}
}

/// @}

} // namespace modm
Expand Down
3 changes: 2 additions & 1 deletion src/modm/platform/can/common/can_bit_timings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <modm/architecture/interface/clock.hpp>
#include <modm/math/units.hpp>
#include <modm/math/utils/misc.hpp>
#include <cmath>

namespace modm
Expand Down Expand Up @@ -77,7 +78,7 @@ class CanBitTiming
for(uint8_t bs1Bs2 = minBs1Bs2; bs1Bs2 <= maxBs1Bs2; ++bs1Bs2) {
float idealPrescaler = float(Clk) / (Bitrate * (1 + bs1Bs2));
uint32_t intPrescaler = round_uint32(idealPrescaler);
float error = fabs(1 - intPrescaler/idealPrescaler);
float error = constexpr_fabs(1 - intPrescaler/idealPrescaler);
if(error <= minError) {
bestPrescaler = intPrescaler;
minError = error;
Expand Down

0 comments on commit 6578b24

Please sign in to comment.