Skip to content

Commit

Permalink
Working on C++17 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Jan 7, 2024
1 parent 58d98dc commit 2618ca4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
24 changes: 24 additions & 0 deletions include/math_approx/src/basic_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,28 @@ xsimd::batch<T> select (xsimd::batch_bool<T> q, xsimd::batch<T> t, xsimd::batch<
return xsimd::select (q, t, f);
}
#endif

#if ! __cpp_lib_bit_cast
// bit_cast requirement.
template <typename From, typename To>
using is_bitwise_castable = std::integral_constant<bool,
(sizeof (From) == sizeof (To)) && std::is_trivially_copyable<From>::value && std::is_trivially_copyable<To>::value>;

// compiler support is needed for bitwise copy with constexpr.
template <typename To, typename From>
inline typename std::enable_if<is_bitwise_castable<From, To>::value, To>::type bit_cast (const From& from) noexcept
{
union U
{
U() {};
char storage[sizeof (To)] {};
typename std::remove_const<To>::type dest;
} u; // instead of To dest; because To doesn't require DefaultConstructible.
std::memcpy (&u.dest, &from, sizeof from);
return u.dest;
}
#else
using std::bit_cast;
#endif

} // namespace math_approx
8 changes: 4 additions & 4 deletions include/math_approx/src/log_approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ namespace log_detail
template <typename Base, int order, bool C1_continuous, typename Log2ProviderType = log_detail::Log2Provider>
constexpr float log (float x)
{
const auto vi = std::bit_cast<int32_t> (x);
const auto vi = bit_cast<int32_t> (x);
const auto ex = vi & 0x7f800000;
const auto e = (ex >> 23) - 127;
const auto vfi = (vi - ex) | 0x3f800000;
const auto vf = std::bit_cast<float> (vfi);
const auto vf = bit_cast<float> (vfi);

constexpr auto log2_base_r = 1.0f / Base::log2_base;
return log2_base_r * ((float) e + Log2ProviderType::template log2_approx<float, order, C1_continuous> (vf));
Expand All @@ -119,11 +119,11 @@ constexpr float log (float x)
template <typename Base, int order, bool C1_continuous, typename Log2ProviderType = log_detail::Log2Provider>
constexpr double log (double x)
{
const auto vi = std::bit_cast<int64_t> (x);
const auto vi = bit_cast<int64_t> (x);
const auto ex = vi & 0x7ff0000000000000;
const auto e = (ex >> 52) - 1023;
const auto vfi = (vi - ex) | 0x3ff0000000000000;
const auto vf = std::bit_cast<double> (vfi);
const auto vf = bit_cast<double> (vfi);

constexpr auto log2_base_r = 1.0 / Base::log2_base;
return log2_base_r * ((double) e + Log2ProviderType::template log2_approx<double, order, C1_continuous> (vf));
Expand Down
2 changes: 2 additions & 0 deletions include/math_approx/src/polylogarithm_approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ constexpr T li2 (T x)
return r + select (sign, li2_reduce, -li2_reduce);
}

#if defined(XSIMD_HPP)
/**
* Approximation of the "dilogarithm" function for all inputs.
*
Expand Down Expand Up @@ -218,4 +219,5 @@ xsimd::batch<T> li2 (const xsimd::batch<T>& x)
const auto li2_reduce = li2_0_half<order> (y);
return r + select (sign, li2_reduce, -li2_reduce);
}
#endif
} // namespace math_approx
4 changes: 2 additions & 2 deletions include/math_approx/src/pow_approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ constexpr float pow (float x)
const auto f = x - (float) l;
const auto vi = (l + 127) << 23;

return std::bit_cast<float> (vi) * pow_detail::pow2_approx<float, order, C1_continuous> (f);
return bit_cast<float> (vi) * pow_detail::pow2_approx<float, order, C1_continuous> (f);
}

/** approximation for pow(Base, x) (64-bit) */
Expand All @@ -162,7 +162,7 @@ constexpr double pow (double x)
const auto d = x - (double) l;
const auto vi = (l + 1023) << 52;

return std::bit_cast<double> (vi) * pow_detail::pow2_approx<double, order, C1_continuous> (d);
return bit_cast<double> (vi) * pow_detail::pow2_approx<double, order, C1_continuous> (d);
}

#if defined(XSIMD_HPP)
Expand Down

0 comments on commit 2618ca4

Please sign in to comment.