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