next.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. // (C) Copyright John Maddock 2008.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_SPECIAL_NEXT_HPP
  6. #define BOOST_MATH_SPECIAL_NEXT_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/policies/error_handling.hpp>
  12. #include <boost/math/special_functions/fpclassify.hpp>
  13. #include <boost/math/special_functions/sign.hpp>
  14. #include <boost/math/special_functions/trunc.hpp>
  15. #include <boost/math/tools/traits.hpp>
  16. #include <type_traits>
  17. #include <cfloat>
  18. #if !defined(_CRAYC) && !defined(__CUDACC__) && (!defined(__GNUC__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 3)))
  19. #if (defined(_M_IX86_FP) && (_M_IX86_FP >= 2)) || defined(__SSE2__)
  20. #include "xmmintrin.h"
  21. #define BOOST_MATH_CHECK_SSE2
  22. #endif
  23. #endif
  24. namespace boost{ namespace math{
  25. namespace concepts {
  26. class real_concept;
  27. class std_real_concept;
  28. }
  29. namespace detail{
  30. template <class T>
  31. struct has_hidden_guard_digits;
  32. template <>
  33. struct has_hidden_guard_digits<float> : public std::false_type {};
  34. template <>
  35. struct has_hidden_guard_digits<double> : public std::false_type {};
  36. template <>
  37. struct has_hidden_guard_digits<long double> : public std::false_type {};
  38. #ifdef BOOST_HAS_FLOAT128
  39. template <>
  40. struct has_hidden_guard_digits<__float128> : public std::false_type {};
  41. #endif
  42. template <>
  43. struct has_hidden_guard_digits<boost::math::concepts::real_concept> : public std::false_type {};
  44. template <>
  45. struct has_hidden_guard_digits<boost::math::concepts::std_real_concept> : public std::false_type {};
  46. template <class T, bool b>
  47. struct has_hidden_guard_digits_10 : public std::false_type {};
  48. template <class T>
  49. struct has_hidden_guard_digits_10<T, true> : public std::integral_constant<bool, (std::numeric_limits<T>::digits10 != std::numeric_limits<T>::max_digits10)> {};
  50. template <class T>
  51. struct has_hidden_guard_digits
  52. : public has_hidden_guard_digits_10<T,
  53. std::numeric_limits<T>::is_specialized
  54. && (std::numeric_limits<T>::radix == 10) >
  55. {};
  56. template <class T>
  57. inline const T& normalize_value(const T& val, const std::false_type&) { return val; }
  58. template <class T>
  59. inline T normalize_value(const T& val, const std::true_type&)
  60. {
  61. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  62. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  63. std::intmax_t shift = (std::intmax_t)std::numeric_limits<T>::digits - (std::intmax_t)ilogb(val) - 1;
  64. T result = scalbn(val, shift);
  65. result = round(result);
  66. return scalbn(result, -shift);
  67. }
  68. template <class T>
  69. inline T get_smallest_value(std::true_type const&) {
  70. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  71. //
  72. // numeric_limits lies about denorms being present - particularly
  73. // when this can be turned on or off at runtime, as is the case
  74. // when using the SSE2 registers in DAZ or FTZ mode.
  75. //
  76. static const T m = std::numeric_limits<T>::denorm_min();
  77. #ifdef BOOST_MATH_CHECK_SSE2
  78. return (_mm_getcsr() & (_MM_FLUSH_ZERO_ON | 0x40)) ? tools::min_value<T>() : m;
  79. #else
  80. return ((tools::min_value<T>() / 2) == 0) ? tools::min_value<T>() : m;
  81. #endif
  82. }
  83. template <class T>
  84. inline T get_smallest_value(std::false_type const&)
  85. {
  86. return tools::min_value<T>();
  87. }
  88. template <class T>
  89. inline T get_smallest_value()
  90. {
  91. return get_smallest_value<T>(std::integral_constant<bool, std::numeric_limits<T>::is_specialized>());
  92. }
  93. template <class T>
  94. inline bool has_denorm_now() {
  95. return get_smallest_value<T>() < tools::min_value<T>();
  96. }
  97. //
  98. // Returns the smallest value that won't generate denorms when
  99. // we calculate the value of the least-significant-bit:
  100. //
  101. template <class T>
  102. T get_min_shift_value();
  103. template <class T>
  104. struct min_shift_initializer
  105. {
  106. struct init
  107. {
  108. init()
  109. {
  110. do_init();
  111. }
  112. static void do_init()
  113. {
  114. get_min_shift_value<T>();
  115. }
  116. void force_instantiate()const{}
  117. };
  118. static const init initializer;
  119. static void force_instantiate()
  120. {
  121. initializer.force_instantiate();
  122. }
  123. };
  124. template <class T>
  125. const typename min_shift_initializer<T>::init min_shift_initializer<T>::initializer;
  126. template <class T>
  127. inline T calc_min_shifted(const std::true_type&)
  128. {
  129. BOOST_MATH_STD_USING
  130. return ldexp(tools::min_value<T>(), tools::digits<T>() + 1);
  131. }
  132. template <class T>
  133. inline T calc_min_shifted(const std::false_type&)
  134. {
  135. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  136. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  137. return scalbn(tools::min_value<T>(), std::numeric_limits<T>::digits + 1);
  138. }
  139. template <class T>
  140. inline T get_min_shift_value()
  141. {
  142. static const T val = calc_min_shifted<T>(std::integral_constant<bool, !std::numeric_limits<T>::is_specialized || std::numeric_limits<T>::radix == 2>());
  143. min_shift_initializer<T>::force_instantiate();
  144. return val;
  145. }
  146. template <class T, bool b = boost::math::tools::detail::has_backend_type<T>::value>
  147. struct exponent_type
  148. {
  149. typedef int type;
  150. };
  151. template <class T>
  152. struct exponent_type<T, true>
  153. {
  154. typedef typename T::backend_type::exponent_type type;
  155. };
  156. template <class T, class Policy>
  157. T float_next_imp(const T& val, const std::true_type&, const Policy& pol)
  158. {
  159. typedef typename exponent_type<T>::type exponent_type;
  160. BOOST_MATH_STD_USING
  161. exponent_type expon;
  162. static const char* function = "float_next<%1%>(%1%)";
  163. int fpclass = (boost::math::fpclassify)(val);
  164. if (fpclass == (int)FP_INFINITE)
  165. {
  166. if (val < 0)
  167. return -tools::max_value<T>();
  168. return val; // +INF
  169. }
  170. else if (fpclass == (int)FP_NAN)
  171. {
  172. return policies::raise_domain_error<T>(
  173. function,
  174. "Argument must be finite, but got %1%", val, pol);
  175. }
  176. if(val >= tools::max_value<T>())
  177. return policies::raise_overflow_error<T>(function, nullptr, pol);
  178. if(val == 0)
  179. return detail::get_smallest_value<T>();
  180. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != -tools::min_value<T>()))
  181. {
  182. //
  183. // Special case: if the value of the least significant bit is a denorm, and the result
  184. // would not be a denorm, then shift the input, increment, and shift back.
  185. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  186. //
  187. return ldexp(float_next(T(ldexp(val, 2 * tools::digits<T>())), pol), -2 * tools::digits<T>());
  188. }
  189. if(-0.5f == frexp(val, &expon))
  190. --expon; // reduce exponent when val is a power of two, and negative.
  191. T diff = ldexp(T(1), expon - tools::digits<T>());
  192. if(diff == 0)
  193. diff = detail::get_smallest_value<T>();
  194. return val + diff;
  195. } // float_next_imp
  196. //
  197. // Special version for some base other than 2:
  198. //
  199. template <class T, class Policy>
  200. T float_next_imp(const T& val, const std::false_type&, const Policy& pol)
  201. {
  202. typedef typename exponent_type<T>::type exponent_type;
  203. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  204. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  205. BOOST_MATH_STD_USING
  206. exponent_type expon;
  207. static const char* function = "float_next<%1%>(%1%)";
  208. int fpclass = (boost::math::fpclassify)(val);
  209. if (fpclass == (int)FP_INFINITE)
  210. {
  211. if (val < 0)
  212. return -tools::max_value<T>();
  213. return val; // +INF
  214. }
  215. else if (fpclass == (int)FP_NAN)
  216. {
  217. return policies::raise_domain_error<T>(
  218. function,
  219. "Argument must be finite, but got %1%", val, pol);
  220. }
  221. if(val >= tools::max_value<T>())
  222. return policies::raise_overflow_error<T>(function, nullptr, pol);
  223. if(val == 0)
  224. return detail::get_smallest_value<T>();
  225. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != -tools::min_value<T>()))
  226. {
  227. //
  228. // Special case: if the value of the least significant bit is a denorm, and the result
  229. // would not be a denorm, then shift the input, increment, and shift back.
  230. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  231. //
  232. return scalbn(float_next(T(scalbn(val, 2 * std::numeric_limits<T>::digits)), pol), -2 * std::numeric_limits<T>::digits);
  233. }
  234. expon = 1 + ilogb(val);
  235. if(-1 == scalbn(val, -expon) * std::numeric_limits<T>::radix)
  236. --expon; // reduce exponent when val is a power of base, and negative.
  237. T diff = scalbn(T(1), expon - std::numeric_limits<T>::digits);
  238. if(diff == 0)
  239. diff = detail::get_smallest_value<T>();
  240. return val + diff;
  241. } // float_next_imp
  242. } // namespace detail
  243. template <class T, class Policy>
  244. inline typename tools::promote_args<T>::type float_next(const T& val, const Policy& pol)
  245. {
  246. typedef typename tools::promote_args<T>::type result_type;
  247. 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)>(), pol);
  248. }
  249. #if 0 //def BOOST_MSVC
  250. //
  251. // We used to use ::_nextafter here, but doing so fails when using
  252. // the SSE2 registers if the FTZ or DAZ flags are set, so use our own
  253. // - albeit slower - code instead as at least that gives the correct answer.
  254. //
  255. template <class Policy>
  256. inline double float_next(const double& val, const Policy& pol)
  257. {
  258. static const char* function = "float_next<%1%>(%1%)";
  259. if(!(boost::math::isfinite)(val) && (val > 0))
  260. return policies::raise_domain_error<double>(
  261. function,
  262. "Argument must be finite, but got %1%", val, pol);
  263. if(val >= tools::max_value<double>())
  264. return policies::raise_overflow_error<double>(function, nullptr, pol);
  265. return ::_nextafter(val, tools::max_value<double>());
  266. }
  267. #endif
  268. template <class T>
  269. inline typename tools::promote_args<T>::type float_next(const T& val)
  270. {
  271. return float_next(val, policies::policy<>());
  272. }
  273. namespace detail{
  274. template <class T, class Policy>
  275. T float_prior_imp(const T& val, const std::true_type&, const Policy& pol)
  276. {
  277. typedef typename exponent_type<T>::type exponent_type;
  278. BOOST_MATH_STD_USING
  279. exponent_type expon;
  280. static const char* function = "float_prior<%1%>(%1%)";
  281. int fpclass = (boost::math::fpclassify)(val);
  282. if (fpclass == (int)FP_INFINITE)
  283. {
  284. if (val > 0)
  285. return tools::max_value<T>();
  286. return val; // -INF
  287. }
  288. else if (fpclass == (int)FP_NAN)
  289. {
  290. return policies::raise_domain_error<T>(
  291. function,
  292. "Argument must be finite, but got %1%", val, pol);
  293. }
  294. if(val <= -tools::max_value<T>())
  295. return -policies::raise_overflow_error<T>(function, nullptr, pol);
  296. if(val == 0)
  297. return -detail::get_smallest_value<T>();
  298. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != tools::min_value<T>()))
  299. {
  300. //
  301. // Special case: if the value of the least significant bit is a denorm, and the result
  302. // would not be a denorm, then shift the input, increment, and shift back.
  303. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  304. //
  305. return ldexp(float_prior(T(ldexp(val, 2 * tools::digits<T>())), pol), -2 * tools::digits<T>());
  306. }
  307. T remain = frexp(val, &expon);
  308. if(remain == 0.5f)
  309. --expon; // when val is a power of two we must reduce the exponent
  310. T diff = ldexp(T(1), expon - tools::digits<T>());
  311. if(diff == 0)
  312. diff = detail::get_smallest_value<T>();
  313. return val - diff;
  314. } // float_prior_imp
  315. //
  316. // Special version for bases other than 2:
  317. //
  318. template <class T, class Policy>
  319. T float_prior_imp(const T& val, const std::false_type&, const Policy& pol)
  320. {
  321. typedef typename exponent_type<T>::type exponent_type;
  322. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  323. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  324. BOOST_MATH_STD_USING
  325. exponent_type expon;
  326. static const char* function = "float_prior<%1%>(%1%)";
  327. int fpclass = (boost::math::fpclassify)(val);
  328. if (fpclass == (int)FP_INFINITE)
  329. {
  330. if (val > 0)
  331. return tools::max_value<T>();
  332. return val; // -INF
  333. }
  334. else if (fpclass == (int)FP_NAN)
  335. {
  336. return policies::raise_domain_error<T>(
  337. function,
  338. "Argument must be finite, but got %1%", val, pol);
  339. }
  340. if(val <= -tools::max_value<T>())
  341. return -policies::raise_overflow_error<T>(function, nullptr, pol);
  342. if(val == 0)
  343. return -detail::get_smallest_value<T>();
  344. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != tools::min_value<T>()))
  345. {
  346. //
  347. // Special case: if the value of the least significant bit is a denorm, and the result
  348. // would not be a denorm, then shift the input, increment, and shift back.
  349. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  350. //
  351. return scalbn(float_prior(T(scalbn(val, 2 * std::numeric_limits<T>::digits)), pol), -2 * std::numeric_limits<T>::digits);
  352. }
  353. expon = 1 + ilogb(val);
  354. T remain = scalbn(val, -expon);
  355. if(remain * std::numeric_limits<T>::radix == 1)
  356. --expon; // when val is a power of two we must reduce the exponent
  357. T diff = scalbn(T(1), expon - std::numeric_limits<T>::digits);
  358. if(diff == 0)
  359. diff = detail::get_smallest_value<T>();
  360. return val - diff;
  361. } // float_prior_imp
  362. } // namespace detail
  363. template <class T, class Policy>
  364. inline typename tools::promote_args<T>::type float_prior(const T& val, const Policy& pol)
  365. {
  366. typedef typename tools::promote_args<T>::type result_type;
  367. 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)>(), pol);
  368. }
  369. #if 0 //def BOOST_MSVC
  370. //
  371. // We used to use ::_nextafter here, but doing so fails when using
  372. // the SSE2 registers if the FTZ or DAZ flags are set, so use our own
  373. // - albeit slower - code instead as at least that gives the correct answer.
  374. //
  375. template <class Policy>
  376. inline double float_prior(const double& val, const Policy& pol)
  377. {
  378. static const char* function = "float_prior<%1%>(%1%)";
  379. if(!(boost::math::isfinite)(val) && (val < 0))
  380. return policies::raise_domain_error<double>(
  381. function,
  382. "Argument must be finite, but got %1%", val, pol);
  383. if(val <= -tools::max_value<double>())
  384. return -policies::raise_overflow_error<double>(function, nullptr, pol);
  385. return ::_nextafter(val, -tools::max_value<double>());
  386. }
  387. #endif
  388. template <class T>
  389. inline typename tools::promote_args<T>::type float_prior(const T& val)
  390. {
  391. return float_prior(val, policies::policy<>());
  392. }
  393. template <class T, class U, class Policy>
  394. inline typename tools::promote_args<T, U>::type nextafter(const T& val, const U& direction, const Policy& pol)
  395. {
  396. typedef typename tools::promote_args<T, U>::type result_type;
  397. return val < direction ? boost::math::float_next<result_type>(val, pol) : val == direction ? val : boost::math::float_prior<result_type>(val, pol);
  398. }
  399. template <class T, class U>
  400. inline typename tools::promote_args<T, U>::type nextafter(const T& val, const U& direction)
  401. {
  402. return nextafter(val, direction, policies::policy<>());
  403. }
  404. namespace detail{
  405. template <class T, class Policy>
  406. T float_distance_imp(const T& a, const T& b, const std::true_type&, const Policy& pol)
  407. {
  408. BOOST_MATH_STD_USING
  409. //
  410. // Error handling:
  411. //
  412. static const char* function = "float_distance<%1%>(%1%, %1%)";
  413. if(!(boost::math::isfinite)(a))
  414. return policies::raise_domain_error<T>(
  415. function,
  416. "Argument a must be finite, but got %1%", a, pol);
  417. if(!(boost::math::isfinite)(b))
  418. return policies::raise_domain_error<T>(
  419. function,
  420. "Argument b must be finite, but got %1%", b, pol);
  421. //
  422. // Special cases:
  423. //
  424. if(a > b)
  425. return -float_distance(b, a, pol);
  426. if(a == b)
  427. return T(0);
  428. if(a == 0)
  429. return 1 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol));
  430. if(b == 0)
  431. return 1 + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  432. if(boost::math::sign(a) != boost::math::sign(b))
  433. return 2 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol))
  434. + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  435. //
  436. // By the time we get here, both a and b must have the same sign, we want
  437. // b > a and both positive for the following logic:
  438. //
  439. if(a < 0)
  440. return float_distance(static_cast<T>(-b), static_cast<T>(-a), pol);
  441. BOOST_MATH_ASSERT(a >= 0);
  442. BOOST_MATH_ASSERT(b >= a);
  443. int expon;
  444. //
  445. // Note that if a is a denorm then the usual formula fails
  446. // because we actually have fewer than tools::digits<T>()
  447. // significant bits in the representation:
  448. //
  449. (void)frexp(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) ? tools::min_value<T>() : a, &expon);
  450. T upper = ldexp(T(1), expon);
  451. T result = T(0);
  452. //
  453. // If b is greater than upper, then we *must* split the calculation
  454. // as the size of the ULP changes with each order of magnitude change:
  455. //
  456. if(b > upper)
  457. {
  458. int expon2;
  459. (void)frexp(b, &expon2);
  460. T upper2 = ldexp(T(0.5), expon2);
  461. result = float_distance(upper2, b);
  462. result += (expon2 - expon - 1) * ldexp(T(1), tools::digits<T>() - 1);
  463. }
  464. //
  465. // Use compensated double-double addition to avoid rounding
  466. // errors in the subtraction:
  467. //
  468. expon = tools::digits<T>() - expon;
  469. T mb, x, y, z;
  470. if(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) || (b - a < tools::min_value<T>()))
  471. {
  472. //
  473. // Special case - either one end of the range is a denormal, or else the difference is.
  474. // The regular code will fail if we're using the SSE2 registers on Intel and either
  475. // the FTZ or DAZ flags are set.
  476. //
  477. T a2 = ldexp(a, tools::digits<T>());
  478. T b2 = ldexp(b, tools::digits<T>());
  479. mb = -(std::min)(T(ldexp(upper, tools::digits<T>())), b2);
  480. x = a2 + mb;
  481. z = x - a2;
  482. y = (a2 - (x - z)) + (mb - z);
  483. expon -= tools::digits<T>();
  484. }
  485. else
  486. {
  487. mb = -(std::min)(upper, b);
  488. x = a + mb;
  489. z = x - a;
  490. y = (a - (x - z)) + (mb - z);
  491. }
  492. if(x < 0)
  493. {
  494. x = -x;
  495. y = -y;
  496. }
  497. result += ldexp(x, expon) + ldexp(y, expon);
  498. //
  499. // Result must be an integer:
  500. //
  501. BOOST_MATH_ASSERT(result == floor(result));
  502. return result;
  503. } // float_distance_imp
  504. //
  505. // Special versions for bases other than 2:
  506. //
  507. template <class T, class Policy>
  508. T float_distance_imp(const T& a, const T& b, const std::false_type&, const Policy& pol)
  509. {
  510. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  511. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  512. BOOST_MATH_STD_USING
  513. //
  514. // Error handling:
  515. //
  516. static const char* function = "float_distance<%1%>(%1%, %1%)";
  517. if(!(boost::math::isfinite)(a))
  518. return policies::raise_domain_error<T>(
  519. function,
  520. "Argument a must be finite, but got %1%", a, pol);
  521. if(!(boost::math::isfinite)(b))
  522. return policies::raise_domain_error<T>(
  523. function,
  524. "Argument b must be finite, but got %1%", b, pol);
  525. //
  526. // Special cases:
  527. //
  528. if(a > b)
  529. return -float_distance(b, a, pol);
  530. if(a == b)
  531. return T(0);
  532. if(a == 0)
  533. return 1 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol));
  534. if(b == 0)
  535. return 1 + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  536. if(boost::math::sign(a) != boost::math::sign(b))
  537. return 2 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol))
  538. + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  539. //
  540. // By the time we get here, both a and b must have the same sign, we want
  541. // b > a and both positive for the following logic:
  542. //
  543. if(a < 0)
  544. return float_distance(static_cast<T>(-b), static_cast<T>(-a), pol);
  545. BOOST_MATH_ASSERT(a >= 0);
  546. BOOST_MATH_ASSERT(b >= a);
  547. std::intmax_t expon;
  548. //
  549. // Note that if a is a denorm then the usual formula fails
  550. // because we actually have fewer than tools::digits<T>()
  551. // significant bits in the representation:
  552. //
  553. expon = 1 + ilogb(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) ? tools::min_value<T>() : a);
  554. T upper = scalbn(T(1), expon);
  555. T result = T(0);
  556. //
  557. // If b is greater than upper, then we *must* split the calculation
  558. // as the size of the ULP changes with each order of magnitude change:
  559. //
  560. if(b > upper)
  561. {
  562. std::intmax_t expon2 = 1 + ilogb(b);
  563. T upper2 = scalbn(T(1), expon2 - 1);
  564. result = float_distance(upper2, b);
  565. result += (expon2 - expon - 1) * scalbn(T(1), std::numeric_limits<T>::digits - 1);
  566. }
  567. //
  568. // Use compensated double-double addition to avoid rounding
  569. // errors in the subtraction:
  570. //
  571. expon = std::numeric_limits<T>::digits - expon;
  572. T mb, x, y, z;
  573. if(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) || (b - a < tools::min_value<T>()))
  574. {
  575. //
  576. // Special case - either one end of the range is a denormal, or else the difference is.
  577. // The regular code will fail if we're using the SSE2 registers on Intel and either
  578. // the FTZ or DAZ flags are set.
  579. //
  580. T a2 = scalbn(a, std::numeric_limits<T>::digits);
  581. T b2 = scalbn(b, std::numeric_limits<T>::digits);
  582. mb = -(std::min)(T(scalbn(upper, std::numeric_limits<T>::digits)), b2);
  583. x = a2 + mb;
  584. z = x - a2;
  585. y = (a2 - (x - z)) + (mb - z);
  586. expon -= std::numeric_limits<T>::digits;
  587. }
  588. else
  589. {
  590. mb = -(std::min)(upper, b);
  591. x = a + mb;
  592. z = x - a;
  593. y = (a - (x - z)) + (mb - z);
  594. }
  595. if(x < 0)
  596. {
  597. x = -x;
  598. y = -y;
  599. }
  600. result += scalbn(x, expon) + scalbn(y, expon);
  601. //
  602. // Result must be an integer:
  603. //
  604. BOOST_MATH_ASSERT(result == floor(result));
  605. return result;
  606. } // float_distance_imp
  607. } // namespace detail
  608. template <class T, class U, class Policy>
  609. inline typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b, const Policy& pol)
  610. {
  611. //
  612. // We allow ONE of a and b to be an integer type, otherwise both must be the SAME type.
  613. //
  614. static_assert(
  615. (std::is_same<T, U>::value
  616. || (std::is_integral<T>::value && !std::is_integral<U>::value)
  617. || (!std::is_integral<T>::value && std::is_integral<U>::value)
  618. || (std::numeric_limits<T>::is_specialized && std::numeric_limits<U>::is_specialized
  619. && (std::numeric_limits<T>::digits == std::numeric_limits<U>::digits)
  620. && (std::numeric_limits<T>::radix == std::numeric_limits<U>::radix)
  621. && !std::numeric_limits<T>::is_integer && !std::numeric_limits<U>::is_integer)),
  622. "Float distance between two different floating point types is undefined.");
  623. BOOST_MATH_IF_CONSTEXPR (!std::is_same<T, U>::value)
  624. {
  625. BOOST_MATH_IF_CONSTEXPR(std::is_integral<T>::value)
  626. {
  627. return float_distance(static_cast<U>(a), b, pol);
  628. }
  629. else
  630. {
  631. return float_distance(a, static_cast<T>(b), pol);
  632. }
  633. }
  634. else
  635. {
  636. typedef typename tools::promote_args<T, U>::type result_type;
  637. return detail::float_distance_imp(detail::normalize_value(static_cast<result_type>(a), typename detail::has_hidden_guard_digits<result_type>::type()), detail::normalize_value(static_cast<result_type>(b), 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)>(), pol);
  638. }
  639. }
  640. template <class T, class U>
  641. typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b)
  642. {
  643. return boost::math::float_distance(a, b, policies::policy<>());
  644. }
  645. namespace detail{
  646. template <class T, class Policy>
  647. T float_advance_imp(T val, int distance, const std::true_type&, const Policy& pol)
  648. {
  649. BOOST_MATH_STD_USING
  650. //
  651. // Error handling:
  652. //
  653. static const char* function = "float_advance<%1%>(%1%, int)";
  654. int fpclass = (boost::math::fpclassify)(val);
  655. if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
  656. return policies::raise_domain_error<T>(
  657. function,
  658. "Argument val must be finite, but got %1%", val, pol);
  659. if(val < 0)
  660. return -float_advance(-val, -distance, pol);
  661. if(distance == 0)
  662. return val;
  663. if(distance == 1)
  664. return float_next(val, pol);
  665. if(distance == -1)
  666. return float_prior(val, pol);
  667. if(fabs(val) < detail::get_min_shift_value<T>())
  668. {
  669. //
  670. // Special case: if the value of the least significant bit is a denorm,
  671. // implement in terms of float_next/float_prior.
  672. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  673. //
  674. if(distance > 0)
  675. {
  676. do{ val = float_next(val, pol); } while(--distance);
  677. }
  678. else
  679. {
  680. do{ val = float_prior(val, pol); } while(++distance);
  681. }
  682. return val;
  683. }
  684. int expon;
  685. (void)frexp(val, &expon);
  686. T limit = ldexp((distance < 0 ? T(0.5f) : T(1)), expon);
  687. if(val <= tools::min_value<T>())
  688. {
  689. limit = sign(T(distance)) * tools::min_value<T>();
  690. }
  691. T limit_distance = float_distance(val, limit);
  692. while(fabs(limit_distance) < abs(distance))
  693. {
  694. distance -= itrunc(limit_distance);
  695. val = limit;
  696. if(distance < 0)
  697. {
  698. limit /= 2;
  699. expon--;
  700. }
  701. else
  702. {
  703. limit *= 2;
  704. expon++;
  705. }
  706. limit_distance = float_distance(val, limit);
  707. if(distance && (limit_distance == 0))
  708. {
  709. return policies::raise_evaluation_error<T>(function, "Internal logic failed while trying to increment floating point value %1%: most likely your FPU is in non-IEEE conforming mode.", val, pol);
  710. }
  711. }
  712. if((0.5f == frexp(val, &expon)) && (distance < 0))
  713. --expon;
  714. T diff = 0;
  715. if(val != 0)
  716. diff = distance * ldexp(T(1), expon - tools::digits<T>());
  717. if(diff == 0)
  718. diff = distance * detail::get_smallest_value<T>();
  719. return val += diff;
  720. } // float_advance_imp
  721. //
  722. // Special version for bases other than 2:
  723. //
  724. template <class T, class Policy>
  725. T float_advance_imp(T val, int distance, const std::false_type&, const Policy& pol)
  726. {
  727. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  728. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  729. BOOST_MATH_STD_USING
  730. //
  731. // Error handling:
  732. //
  733. static const char* function = "float_advance<%1%>(%1%, int)";
  734. int fpclass = (boost::math::fpclassify)(val);
  735. if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
  736. return policies::raise_domain_error<T>(
  737. function,
  738. "Argument val must be finite, but got %1%", val, pol);
  739. if(val < 0)
  740. return -float_advance(-val, -distance, pol);
  741. if(distance == 0)
  742. return val;
  743. if(distance == 1)
  744. return float_next(val, pol);
  745. if(distance == -1)
  746. return float_prior(val, pol);
  747. if(fabs(val) < detail::get_min_shift_value<T>())
  748. {
  749. //
  750. // Special case: if the value of the least significant bit is a denorm,
  751. // implement in terms of float_next/float_prior.
  752. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  753. //
  754. if(distance > 0)
  755. {
  756. do{ val = float_next(val, pol); } while(--distance);
  757. }
  758. else
  759. {
  760. do{ val = float_prior(val, pol); } while(++distance);
  761. }
  762. return val;
  763. }
  764. std::intmax_t expon = 1 + ilogb(val);
  765. T limit = scalbn(T(1), distance < 0 ? expon - 1 : expon);
  766. if(val <= tools::min_value<T>())
  767. {
  768. limit = sign(T(distance)) * tools::min_value<T>();
  769. }
  770. T limit_distance = float_distance(val, limit);
  771. while(fabs(limit_distance) < abs(distance))
  772. {
  773. distance -= itrunc(limit_distance);
  774. val = limit;
  775. if(distance < 0)
  776. {
  777. limit /= std::numeric_limits<T>::radix;
  778. expon--;
  779. }
  780. else
  781. {
  782. limit *= std::numeric_limits<T>::radix;
  783. expon++;
  784. }
  785. limit_distance = float_distance(val, limit);
  786. if(distance && (limit_distance == 0))
  787. {
  788. return policies::raise_evaluation_error<T>(function, "Internal logic failed while trying to increment floating point value %1%: most likely your FPU is in non-IEEE conforming mode.", val, pol);
  789. }
  790. }
  791. /*expon = 1 + ilogb(val);
  792. if((1 == scalbn(val, 1 + expon)) && (distance < 0))
  793. --expon;*/
  794. T diff = 0;
  795. if(val != 0)
  796. diff = distance * scalbn(T(1), expon - std::numeric_limits<T>::digits);
  797. if(diff == 0)
  798. diff = distance * detail::get_smallest_value<T>();
  799. return val += diff;
  800. } // float_advance_imp
  801. } // namespace detail
  802. template <class T, class Policy>
  803. inline typename tools::promote_args<T>::type float_advance(T val, int distance, const Policy& pol)
  804. {
  805. typedef typename tools::promote_args<T>::type result_type;
  806. return detail::float_advance_imp(detail::normalize_value(static_cast<result_type>(val), typename detail::has_hidden_guard_digits<result_type>::type()), distance, std::integral_constant<bool, !std::numeric_limits<result_type>::is_specialized || (std::numeric_limits<result_type>::radix == 2)>(), pol);
  807. }
  808. template <class T>
  809. inline typename tools::promote_args<T>::type float_advance(const T& val, int distance)
  810. {
  811. return boost::math::float_advance(val, distance, policies::policy<>());
  812. }
  813. }} // boost math namespaces
  814. #endif // BOOST_MATH_SPECIAL_NEXT_HPP