brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · ba61cc9 Raw
115 lines · plain
1//  (C) Copyright Matt Borland 2021 - 2022.2//  Use, modification and distribution are subject to the3//  Boost Software License, Version 1.0. (See accompanying file4//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_CCMATH_FMOD_HPP7#define BOOST_MATH_CCMATH_FMOD_HPP8 9#include <boost/math/ccmath/detail/config.hpp>10 11#ifdef BOOST_MATH_NO_CCMATH12#error "The header <boost/math/fmod.hpp> can only be used in C++17 and later."13#endif14 15#include <cstdint>16#include <boost/math/tools/promotion.hpp>17#include <boost/math/ccmath/abs.hpp>18#include <boost/math/ccmath/isinf.hpp>19#include <boost/math/ccmath/isnan.hpp>20#include <boost/math/ccmath/isfinite.hpp>21 22namespace boost::math::ccmath {23 24namespace detail {25 26template <typename T>27constexpr T fmod_impl(T x, T y)28{29    if (x == y)30    {31        return static_cast<T>(0);32    }33    else34    {35        while (x >= y)36        {37            x -= y;38        }39 40        return static_cast<T>(x);41    }42}43 44} // Namespace detail45 46template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>47constexpr Real fmod(Real x, Real y)48{49    if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))50    {51        if (boost::math::ccmath::abs(x) == static_cast<Real>(0) && y != static_cast<Real>(0))52        {53            return x;54        }55        else if (boost::math::ccmath::isinf(x) && !boost::math::ccmath::isnan(y))56        {57            return std::numeric_limits<Real>::quiet_NaN();58        }59        else if (boost::math::ccmath::abs(y) == static_cast<Real>(0) && !boost::math::ccmath::isnan(x))60        {61            return std::numeric_limits<Real>::quiet_NaN();62        }63        else if (boost::math::ccmath::isinf(y) && boost::math::ccmath::isfinite(x))64        {65            return x;66        }67        else if (boost::math::ccmath::isnan(x))68        {69            return x;70        }71        else if (boost::math::ccmath::isnan(y))72        {73            return y;74        }75 76        return boost::math::ccmath::detail::fmod_impl<Real>(x, y);77    }78    else79    {80        using std::fmod;81        return fmod(x, y);82    }83}84 85template <typename T1, typename T2>86constexpr auto fmod(T1 x, T2 y)87{88    if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))89    {90        using promoted_type = boost::math::tools::promote_args_t<T1, T2>;91        return boost::math::ccmath::fmod(promoted_type(x), promoted_type(y));92    }93    else94    {95        using std::fmod;96        return fmod(x, y);97    }98}99 100constexpr float fmodf(float x, float y)101{102    return boost::math::ccmath::fmod(x, y);103}104 105#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS106constexpr long double fmodl(long double x, long double y)107{108    return boost::math::ccmath::fmod(x, y);109}110#endif111 112} // Namespaces113 114#endif // BOOST_MATH_CCMATH_FMOD_HPP115