next.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // (C) Copyright John Maddock 2008 - 2022.
  2. // (C) Copyright Matt Borland 2022.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_CCMATH_NEXT_HPP
  7. #define BOOST_MATH_CCMATH_NEXT_HPP
  8. #include <boost/math/ccmath/detail/config.hpp>
  9. #ifdef BOOST_MATH_NO_CCMATH
  10. #error "The header <boost/math/next.hpp> can only be used in C++17 and later."
  11. #endif
  12. #include <stdexcept>
  13. #include <cfloat>
  14. #include <cstdint>
  15. #include <boost/math/policies/policy.hpp>
  16. #include <boost/math/policies/error_handling.hpp>
  17. #include <boost/math/tools/assert.hpp>
  18. #include <boost/math/tools/config.hpp>
  19. #include <boost/math/tools/precision.hpp>
  20. #include <boost/math/tools/traits.hpp>
  21. #include <boost/math/tools/promotion.hpp>
  22. #include <boost/math/ccmath/ilogb.hpp>
  23. #include <boost/math/ccmath/ldexp.hpp>
  24. #include <boost/math/ccmath/scalbln.hpp>
  25. #include <boost/math/ccmath/round.hpp>
  26. #include <boost/math/ccmath/fabs.hpp>
  27. #include <boost/math/ccmath/fpclassify.hpp>
  28. #include <boost/math/ccmath/isfinite.hpp>
  29. #include <boost/math/ccmath/fmod.hpp>
  30. namespace boost::math::ccmath {
  31. namespace detail {
  32. // Forward Declarations
  33. template <typename T, typename result_type = tools::promote_args_t<T>>
  34. constexpr result_type float_prior(const T& val);
  35. template <typename T, typename result_type = tools::promote_args_t<T>>
  36. constexpr result_type float_next(const T& val);
  37. template <typename T>
  38. struct has_hidden_guard_digits;
  39. template <>
  40. struct has_hidden_guard_digits<float> : public std::false_type {};
  41. template <>
  42. struct has_hidden_guard_digits<double> : public std::false_type {};
  43. template <>
  44. struct has_hidden_guard_digits<long double> : public std::false_type {};
  45. #ifdef BOOST_HAS_FLOAT128
  46. template <>
  47. struct has_hidden_guard_digits<__float128> : public std::false_type {};
  48. #endif
  49. template <typename T, bool b>
  50. struct has_hidden_guard_digits_10 : public std::false_type {};
  51. template <typename T>
  52. struct has_hidden_guard_digits_10<T, true> : public std::integral_constant<bool, (std::numeric_limits<T>::digits10 != std::numeric_limits<T>::max_digits10)> {};
  53. template <typename T>
  54. struct has_hidden_guard_digits
  55. : public has_hidden_guard_digits_10<T,
  56. std::numeric_limits<T>::is_specialized
  57. && (std::numeric_limits<T>::radix == 10) >
  58. {};
  59. template <typename T>
  60. constexpr T normalize_value(const T& val, const std::false_type&) { return val; }
  61. template <typename T>
  62. constexpr T normalize_value(const T& val, const std::true_type&)
  63. {
  64. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  65. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  66. std::intmax_t shift = static_cast<std::intmax_t>(std::numeric_limits<T>::digits) - static_cast<std::intmax_t>(boost::math::ccmath::ilogb(val)) - 1;
  67. T result = boost::math::ccmath::scalbn(val, shift);
  68. result = boost::math::ccmath::round(result);
  69. return boost::math::ccmath::scalbn(result, -shift);
  70. }
  71. template <typename T>
  72. constexpr T get_smallest_value(const std::true_type&)
  73. {
  74. //
  75. // numeric_limits lies about denorms being present - particularly
  76. // when this can be turned on or off at runtime, as is the case
  77. // when using the SSE2 registers in DAZ or FTZ mode.
  78. //
  79. constexpr T m = std::numeric_limits<T>::denorm_min();
  80. return ((tools::min_value<T>() / 2) == 0) ? tools::min_value<T>() : m;
  81. }
  82. template <typename T>
  83. constexpr T get_smallest_value(const std::false_type&)
  84. {
  85. return tools::min_value<T>();
  86. }
  87. template <typename T>
  88. constexpr T get_smallest_value()
  89. {
  90. return get_smallest_value<T>(std::integral_constant<bool, std::numeric_limits<T>::is_specialized>());
  91. }
  92. template <typename T>
  93. constexpr T calc_min_shifted(const std::true_type&)
  94. {
  95. return boost::math::ccmath::ldexp(tools::min_value<T>(), tools::digits<T>() + 1);
  96. }
  97. template <typename T>
  98. constexpr T calc_min_shifted(const std::false_type&)
  99. {
  100. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  101. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  102. return boost::math::ccmath::scalbn(tools::min_value<T>(), std::numeric_limits<T>::digits + 1);
  103. }
  104. template <typename T>
  105. constexpr T get_min_shift_value()
  106. {
  107. const T val = calc_min_shifted<T>(std::integral_constant<bool, !std::numeric_limits<T>::is_specialized || std::numeric_limits<T>::radix == 2>());
  108. return val;
  109. }
  110. template <typename T, bool b = boost::math::tools::detail::has_backend_type_v<T>>
  111. struct exponent_type
  112. {
  113. using type = int;
  114. };
  115. template <typename T>
  116. struct exponent_type<T, true>
  117. {
  118. using type = typename T::backend_type::exponent_type;
  119. };
  120. template <typename T, bool b = boost::math::tools::detail::has_backend_type_v<T>>
  121. using exponent_type_t = typename exponent_type<T>::type;
  122. template <typename T>
  123. constexpr T float_next_imp(const T& val, const std::true_type&)
  124. {
  125. using exponent_type = exponent_type_t<T>;
  126. exponent_type expon {};
  127. int fpclass = boost::math::ccmath::fpclassify(val);
  128. if (fpclass == FP_NAN)
  129. {
  130. return val;
  131. }
  132. else if (fpclass == FP_INFINITE)
  133. {
  134. return val;
  135. }
  136. else if (val <= -tools::max_value<T>())
  137. {
  138. return val;
  139. }
  140. if (val == 0)
  141. {
  142. return detail::get_smallest_value<T>();
  143. }
  144. if ((fpclass != FP_SUBNORMAL) && (fpclass != FP_ZERO)
  145. && (boost::math::ccmath::fabs(val) < detail::get_min_shift_value<T>())
  146. && (val != -tools::min_value<T>()))
  147. {
  148. //
  149. // Special case: if the value of the least significant bit is a denorm, and the result
  150. // would not be a denorm, then shift the input, increment, and shift back.
  151. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  152. //
  153. return boost::math::ccmath::ldexp(boost::math::ccmath::detail::float_next(static_cast<T>(boost::math::ccmath::ldexp(val, 2 * tools::digits<T>()))), -2 * tools::digits<T>());
  154. }
  155. if (-0.5f == boost::math::ccmath::frexp(val, &expon))
  156. {
  157. --expon; // reduce exponent when val is a power of two, and negative.
  158. }
  159. T diff = boost::math::ccmath::ldexp(static_cast<T>(1), expon - tools::digits<T>());
  160. if(diff == 0)
  161. {
  162. diff = detail::get_smallest_value<T>();
  163. }
  164. return val + diff;
  165. }
  166. //
  167. // Special version for some base other than 2:
  168. //
  169. template <typename T>
  170. constexpr T float_next_imp(const T& val, const std::false_type&)
  171. {
  172. using exponent_type = exponent_type_t<T>;
  173. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  174. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  175. exponent_type expon {};
  176. int fpclass = boost::math::ccmath::fpclassify(val);
  177. if (fpclass == FP_NAN)
  178. {
  179. return val;
  180. }
  181. else if (fpclass == FP_INFINITE)
  182. {
  183. return val;
  184. }
  185. else if (val <= -tools::max_value<T>())
  186. {
  187. return val;
  188. }
  189. if (val == 0)
  190. {
  191. return detail::get_smallest_value<T>();
  192. }
  193. if ((fpclass != FP_SUBNORMAL) && (fpclass != FP_ZERO)
  194. && (boost::math::ccmath::fabs(val) < detail::get_min_shift_value<T>())
  195. && (val != -tools::min_value<T>()))
  196. {
  197. //
  198. // Special case: if the value of the least significant bit is a denorm, and the result
  199. // would not be a denorm, then shift the input, increment, and shift back.
  200. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  201. //
  202. return boost::math::ccmath::scalbn(boost::math::ccmath::detail::float_next(static_cast<T>(boost::math::ccmath::scalbn(val, 2 * std::numeric_limits<T>::digits))), -2 * std::numeric_limits<T>::digits);
  203. }
  204. expon = 1 + boost::math::ccmath::ilogb(val);
  205. if(-1 == boost::math::ccmath::scalbn(val, -expon) * std::numeric_limits<T>::radix)
  206. {
  207. --expon; // reduce exponent when val is a power of base, and negative.
  208. }
  209. T diff = boost::math::ccmath::scalbn(static_cast<T>(1), expon - std::numeric_limits<T>::digits);
  210. if(diff == 0)
  211. {
  212. diff = detail::get_smallest_value<T>();
  213. }
  214. return val + diff;
  215. }
  216. template <typename T, typename result_type>
  217. constexpr result_type float_next(const T& val)
  218. {
  219. return detail::float_next_imp(detail::normalize_value(static_cast<result_type>(val), typename detail::has_hidden_guard_digits<result_type>::type()), std::integral_constant<bool, !std::numeric_limits<result_type>::is_specialized || (std::numeric_limits<result_type>::radix == 2)>());
  220. }
  221. template <typename T>
  222. constexpr T float_prior_imp(const T& val, const std::true_type&)
  223. {
  224. using exponent_type = exponent_type_t<T>;
  225. exponent_type expon {};
  226. int fpclass = boost::math::ccmath::fpclassify(val);
  227. if (fpclass == FP_NAN)
  228. {
  229. return val;
  230. }
  231. else if (fpclass == FP_INFINITE)
  232. {
  233. return val;
  234. }
  235. else if (val <= -tools::max_value<T>())
  236. {
  237. return val;
  238. }
  239. if (val == 0)
  240. {
  241. return -detail::get_smallest_value<T>();
  242. }
  243. if ((fpclass != FP_SUBNORMAL) && (fpclass != FP_ZERO)
  244. && (boost::math::ccmath::fabs(val) < detail::get_min_shift_value<T>())
  245. && (val != tools::min_value<T>()))
  246. {
  247. //
  248. // Special case: if the value of the least significant bit is a denorm, and the result
  249. // would not be a denorm, then shift the input, increment, and shift back.
  250. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  251. //
  252. return boost::math::ccmath::ldexp(boost::math::ccmath::detail::float_prior(static_cast<T>(boost::math::ccmath::ldexp(val, 2 * tools::digits<T>()))), -2 * tools::digits<T>());
  253. }
  254. if(T remain = boost::math::ccmath::frexp(val, &expon); remain == 0.5f)
  255. {
  256. --expon; // when val is a power of two we must reduce the exponent
  257. }
  258. T diff = boost::math::ccmath::ldexp(static_cast<T>(1), expon - tools::digits<T>());
  259. if(diff == 0)
  260. {
  261. diff = detail::get_smallest_value<T>();
  262. }
  263. return val - diff;
  264. }
  265. //
  266. // Special version for bases other than 2:
  267. //
  268. template <typename T>
  269. constexpr T float_prior_imp(const T& val, const std::false_type&)
  270. {
  271. using exponent_type = exponent_type_t<T>;
  272. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  273. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  274. exponent_type expon {};
  275. int fpclass = boost::math::ccmath::fpclassify(val);
  276. if (fpclass == FP_NAN)
  277. {
  278. return val;
  279. }
  280. else if (fpclass == FP_INFINITE)
  281. {
  282. return val;
  283. }
  284. else if (val <= -tools::max_value<T>())
  285. {
  286. return val;
  287. }
  288. if (val == 0)
  289. {
  290. return -detail::get_smallest_value<T>();
  291. }
  292. if ((fpclass != FP_SUBNORMAL) && (fpclass != FP_ZERO)
  293. && (boost::math::ccmath::fabs(val) < detail::get_min_shift_value<T>())
  294. && (val != tools::min_value<T>()))
  295. {
  296. //
  297. // Special case: if the value of the least significant bit is a denorm, and the result
  298. // would not be a denorm, then shift the input, increment, and shift back.
  299. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  300. //
  301. return boost::math::ccmath::scalbn(boost::math::ccmath::detail::float_prior(static_cast<T>(boost::math::ccmath::scalbn(val, 2 * std::numeric_limits<T>::digits))), -2 * std::numeric_limits<T>::digits);
  302. }
  303. expon = 1 + boost::math::ccmath::ilogb(val);
  304. if (T remain = boost::math::ccmath::scalbn(val, -expon); remain * std::numeric_limits<T>::radix == 1)
  305. {
  306. --expon; // when val is a power of two we must reduce the exponent
  307. }
  308. T diff = boost::math::ccmath::scalbn(static_cast<T>(1), expon - std::numeric_limits<T>::digits);
  309. if (diff == 0)
  310. {
  311. diff = detail::get_smallest_value<T>();
  312. }
  313. return val - diff;
  314. } // float_prior_imp
  315. template <typename T, typename result_type>
  316. constexpr result_type float_prior(const T& val)
  317. {
  318. return detail::float_prior_imp(detail::normalize_value(static_cast<result_type>(val), typename detail::has_hidden_guard_digits<result_type>::type()), std::integral_constant<bool, !std::numeric_limits<result_type>::is_specialized || (std::numeric_limits<result_type>::radix == 2)>());
  319. }
  320. } // namespace detail
  321. template <typename T, typename U, typename result_type = tools::promote_args_t<T, U>>
  322. constexpr result_type nextafter(const T& val, const U& direction)
  323. {
  324. if (BOOST_MATH_IS_CONSTANT_EVALUATED(val))
  325. {
  326. if (boost::math::ccmath::isnan(val))
  327. {
  328. return val;
  329. }
  330. else if (boost::math::ccmath::isnan(direction))
  331. {
  332. return direction;
  333. }
  334. else if (val < direction)
  335. {
  336. return boost::math::ccmath::detail::float_next(val);
  337. }
  338. else if (val == direction)
  339. {
  340. // IEC 60559 recommends that from is returned whenever from == to. These functions return to instead,
  341. // which makes the behavior around zero consistent: std::nextafter(-0.0, +0.0) returns +0.0 and
  342. // std::nextafter(+0.0, -0.0) returns -0.0.
  343. return direction;
  344. }
  345. return boost::math::ccmath::detail::float_prior(val);
  346. }
  347. else
  348. {
  349. using std::nextafter;
  350. return nextafter(static_cast<result_type>(val), static_cast<result_type>(direction));
  351. }
  352. }
  353. constexpr float nextafterf(float val, float direction)
  354. {
  355. return boost::math::ccmath::nextafter(val, direction);
  356. }
  357. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  358. constexpr long double nextafterl(long double val, long double direction)
  359. {
  360. return boost::math::ccmath::nextafter(val, direction);
  361. }
  362. template <typename T, typename result_type = tools::promote_args_t<T, long double>, typename return_type = std::conditional_t<std::is_integral_v<T>, double, T>>
  363. constexpr return_type nexttoward(T val, long double direction)
  364. {
  365. if (BOOST_MATH_IS_CONSTANT_EVALUATED(val))
  366. {
  367. return static_cast<return_type>(boost::math::ccmath::nextafter(static_cast<result_type>(val), direction));
  368. }
  369. else
  370. {
  371. using std::nexttoward;
  372. return nexttoward(val, direction);
  373. }
  374. }
  375. constexpr float nexttowardf(float val, long double direction)
  376. {
  377. return boost::math::ccmath::nexttoward(val, direction);
  378. }
  379. constexpr long double nexttowardl(long double val, long double direction)
  380. {
  381. return boost::math::ccmath::nexttoward(val, direction);
  382. }
  383. #endif
  384. } // Namespaces
  385. #endif // BOOST_MATH_SPECIAL_NEXT_HPP