xref: /openbsd/lib/libm/src/ld128/s_ceill.c (revision 49393c00)
1*49393c00Smartynas /* @(#)s_ceil.c 5.1 93/09/24 */
2*49393c00Smartynas /*
3*49393c00Smartynas  * ====================================================
4*49393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*49393c00Smartynas  *
6*49393c00Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*49393c00Smartynas  * Permission to use, copy, modify, and distribute this
8*49393c00Smartynas  * software is freely granted, provided that this notice
9*49393c00Smartynas  * is preserved.
10*49393c00Smartynas  * ====================================================
11*49393c00Smartynas  */
12*49393c00Smartynas 
13*49393c00Smartynas /*
14*49393c00Smartynas  * ceill(x)
15*49393c00Smartynas  * Return x rounded toward -inf to integral value
16*49393c00Smartynas  * Method:
17*49393c00Smartynas  *	Bit twiddling.
18*49393c00Smartynas  * Exception:
19*49393c00Smartynas  *	Inexact flag raised if x not equal to ceil(x).
20*49393c00Smartynas  */
21*49393c00Smartynas 
22*49393c00Smartynas #include <math.h>
23*49393c00Smartynas 
24*49393c00Smartynas #include "math_private.h"
25*49393c00Smartynas 
26*49393c00Smartynas static const long double huge = 1.0e4930L;
27*49393c00Smartynas 
28*49393c00Smartynas long double
ceill(long double x)29*49393c00Smartynas ceill(long double x)
30*49393c00Smartynas {
31*49393c00Smartynas 	int64_t i0,i1,jj0;
32*49393c00Smartynas 	u_int64_t i,j;
33*49393c00Smartynas 	GET_LDOUBLE_WORDS64(i0,i1,x);
34*49393c00Smartynas 	jj0 = ((i0>>48)&0x7fff)-0x3fff;
35*49393c00Smartynas 	if(jj0<48) {
36*49393c00Smartynas 	    if(jj0<0) {		/* raise inexact if x != 0 */
37*49393c00Smartynas 		if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
38*49393c00Smartynas 		    if(i0<0) {i0=0x8000000000000000ULL;i1=0;}
39*49393c00Smartynas 		    else if((i0|i1)!=0) { i0=0x3fff000000000000ULL;i1=0;}
40*49393c00Smartynas 		}
41*49393c00Smartynas 	    } else {
42*49393c00Smartynas 		i = (0x0000ffffffffffffULL)>>jj0;
43*49393c00Smartynas 		if(((i0&i)|i1)==0) return x; /* x is integral */
44*49393c00Smartynas 		if(huge+x>0.0) {	/* raise inexact flag */
45*49393c00Smartynas 		    if(i0>0) i0 += (0x0001000000000000LL)>>jj0;
46*49393c00Smartynas 		    i0 &= (~i); i1=0;
47*49393c00Smartynas 		}
48*49393c00Smartynas 	    }
49*49393c00Smartynas 	} else if (jj0>111) {
50*49393c00Smartynas 	    if(jj0==0x4000) return x+x;	/* inf or NaN */
51*49393c00Smartynas 	    else return x;		/* x is integral */
52*49393c00Smartynas 	} else {
53*49393c00Smartynas 	    i = -1ULL>>(jj0-48);
54*49393c00Smartynas 	    if((i1&i)==0) return x;	/* x is integral */
55*49393c00Smartynas 	    if(huge+x>0.0) {		/* raise inexact flag */
56*49393c00Smartynas 		if(i0>0) {
57*49393c00Smartynas 		    if(jj0==48) i0+=1;
58*49393c00Smartynas 		    else {
59*49393c00Smartynas 			j = i1+(1LL<<(112-jj0));
60*49393c00Smartynas 			if(j<i1) i0 +=1 ;	/* got a carry */
61*49393c00Smartynas 			i1=j;
62*49393c00Smartynas 		    }
63*49393c00Smartynas 		}
64*49393c00Smartynas 		i1 &= (~i);
65*49393c00Smartynas 	    }
66*49393c00Smartynas 	}
67*49393c00Smartynas 	SET_LDOUBLE_WORDS64(x,i0,i1);
68*49393c00Smartynas 	return x;
69*49393c00Smartynas }
70