1 /*
2  * Copyright © 2005-2014 Rich Felker, et al.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "math_config.h"
25 
26 // This is only necessary because the implementation of isnan only works
27 // properly when long double == double.
28 // See: https://sourceware.org/ml/newlib/2014/msg00684.html
29 
30 #if !defined(_NEED_FLOAT_HUGE)
31 
32 float
nexttowardf(float x,long double y)33 nexttowardf (float x, long double y)
34 {
35   uint32_t ux;
36   uint32_t e;
37 
38   /*
39    * We can't do this if y isn't nan as that might raise INEXACT doing
40    * long double -> float conversion, and we don't want to do this
41    * in long double for machines without long double HW as we won't
42    * get any exceptions in that case.
43    */
44   if (isnan(y))
45       return x + (issignaling(y) ? __builtin_nansf("") : (float) y);
46   if (isnan(x))
47       return x + x;
48 
49   if ((long double) x == y)
50       return (float) y;
51   ux = asuint(x);
52   if (x == 0) {
53     ux = 1;
54     if (signbit(y))
55       ux |= 0x80000000;
56     x = asfloat(ux);
57     force_eval_float(opt_barrier_float(x) * x);
58     return x;
59   } else if ((long double) x < y) {
60     if (signbit(x))
61       ux--;
62     else
63       ux++;
64   } else {
65     if (signbit(x))
66       ux++;
67     else
68       ux--;
69   }
70   e = ux & 0x7f800000;
71   /* raise overflow if ux.value is infinite and x is finite */
72   if (e == 0x7f800000)
73     return check_oflowf(opt_barrier_float(x+x));
74   /* raise underflow if ux.value is subnormal or zero */
75   x = asfloat(ux);
76   if (e == 0)
77       return __math_denormf(x);
78   return x;
79 }
80 
81 _MATH_ALIAS_f_fl(nexttoward)
82 
83 #endif /* _NEED_FLOAT_HUGE */
84