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