math: minor cleanups in ceil and floor
[musl] / src / math / ceilf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_ceilf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4  */
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15
16 #include "libm.h"
17
18 static const float huge = 1.0e30;
19
20 float ceilf(float x)
21 {
22         int32_t i0,j0;
23         uint32_t i;
24
25         GET_FLOAT_WORD(i0, x);
26         j0 = ((i0>>23)&0xff) - 0x7f;
27         if (j0 < 23) {
28                 if (j0 < 0) {
29                         /* raise inexact if x != 0 */
30                         if (huge+x > 0.0f) {
31                                 if (i0 < 0)
32                                         i0 = 0x80000000;
33                                 else if(i0 != 0)
34                                         i0 = 0x3f800000;
35                         }
36                 } else {
37                         i = 0x007fffff>>j0;
38                         if ((i0&i) == 0)
39                                 return x; /* x is integral */
40                         /* raise inexact flag */
41                         if (huge+x > 0.0f) {
42                                 if (i0 > 0)
43                                         i0 += 0x00800000>>j0;
44                                 i0 &= ~i;
45                         }
46                 }
47         } else {
48                 if (j0 == 0x80)  /* inf or NaN */
49                         return x+x;
50                 return x; /* x is integral */
51         }
52         SET_FLOAT_WORD(x, i0);
53         return x;
54 }