]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/msun/src/s_nexttowardf.c
pow,powf(3),__ieee754_rem_pio2(f): Avoid negative integer left shift UB
[FreeBSD/FreeBSD.git] / lib / msun / src / s_nexttowardf.c
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11
12 #include <float.h>
13
14 #include "fpmath.h"
15 #include "math.h"
16 #include "math_private.h"
17
18 #define LDBL_INFNAN_EXP (LDBL_MAX_EXP * 2 - 1)
19
20 float
21 nexttowardf(float x, long double y)
22 {
23         union IEEEl2bits uy;
24         volatile float t;
25         int32_t hx,ix;
26
27         GET_FLOAT_WORD(hx,x);
28         ix = hx&0x7fffffff;             /* |x| */
29         uy.e = y;
30
31         if((ix>0x7f800000) ||
32            (uy.bits.exp == LDBL_INFNAN_EXP &&
33             ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
34            return x+y;  /* x or y is nan */
35         if(x==y) return (float)y;               /* x=y, return y */
36         if(ix==0) {                             /* x == 0 */
37             SET_FLOAT_WORD(x,(uy.bits.sign<<31)|1);/* return +-minsubnormal */
38             t = x*x;
39             if(t==x) return t; else return x;   /* raise underflow flag */
40         }
41         if(hx>=0 ^ x < y)                       /* x -= ulp */
42             hx -= 1;
43         else                                    /* x += ulp */
44             hx += 1;
45         ix = hx&0x7f800000;
46         if(ix>=0x7f800000) return x+x;  /* overflow  */
47         if(ix<0x00800000) {             /* underflow */
48             t = x*x;
49             if(t!=x) {          /* raise underflow flag */
50                 SET_FLOAT_WORD(x,hx);
51                 return x;
52             }
53         }
54         SET_FLOAT_WORD(x,hx);
55         return x;
56 }