libm: Properly end some comments before starting a new one.
[dragonfly.git] / lib / libm / 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  * $FreeBSD: head/lib/msun/src/s_nexttowardf.c 218511 2011-02-10 07:38:38Z das $
12  */
13
14 #include <float.h>
15
16 #include "fpmath.h"
17 #include "math.h"
18 #include "math_private.h"
19
20 #define LDBL_INFNAN_EXP (LDBL_MAX_EXP * 2 - 1)
21
22 float
23 nexttowardf(float x, long double y)
24 {
25         union IEEEl2bits uy;
26         volatile float t;
27         int32_t hx,ix;
28
29         GET_FLOAT_WORD(hx,x);
30         ix = hx&0x7fffffff;             /* |x| */
31         uy.e = y;
32
33         if((ix>0x7f800000) ||
34            (uy.bits.exp == LDBL_INFNAN_EXP &&
35             ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
36            return x+y;  /* x or y is nan */
37         if(x==y) return (float)y;               /* x=y, return y */
38         if(ix==0) {                             /* x == 0 */
39             SET_FLOAT_WORD(x,(uy.bits.sign<<31)|1);/* return +-minsubnormal */
40             t = x*x;
41             if(t==x) return t; else return x;   /* raise underflow flag */
42         }
43         if(hx>=0 ^ x < y)                       /* x -= ulp */
44             hx -= 1;
45         else                                    /* x += ulp */
46             hx += 1;
47         ix = hx&0x7f800000;
48         if(ix>=0x7f800000) return x+x;  /* overflow  */
49         if(ix<0x00800000) {             /* underflow */
50             t = x*x;
51             if(t!=x) {          /* raise underflow flag */
52                 SET_FLOAT_WORD(x,hx);
53                 return x;
54             }
55         }
56         SET_FLOAT_WORD(x,hx);
57         return x;
58 }