Skip to content

Commit

Permalink
[cpp] Add support for _Float16 in Double<Real>
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Sep 28, 2024
1 parent a86c309 commit a6ff6a7
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/cpp/pffdtd/double.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace pffdtd {

/// https://github.com/sukop/doubledouble/blob/master/doubledouble.py
template<std::floating_point Real>
template<typename Real>
struct Double {
constexpr Double() = default;

Expand All @@ -19,12 +19,12 @@ struct Double {

[[nodiscard]] constexpr auto low() const noexcept -> Real { return _low; }

template<std::floating_point OtherReal>
template<typename OtherReal>
explicit constexpr operator OtherReal() const noexcept {
return static_cast<OtherReal>(high());
}

template<std::floating_point OtherReal>
template<typename OtherReal>
explicit constexpr operator Double<OtherReal>() const noexcept {
return Double<OtherReal>{
static_cast<OtherReal>(high()),
Expand Down Expand Up @@ -104,8 +104,16 @@ struct Double {

[[nodiscard]] static constexpr auto twoProduct(Real x, Real y) noexcept -> Double {
auto r = x * y;
auto e = std::fma(x, y, -r);
return Double{r, e};
#if defined(__APPLE__)
if constexpr (std::same_as<Real, _Float16>) {
auto e = static_cast<_Float16>(std::fma(float{x}, float{y}, float{-r}));
return Double{r, e};
} else
#endif
{
auto e = std::fma(x, y, -r);
return Double{r, e};
}
}

Real _high{static_cast<Real>(0)};
Expand All @@ -114,5 +122,5 @@ struct Double {

} // namespace pffdtd

template<std::floating_point Real>
template<typename Real>
struct std::numeric_limits<pffdtd::Double<Real>> : std::numeric_limits<Real> {};

0 comments on commit a6ff6a7

Please sign in to comment.