fpclassify.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. // Copyright John Maddock 2005-2008.
  2. // Copyright (c) 2006-2008 Johan Rade
  3. // Copyright (c) 2024 Matt Borland
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MATH_FPCLASSIFY_HPP
  8. #define BOOST_MATH_FPCLASSIFY_HPP
  9. #ifdef _MSC_VER
  10. #pragma once
  11. #endif
  12. #include <boost/math/tools/config.hpp>
  13. #ifndef BOOST_MATH_HAS_NVRTC
  14. #include <boost/math/tools/real_cast.hpp>
  15. #include <boost/math/special_functions/math_fwd.hpp>
  16. #include <boost/math/special_functions/detail/fp_traits.hpp>
  17. #include <limits>
  18. #include <type_traits>
  19. #include <cmath>
  20. /*!
  21. \file fpclassify.hpp
  22. \brief Classify floating-point value as normal, subnormal, zero, infinite, or NaN.
  23. \version 1.0
  24. \author John Maddock
  25. */
  26. /*
  27. 1. If the platform is C99 compliant, then the native floating point
  28. classification functions are used. However, note that we must only
  29. define the functions which call std::fpclassify etc if that function
  30. really does exist: otherwise a compiler may reject the code even though
  31. the template is never instantiated.
  32. 2. If the platform is not C99 compliant, and the binary format for
  33. a floating point type (float, double or long double) can be determined
  34. at compile time, then the following algorithm is used:
  35. If all exponent bits, the flag bit (if there is one),
  36. and all significand bits are 0, then the number is zero.
  37. If all exponent bits and the flag bit (if there is one) are 0,
  38. and at least one significand bit is 1, then the number is subnormal.
  39. If all exponent bits are 1 and all significand bits are 0,
  40. then the number is infinity.
  41. If all exponent bits are 1 and at least one significand bit is 1,
  42. then the number is a not-a-number.
  43. Otherwise the number is normal.
  44. This algorithm works for the IEEE 754 representation,
  45. and also for several non IEEE 754 formats.
  46. Most formats have the structure
  47. sign bit + exponent bits + significand bits.
  48. A few have the structure
  49. sign bit + exponent bits + flag bit + significand bits.
  50. The flag bit is 0 for zero and subnormal numbers,
  51. and 1 for normal numbers and NaN.
  52. It is 0 (Motorola 68K) or 1 (Intel) for infinity.
  53. To get the bits, the four or eight most significant bytes are copied
  54. into an uint32_t or uint64_t and bit masks are applied.
  55. This covers all the exponent bits and the flag bit (if there is one),
  56. but not always all the significand bits.
  57. Some of the functions below have two implementations,
  58. depending on whether all the significand bits are copied or not.
  59. 3. If the platform is not C99 compliant, and the binary format for
  60. a floating point type (float, double or long double) can not be determined
  61. at compile time, then comparison with std::numeric_limits values
  62. is used.
  63. */
  64. #ifdef BOOST_MATH_HAS_GPU_SUPPORT
  65. namespace boost { namespace math {
  66. template<> BOOST_MATH_GPU_ENABLED inline bool (isnan)(float x) { return x != x; }
  67. template<> BOOST_MATH_GPU_ENABLED inline bool (isnan)(double x) { return x != x; }
  68. template<> BOOST_MATH_GPU_ENABLED inline bool (isinf)(float x) { return x > FLT_MAX || x < -FLT_MAX; }
  69. template<> BOOST_MATH_GPU_ENABLED inline bool (isinf)(double x) { return x > DBL_MAX || x < -DBL_MAX; }
  70. template<> BOOST_MATH_GPU_ENABLED inline bool (isfinite)(float x) { return !isnan(x) && !isinf(x); }
  71. template<> BOOST_MATH_GPU_ENABLED inline bool (isfinite)(double x) { return !isnan(x) && !isinf(x); }
  72. template<> BOOST_MATH_GPU_ENABLED inline bool (isnormal)(float x)
  73. {
  74. if(x < 0) x = -x;
  75. return (x >= FLT_MIN) && (x <= FLT_MAX);
  76. }
  77. template<> BOOST_MATH_GPU_ENABLED inline bool (isnormal)(double x)
  78. {
  79. if(x < 0) x = -x;
  80. return (x >= DBL_MIN) && (x <= DBL_MAX);
  81. }
  82. template<> BOOST_MATH_GPU_ENABLED inline int (fpclassify)(float t)
  83. {
  84. if((boost::math::isnan)(t))
  85. return FP_NAN;
  86. // std::fabs broken on a few systems especially for long long!!!!
  87. float at = (t < 0.0f) ? -t : t;
  88. // Use a process of exclusion to figure out
  89. // what kind of type we have, this relies on
  90. // IEEE conforming reals that will treat
  91. // Nan's as unordered. Some compilers
  92. // don't do this once optimisations are
  93. // turned on, hence the check for nan's above.
  94. if(at <= FLT_MAX)
  95. {
  96. if(at >= FLT_MIN)
  97. return FP_NORMAL;
  98. return (at != 0) ? FP_SUBNORMAL : FP_ZERO;
  99. }
  100. else if(at > FLT_MAX)
  101. return FP_INFINITE;
  102. return FP_NAN; // LCOV_EXCL_LINE should not normally be reachable.
  103. }
  104. template<> BOOST_MATH_GPU_ENABLED inline int (fpclassify)(double t)
  105. {
  106. if((boost::math::isnan)(t))
  107. return FP_NAN;
  108. // std::fabs broken on a few systems especially for long long!!!!
  109. double at = (t < 0.0) ? -t : t;
  110. // Use a process of exclusion to figure out
  111. // what kind of type we have, this relies on
  112. // IEEE conforming reals that will treat
  113. // Nan's as unordered. Some compilers
  114. // don't do this once optimisations are
  115. // turned on, hence the check for nan's above.
  116. if(at <= DBL_MAX)
  117. {
  118. if(at >= DBL_MIN)
  119. return FP_NORMAL;
  120. return (at != 0) ? FP_SUBNORMAL : FP_ZERO;
  121. }
  122. else if(at > DBL_MAX)
  123. return FP_INFINITE;
  124. return FP_NAN;
  125. }
  126. #else
  127. #if defined(_MSC_VER) || defined(BOOST_BORLANDC)
  128. #include <cfloat>
  129. #endif
  130. #ifdef BOOST_MATH_USE_FLOAT128
  131. #ifdef __has_include
  132. #if __has_include("quadmath.h")
  133. #include "quadmath.h"
  134. #define BOOST_MATH_HAS_QUADMATH_H
  135. #endif
  136. #endif
  137. #endif
  138. #ifdef BOOST_NO_STDC_NAMESPACE
  139. namespace std{ using ::abs; using ::fabs; }
  140. #endif
  141. namespace boost{
  142. //
  143. // This must not be located in any namespace under boost::math
  144. // otherwise we can get into an infinite loop if isnan is
  145. // a #define for "isnan" !
  146. //
  147. namespace math_detail{
  148. #ifdef _MSC_VER
  149. #pragma warning(push)
  150. #pragma warning(disable:4800)
  151. #endif
  152. template <class T>
  153. inline bool is_nan_helper(T t, const std::true_type&)
  154. {
  155. #ifdef isnan
  156. return isnan(t);
  157. #elif defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY) || !defined(BOOST_HAS_FPCLASSIFY)
  158. (void)t;
  159. return false;
  160. #else // BOOST_HAS_FPCLASSIFY
  161. return (BOOST_FPCLASSIFY_PREFIX fpclassify(t) == (int)FP_NAN);
  162. #endif
  163. }
  164. #ifdef _MSC_VER
  165. #pragma warning(pop)
  166. #endif
  167. template <class T>
  168. inline bool is_nan_helper(T, const std::false_type&)
  169. {
  170. return false;
  171. }
  172. #if defined(BOOST_MATH_USE_FLOAT128)
  173. #if defined(BOOST_MATH_HAS_QUADMATH_H)
  174. inline bool is_nan_helper(__float128 f, const std::true_type&) { return ::isnanq(f); }
  175. inline bool is_nan_helper(__float128 f, const std::false_type&) { return ::isnanq(f); }
  176. #elif defined(BOOST_GNU_STDLIB) && BOOST_GNU_STDLIB && \
  177. _GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
  178. inline bool is_nan_helper(__float128 f, const std::true_type&) { return std::isnan(static_cast<double>(f)); }
  179. inline bool is_nan_helper(__float128 f, const std::false_type&) { return std::isnan(static_cast<double>(f)); }
  180. #else
  181. inline bool is_nan_helper(__float128 f, const std::true_type&) { return boost::math::isnan(static_cast<double>(f)); }
  182. inline bool is_nan_helper(__float128 f, const std::false_type&) { return boost::math::isnan(static_cast<double>(f)); }
  183. #endif
  184. #endif
  185. }
  186. namespace math{
  187. namespace detail{
  188. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  189. template <class T>
  190. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const native_tag&)
  191. {
  192. return (std::fpclassify)(t);
  193. }
  194. #endif
  195. template <class T>
  196. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const generic_tag<true>&)
  197. {
  198. BOOST_MATH_INSTRUMENT_VARIABLE(t);
  199. // whenever possible check for Nan's first:
  200. #if defined(BOOST_HAS_FPCLASSIFY) && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)
  201. if(::boost::math_detail::is_nan_helper(t, typename std::is_floating_point<T>::type()))
  202. return FP_NAN; // LCOV_EXCL_LINE only called in UDT contexts (excluded from coverage checks).
  203. #elif defined(isnan)
  204. if(boost::math_detail::is_nan_helper(t, typename std::is_floating_point<T>::type()))
  205. return FP_NAN;
  206. #elif defined(_MSC_VER) || defined(BOOST_BORLANDC)
  207. if(::_isnan(boost::math::tools::real_cast<double>(t)))
  208. return FP_NAN;
  209. #endif
  210. // std::fabs broken on a few systems especially for long long!!!!
  211. T at = (t < T(0)) ? -t : t;
  212. // Use a process of exclusion to figure out
  213. // what kind of type we have, this relies on
  214. // IEEE conforming reals that will treat
  215. // Nan's as unordered. Some compilers
  216. // don't do this once optimisations are
  217. // turned on, hence the check for nan's above.
  218. if(at <= (std::numeric_limits<T>::max)())
  219. {
  220. if(at >= (std::numeric_limits<T>::min)())
  221. return FP_NORMAL;
  222. return (at != 0) ? FP_SUBNORMAL : FP_ZERO;
  223. }
  224. else if(at > (std::numeric_limits<T>::max)())
  225. return FP_INFINITE;
  226. return FP_NAN;
  227. }
  228. template <class T>
  229. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(T t, const generic_tag<false>&)
  230. {
  231. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  232. if(std::numeric_limits<T>::is_specialized)
  233. return fpclassify_imp(t, generic_tag<true>());
  234. #endif
  235. //
  236. // An unknown type with no numeric_limits support,
  237. // so what are we supposed to do we do here?
  238. //
  239. BOOST_MATH_INSTRUMENT_VARIABLE(t);
  240. return t == 0 ? FP_ZERO : FP_NORMAL;
  241. }
  242. template<class T>
  243. int fpclassify_imp BOOST_NO_MACRO_EXPAND(T x, ieee_copy_all_bits_tag)
  244. {
  245. typedef typename fp_traits<T>::type traits;
  246. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  247. typename traits::bits a;
  248. traits::get_bits(x,a);
  249. BOOST_MATH_INSTRUMENT_VARIABLE(a);
  250. a &= traits::exponent | traits::flag | traits::significand;
  251. BOOST_MATH_INSTRUMENT_VARIABLE((traits::exponent | traits::flag | traits::significand));
  252. BOOST_MATH_INSTRUMENT_VARIABLE(a);
  253. if(a <= traits::significand) {
  254. if(a == 0)
  255. return FP_ZERO;
  256. else
  257. return FP_SUBNORMAL;
  258. }
  259. if(a < traits::exponent) return FP_NORMAL;
  260. a &= traits::significand;
  261. if(a == 0) return FP_INFINITE;
  262. return FP_NAN;
  263. }
  264. template<class T>
  265. int fpclassify_imp BOOST_NO_MACRO_EXPAND(T x, ieee_copy_leading_bits_tag)
  266. {
  267. typedef typename fp_traits<T>::type traits;
  268. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  269. typename traits::bits a;
  270. traits::get_bits(x,a);
  271. a &= traits::exponent | traits::flag | traits::significand;
  272. if(a <= traits::significand) {
  273. if(x == 0)
  274. return FP_ZERO;
  275. else
  276. return FP_SUBNORMAL;
  277. }
  278. if(a < traits::exponent) return FP_NORMAL;
  279. a &= traits::significand;
  280. traits::set_bits(x,a);
  281. if(x == 0) return FP_INFINITE;
  282. return FP_NAN;
  283. }
  284. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && (defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY) || defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS))
  285. inline int fpclassify_imp BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  286. {
  287. return boost::math::detail::fpclassify_imp(t, generic_tag<true>());
  288. }
  289. #endif
  290. } // namespace detail
  291. template <class T>
  292. inline int fpclassify BOOST_NO_MACRO_EXPAND(T t)
  293. {
  294. typedef typename detail::fp_traits<T>::type traits;
  295. typedef typename traits::method method;
  296. typedef typename tools::promote_args_permissive<T>::type value_type;
  297. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  298. if(std::numeric_limits<T>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(nullptr)))
  299. return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>());
  300. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  301. #else
  302. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  303. #endif
  304. }
  305. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  306. template <>
  307. inline int fpclassify<long double> BOOST_NO_MACRO_EXPAND(long double t)
  308. {
  309. typedef detail::fp_traits<long double>::type traits;
  310. typedef traits::method method;
  311. typedef long double value_type;
  312. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  313. if(std::numeric_limits<long double>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(nullptr)))
  314. return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>());
  315. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  316. #else
  317. return detail::fpclassify_imp(static_cast<value_type>(t), method());
  318. #endif
  319. }
  320. #endif
  321. namespace detail {
  322. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  323. template<class T>
  324. inline bool isfinite_impl(T x, native_tag const&)
  325. {
  326. return (std::isfinite)(x);
  327. }
  328. #endif
  329. template<class T>
  330. inline bool isfinite_impl(T x, generic_tag<true> const&)
  331. {
  332. return x >= -(std::numeric_limits<T>::max)()
  333. && x <= (std::numeric_limits<T>::max)();
  334. }
  335. template<class T>
  336. inline bool isfinite_impl(T x, generic_tag<false> const&)
  337. {
  338. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  339. if(std::numeric_limits<T>::is_specialized)
  340. return isfinite_impl(x, generic_tag<true>());
  341. #endif
  342. (void)x; // warning suppression.
  343. return true;
  344. }
  345. template<class T>
  346. inline bool isfinite_impl(T x, ieee_tag const&)
  347. {
  348. typedef typename detail::fp_traits<T>::type traits;
  349. typename traits::bits a;
  350. traits::get_bits(x,a);
  351. a &= traits::exponent;
  352. return a != traits::exponent;
  353. }
  354. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
  355. inline bool isfinite_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  356. {
  357. return boost::math::detail::isfinite_impl(t, generic_tag<true>());
  358. }
  359. #endif
  360. }
  361. template<class T>
  362. inline bool (isfinite)(T x)
  363. { //!< \brief return true if floating-point type t is finite.
  364. typedef typename detail::fp_traits<T>::type traits;
  365. typedef typename traits::method method;
  366. // typedef typename boost::is_floating_point<T>::type fp_tag;
  367. typedef typename tools::promote_args_permissive<T>::type value_type;
  368. return detail::isfinite_impl(static_cast<value_type>(x), method());
  369. }
  370. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  371. template<>
  372. inline bool (isfinite)(long double x)
  373. { //!< \brief return true if floating-point type t is finite.
  374. typedef detail::fp_traits<long double>::type traits;
  375. typedef traits::method method;
  376. //typedef boost::is_floating_point<long double>::type fp_tag;
  377. typedef long double value_type;
  378. return detail::isfinite_impl(static_cast<value_type>(x), method());
  379. }
  380. #endif
  381. //------------------------------------------------------------------------------
  382. namespace detail {
  383. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  384. template<class T>
  385. inline bool isnormal_impl(T x, native_tag const&)
  386. {
  387. return (std::isnormal)(x);
  388. }
  389. #endif
  390. template<class T>
  391. inline bool isnormal_impl(T x, generic_tag<true> const&)
  392. {
  393. if(x < 0) x = -x;
  394. return x >= (std::numeric_limits<T>::min)()
  395. && x <= (std::numeric_limits<T>::max)();
  396. }
  397. template<class T>
  398. inline bool isnormal_impl(T x, generic_tag<false> const&)
  399. {
  400. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  401. if(std::numeric_limits<T>::is_specialized)
  402. return isnormal_impl(x, generic_tag<true>());
  403. #endif
  404. return !(x == 0);
  405. }
  406. template<class T>
  407. inline bool isnormal_impl(T x, ieee_tag const&)
  408. {
  409. typedef typename detail::fp_traits<T>::type traits;
  410. typename traits::bits a;
  411. traits::get_bits(x,a);
  412. a &= traits::exponent | traits::flag;
  413. return (a != 0) && (a < traits::exponent);
  414. }
  415. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
  416. inline bool isnormal_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  417. {
  418. return boost::math::detail::isnormal_impl(t, generic_tag<true>());
  419. }
  420. #endif
  421. }
  422. template<class T>
  423. inline bool (isnormal)(T x)
  424. {
  425. typedef typename detail::fp_traits<T>::type traits;
  426. typedef typename traits::method method;
  427. //typedef typename boost::is_floating_point<T>::type fp_tag;
  428. typedef typename tools::promote_args_permissive<T>::type value_type;
  429. return detail::isnormal_impl(static_cast<value_type>(x), method());
  430. }
  431. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  432. template<>
  433. inline bool (isnormal)(long double x)
  434. {
  435. typedef detail::fp_traits<long double>::type traits;
  436. typedef traits::method method;
  437. //typedef boost::is_floating_point<long double>::type fp_tag;
  438. typedef long double value_type;
  439. return detail::isnormal_impl(static_cast<value_type>(x), method());
  440. }
  441. #endif
  442. //------------------------------------------------------------------------------
  443. namespace detail {
  444. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  445. template<class T>
  446. inline bool isinf_impl(T x, native_tag const&)
  447. {
  448. return (std::isinf)(x);
  449. }
  450. #endif
  451. template<class T>
  452. inline bool isinf_impl(T x, generic_tag<true> const&)
  453. {
  454. (void)x; // in case the compiler thinks that x is unused because std::numeric_limits<T>::has_infinity is false
  455. return std::numeric_limits<T>::has_infinity
  456. && ( x == std::numeric_limits<T>::infinity()
  457. || x == -std::numeric_limits<T>::infinity());
  458. }
  459. template<class T>
  460. inline bool isinf_impl(T x, generic_tag<false> const&)
  461. {
  462. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  463. if(std::numeric_limits<T>::is_specialized)
  464. return isinf_impl(x, generic_tag<true>());
  465. #endif
  466. (void)x; // warning suppression.
  467. return false;
  468. }
  469. template<class T>
  470. inline bool isinf_impl(T x, ieee_copy_all_bits_tag const&)
  471. {
  472. typedef typename fp_traits<T>::type traits;
  473. typename traits::bits a;
  474. traits::get_bits(x,a);
  475. a &= traits::exponent | traits::significand;
  476. return a == traits::exponent;
  477. }
  478. template<class T>
  479. inline bool isinf_impl(T x, ieee_copy_leading_bits_tag const&)
  480. {
  481. typedef typename fp_traits<T>::type traits;
  482. typename traits::bits a;
  483. traits::get_bits(x,a);
  484. a &= traits::exponent | traits::significand;
  485. if(a != traits::exponent)
  486. return false;
  487. traits::set_bits(x,0);
  488. return x == 0;
  489. }
  490. #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && defined(BOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY)
  491. inline bool isinf_impl BOOST_NO_MACRO_EXPAND(long double t, const native_tag&)
  492. {
  493. return boost::math::detail::isinf_impl(t, generic_tag<true>());
  494. }
  495. #endif
  496. } // namespace detail
  497. template<class T>
  498. inline bool (isinf)(T x)
  499. {
  500. typedef typename detail::fp_traits<T>::type traits;
  501. typedef typename traits::method method;
  502. // typedef typename boost::is_floating_point<T>::type fp_tag;
  503. typedef typename tools::promote_args_permissive<T>::type value_type;
  504. return detail::isinf_impl(static_cast<value_type>(x), method());
  505. }
  506. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  507. template<>
  508. inline bool (isinf)(long double x)
  509. {
  510. typedef detail::fp_traits<long double>::type traits;
  511. typedef traits::method method;
  512. //typedef boost::is_floating_point<long double>::type fp_tag;
  513. typedef long double value_type;
  514. return detail::isinf_impl(static_cast<value_type>(x), method());
  515. }
  516. #endif
  517. #if defined(BOOST_MATH_USE_FLOAT128) && defined(BOOST_MATH_HAS_QUADMATH_H)
  518. template<>
  519. inline bool (isinf)(__float128 x)
  520. {
  521. return ::isinfq(x);
  522. }
  523. #endif
  524. //------------------------------------------------------------------------------
  525. namespace detail {
  526. #ifdef BOOST_MATH_USE_STD_FPCLASSIFY
  527. template<class T>
  528. inline bool isnan_impl(T x, native_tag const&)
  529. {
  530. return (std::isnan)(x);
  531. }
  532. #endif
  533. template<class T>
  534. inline bool isnan_impl(T x, generic_tag<true> const&)
  535. {
  536. return std::numeric_limits<T>::has_infinity
  537. ? !(x <= std::numeric_limits<T>::infinity())
  538. : x != x;
  539. }
  540. template<class T>
  541. inline bool isnan_impl(T x, generic_tag<false> const&)
  542. {
  543. #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  544. if(std::numeric_limits<T>::is_specialized)
  545. return isnan_impl(x, generic_tag<true>());
  546. #endif
  547. (void)x; // warning suppression
  548. return false;
  549. }
  550. template<class T>
  551. inline bool isnan_impl(T x, ieee_copy_all_bits_tag const&)
  552. {
  553. typedef typename fp_traits<T>::type traits;
  554. typename traits::bits a;
  555. traits::get_bits(x,a);
  556. a &= traits::exponent | traits::significand;
  557. return a > traits::exponent;
  558. }
  559. template<class T>
  560. inline bool isnan_impl(T x, ieee_copy_leading_bits_tag const&)
  561. {
  562. typedef typename fp_traits<T>::type traits;
  563. typename traits::bits a;
  564. traits::get_bits(x,a);
  565. a &= traits::exponent | traits::significand;
  566. if(a < traits::exponent)
  567. return false;
  568. a &= traits::significand;
  569. traits::set_bits(x,a);
  570. return x != 0;
  571. }
  572. } // namespace detail
  573. template<class T>
  574. inline bool (isnan)(T x)
  575. { //!< \brief return true if floating-point type t is NaN (Not A Number).
  576. typedef typename detail::fp_traits<T>::type traits;
  577. typedef typename traits::method method;
  578. // typedef typename boost::is_floating_point<T>::type fp_tag;
  579. return detail::isnan_impl(x, method());
  580. }
  581. #ifdef isnan
  582. template <> inline bool isnan BOOST_NO_MACRO_EXPAND<float>(float t){ return ::boost::math_detail::is_nan_helper(t, std::true_type()); }
  583. template <> inline bool isnan BOOST_NO_MACRO_EXPAND<double>(double t){ return ::boost::math_detail::is_nan_helper(t, std::true_type()); }
  584. template <> inline bool isnan BOOST_NO_MACRO_EXPAND<long double>(long double t){ return ::boost::math_detail::is_nan_helper(t, std::true_type()); }
  585. #elif defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
  586. template<>
  587. inline bool (isnan)(long double x)
  588. { //!< \brief return true if floating-point type t is NaN (Not A Number).
  589. typedef detail::fp_traits<long double>::type traits;
  590. typedef traits::method method;
  591. //typedef boost::is_floating_point<long double>::type fp_tag;
  592. return detail::isnan_impl(x, method());
  593. }
  594. #endif
  595. #if defined(BOOST_MATH_USE_FLOAT128) && defined(BOOST_MATH_HAS_QUADMATH_H)
  596. template<>
  597. inline bool (isnan)(__float128 x)
  598. {
  599. return ::isnanq(x);
  600. }
  601. #endif
  602. #endif
  603. } // namespace math
  604. } // namespace boost
  605. #else // Special handling generally using the CUDA library
  606. #include <boost/math/tools/type_traits.hpp>
  607. namespace boost {
  608. namespace math {
  609. template <typename T, boost::math::enable_if_t<boost::math::is_integral_v<T>, bool> = true>
  610. BOOST_MATH_GPU_ENABLED inline bool isnan(T x)
  611. {
  612. return false;
  613. }
  614. template <typename T, boost::math::enable_if_t<!boost::math::is_integral_v<T>, bool> = true>
  615. BOOST_MATH_GPU_ENABLED inline bool isnan(T x)
  616. {
  617. return ::isnan(x);
  618. }
  619. template <typename T, boost::math::enable_if_t<boost::math::is_integral_v<T>, bool> = true>
  620. BOOST_MATH_GPU_ENABLED inline bool isinf(T x)
  621. {
  622. return false;
  623. }
  624. template <typename T, boost::math::enable_if_t<!boost::math::is_integral_v<T>, bool> = true>
  625. BOOST_MATH_GPU_ENABLED inline bool isinf(T x)
  626. {
  627. return ::isinf(x);
  628. }
  629. template <typename T, boost::math::enable_if_t<boost::math::is_integral_v<T>, bool> = true>
  630. BOOST_MATH_GPU_ENABLED inline bool isfinite(T x)
  631. {
  632. return true;
  633. }
  634. template <typename T, boost::math::enable_if_t<!boost::math::is_integral_v<T>, bool> = true>
  635. BOOST_MATH_GPU_ENABLED inline bool isfinite(T x)
  636. {
  637. return ::isfinite(x);
  638. }
  639. template <typename T>
  640. BOOST_MATH_GPU_ENABLED inline bool isnormal(T x)
  641. {
  642. return x != static_cast<T>(0) && x != static_cast<T>(-0) &&
  643. !boost::math::isnan(x) &&
  644. !boost::math::isinf(x);
  645. }
  646. // We skip the check for FP_SUBNORMAL since they are not supported on these platforms
  647. template <typename T>
  648. BOOST_MATH_GPU_ENABLED inline int fpclassify(T x)
  649. {
  650. if (boost::math::isnan(x))
  651. {
  652. return BOOST_MATH_FP_NAN;
  653. }
  654. else if (boost::math::isinf(x))
  655. {
  656. return BOOST_MATH_FP_INFINITE;
  657. }
  658. else if (x == static_cast<T>(0) || x == static_cast<T>(-0))
  659. {
  660. return BOOST_MATH_FP_ZERO;
  661. }
  662. return BOOST_MATH_FP_NORMAL;
  663. }
  664. } // Namespace math
  665. } // Namespace boost
  666. #endif // BOOST_MATH_HAS_NVRTC
  667. #endif // BOOST_MATH_FPCLASSIFY_HPP