next.hpp 30 KB

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