next.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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_NAN) || (fpclass == (int)FP_INFINITE))
  165. {
  166. if(val < 0)
  167. return -tools::max_value<T>();
  168. return policies::raise_domain_error<T>(
  169. function,
  170. "Argument must be finite, but got %1%", val, pol);
  171. }
  172. if(val >= tools::max_value<T>())
  173. return policies::raise_overflow_error<T>(function, nullptr, pol);
  174. if(val == 0)
  175. return detail::get_smallest_value<T>();
  176. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != -tools::min_value<T>()))
  177. {
  178. //
  179. // Special case: if the value of the least significant bit is a denorm, and the result
  180. // would not be a denorm, then shift the input, increment, and shift back.
  181. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  182. //
  183. return ldexp(float_next(T(ldexp(val, 2 * tools::digits<T>())), pol), -2 * tools::digits<T>());
  184. }
  185. if(-0.5f == frexp(val, &expon))
  186. --expon; // reduce exponent when val is a power of two, and negative.
  187. T diff = ldexp(T(1), expon - tools::digits<T>());
  188. if(diff == 0)
  189. diff = detail::get_smallest_value<T>();
  190. return val + diff;
  191. } // float_next_imp
  192. //
  193. // Special version for some base other than 2:
  194. //
  195. template <class T, class Policy>
  196. T float_next_imp(const T& val, const std::false_type&, const Policy& pol)
  197. {
  198. typedef typename exponent_type<T>::type exponent_type;
  199. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  200. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  201. BOOST_MATH_STD_USING
  202. exponent_type expon;
  203. static const char* function = "float_next<%1%>(%1%)";
  204. int fpclass = (boost::math::fpclassify)(val);
  205. if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
  206. {
  207. if(val < 0)
  208. return -tools::max_value<T>();
  209. return policies::raise_domain_error<T>(
  210. function,
  211. "Argument must be finite, but got %1%", val, pol);
  212. }
  213. if(val >= tools::max_value<T>())
  214. return policies::raise_overflow_error<T>(function, nullptr, pol);
  215. if(val == 0)
  216. return detail::get_smallest_value<T>();
  217. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != -tools::min_value<T>()))
  218. {
  219. //
  220. // Special case: if the value of the least significant bit is a denorm, and the result
  221. // would not be a denorm, then shift the input, increment, and shift back.
  222. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  223. //
  224. return scalbn(float_next(T(scalbn(val, 2 * std::numeric_limits<T>::digits)), pol), -2 * std::numeric_limits<T>::digits);
  225. }
  226. expon = 1 + ilogb(val);
  227. if(-1 == scalbn(val, -expon) * std::numeric_limits<T>::radix)
  228. --expon; // reduce exponent when val is a power of base, and negative.
  229. T diff = scalbn(T(1), expon - std::numeric_limits<T>::digits);
  230. if(diff == 0)
  231. diff = detail::get_smallest_value<T>();
  232. return val + diff;
  233. } // float_next_imp
  234. } // namespace detail
  235. template <class T, class Policy>
  236. inline typename tools::promote_args<T>::type float_next(const T& val, const Policy& pol)
  237. {
  238. typedef typename tools::promote_args<T>::type result_type;
  239. 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);
  240. }
  241. #if 0 //def BOOST_MSVC
  242. //
  243. // We used to use ::_nextafter here, but doing so fails when using
  244. // the SSE2 registers if the FTZ or DAZ flags are set, so use our own
  245. // - albeit slower - code instead as at least that gives the correct answer.
  246. //
  247. template <class Policy>
  248. inline double float_next(const double& val, const Policy& pol)
  249. {
  250. static const char* function = "float_next<%1%>(%1%)";
  251. if(!(boost::math::isfinite)(val) && (val > 0))
  252. return policies::raise_domain_error<double>(
  253. function,
  254. "Argument must be finite, but got %1%", val, pol);
  255. if(val >= tools::max_value<double>())
  256. return policies::raise_overflow_error<double>(function, nullptr, pol);
  257. return ::_nextafter(val, tools::max_value<double>());
  258. }
  259. #endif
  260. template <class T>
  261. inline typename tools::promote_args<T>::type float_next(const T& val)
  262. {
  263. return float_next(val, policies::policy<>());
  264. }
  265. namespace detail{
  266. template <class T, class Policy>
  267. T float_prior_imp(const T& val, const std::true_type&, const Policy& pol)
  268. {
  269. typedef typename exponent_type<T>::type exponent_type;
  270. BOOST_MATH_STD_USING
  271. exponent_type expon;
  272. static const char* function = "float_prior<%1%>(%1%)";
  273. int fpclass = (boost::math::fpclassify)(val);
  274. if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
  275. {
  276. if(val > 0)
  277. return tools::max_value<T>();
  278. return policies::raise_domain_error<T>(
  279. function,
  280. "Argument must be finite, but got %1%", val, pol);
  281. }
  282. if(val <= -tools::max_value<T>())
  283. return -policies::raise_overflow_error<T>(function, nullptr, pol);
  284. if(val == 0)
  285. return -detail::get_smallest_value<T>();
  286. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != tools::min_value<T>()))
  287. {
  288. //
  289. // Special case: if the value of the least significant bit is a denorm, and the result
  290. // would not be a denorm, then shift the input, increment, and shift back.
  291. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  292. //
  293. return ldexp(float_prior(T(ldexp(val, 2 * tools::digits<T>())), pol), -2 * tools::digits<T>());
  294. }
  295. T remain = frexp(val, &expon);
  296. if(remain == 0.5f)
  297. --expon; // when val is a power of two we must reduce the exponent
  298. T diff = ldexp(T(1), expon - tools::digits<T>());
  299. if(diff == 0)
  300. diff = detail::get_smallest_value<T>();
  301. return val - diff;
  302. } // float_prior_imp
  303. //
  304. // Special version for bases other than 2:
  305. //
  306. template <class T, class Policy>
  307. T float_prior_imp(const T& val, const std::false_type&, const Policy& pol)
  308. {
  309. typedef typename exponent_type<T>::type exponent_type;
  310. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  311. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  312. BOOST_MATH_STD_USING
  313. exponent_type expon;
  314. static const char* function = "float_prior<%1%>(%1%)";
  315. int fpclass = (boost::math::fpclassify)(val);
  316. if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
  317. {
  318. if(val > 0)
  319. return tools::max_value<T>();
  320. return policies::raise_domain_error<T>(
  321. function,
  322. "Argument must be finite, but got %1%", val, pol);
  323. }
  324. if(val <= -tools::max_value<T>())
  325. return -policies::raise_overflow_error<T>(function, nullptr, pol);
  326. if(val == 0)
  327. return -detail::get_smallest_value<T>();
  328. if((fpclass != (int)FP_SUBNORMAL) && (fpclass != (int)FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != tools::min_value<T>()))
  329. {
  330. //
  331. // Special case: if the value of the least significant bit is a denorm, and the result
  332. // would not be a denorm, then shift the input, increment, and shift back.
  333. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  334. //
  335. return scalbn(float_prior(T(scalbn(val, 2 * std::numeric_limits<T>::digits)), pol), -2 * std::numeric_limits<T>::digits);
  336. }
  337. expon = 1 + ilogb(val);
  338. T remain = scalbn(val, -expon);
  339. if(remain * std::numeric_limits<T>::radix == 1)
  340. --expon; // when val is a power of two we must reduce the exponent
  341. T diff = scalbn(T(1), expon - std::numeric_limits<T>::digits);
  342. if(diff == 0)
  343. diff = detail::get_smallest_value<T>();
  344. return val - diff;
  345. } // float_prior_imp
  346. } // namespace detail
  347. template <class T, class Policy>
  348. inline typename tools::promote_args<T>::type float_prior(const T& val, const Policy& pol)
  349. {
  350. typedef typename tools::promote_args<T>::type result_type;
  351. 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);
  352. }
  353. #if 0 //def BOOST_MSVC
  354. //
  355. // We used to use ::_nextafter here, but doing so fails when using
  356. // the SSE2 registers if the FTZ or DAZ flags are set, so use our own
  357. // - albeit slower - code instead as at least that gives the correct answer.
  358. //
  359. template <class Policy>
  360. inline double float_prior(const double& val, const Policy& pol)
  361. {
  362. static const char* function = "float_prior<%1%>(%1%)";
  363. if(!(boost::math::isfinite)(val) && (val < 0))
  364. return policies::raise_domain_error<double>(
  365. function,
  366. "Argument must be finite, but got %1%", val, pol);
  367. if(val <= -tools::max_value<double>())
  368. return -policies::raise_overflow_error<double>(function, nullptr, pol);
  369. return ::_nextafter(val, -tools::max_value<double>());
  370. }
  371. #endif
  372. template <class T>
  373. inline typename tools::promote_args<T>::type float_prior(const T& val)
  374. {
  375. return float_prior(val, policies::policy<>());
  376. }
  377. template <class T, class U, class Policy>
  378. inline typename tools::promote_args<T, U>::type nextafter(const T& val, const U& direction, const Policy& pol)
  379. {
  380. typedef typename tools::promote_args<T, U>::type result_type;
  381. return val < direction ? boost::math::float_next<result_type>(val, pol) : val == direction ? val : boost::math::float_prior<result_type>(val, pol);
  382. }
  383. template <class T, class U>
  384. inline typename tools::promote_args<T, U>::type nextafter(const T& val, const U& direction)
  385. {
  386. return nextafter(val, direction, policies::policy<>());
  387. }
  388. namespace detail{
  389. template <class T, class Policy>
  390. T float_distance_imp(const T& a, const T& b, const std::true_type&, const Policy& pol)
  391. {
  392. BOOST_MATH_STD_USING
  393. //
  394. // Error handling:
  395. //
  396. static const char* function = "float_distance<%1%>(%1%, %1%)";
  397. if(!(boost::math::isfinite)(a))
  398. return policies::raise_domain_error<T>(
  399. function,
  400. "Argument a must be finite, but got %1%", a, pol);
  401. if(!(boost::math::isfinite)(b))
  402. return policies::raise_domain_error<T>(
  403. function,
  404. "Argument b must be finite, but got %1%", b, pol);
  405. //
  406. // Special cases:
  407. //
  408. if(a > b)
  409. return -float_distance(b, a, pol);
  410. if(a == b)
  411. return T(0);
  412. if(a == 0)
  413. return 1 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol));
  414. if(b == 0)
  415. return 1 + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  416. if(boost::math::sign(a) != boost::math::sign(b))
  417. return 2 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol))
  418. + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  419. //
  420. // By the time we get here, both a and b must have the same sign, we want
  421. // b > a and both positive for the following logic:
  422. //
  423. if(a < 0)
  424. return float_distance(static_cast<T>(-b), static_cast<T>(-a), pol);
  425. BOOST_MATH_ASSERT(a >= 0);
  426. BOOST_MATH_ASSERT(b >= a);
  427. int expon;
  428. //
  429. // Note that if a is a denorm then the usual formula fails
  430. // because we actually have fewer than tools::digits<T>()
  431. // significant bits in the representation:
  432. //
  433. (void)frexp(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) ? tools::min_value<T>() : a, &expon);
  434. T upper = ldexp(T(1), expon);
  435. T result = T(0);
  436. //
  437. // If b is greater than upper, then we *must* split the calculation
  438. // as the size of the ULP changes with each order of magnitude change:
  439. //
  440. if(b > upper)
  441. {
  442. int expon2;
  443. (void)frexp(b, &expon2);
  444. T upper2 = ldexp(T(0.5), expon2);
  445. result = float_distance(upper2, b);
  446. result += (expon2 - expon - 1) * ldexp(T(1), tools::digits<T>() - 1);
  447. }
  448. //
  449. // Use compensated double-double addition to avoid rounding
  450. // errors in the subtraction:
  451. //
  452. expon = tools::digits<T>() - expon;
  453. T mb, x, y, z;
  454. if(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) || (b - a < tools::min_value<T>()))
  455. {
  456. //
  457. // Special case - either one end of the range is a denormal, or else the difference is.
  458. // The regular code will fail if we're using the SSE2 registers on Intel and either
  459. // the FTZ or DAZ flags are set.
  460. //
  461. T a2 = ldexp(a, tools::digits<T>());
  462. T b2 = ldexp(b, tools::digits<T>());
  463. mb = -(std::min)(T(ldexp(upper, tools::digits<T>())), b2);
  464. x = a2 + mb;
  465. z = x - a2;
  466. y = (a2 - (x - z)) + (mb - z);
  467. expon -= tools::digits<T>();
  468. }
  469. else
  470. {
  471. mb = -(std::min)(upper, b);
  472. x = a + mb;
  473. z = x - a;
  474. y = (a - (x - z)) + (mb - z);
  475. }
  476. if(x < 0)
  477. {
  478. x = -x;
  479. y = -y;
  480. }
  481. result += ldexp(x, expon) + ldexp(y, expon);
  482. //
  483. // Result must be an integer:
  484. //
  485. BOOST_MATH_ASSERT(result == floor(result));
  486. return result;
  487. } // float_distance_imp
  488. //
  489. // Special versions for bases other than 2:
  490. //
  491. template <class T, class Policy>
  492. T float_distance_imp(const T& a, const T& b, const std::false_type&, const Policy& pol)
  493. {
  494. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  495. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  496. BOOST_MATH_STD_USING
  497. //
  498. // Error handling:
  499. //
  500. static const char* function = "float_distance<%1%>(%1%, %1%)";
  501. if(!(boost::math::isfinite)(a))
  502. return policies::raise_domain_error<T>(
  503. function,
  504. "Argument a must be finite, but got %1%", a, pol);
  505. if(!(boost::math::isfinite)(b))
  506. return policies::raise_domain_error<T>(
  507. function,
  508. "Argument b must be finite, but got %1%", b, pol);
  509. //
  510. // Special cases:
  511. //
  512. if(a > b)
  513. return -float_distance(b, a, pol);
  514. if(a == b)
  515. return T(0);
  516. if(a == 0)
  517. return 1 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol));
  518. if(b == 0)
  519. return 1 + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  520. if(boost::math::sign(a) != boost::math::sign(b))
  521. return 2 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol))
  522. + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
  523. //
  524. // By the time we get here, both a and b must have the same sign, we want
  525. // b > a and both positive for the following logic:
  526. //
  527. if(a < 0)
  528. return float_distance(static_cast<T>(-b), static_cast<T>(-a), pol);
  529. BOOST_MATH_ASSERT(a >= 0);
  530. BOOST_MATH_ASSERT(b >= a);
  531. std::intmax_t expon;
  532. //
  533. // Note that if a is a denorm then the usual formula fails
  534. // because we actually have fewer than tools::digits<T>()
  535. // significant bits in the representation:
  536. //
  537. expon = 1 + ilogb(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) ? tools::min_value<T>() : a);
  538. T upper = scalbn(T(1), expon);
  539. T result = T(0);
  540. //
  541. // If b is greater than upper, then we *must* split the calculation
  542. // as the size of the ULP changes with each order of magnitude change:
  543. //
  544. if(b > upper)
  545. {
  546. std::intmax_t expon2 = 1 + ilogb(b);
  547. T upper2 = scalbn(T(1), expon2 - 1);
  548. result = float_distance(upper2, b);
  549. result += (expon2 - expon - 1) * scalbn(T(1), std::numeric_limits<T>::digits - 1);
  550. }
  551. //
  552. // Use compensated double-double addition to avoid rounding
  553. // errors in the subtraction:
  554. //
  555. expon = std::numeric_limits<T>::digits - expon;
  556. T mb, x, y, z;
  557. if(((boost::math::fpclassify)(a) == (int)FP_SUBNORMAL) || (b - a < tools::min_value<T>()))
  558. {
  559. //
  560. // Special case - either one end of the range is a denormal, or else the difference is.
  561. // The regular code will fail if we're using the SSE2 registers on Intel and either
  562. // the FTZ or DAZ flags are set.
  563. //
  564. T a2 = scalbn(a, std::numeric_limits<T>::digits);
  565. T b2 = scalbn(b, std::numeric_limits<T>::digits);
  566. mb = -(std::min)(T(scalbn(upper, std::numeric_limits<T>::digits)), b2);
  567. x = a2 + mb;
  568. z = x - a2;
  569. y = (a2 - (x - z)) + (mb - z);
  570. expon -= std::numeric_limits<T>::digits;
  571. }
  572. else
  573. {
  574. mb = -(std::min)(upper, b);
  575. x = a + mb;
  576. z = x - a;
  577. y = (a - (x - z)) + (mb - z);
  578. }
  579. if(x < 0)
  580. {
  581. x = -x;
  582. y = -y;
  583. }
  584. result += scalbn(x, expon) + scalbn(y, expon);
  585. //
  586. // Result must be an integer:
  587. //
  588. BOOST_MATH_ASSERT(result == floor(result));
  589. return result;
  590. } // float_distance_imp
  591. } // namespace detail
  592. template <class T, class U, class Policy>
  593. inline typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b, const Policy& pol)
  594. {
  595. //
  596. // We allow ONE of a and b to be an integer type, otherwise both must be the SAME type.
  597. //
  598. static_assert(
  599. (std::is_same<T, U>::value
  600. || (std::is_integral<T>::value && !std::is_integral<U>::value)
  601. || (!std::is_integral<T>::value && std::is_integral<U>::value)
  602. || (std::numeric_limits<T>::is_specialized && std::numeric_limits<U>::is_specialized
  603. && (std::numeric_limits<T>::digits == std::numeric_limits<U>::digits)
  604. && (std::numeric_limits<T>::radix == std::numeric_limits<U>::radix)
  605. && !std::numeric_limits<T>::is_integer && !std::numeric_limits<U>::is_integer)),
  606. "Float distance between two different floating point types is undefined.");
  607. BOOST_IF_CONSTEXPR (!std::is_same<T, U>::value)
  608. {
  609. BOOST_IF_CONSTEXPR(std::is_integral<T>::value)
  610. {
  611. return float_distance(static_cast<U>(a), b, pol);
  612. }
  613. else
  614. {
  615. return float_distance(a, static_cast<T>(b), pol);
  616. }
  617. }
  618. else
  619. {
  620. typedef typename tools::promote_args<T, U>::type result_type;
  621. 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);
  622. }
  623. }
  624. template <class T, class U>
  625. typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b)
  626. {
  627. return boost::math::float_distance(a, b, policies::policy<>());
  628. }
  629. namespace detail{
  630. template <class T, class Policy>
  631. T float_advance_imp(T val, int distance, const std::true_type&, const Policy& pol)
  632. {
  633. BOOST_MATH_STD_USING
  634. //
  635. // Error handling:
  636. //
  637. static const char* function = "float_advance<%1%>(%1%, int)";
  638. int fpclass = (boost::math::fpclassify)(val);
  639. if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
  640. return policies::raise_domain_error<T>(
  641. function,
  642. "Argument val must be finite, but got %1%", val, pol);
  643. if(val < 0)
  644. return -float_advance(-val, -distance, pol);
  645. if(distance == 0)
  646. return val;
  647. if(distance == 1)
  648. return float_next(val, pol);
  649. if(distance == -1)
  650. return float_prior(val, pol);
  651. if(fabs(val) < detail::get_min_shift_value<T>())
  652. {
  653. //
  654. // Special case: if the value of the least significant bit is a denorm,
  655. // implement in terms of float_next/float_prior.
  656. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  657. //
  658. if(distance > 0)
  659. {
  660. do{ val = float_next(val, pol); } while(--distance);
  661. }
  662. else
  663. {
  664. do{ val = float_prior(val, pol); } while(++distance);
  665. }
  666. return val;
  667. }
  668. int expon;
  669. (void)frexp(val, &expon);
  670. T limit = ldexp((distance < 0 ? T(0.5f) : T(1)), expon);
  671. if(val <= tools::min_value<T>())
  672. {
  673. limit = sign(T(distance)) * tools::min_value<T>();
  674. }
  675. T limit_distance = float_distance(val, limit);
  676. while(fabs(limit_distance) < abs(distance))
  677. {
  678. distance -= itrunc(limit_distance);
  679. val = limit;
  680. if(distance < 0)
  681. {
  682. limit /= 2;
  683. expon--;
  684. }
  685. else
  686. {
  687. limit *= 2;
  688. expon++;
  689. }
  690. limit_distance = float_distance(val, limit);
  691. if(distance && (limit_distance == 0))
  692. {
  693. 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);
  694. }
  695. }
  696. if((0.5f == frexp(val, &expon)) && (distance < 0))
  697. --expon;
  698. T diff = 0;
  699. if(val != 0)
  700. diff = distance * ldexp(T(1), expon - tools::digits<T>());
  701. if(diff == 0)
  702. diff = distance * detail::get_smallest_value<T>();
  703. return val += diff;
  704. } // float_advance_imp
  705. //
  706. // Special version for bases other than 2:
  707. //
  708. template <class T, class Policy>
  709. T float_advance_imp(T val, int distance, const std::false_type&, const Policy& pol)
  710. {
  711. static_assert(std::numeric_limits<T>::is_specialized, "Type T must be specialized.");
  712. static_assert(std::numeric_limits<T>::radix != 2, "Type T must be specialized.");
  713. BOOST_MATH_STD_USING
  714. //
  715. // Error handling:
  716. //
  717. static const char* function = "float_advance<%1%>(%1%, int)";
  718. int fpclass = (boost::math::fpclassify)(val);
  719. if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
  720. return policies::raise_domain_error<T>(
  721. function,
  722. "Argument val must be finite, but got %1%", val, pol);
  723. if(val < 0)
  724. return -float_advance(-val, -distance, pol);
  725. if(distance == 0)
  726. return val;
  727. if(distance == 1)
  728. return float_next(val, pol);
  729. if(distance == -1)
  730. return float_prior(val, pol);
  731. if(fabs(val) < detail::get_min_shift_value<T>())
  732. {
  733. //
  734. // Special case: if the value of the least significant bit is a denorm,
  735. // implement in terms of float_next/float_prior.
  736. // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
  737. //
  738. if(distance > 0)
  739. {
  740. do{ val = float_next(val, pol); } while(--distance);
  741. }
  742. else
  743. {
  744. do{ val = float_prior(val, pol); } while(++distance);
  745. }
  746. return val;
  747. }
  748. std::intmax_t expon = 1 + ilogb(val);
  749. T limit = scalbn(T(1), distance < 0 ? expon - 1 : expon);
  750. if(val <= tools::min_value<T>())
  751. {
  752. limit = sign(T(distance)) * tools::min_value<T>();
  753. }
  754. T limit_distance = float_distance(val, limit);
  755. while(fabs(limit_distance) < abs(distance))
  756. {
  757. distance -= itrunc(limit_distance);
  758. val = limit;
  759. if(distance < 0)
  760. {
  761. limit /= std::numeric_limits<T>::radix;
  762. expon--;
  763. }
  764. else
  765. {
  766. limit *= std::numeric_limits<T>::radix;
  767. expon++;
  768. }
  769. limit_distance = float_distance(val, limit);
  770. if(distance && (limit_distance == 0))
  771. {
  772. 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);
  773. }
  774. }
  775. /*expon = 1 + ilogb(val);
  776. if((1 == scalbn(val, 1 + expon)) && (distance < 0))
  777. --expon;*/
  778. T diff = 0;
  779. if(val != 0)
  780. diff = distance * scalbn(T(1), expon - std::numeric_limits<T>::digits);
  781. if(diff == 0)
  782. diff = distance * detail::get_smallest_value<T>();
  783. return val += diff;
  784. } // float_advance_imp
  785. } // namespace detail
  786. template <class T, class Policy>
  787. inline typename tools::promote_args<T>::type float_advance(T val, int distance, const Policy& pol)
  788. {
  789. typedef typename tools::promote_args<T>::type result_type;
  790. 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);
  791. }
  792. template <class T>
  793. inline typename tools::promote_args<T>::type float_advance(const T& val, int distance)
  794. {
  795. return boost::math::float_advance(val, distance, policies::policy<>());
  796. }
  797. }} // boost math namespaces
  798. #endif // BOOST_MATH_SPECIAL_NEXT_HPP