xref: /freebsd/lib/msun/src/s_ceill.c (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
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 <sys/cdefs.h>
13 /*
14  * ceill(x)
15  * Return x rounded toward -inf to integral value
16  * Method:
17  *	Bit twiddling.
18  * Exception:
19  *	Inexact flag raised if x not equal to ceill(x).
20  */
21 
22 #include <float.h>
23 #include <math.h>
24 #include <stdint.h>
25 
26 #include "fpmath.h"
27 
28 #ifdef LDBL_IMPLICIT_NBIT
29 #define	MANH_SIZE	(LDBL_MANH_SIZE + 1)
30 #define	INC_MANH(u, c)	do {					\
31 	uint64_t o = u.bits.manh;				\
32 	u.bits.manh += (c);					\
33 	if (u.bits.manh < o)					\
34 		u.bits.exp++;					\
35 } while (0)
36 #else
37 #define	MANH_SIZE	LDBL_MANH_SIZE
38 #define	INC_MANH(u, c)	do {					\
39 	uint64_t o = u.bits.manh;				\
40 	u.bits.manh += (c);					\
41 	if (u.bits.manh < o) {					\
42 		u.bits.exp++;					\
43 		u.bits.manh |= 1llu << (LDBL_MANH_SIZE - 1);	\
44 	}							\
45 } while (0)
46 #endif
47 
48 static const long double huge = 1.0e300;
49 
50 long double
51 ceill(long double x)
52 {
53 	union IEEEl2bits u = { .e = x };
54 	int e = u.bits.exp - LDBL_MAX_EXP + 1;
55 
56 	if (e < MANH_SIZE - 1) {
57 		if (e < 0) {			/* raise inexact if x != 0 */
58 			if (huge + x > 0.0)
59 				if (u.bits.exp > 0 ||
60 				    (u.bits.manh | u.bits.manl) != 0)
61 					u.e = u.bits.sign ? -0.0 : 1.0;
62 		} else {
63 			uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
64 			if (((u.bits.manh & m) | u.bits.manl) == 0)
65 				return (x);	/* x is integral */
66 			if (!u.bits.sign) {
67 #ifdef LDBL_IMPLICIT_NBIT
68 				if (e == 0)
69 					u.bits.exp++;
70 				else
71 #endif
72 				INC_MANH(u, 1llu << (MANH_SIZE - e - 1));
73 			}
74 			if (huge + x > 0.0) {	/* raise inexact flag */
75 				u.bits.manh &= ~m;
76 				u.bits.manl = 0;
77 			}
78 		}
79 	} else if (e < LDBL_MANT_DIG - 1) {
80 		uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
81 		if ((u.bits.manl & m) == 0)
82 			return (x);	/* x is integral */
83 		if (!u.bits.sign) {
84 			if (e == MANH_SIZE - 1)
85 				INC_MANH(u, 1);
86 			else {
87 				uint64_t o = u.bits.manl;
88 				u.bits.manl += 1llu << (LDBL_MANT_DIG - e - 1);
89 				if (u.bits.manl < o)	/* got a carry */
90 					INC_MANH(u, 1);
91 			}
92 		}
93 		if (huge + x > 0.0)		/* raise inexact flag */
94 			u.bits.manl &= ~m;
95 	}
96 	return (u.e);
97 }
98