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