1*84d9c625SLionel Sambuc /*
2*84d9c625SLionel Sambuc * ====================================================
3*84d9c625SLionel Sambuc * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4*84d9c625SLionel Sambuc *
5*84d9c625SLionel Sambuc * Developed at SunPro, a Sun Microsystems, Inc. business.
6*84d9c625SLionel Sambuc * Permission to use, copy, modify, and distribute this
7*84d9c625SLionel Sambuc * software is freely granted, provided that this notice
8*84d9c625SLionel Sambuc * is preserved.
9*84d9c625SLionel Sambuc * ====================================================
10*84d9c625SLionel Sambuc *
11*84d9c625SLionel Sambuc * From: @(#)s_ceil.c 5.1 93/09/24
12*84d9c625SLionel Sambuc */
13*84d9c625SLionel Sambuc
14*84d9c625SLionel Sambuc #include <sys/cdefs.h>
15*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_ceill.c,v 1.1 2013/11/11 23:57:34 joerg Exp $");
16*84d9c625SLionel Sambuc #if 0
17*84d9c625SLionel Sambuc __FBSDID("$FreeBSD: head/lib/msun/src/s_ceill.c 176280 2008-02-14 15:10:34Z bde $");
18*84d9c625SLionel Sambuc #endif
19*84d9c625SLionel Sambuc
20*84d9c625SLionel Sambuc /*
21*84d9c625SLionel Sambuc * ceill(x)
22*84d9c625SLionel Sambuc * Return x rounded toward -inf to integral value
23*84d9c625SLionel Sambuc * Method:
24*84d9c625SLionel Sambuc * Bit twiddling.
25*84d9c625SLionel Sambuc * Exception:
26*84d9c625SLionel Sambuc * Inexact flag raised if x not equal to ceill(x).
27*84d9c625SLionel Sambuc */
28*84d9c625SLionel Sambuc #include "namespace.h"
29*84d9c625SLionel Sambuc
30*84d9c625SLionel Sambuc #include <float.h>
31*84d9c625SLionel Sambuc #include <math.h>
32*84d9c625SLionel Sambuc #include <stdint.h>
33*84d9c625SLionel Sambuc #include <machine/ieee.h>
34*84d9c625SLionel Sambuc
35*84d9c625SLionel Sambuc #ifdef __HAVE_LONG_DOUBLE
36*84d9c625SLionel Sambuc
37*84d9c625SLionel Sambuc #ifdef __weak_alias
38*84d9c625SLionel Sambuc __weak_alias(ceill, _ceill)
39*84d9c625SLionel Sambuc #endif
40*84d9c625SLionel Sambuc
41*84d9c625SLionel Sambuc #ifdef LDBL_IMPLICIT_NBIT
42*84d9c625SLionel Sambuc #define MANH_SIZE (EXT_FRACHBITS + 1)
43*84d9c625SLionel Sambuc #define INC_MANH(ux, c) do { \
44*84d9c625SLionel Sambuc uint64_t oi = ux.extu_frach; \
45*84d9c625SLionel Sambuc ux.extu_frach += (c); \
46*84d9c625SLionel Sambuc if (ux.extu_frach < oi) \
47*84d9c625SLionel Sambuc ux.extu_exp++; \
48*84d9c625SLionel Sambuc } while (0)
49*84d9c625SLionel Sambuc #else
50*84d9c625SLionel Sambuc #define MANH_SIZE EXT_FRACHBITS
51*84d9c625SLionel Sambuc #define INC_MANH(ux, c) do { \
52*84d9c625SLionel Sambuc uint64_t oi = ux.extu_frach; \
53*84d9c625SLionel Sambuc ux.extu_frach += (c); \
54*84d9c625SLionel Sambuc if (ux.extu_frach < oi) { \
55*84d9c625SLionel Sambuc ux.extu_exp++; \
56*84d9c625SLionel Sambuc ux.extu_frach |= 1llu << (EXT_FRACHBITS - 1); \
57*84d9c625SLionel Sambuc } \
58*84d9c625SLionel Sambuc } while (0)
59*84d9c625SLionel Sambuc #endif
60*84d9c625SLionel Sambuc
61*84d9c625SLionel Sambuc static const long double huge = 1.0e300;
62*84d9c625SLionel Sambuc
63*84d9c625SLionel Sambuc long double
ceill(long double x)64*84d9c625SLionel Sambuc ceill(long double x)
65*84d9c625SLionel Sambuc {
66*84d9c625SLionel Sambuc union ieee_ext_u ux = { .extu_ld = x, };
67*84d9c625SLionel Sambuc int e = ux.extu_exp - LDBL_MAX_EXP + 1;
68*84d9c625SLionel Sambuc
69*84d9c625SLionel Sambuc if (e < MANH_SIZE - 1) {
70*84d9c625SLionel Sambuc if (e < 0) { /* raise inexact if x != 0 */
71*84d9c625SLionel Sambuc if (huge + x > 0.0)
72*84d9c625SLionel Sambuc if (ux.extu_exp > 0 ||
73*84d9c625SLionel Sambuc (ux.extu_frach | ux.extu_fracl) != 0)
74*84d9c625SLionel Sambuc ux.extu_ld = ux.extu_sign ? -0.0 : 1.0;
75*84d9c625SLionel Sambuc } else {
76*84d9c625SLionel Sambuc uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
77*84d9c625SLionel Sambuc if (((ux.extu_frach & m) | ux.extu_fracl) == 0)
78*84d9c625SLionel Sambuc return (x); /* x is integral */
79*84d9c625SLionel Sambuc if (!ux.extu_sign) {
80*84d9c625SLionel Sambuc #ifdef LDBL_IMPLICIT_NBIT
81*84d9c625SLionel Sambuc if (e == 0)
82*84d9c625SLionel Sambuc ux.extu_exp++;
83*84d9c625SLionel Sambuc else
84*84d9c625SLionel Sambuc #endif
85*84d9c625SLionel Sambuc INC_MANH(ux, 1llu << (MANH_SIZE - e - 1));
86*84d9c625SLionel Sambuc }
87*84d9c625SLionel Sambuc if (huge + x > 0.0) { /* raise inexact flag */
88*84d9c625SLionel Sambuc ux.extu_frach &= ~m;
89*84d9c625SLionel Sambuc ux.extu_fracl = 0;
90*84d9c625SLionel Sambuc }
91*84d9c625SLionel Sambuc }
92*84d9c625SLionel Sambuc } else if (e < LDBL_MANT_DIG - 1) {
93*84d9c625SLionel Sambuc uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
94*84d9c625SLionel Sambuc if ((ux.extu_fracl & m) == 0)
95*84d9c625SLionel Sambuc return (x); /* x is integral */
96*84d9c625SLionel Sambuc if (!ux.extu_sign) {
97*84d9c625SLionel Sambuc if (e == MANH_SIZE - 1)
98*84d9c625SLionel Sambuc INC_MANH(ux, 1);
99*84d9c625SLionel Sambuc else {
100*84d9c625SLionel Sambuc uint64_t o = ux.extu_fracl;
101*84d9c625SLionel Sambuc ux.extu_fracl += 1llu << (LDBL_MANT_DIG - e - 1);
102*84d9c625SLionel Sambuc if (ux.extu_fracl < o) /* got a carry */
103*84d9c625SLionel Sambuc INC_MANH(ux, 1);
104*84d9c625SLionel Sambuc }
105*84d9c625SLionel Sambuc }
106*84d9c625SLionel Sambuc if (huge + x > 0.0) /* raise inexact flag */
107*84d9c625SLionel Sambuc ux.extu_fracl &= ~m;
108*84d9c625SLionel Sambuc }
109*84d9c625SLionel Sambuc return (ux.extu_ld);
110*84d9c625SLionel Sambuc }
111*84d9c625SLionel Sambuc
112*84d9c625SLionel Sambuc #endif /* __HAVE_LONG_DOUBLE */
113