2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
9 * ====================================================
11 * From: @(#)s_ceil.c 5.1 93/09/24
12 * $FreeBSD: head/lib/msun/src/s_ceill.c 176280 2008-02-14 15:10:34Z bde $
17 * Return x rounded toward -inf to integral value
21 * Inexact flag raised if x not equal to ceill(x).
30 #ifdef LDBL_IMPLICIT_NBIT
31 #define MANH_SIZE (LDBL_MANH_SIZE + 1)
32 #define INC_MANH(u, c) do { \
33 uint64_t o = u.bits.manh; \
35 if (u.bits.manh < o) \
39 #define MANH_SIZE LDBL_MANH_SIZE
40 #define INC_MANH(u, c) do { \
41 uint64_t o = u.bits.manh; \
43 if (u.bits.manh < o) { \
45 u.bits.manh |= 1llu << (LDBL_MANH_SIZE - 1); \
50 static const long double huge = 1.0e300;
55 union IEEEl2bits u = { .e = x };
56 int e = u.bits.exp - LDBL_MAX_EXP + 1;
58 if (e < MANH_SIZE - 1) {
59 if (e < 0) { /* raise inexact if x != 0 */
62 (u.bits.manh | u.bits.manl) != 0)
63 u.e = u.bits.sign ? -0.0 : 1.0;
65 uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
66 if (((u.bits.manh & m) | u.bits.manl) == 0)
67 return (x); /* x is integral */
69 #ifdef LDBL_IMPLICIT_NBIT
74 INC_MANH(u, 1llu << (MANH_SIZE - e - 1));
76 if (huge + x > 0.0) { /* raise inexact flag */
81 } else if (e < LDBL_MANT_DIG - 1) {
82 uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
83 if ((u.bits.manl & m) == 0)
84 return (x); /* x is integral */
86 if (e == MANH_SIZE - 1)
89 uint64_t o = u.bits.manl;
90 u.bits.manl += 1llu << (LDBL_MANT_DIG - e - 1);
91 if (u.bits.manl < o) /* got a carry */
95 if (huge + x > 0.0) /* raise inexact flag */