annotate DEPENDENCIES/generic/include/boost/math/special_functions/next.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 // (C) Copyright John Maddock 2008.
Chris@16 2 // Use, modification and distribution are subject to the
Chris@16 3 // Boost Software License, Version 1.0. (See accompanying file
Chris@16 4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 5
Chris@16 6 #ifndef BOOST_MATH_SPECIAL_NEXT_HPP
Chris@16 7 #define BOOST_MATH_SPECIAL_NEXT_HPP
Chris@16 8
Chris@16 9 #ifdef _MSC_VER
Chris@16 10 #pragma once
Chris@16 11 #endif
Chris@16 12
Chris@16 13 #include <boost/math/policies/error_handling.hpp>
Chris@16 14 #include <boost/math/special_functions/fpclassify.hpp>
Chris@16 15 #include <boost/math/special_functions/sign.hpp>
Chris@16 16 #include <boost/math/special_functions/trunc.hpp>
Chris@16 17
Chris@16 18 #ifdef BOOST_MSVC
Chris@16 19 #include <float.h>
Chris@16 20 #endif
Chris@16 21
Chris@16 22 namespace boost{ namespace math{
Chris@16 23
Chris@16 24 namespace detail{
Chris@16 25
Chris@16 26 template <class T>
Chris@16 27 inline T get_smallest_value(mpl::true_ const&)
Chris@16 28 {
Chris@16 29 //
Chris@16 30 // numeric_limits lies about denorms being present - particularly
Chris@16 31 // when this can be turned on or off at runtime, as is the case
Chris@16 32 // when using the SSE2 registers in DAZ or FTZ mode.
Chris@16 33 //
Chris@16 34 static const T m = std::numeric_limits<T>::denorm_min();
Chris@16 35 return ((tools::min_value<T>() - m) == tools::min_value<T>()) ? tools::min_value<T>() : m;
Chris@16 36 }
Chris@16 37
Chris@16 38 template <class T>
Chris@16 39 inline T get_smallest_value(mpl::false_ const&)
Chris@16 40 {
Chris@16 41 return tools::min_value<T>();
Chris@16 42 }
Chris@16 43
Chris@16 44 template <class T>
Chris@16 45 inline T get_smallest_value()
Chris@16 46 {
Chris@16 47 #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1310)
Chris@16 48 return get_smallest_value<T>(mpl::bool_<std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_denorm == 1)>());
Chris@16 49 #else
Chris@16 50 return get_smallest_value<T>(mpl::bool_<std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_denorm == std::denorm_present)>());
Chris@16 51 #endif
Chris@16 52 }
Chris@16 53
Chris@16 54 //
Chris@16 55 // Returns the smallest value that won't generate denorms when
Chris@16 56 // we calculate the value of the least-significant-bit:
Chris@16 57 //
Chris@16 58 template <class T>
Chris@16 59 T get_min_shift_value();
Chris@16 60
Chris@16 61 template <class T>
Chris@16 62 struct min_shift_initializer
Chris@16 63 {
Chris@16 64 struct init
Chris@16 65 {
Chris@16 66 init()
Chris@16 67 {
Chris@16 68 do_init();
Chris@16 69 }
Chris@16 70 static void do_init()
Chris@16 71 {
Chris@16 72 get_min_shift_value<T>();
Chris@16 73 }
Chris@16 74 void force_instantiate()const{}
Chris@16 75 };
Chris@16 76 static const init initializer;
Chris@16 77 static void force_instantiate()
Chris@16 78 {
Chris@16 79 initializer.force_instantiate();
Chris@16 80 }
Chris@16 81 };
Chris@16 82
Chris@16 83 template <class T>
Chris@16 84 const typename min_shift_initializer<T>::init min_shift_initializer<T>::initializer;
Chris@16 85
Chris@16 86
Chris@16 87 template <class T>
Chris@16 88 inline T get_min_shift_value()
Chris@16 89 {
Chris@16 90 BOOST_MATH_STD_USING
Chris@16 91 static const T val = ldexp(tools::min_value<T>(), tools::digits<T>() + 1);
Chris@16 92 min_shift_initializer<T>::force_instantiate();
Chris@16 93
Chris@16 94 return val;
Chris@16 95 }
Chris@16 96
Chris@16 97 template <class T, class Policy>
Chris@16 98 T float_next_imp(const T& val, const Policy& pol)
Chris@16 99 {
Chris@16 100 BOOST_MATH_STD_USING
Chris@16 101 int expon;
Chris@16 102 static const char* function = "float_next<%1%>(%1%)";
Chris@16 103
Chris@16 104 int fpclass = (boost::math::fpclassify)(val);
Chris@16 105
Chris@16 106 if((fpclass == FP_NAN) || (fpclass == FP_INFINITE))
Chris@16 107 {
Chris@16 108 if(val < 0)
Chris@16 109 return -tools::max_value<T>();
Chris@16 110 return policies::raise_domain_error<T>(
Chris@16 111 function,
Chris@16 112 "Argument must be finite, but got %1%", val, pol);
Chris@16 113 }
Chris@16 114
Chris@16 115 if(val >= tools::max_value<T>())
Chris@16 116 return policies::raise_overflow_error<T>(function, 0, pol);
Chris@16 117
Chris@16 118 if(val == 0)
Chris@16 119 return detail::get_smallest_value<T>();
Chris@16 120
Chris@16 121 if((fpclass != FP_SUBNORMAL) && (fpclass != FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != -tools::min_value<T>()))
Chris@16 122 {
Chris@16 123 //
Chris@16 124 // Special case: if the value of the least significant bit is a denorm, and the result
Chris@16 125 // would not be a denorm, then shift the input, increment, and shift back.
Chris@16 126 // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
Chris@16 127 //
Chris@16 128 return ldexp(float_next(T(ldexp(val, 2 * tools::digits<T>())), pol), -2 * tools::digits<T>());
Chris@16 129 }
Chris@16 130
Chris@16 131 if(-0.5f == frexp(val, &expon))
Chris@16 132 --expon; // reduce exponent when val is a power of two, and negative.
Chris@16 133 T diff = ldexp(T(1), expon - tools::digits<T>());
Chris@16 134 if(diff == 0)
Chris@16 135 diff = detail::get_smallest_value<T>();
Chris@16 136 return val + diff;
Chris@16 137 }
Chris@16 138
Chris@16 139 }
Chris@16 140
Chris@16 141 template <class T, class Policy>
Chris@16 142 inline typename tools::promote_args<T>::type float_next(const T& val, const Policy& pol)
Chris@16 143 {
Chris@16 144 typedef typename tools::promote_args<T>::type result_type;
Chris@16 145 return detail::float_next_imp(static_cast<result_type>(val), pol);
Chris@16 146 }
Chris@16 147
Chris@16 148 #if 0 //def BOOST_MSVC
Chris@16 149 //
Chris@16 150 // We used to use ::_nextafter here, but doing so fails when using
Chris@16 151 // the SSE2 registers if the FTZ or DAZ flags are set, so use our own
Chris@16 152 // - albeit slower - code instead as at least that gives the correct answer.
Chris@16 153 //
Chris@16 154 template <class Policy>
Chris@16 155 inline double float_next(const double& val, const Policy& pol)
Chris@16 156 {
Chris@16 157 static const char* function = "float_next<%1%>(%1%)";
Chris@16 158
Chris@16 159 if(!(boost::math::isfinite)(val) && (val > 0))
Chris@16 160 return policies::raise_domain_error<double>(
Chris@16 161 function,
Chris@16 162 "Argument must be finite, but got %1%", val, pol);
Chris@16 163
Chris@16 164 if(val >= tools::max_value<double>())
Chris@16 165 return policies::raise_overflow_error<double>(function, 0, pol);
Chris@16 166
Chris@16 167 return ::_nextafter(val, tools::max_value<double>());
Chris@16 168 }
Chris@16 169 #endif
Chris@16 170
Chris@16 171 template <class T>
Chris@16 172 inline typename tools::promote_args<T>::type float_next(const T& val)
Chris@16 173 {
Chris@16 174 return float_next(val, policies::policy<>());
Chris@16 175 }
Chris@16 176
Chris@16 177 namespace detail{
Chris@16 178
Chris@16 179 template <class T, class Policy>
Chris@16 180 T float_prior_imp(const T& val, const Policy& pol)
Chris@16 181 {
Chris@16 182 BOOST_MATH_STD_USING
Chris@16 183 int expon;
Chris@16 184 static const char* function = "float_prior<%1%>(%1%)";
Chris@16 185
Chris@16 186 int fpclass = (boost::math::fpclassify)(val);
Chris@16 187
Chris@16 188 if((fpclass == FP_NAN) || (fpclass == FP_INFINITE))
Chris@16 189 {
Chris@16 190 if(val > 0)
Chris@16 191 return tools::max_value<T>();
Chris@16 192 return policies::raise_domain_error<T>(
Chris@16 193 function,
Chris@16 194 "Argument must be finite, but got %1%", val, pol);
Chris@16 195 }
Chris@16 196
Chris@16 197 if(val <= -tools::max_value<T>())
Chris@16 198 return -policies::raise_overflow_error<T>(function, 0, pol);
Chris@16 199
Chris@16 200 if(val == 0)
Chris@16 201 return -detail::get_smallest_value<T>();
Chris@16 202
Chris@16 203 if((fpclass != FP_SUBNORMAL) && (fpclass != FP_ZERO) && (fabs(val) < detail::get_min_shift_value<T>()) && (val != tools::min_value<T>()))
Chris@16 204 {
Chris@16 205 //
Chris@16 206 // Special case: if the value of the least significant bit is a denorm, and the result
Chris@16 207 // would not be a denorm, then shift the input, increment, and shift back.
Chris@16 208 // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
Chris@16 209 //
Chris@16 210 return ldexp(float_prior(T(ldexp(val, 2 * tools::digits<T>())), pol), -2 * tools::digits<T>());
Chris@16 211 }
Chris@16 212
Chris@16 213 T remain = frexp(val, &expon);
Chris@16 214 if(remain == 0.5)
Chris@16 215 --expon; // when val is a power of two we must reduce the exponent
Chris@16 216 T diff = ldexp(T(1), expon - tools::digits<T>());
Chris@16 217 if(diff == 0)
Chris@16 218 diff = detail::get_smallest_value<T>();
Chris@16 219 return val - diff;
Chris@16 220 }
Chris@16 221
Chris@16 222 }
Chris@16 223
Chris@16 224 template <class T, class Policy>
Chris@16 225 inline typename tools::promote_args<T>::type float_prior(const T& val, const Policy& pol)
Chris@16 226 {
Chris@16 227 typedef typename tools::promote_args<T>::type result_type;
Chris@16 228 return detail::float_prior_imp(static_cast<result_type>(val), pol);
Chris@16 229 }
Chris@16 230
Chris@16 231 #if 0 //def BOOST_MSVC
Chris@16 232 //
Chris@16 233 // We used to use ::_nextafter here, but doing so fails when using
Chris@16 234 // the SSE2 registers if the FTZ or DAZ flags are set, so use our own
Chris@16 235 // - albeit slower - code instead as at least that gives the correct answer.
Chris@16 236 //
Chris@16 237 template <class Policy>
Chris@16 238 inline double float_prior(const double& val, const Policy& pol)
Chris@16 239 {
Chris@16 240 static const char* function = "float_prior<%1%>(%1%)";
Chris@16 241
Chris@16 242 if(!(boost::math::isfinite)(val) && (val < 0))
Chris@16 243 return policies::raise_domain_error<double>(
Chris@16 244 function,
Chris@16 245 "Argument must be finite, but got %1%", val, pol);
Chris@16 246
Chris@16 247 if(val <= -tools::max_value<double>())
Chris@16 248 return -policies::raise_overflow_error<double>(function, 0, pol);
Chris@16 249
Chris@16 250 return ::_nextafter(val, -tools::max_value<double>());
Chris@16 251 }
Chris@16 252 #endif
Chris@16 253
Chris@16 254 template <class T>
Chris@16 255 inline typename tools::promote_args<T>::type float_prior(const T& val)
Chris@16 256 {
Chris@16 257 return float_prior(val, policies::policy<>());
Chris@16 258 }
Chris@16 259
Chris@16 260 template <class T, class U, class Policy>
Chris@16 261 inline typename tools::promote_args<T, U>::type nextafter(const T& val, const U& direction, const Policy& pol)
Chris@16 262 {
Chris@16 263 typedef typename tools::promote_args<T, U>::type result_type;
Chris@16 264 return val < direction ? boost::math::float_next<result_type>(val, pol) : val == direction ? val : boost::math::float_prior<result_type>(val, pol);
Chris@16 265 }
Chris@16 266
Chris@16 267 template <class T, class U>
Chris@16 268 inline typename tools::promote_args<T, U>::type nextafter(const T& val, const U& direction)
Chris@16 269 {
Chris@16 270 return nextafter(val, direction, policies::policy<>());
Chris@16 271 }
Chris@16 272
Chris@16 273 namespace detail{
Chris@16 274
Chris@16 275 template <class T, class Policy>
Chris@16 276 T float_distance_imp(const T& a, const T& b, const Policy& pol)
Chris@16 277 {
Chris@16 278 BOOST_MATH_STD_USING
Chris@16 279 //
Chris@16 280 // Error handling:
Chris@16 281 //
Chris@16 282 static const char* function = "float_distance<%1%>(%1%, %1%)";
Chris@16 283 if(!(boost::math::isfinite)(a))
Chris@16 284 return policies::raise_domain_error<T>(
Chris@16 285 function,
Chris@16 286 "Argument a must be finite, but got %1%", a, pol);
Chris@16 287 if(!(boost::math::isfinite)(b))
Chris@16 288 return policies::raise_domain_error<T>(
Chris@16 289 function,
Chris@16 290 "Argument b must be finite, but got %1%", b, pol);
Chris@16 291 //
Chris@16 292 // Special cases:
Chris@16 293 //
Chris@16 294 if(a > b)
Chris@16 295 return -float_distance(b, a, pol);
Chris@16 296 if(a == b)
Chris@16 297 return 0;
Chris@16 298 if(a == 0)
Chris@16 299 return 1 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol));
Chris@16 300 if(b == 0)
Chris@16 301 return 1 + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
Chris@16 302 if(boost::math::sign(a) != boost::math::sign(b))
Chris@16 303 return 2 + fabs(float_distance(static_cast<T>((b < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), b, pol))
Chris@16 304 + fabs(float_distance(static_cast<T>((a < 0) ? T(-detail::get_smallest_value<T>()) : detail::get_smallest_value<T>()), a, pol));
Chris@16 305 //
Chris@16 306 // By the time we get here, both a and b must have the same sign, we want
Chris@16 307 // b > a and both postive for the following logic:
Chris@16 308 //
Chris@16 309 if(a < 0)
Chris@16 310 return float_distance(static_cast<T>(-b), static_cast<T>(-a), pol);
Chris@16 311
Chris@16 312 BOOST_ASSERT(a >= 0);
Chris@16 313 BOOST_ASSERT(b >= a);
Chris@16 314
Chris@16 315 int expon;
Chris@16 316 //
Chris@16 317 // Note that if a is a denorm then the usual formula fails
Chris@16 318 // because we actually have fewer than tools::digits<T>()
Chris@16 319 // significant bits in the representation:
Chris@16 320 //
Chris@16 321 frexp(((boost::math::fpclassify)(a) == FP_SUBNORMAL) ? tools::min_value<T>() : a, &expon);
Chris@16 322 T upper = ldexp(T(1), expon);
Chris@16 323 T result = 0;
Chris@16 324 expon = tools::digits<T>() - expon;
Chris@16 325 //
Chris@16 326 // If b is greater than upper, then we *must* split the calculation
Chris@16 327 // as the size of the ULP changes with each order of magnitude change:
Chris@16 328 //
Chris@16 329 if(b > upper)
Chris@16 330 {
Chris@16 331 result = float_distance(upper, b);
Chris@16 332 }
Chris@16 333 //
Chris@16 334 // Use compensated double-double addition to avoid rounding
Chris@16 335 // errors in the subtraction:
Chris@16 336 //
Chris@16 337 T mb, x, y, z;
Chris@16 338 if(((boost::math::fpclassify)(a) == FP_SUBNORMAL) || (b - a < tools::min_value<T>()))
Chris@16 339 {
Chris@16 340 //
Chris@16 341 // Special case - either one end of the range is a denormal, or else the difference is.
Chris@16 342 // The regular code will fail if we're using the SSE2 registers on Intel and either
Chris@16 343 // the FTZ or DAZ flags are set.
Chris@16 344 //
Chris@16 345 T a2 = ldexp(a, tools::digits<T>());
Chris@16 346 T b2 = ldexp(b, tools::digits<T>());
Chris@16 347 mb = -(std::min)(T(ldexp(upper, tools::digits<T>())), b2);
Chris@16 348 x = a2 + mb;
Chris@16 349 z = x - a2;
Chris@16 350 y = (a2 - (x - z)) + (mb - z);
Chris@16 351
Chris@16 352 expon -= tools::digits<T>();
Chris@16 353 }
Chris@16 354 else
Chris@16 355 {
Chris@16 356 mb = -(std::min)(upper, b);
Chris@16 357 x = a + mb;
Chris@16 358 z = x - a;
Chris@16 359 y = (a - (x - z)) + (mb - z);
Chris@16 360 }
Chris@16 361 if(x < 0)
Chris@16 362 {
Chris@16 363 x = -x;
Chris@16 364 y = -y;
Chris@16 365 }
Chris@16 366 result += ldexp(x, expon) + ldexp(y, expon);
Chris@16 367 //
Chris@16 368 // Result must be an integer:
Chris@16 369 //
Chris@16 370 BOOST_ASSERT(result == floor(result));
Chris@16 371 return result;
Chris@16 372 }
Chris@16 373
Chris@16 374 }
Chris@16 375
Chris@16 376 template <class T, class U, class Policy>
Chris@16 377 inline typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b, const Policy& pol)
Chris@16 378 {
Chris@16 379 typedef typename tools::promote_args<T, U>::type result_type;
Chris@16 380 return detail::float_distance_imp(static_cast<result_type>(a), static_cast<result_type>(b), pol);
Chris@16 381 }
Chris@16 382
Chris@16 383 template <class T, class U>
Chris@16 384 typename tools::promote_args<T, U>::type float_distance(const T& a, const U& b)
Chris@16 385 {
Chris@16 386 return boost::math::float_distance(a, b, policies::policy<>());
Chris@16 387 }
Chris@16 388
Chris@16 389 namespace detail{
Chris@16 390
Chris@16 391 template <class T, class Policy>
Chris@16 392 T float_advance_imp(T val, int distance, const Policy& pol)
Chris@16 393 {
Chris@16 394 BOOST_MATH_STD_USING
Chris@16 395 //
Chris@16 396 // Error handling:
Chris@16 397 //
Chris@16 398 static const char* function = "float_advance<%1%>(%1%, int)";
Chris@16 399
Chris@16 400 int fpclass = (boost::math::fpclassify)(val);
Chris@16 401
Chris@16 402 if((fpclass == FP_NAN) || (fpclass == FP_INFINITE))
Chris@16 403 return policies::raise_domain_error<T>(
Chris@16 404 function,
Chris@16 405 "Argument val must be finite, but got %1%", val, pol);
Chris@16 406
Chris@16 407 if(val < 0)
Chris@16 408 return -float_advance(-val, -distance, pol);
Chris@16 409 if(distance == 0)
Chris@16 410 return val;
Chris@16 411 if(distance == 1)
Chris@16 412 return float_next(val, pol);
Chris@16 413 if(distance == -1)
Chris@16 414 return float_prior(val, pol);
Chris@16 415
Chris@16 416 if(fabs(val) < detail::get_min_shift_value<T>())
Chris@16 417 {
Chris@16 418 //
Chris@16 419 // Special case: if the value of the least significant bit is a denorm,
Chris@16 420 // implement in terms of float_next/float_prior.
Chris@16 421 // This avoids issues with the Intel SSE2 registers when the FTZ or DAZ flags are set.
Chris@16 422 //
Chris@16 423 if(distance > 0)
Chris@16 424 {
Chris@16 425 do{ val = float_next(val, pol); } while(--distance);
Chris@16 426 }
Chris@16 427 else
Chris@16 428 {
Chris@16 429 do{ val = float_prior(val, pol); } while(++distance);
Chris@16 430 }
Chris@16 431 return val;
Chris@16 432 }
Chris@16 433
Chris@16 434 int expon;
Chris@16 435 frexp(val, &expon);
Chris@16 436 T limit = ldexp((distance < 0 ? T(0.5f) : T(1)), expon);
Chris@16 437 if(val <= tools::min_value<T>())
Chris@16 438 {
Chris@16 439 limit = sign(T(distance)) * tools::min_value<T>();
Chris@16 440 }
Chris@16 441 T limit_distance = float_distance(val, limit);
Chris@16 442 while(fabs(limit_distance) < abs(distance))
Chris@16 443 {
Chris@16 444 distance -= itrunc(limit_distance);
Chris@16 445 val = limit;
Chris@16 446 if(distance < 0)
Chris@16 447 {
Chris@16 448 limit /= 2;
Chris@16 449 expon--;
Chris@16 450 }
Chris@16 451 else
Chris@16 452 {
Chris@16 453 limit *= 2;
Chris@16 454 expon++;
Chris@16 455 }
Chris@16 456 limit_distance = float_distance(val, limit);
Chris@16 457 if(distance && (limit_distance == 0))
Chris@16 458 {
Chris@16 459 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);
Chris@16 460 }
Chris@16 461 }
Chris@16 462 if((0.5f == frexp(val, &expon)) && (distance < 0))
Chris@16 463 --expon;
Chris@16 464 T diff = 0;
Chris@16 465 if(val != 0)
Chris@16 466 diff = distance * ldexp(T(1), expon - tools::digits<T>());
Chris@16 467 if(diff == 0)
Chris@16 468 diff = distance * detail::get_smallest_value<T>();
Chris@16 469 return val += diff;
Chris@16 470 }
Chris@16 471
Chris@16 472 }
Chris@16 473
Chris@16 474 template <class T, class Policy>
Chris@16 475 inline typename tools::promote_args<T>::type float_advance(T val, int distance, const Policy& pol)
Chris@16 476 {
Chris@16 477 typedef typename tools::promote_args<T>::type result_type;
Chris@16 478 return detail::float_advance_imp(static_cast<result_type>(val), distance, pol);
Chris@16 479 }
Chris@16 480
Chris@16 481 template <class T>
Chris@16 482 inline typename tools::promote_args<T>::type float_advance(const T& val, int distance)
Chris@16 483 {
Chris@16 484 return boost::math::float_advance(val, distance, policies::policy<>());
Chris@16 485 }
Chris@16 486
Chris@16 487 }} // namespaces
Chris@16 488
Chris@16 489 #endif // BOOST_MATH_SPECIAL_NEXT_HPP
Chris@16 490