xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/rintq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* s_rintl.c -- long double version of s_rint.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  * rintq(x)
22*181254a7Smrg  * Return x rounded to integral value according to the prevailing
23*181254a7Smrg  * rounding mode.
24*181254a7Smrg  * Method:
25*181254a7Smrg  *	Using floating addition.
26*181254a7Smrg  * Exception:
27*181254a7Smrg  *	Inexact flag raised if x not equal to rintq(x).
28*181254a7Smrg  */
29*181254a7Smrg 
30*181254a7Smrg #define NO_MATH_REDIRECT
31*181254a7Smrg 
32*181254a7Smrg #include "quadmath-imp.h"
33*181254a7Smrg 
34*181254a7Smrg static const __float128
35*181254a7Smrg TWO112[2]={
36*181254a7Smrg   5.19229685853482762853049632922009600E+33L, /* 0x406F000000000000, 0 */
37*181254a7Smrg  -5.19229685853482762853049632922009600E+33L  /* 0xC06F000000000000, 0 */
38*181254a7Smrg };
39*181254a7Smrg 
rintq(__float128 x)40*181254a7Smrg __float128 rintq(__float128 x)
41*181254a7Smrg {
42*181254a7Smrg 	int64_t i0,j0,sx;
43*181254a7Smrg 	uint64_t i1 __attribute__ ((unused));
44*181254a7Smrg 	__float128 w,t;
45*181254a7Smrg 	GET_FLT128_WORDS64(i0,i1,x);
46*181254a7Smrg 	sx = (((uint64_t)i0)>>63);
47*181254a7Smrg 	j0 = ((i0>>48)&0x7fff)-0x3fff;
48*181254a7Smrg 	if(j0<112) {
49*181254a7Smrg 	    if(j0<0) {
50*181254a7Smrg 	        w = TWO112[sx]+x;
51*181254a7Smrg 	        t = w-TWO112[sx];
52*181254a7Smrg 		GET_FLT128_MSW64(i0,t);
53*181254a7Smrg 		SET_FLT128_MSW64(t,(i0&0x7fffffffffffffffLL)|(sx<<63));
54*181254a7Smrg 	        return t;
55*181254a7Smrg 	    }
56*181254a7Smrg 	} else {
57*181254a7Smrg 	    if(j0==0x4000) return x+x;	/* inf or NaN */
58*181254a7Smrg 	    else return x;		/* x is integral */
59*181254a7Smrg 	}
60*181254a7Smrg 	w = TWO112[sx]+x;
61*181254a7Smrg 	return w-TWO112[sx];
62*181254a7Smrg }
63