xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/ceilq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* s_ceill.c -- long double version of s_ceil.c.
2*181254a7Smrg  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
3*181254a7Smrg  */
4*181254a7Smrg 
5*181254a7Smrg /*
6*181254a7Smrg  * ====================================================
7*181254a7Smrg  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*181254a7Smrg  *
9*181254a7Smrg  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*181254a7Smrg  * Permission to use, copy, modify, and distribute this
11*181254a7Smrg  * software is freely granted, provided that this notice
12*181254a7Smrg  * is preserved.
13*181254a7Smrg  * ====================================================
14*181254a7Smrg  */
15*181254a7Smrg 
16*181254a7Smrg #if defined(LIBM_SCCS) && !defined(lint)
17*181254a7Smrg static char rcsid[] = "NetBSD: ";
18*181254a7Smrg #endif
19*181254a7Smrg 
20*181254a7Smrg /*
21*181254a7Smrg  * ceilq(x)
22*181254a7Smrg  * Return x rounded toward -inf to integral value
23*181254a7Smrg  * Method:
24*181254a7Smrg  *	Bit twiddling.
25*181254a7Smrg  */
26*181254a7Smrg 
27*181254a7Smrg #define NO_MATH_REDIRECT
28*181254a7Smrg 
29*181254a7Smrg #include "quadmath-imp.h"
30*181254a7Smrg 
ceilq(__float128 x)31*181254a7Smrg __float128 ceilq(__float128 x)
32*181254a7Smrg {
33*181254a7Smrg 	int64_t i0,i1,j0;
34*181254a7Smrg 	uint64_t i,j;
35*181254a7Smrg 	GET_FLT128_WORDS64(i0,i1,x);
36*181254a7Smrg 	j0 = ((i0>>48)&0x7fff)-0x3fff;
37*181254a7Smrg 	if(j0<48) {
38*181254a7Smrg 	    if(j0<0) {
39*181254a7Smrg 		/* return 0*sign(x) if |x|<1 */
40*181254a7Smrg 		if(i0<0) {i0=0x8000000000000000ULL;i1=0;}
41*181254a7Smrg 		else if((i0|i1)!=0) { i0=0x3fff000000000000ULL;i1=0;}
42*181254a7Smrg 	    } else {
43*181254a7Smrg 		i = (0x0000ffffffffffffULL)>>j0;
44*181254a7Smrg 		if(((i0&i)|i1)==0) return x; /* x is integral */
45*181254a7Smrg 		if(i0>0) i0 += (0x0001000000000000LL)>>j0;
46*181254a7Smrg 		i0 &= (~i); i1=0;
47*181254a7Smrg 	    }
48*181254a7Smrg 	} else if (j0>111) {
49*181254a7Smrg 	    if(j0==0x4000) return x+x;	/* inf or NaN */
50*181254a7Smrg 	    else return x;		/* x is integral */
51*181254a7Smrg 	} else {
52*181254a7Smrg 	    i = -1ULL>>(j0-48);
53*181254a7Smrg 	    if((i1&i)==0) return x;	/* x is integral */
54*181254a7Smrg 	    if(i0>0) {
55*181254a7Smrg 		if(j0==48) i0+=1;
56*181254a7Smrg 		else {
57*181254a7Smrg 		    j = i1+(1LL<<(112-j0));
58*181254a7Smrg 		    if(j<i1) i0 +=1 ; 	/* got a carry */
59*181254a7Smrg 		    i1=j;
60*181254a7Smrg 		}
61*181254a7Smrg 	    }
62*181254a7Smrg 	    i1 &= (~i);
63*181254a7Smrg 	}
64*181254a7Smrg 	SET_FLT128_WORDS64(x,i0,i1);
65*181254a7Smrg 	return x;
66*181254a7Smrg }
67