| // Copyright John Maddock 2005-2008. |
| // Copyright (c) 2006-2008 Johan Rade |
| // Use, modification and distribution are subject to the |
| // Boost Software License, Version 1.0. (See accompanying file |
| // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| |
| #ifndef NDNBOOST_MATH_FPCLASSIFY_HPP |
| #define NDNBOOST_MATH_FPCLASSIFY_HPP |
| |
| #ifdef _MSC_VER |
| #pragma once |
| #endif |
| |
| #include <math.h> |
| #include <ndnboost/config/no_tr1/cmath.hpp> |
| #include <ndnboost/limits.hpp> |
| #include <ndnboost/math/tools/real_cast.hpp> |
| #include <ndnboost/type_traits/is_floating_point.hpp> |
| #include <ndnboost/math/special_functions/math_fwd.hpp> |
| #include <ndnboost/math/special_functions/detail/fp_traits.hpp> |
| /*! |
| \file fpclassify.hpp |
| \brief Classify floating-point value as normal, subnormal, zero, infinite, or NaN. |
| \version 1.0 |
| \author John Maddock |
| */ |
| |
| /* |
| |
| 1. If the platform is C99 compliant, then the native floating point |
| classification functions are used. However, note that we must only |
| define the functions which call std::fpclassify etc if that function |
| really does exist: otherwise a compiler may reject the code even though |
| the template is never instantiated. |
| |
| 2. If the platform is not C99 compliant, and the binary format for |
| a floating point type (float, double or long double) can be determined |
| at compile time, then the following algorithm is used: |
| |
| If all exponent bits, the flag bit (if there is one), |
| and all significand bits are 0, then the number is zero. |
| |
| If all exponent bits and the flag bit (if there is one) are 0, |
| and at least one significand bit is 1, then the number is subnormal. |
| |
| If all exponent bits are 1 and all significand bits are 0, |
| then the number is infinity. |
| |
| If all exponent bits are 1 and at least one significand bit is 1, |
| then the number is a not-a-number. |
| |
| Otherwise the number is normal. |
| |
| This algorithm works for the IEEE 754 representation, |
| and also for several non IEEE 754 formats. |
| |
| Most formats have the structure |
| sign bit + exponent bits + significand bits. |
| |
| A few have the structure |
| sign bit + exponent bits + flag bit + significand bits. |
| The flag bit is 0 for zero and subnormal numbers, |
| and 1 for normal numbers and NaN. |
| It is 0 (Motorola 68K) or 1 (Intel) for infinity. |
| |
| To get the bits, the four or eight most significant bytes are copied |
| into an uint32_t or uint64_t and bit masks are applied. |
| This covers all the exponent bits and the flag bit (if there is one), |
| but not always all the significand bits. |
| Some of the functions below have two implementations, |
| depending on whether all the significand bits are copied or not. |
| |
| 3. If the platform is not C99 compliant, and the binary format for |
| a floating point type (float, double or long double) can not be determined |
| at compile time, then comparison with std::numeric_limits values |
| is used. |
| |
| */ |
| |
| #if defined(_MSC_VER) || defined(__BORLANDC__) |
| #include <float.h> |
| #endif |
| |
| #ifdef NDNBOOST_NO_STDC_NAMESPACE |
| namespace std{ using ::abs; using ::fabs; } |
| #endif |
| |
| namespace ndnboost{ |
| |
| // |
| // This must not be located in any namespace under ndnboost::math |
| // otherwise we can get into an infinite loop if isnan is |
| // a #define for "isnan" ! |
| // |
| namespace math_detail{ |
| |
| #ifdef NDNBOOST_MSVC |
| #pragma warning(push) |
| #pragma warning(disable:4800) |
| #endif |
| |
| template <class T> |
| inline bool is_nan_helper(T t, const ndnboost::true_type&) |
| { |
| #ifdef isnan |
| return isnan(t); |
| #elif defined(NDNBOOST_MATH_DISABLE_STD_FPCLASSIFY) || !defined(NDNBOOST_HAS_FPCLASSIFY) |
| (void)t; |
| return false; |
| #else // NDNBOOST_HAS_FPCLASSIFY |
| return (NDNBOOST_FPCLASSIFY_PREFIX fpclassify(t) == (int)FP_NAN); |
| #endif |
| } |
| |
| #ifdef NDNBOOST_MSVC |
| #pragma warning(pop) |
| #endif |
| |
| template <class T> |
| inline bool is_nan_helper(T, const ndnboost::false_type&) |
| { |
| return false; |
| } |
| |
| } |
| |
| namespace math{ |
| |
| namespace detail{ |
| |
| #ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY |
| template <class T> |
| inline int fpclassify_imp NDNBOOST_NO_MACRO_EXPAND(T t, const native_tag&) |
| { |
| return (std::fpclassify)(t); |
| } |
| #endif |
| |
| template <class T> |
| inline int fpclassify_imp NDNBOOST_NO_MACRO_EXPAND(T t, const generic_tag<true>&) |
| { |
| NDNBOOST_MATH_INSTRUMENT_VARIABLE(t); |
| |
| // whenever possible check for Nan's first: |
| #if defined(NDNBOOST_HAS_FPCLASSIFY) && !defined(NDNBOOST_MATH_DISABLE_STD_FPCLASSIFY) |
| if(::ndnboost::math_detail::is_nan_helper(t, ::ndnboost::is_floating_point<T>())) |
| return FP_NAN; |
| #elif defined(isnan) |
| if(ndnboost::math_detail::is_nan_helper(t, ::ndnboost::is_floating_point<T>())) |
| return FP_NAN; |
| #elif defined(_MSC_VER) || defined(__BORLANDC__) |
| if(::_isnan(ndnboost::math::tools::real_cast<double>(t))) |
| return FP_NAN; |
| #endif |
| // std::fabs broken on a few systems especially for long long!!!! |
| T at = (t < T(0)) ? -t : t; |
| |
| // Use a process of exclusion to figure out |
| // what kind of type we have, this relies on |
| // IEEE conforming reals that will treat |
| // Nan's as unordered. Some compilers |
| // don't do this once optimisations are |
| // turned on, hence the check for nan's above. |
| if(at <= (std::numeric_limits<T>::max)()) |
| { |
| if(at >= (std::numeric_limits<T>::min)()) |
| return FP_NORMAL; |
| return (at != 0) ? FP_SUBNORMAL : FP_ZERO; |
| } |
| else if(at > (std::numeric_limits<T>::max)()) |
| return FP_INFINITE; |
| return FP_NAN; |
| } |
| |
| template <class T> |
| inline int fpclassify_imp NDNBOOST_NO_MACRO_EXPAND(T t, const generic_tag<false>&) |
| { |
| #ifdef NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
| if(std::numeric_limits<T>::is_specialized) |
| return fpclassify_imp(t, generic_tag<true>()); |
| #endif |
| // |
| // An unknown type with no numeric_limits support, |
| // so what are we supposed to do we do here? |
| // |
| NDNBOOST_MATH_INSTRUMENT_VARIABLE(t); |
| |
| return t == 0 ? FP_ZERO : FP_NORMAL; |
| } |
| |
| template<class T> |
| int fpclassify_imp NDNBOOST_NO_MACRO_EXPAND(T x, ieee_copy_all_bits_tag) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
| |
| NDNBOOST_MATH_INSTRUMENT_VARIABLE(x); |
| |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| NDNBOOST_MATH_INSTRUMENT_VARIABLE(a); |
| a &= traits::exponent | traits::flag | traits::significand; |
| NDNBOOST_MATH_INSTRUMENT_VARIABLE((traits::exponent | traits::flag | traits::significand)); |
| NDNBOOST_MATH_INSTRUMENT_VARIABLE(a); |
| |
| if(a <= traits::significand) { |
| if(a == 0) |
| return FP_ZERO; |
| else |
| return FP_SUBNORMAL; |
| } |
| |
| if(a < traits::exponent) return FP_NORMAL; |
| |
| a &= traits::significand; |
| if(a == 0) return FP_INFINITE; |
| |
| return FP_NAN; |
| } |
| |
| template<class T> |
| int fpclassify_imp NDNBOOST_NO_MACRO_EXPAND(T x, ieee_copy_leading_bits_tag) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
| |
| NDNBOOST_MATH_INSTRUMENT_VARIABLE(x); |
| |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| a &= traits::exponent | traits::flag | traits::significand; |
| |
| if(a <= traits::significand) { |
| if(x == 0) |
| return FP_ZERO; |
| else |
| return FP_SUBNORMAL; |
| } |
| |
| if(a < traits::exponent) return FP_NORMAL; |
| |
| a &= traits::significand; |
| traits::set_bits(x,a); |
| if(x == 0) return FP_INFINITE; |
| |
| return FP_NAN; |
| } |
| |
| #if defined(NDNBOOST_MATH_USE_STD_FPCLASSIFY) && (defined(NDNBOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY) || defined(NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)) |
| inline int fpclassify_imp NDNBOOST_NO_MACRO_EXPAND(long double t, const native_tag&) |
| { |
| return ndnboost::math::detail::fpclassify_imp(t, generic_tag<true>()); |
| } |
| #endif |
| |
| } // namespace detail |
| |
| template <class T> |
| inline int fpclassify NDNBOOST_NO_MACRO_EXPAND(T t) |
| { |
| typedef typename detail::fp_traits<T>::type traits; |
| typedef typename traits::method method; |
| typedef typename tools::promote_args<T>::type value_type; |
| #ifdef NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
| if(std::numeric_limits<T>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(0))) |
| return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>()); |
| return detail::fpclassify_imp(static_cast<value_type>(t), method()); |
| #else |
| return detail::fpclassify_imp(static_cast<value_type>(t), method()); |
| #endif |
| } |
| |
| #ifdef NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| template <> |
| inline int fpclassify<long double> NDNBOOST_NO_MACRO_EXPAND(long double t) |
| { |
| typedef detail::fp_traits<long double>::type traits; |
| typedef traits::method method; |
| typedef long double value_type; |
| #ifdef NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
| if(std::numeric_limits<long double>::is_specialized && detail::is_generic_tag_false(static_cast<method*>(0))) |
| return detail::fpclassify_imp(static_cast<value_type>(t), detail::generic_tag<true>()); |
| return detail::fpclassify_imp(static_cast<value_type>(t), method()); |
| #else |
| return detail::fpclassify_imp(static_cast<value_type>(t), method()); |
| #endif |
| } |
| #endif |
| |
| namespace detail { |
| |
| #ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY |
| template<class T> |
| inline bool isfinite_impl(T x, native_tag const&) |
| { |
| return (std::isfinite)(x); |
| } |
| #endif |
| |
| template<class T> |
| inline bool isfinite_impl(T x, generic_tag<true> const&) |
| { |
| return x >= -(std::numeric_limits<T>::max)() |
| && x <= (std::numeric_limits<T>::max)(); |
| } |
| |
| template<class T> |
| inline bool isfinite_impl(T x, generic_tag<false> const&) |
| { |
| #ifdef NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
| if(std::numeric_limits<T>::is_specialized) |
| return isfinite_impl(x, generic_tag<true>()); |
| #endif |
| (void)x; // warning supression. |
| return true; |
| } |
| |
| template<class T> |
| inline bool isfinite_impl(T x, ieee_tag const&) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME detail::fp_traits<T>::type traits; |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| a &= traits::exponent; |
| return a != traits::exponent; |
| } |
| |
| #if defined(NDNBOOST_MATH_USE_STD_FPCLASSIFY) && defined(NDNBOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY) |
| inline bool isfinite_impl NDNBOOST_NO_MACRO_EXPAND(long double t, const native_tag&) |
| { |
| return ndnboost::math::detail::isfinite_impl(t, generic_tag<true>()); |
| } |
| #endif |
| |
| } |
| |
| template<class T> |
| inline bool (isfinite)(T x) |
| { //!< \brief return true if floating-point type t is finite. |
| typedef typename detail::fp_traits<T>::type traits; |
| typedef typename traits::method method; |
| // typedef typename ndnboost::is_floating_point<T>::type fp_tag; |
| typedef typename tools::promote_args<T>::type value_type; |
| return detail::isfinite_impl(static_cast<value_type>(x), method()); |
| } |
| |
| #ifdef NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| template<> |
| inline bool (isfinite)(long double x) |
| { //!< \brief return true if floating-point type t is finite. |
| typedef detail::fp_traits<long double>::type traits; |
| typedef traits::method method; |
| typedef ndnboost::is_floating_point<long double>::type fp_tag; |
| typedef long double value_type; |
| return detail::isfinite_impl(static_cast<value_type>(x), method()); |
| } |
| #endif |
| |
| //------------------------------------------------------------------------------ |
| |
| namespace detail { |
| |
| #ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY |
| template<class T> |
| inline bool isnormal_impl(T x, native_tag const&) |
| { |
| return (std::isnormal)(x); |
| } |
| #endif |
| |
| template<class T> |
| inline bool isnormal_impl(T x, generic_tag<true> const&) |
| { |
| if(x < 0) x = -x; |
| return x >= (std::numeric_limits<T>::min)() |
| && x <= (std::numeric_limits<T>::max)(); |
| } |
| |
| template<class T> |
| inline bool isnormal_impl(T x, generic_tag<false> const&) |
| { |
| #ifdef NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
| if(std::numeric_limits<T>::is_specialized) |
| return isnormal_impl(x, generic_tag<true>()); |
| #endif |
| return !(x == 0); |
| } |
| |
| template<class T> |
| inline bool isnormal_impl(T x, ieee_tag const&) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME detail::fp_traits<T>::type traits; |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| a &= traits::exponent | traits::flag; |
| return (a != 0) && (a < traits::exponent); |
| } |
| |
| #if defined(NDNBOOST_MATH_USE_STD_FPCLASSIFY) && defined(NDNBOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY) |
| inline bool isnormal_impl NDNBOOST_NO_MACRO_EXPAND(long double t, const native_tag&) |
| { |
| return ndnboost::math::detail::isnormal_impl(t, generic_tag<true>()); |
| } |
| #endif |
| |
| } |
| |
| template<class T> |
| inline bool (isnormal)(T x) |
| { |
| typedef typename detail::fp_traits<T>::type traits; |
| typedef typename traits::method method; |
| //typedef typename ndnboost::is_floating_point<T>::type fp_tag; |
| typedef typename tools::promote_args<T>::type value_type; |
| return detail::isnormal_impl(static_cast<value_type>(x), method()); |
| } |
| |
| #ifdef NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| template<> |
| inline bool (isnormal)(long double x) |
| { |
| typedef detail::fp_traits<long double>::type traits; |
| typedef traits::method method; |
| typedef ndnboost::is_floating_point<long double>::type fp_tag; |
| typedef long double value_type; |
| return detail::isnormal_impl(static_cast<value_type>(x), method()); |
| } |
| #endif |
| |
| //------------------------------------------------------------------------------ |
| |
| namespace detail { |
| |
| #ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY |
| template<class T> |
| inline bool isinf_impl(T x, native_tag const&) |
| { |
| return (std::isinf)(x); |
| } |
| #endif |
| |
| template<class T> |
| inline bool isinf_impl(T x, generic_tag<true> const&) |
| { |
| (void)x; // in case the compiler thinks that x is unused because std::numeric_limits<T>::has_infinity is false |
| return std::numeric_limits<T>::has_infinity |
| && ( x == std::numeric_limits<T>::infinity() |
| || x == -std::numeric_limits<T>::infinity()); |
| } |
| |
| template<class T> |
| inline bool isinf_impl(T x, generic_tag<false> const&) |
| { |
| #ifdef NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
| if(std::numeric_limits<T>::is_specialized) |
| return isinf_impl(x, generic_tag<true>()); |
| #endif |
| (void)x; // warning supression. |
| return false; |
| } |
| |
| template<class T> |
| inline bool isinf_impl(T x, ieee_copy_all_bits_tag const&) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
| |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| a &= traits::exponent | traits::significand; |
| return a == traits::exponent; |
| } |
| |
| template<class T> |
| inline bool isinf_impl(T x, ieee_copy_leading_bits_tag const&) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
| |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| a &= traits::exponent | traits::significand; |
| if(a != traits::exponent) |
| return false; |
| |
| traits::set_bits(x,0); |
| return x == 0; |
| } |
| |
| #if defined(NDNBOOST_MATH_USE_STD_FPCLASSIFY) && defined(NDNBOOST_MATH_NO_NATIVE_LONG_DOUBLE_FP_CLASSIFY) |
| inline bool isinf_impl NDNBOOST_NO_MACRO_EXPAND(long double t, const native_tag&) |
| { |
| return ndnboost::math::detail::isinf_impl(t, generic_tag<true>()); |
| } |
| #endif |
| |
| } // namespace detail |
| |
| template<class T> |
| inline bool (isinf)(T x) |
| { |
| typedef typename detail::fp_traits<T>::type traits; |
| typedef typename traits::method method; |
| // typedef typename ndnboost::is_floating_point<T>::type fp_tag; |
| typedef typename tools::promote_args<T>::type value_type; |
| return detail::isinf_impl(static_cast<value_type>(x), method()); |
| } |
| |
| #ifdef NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| template<> |
| inline bool (isinf)(long double x) |
| { |
| typedef detail::fp_traits<long double>::type traits; |
| typedef traits::method method; |
| typedef ndnboost::is_floating_point<long double>::type fp_tag; |
| typedef long double value_type; |
| return detail::isinf_impl(static_cast<value_type>(x), method()); |
| } |
| #endif |
| |
| //------------------------------------------------------------------------------ |
| |
| namespace detail { |
| |
| #ifdef NDNBOOST_MATH_USE_STD_FPCLASSIFY |
| template<class T> |
| inline bool isnan_impl(T x, native_tag const&) |
| { |
| return (std::isnan)(x); |
| } |
| #endif |
| |
| template<class T> |
| inline bool isnan_impl(T x, generic_tag<true> const&) |
| { |
| return std::numeric_limits<T>::has_infinity |
| ? !(x <= std::numeric_limits<T>::infinity()) |
| : x != x; |
| } |
| |
| template<class T> |
| inline bool isnan_impl(T x, generic_tag<false> const&) |
| { |
| #ifdef NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
| if(std::numeric_limits<T>::is_specialized) |
| return isnan_impl(x, generic_tag<true>()); |
| #endif |
| (void)x; // warning supression |
| return false; |
| } |
| |
| template<class T> |
| inline bool isnan_impl(T x, ieee_copy_all_bits_tag const&) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
| |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| a &= traits::exponent | traits::significand; |
| return a > traits::exponent; |
| } |
| |
| template<class T> |
| inline bool isnan_impl(T x, ieee_copy_leading_bits_tag const&) |
| { |
| typedef NDNBOOST_DEDUCED_TYPENAME fp_traits<T>::type traits; |
| |
| NDNBOOST_DEDUCED_TYPENAME traits::bits a; |
| traits::get_bits(x,a); |
| |
| a &= traits::exponent | traits::significand; |
| if(a < traits::exponent) |
| return false; |
| |
| a &= traits::significand; |
| traits::set_bits(x,a); |
| return x != 0; |
| } |
| |
| } // namespace detail |
| |
| template<class T> |
| inline bool (isnan)(T x) |
| { //!< \brief return true if floating-point type t is NaN (Not A Number). |
| typedef typename detail::fp_traits<T>::type traits; |
| typedef typename traits::method method; |
| // typedef typename ndnboost::is_floating_point<T>::type fp_tag; |
| return detail::isnan_impl(x, method()); |
| } |
| |
| #ifdef isnan |
| template <> inline bool isnan NDNBOOST_NO_MACRO_EXPAND<float>(float t){ return ::ndnboost::math_detail::is_nan_helper(t, ndnboost::true_type()); } |
| template <> inline bool isnan NDNBOOST_NO_MACRO_EXPAND<double>(double t){ return ::ndnboost::math_detail::is_nan_helper(t, ndnboost::true_type()); } |
| template <> inline bool isnan NDNBOOST_NO_MACRO_EXPAND<long double>(long double t){ return ::ndnboost::math_detail::is_nan_helper(t, ndnboost::true_type()); } |
| #elif defined(NDNBOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS) |
| template<> |
| inline bool (isnan)(long double x) |
| { //!< \brief return true if floating-point type t is NaN (Not A Number). |
| typedef detail::fp_traits<long double>::type traits; |
| typedef traits::method method; |
| typedef ndnboost::is_floating_point<long double>::type fp_tag; |
| return detail::isnan_impl(x, method()); |
| } |
| #endif |
| |
| } // namespace math |
| } // namespace ndnboost |
| |
| #endif // NDNBOOST_MATH_FPCLASSIFY_HPP |
| |