xref: /titanic_50/usr/src/lib/libm/common/Q/rndintl.c (revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af)
1*25c28e83SPiotr Jasiukajtis /*
2*25c28e83SPiotr Jasiukajtis  * CDDL HEADER START
3*25c28e83SPiotr Jasiukajtis  *
4*25c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
5*25c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
6*25c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
7*25c28e83SPiotr Jasiukajtis  *
8*25c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*25c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
10*25c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
11*25c28e83SPiotr Jasiukajtis  * and limitations under the License.
12*25c28e83SPiotr Jasiukajtis  *
13*25c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
14*25c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*25c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
16*25c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
17*25c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
18*25c28e83SPiotr Jasiukajtis  *
19*25c28e83SPiotr Jasiukajtis  * CDDL HEADER END
20*25c28e83SPiotr Jasiukajtis  */
21*25c28e83SPiotr Jasiukajtis /*
22*25c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
23*25c28e83SPiotr Jasiukajtis  */
24*25c28e83SPiotr Jasiukajtis /*
25*25c28e83SPiotr Jasiukajtis  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26*25c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
27*25c28e83SPiotr Jasiukajtis  */
28*25c28e83SPiotr Jasiukajtis 
29*25c28e83SPiotr Jasiukajtis /*
30*25c28e83SPiotr Jasiukajtis  * aintl(x)	return x chopped to integral value
31*25c28e83SPiotr Jasiukajtis  * anintl(x)	return sign(x)*(|x|+0.5) chopped to integral value
32*25c28e83SPiotr Jasiukajtis  * irintl(x)	return rint(x) in integer format
33*25c28e83SPiotr Jasiukajtis  * nintl(x)	return anint(x) in integer format
34*25c28e83SPiotr Jasiukajtis  *
35*25c28e83SPiotr Jasiukajtis  * NOTE: aintl(x), anintl(x), ceill(x), floorl(x), and rintl(x) return result
36*25c28e83SPiotr Jasiukajtis  * with the same sign as x's,  including 0.0.
37*25c28e83SPiotr Jasiukajtis  */
38*25c28e83SPiotr Jasiukajtis 
39*25c28e83SPiotr Jasiukajtis #include "libm.h"
40*25c28e83SPiotr Jasiukajtis #include "longdouble.h"
41*25c28e83SPiotr Jasiukajtis 
42*25c28e83SPiotr Jasiukajtis extern enum fp_direction_type __swapRD(enum fp_direction_type);
43*25c28e83SPiotr Jasiukajtis 
44*25c28e83SPiotr Jasiukajtis static const long double qone = 1.0L, qhalf = 0.5L, qmhalf = -0.5L;
45*25c28e83SPiotr Jasiukajtis 
46*25c28e83SPiotr Jasiukajtis long double
aintl(long double x)47*25c28e83SPiotr Jasiukajtis aintl(long double x) {
48*25c28e83SPiotr Jasiukajtis 	long double t, w;
49*25c28e83SPiotr Jasiukajtis 
50*25c28e83SPiotr Jasiukajtis 	if (!finitel(x))
51*25c28e83SPiotr Jasiukajtis 		return (x + x);
52*25c28e83SPiotr Jasiukajtis 	w = fabsl(x);
53*25c28e83SPiotr Jasiukajtis 	t = rintl(w);
54*25c28e83SPiotr Jasiukajtis 	if (t <= w)
55*25c28e83SPiotr Jasiukajtis 		return (copysignl(t, x));	/* NaN or already aint(|x|) */
56*25c28e83SPiotr Jasiukajtis 	else	/* |t|>|x| case */
57*25c28e83SPiotr Jasiukajtis 		return (copysignl(t - qone, x));	/* |t-1|*sign(x) */
58*25c28e83SPiotr Jasiukajtis }
59*25c28e83SPiotr Jasiukajtis 
60*25c28e83SPiotr Jasiukajtis long double
anintl(long double x)61*25c28e83SPiotr Jasiukajtis anintl(long double x) {
62*25c28e83SPiotr Jasiukajtis 	long double t, w, z;
63*25c28e83SPiotr Jasiukajtis 
64*25c28e83SPiotr Jasiukajtis 	if (!finitel(x))
65*25c28e83SPiotr Jasiukajtis 		return (x + x);
66*25c28e83SPiotr Jasiukajtis 	w = fabsl(x);
67*25c28e83SPiotr Jasiukajtis 	t = rintl(w);
68*25c28e83SPiotr Jasiukajtis 	if (t == w)
69*25c28e83SPiotr Jasiukajtis 		return (copysignl(t, x));
70*25c28e83SPiotr Jasiukajtis 	z = t - w;
71*25c28e83SPiotr Jasiukajtis 	if (z > qhalf)
72*25c28e83SPiotr Jasiukajtis 		t = t - qone;
73*25c28e83SPiotr Jasiukajtis 	else if (z <= qmhalf)
74*25c28e83SPiotr Jasiukajtis 		t = t + qone;
75*25c28e83SPiotr Jasiukajtis 	return (copysignl(t, x));
76*25c28e83SPiotr Jasiukajtis }
77*25c28e83SPiotr Jasiukajtis 
78*25c28e83SPiotr Jasiukajtis int
irintl(long double x)79*25c28e83SPiotr Jasiukajtis irintl(long double x) {
80*25c28e83SPiotr Jasiukajtis 	enum fp_direction_type rd;
81*25c28e83SPiotr Jasiukajtis 
82*25c28e83SPiotr Jasiukajtis 	rd = __swapRD(fp_nearest);
83*25c28e83SPiotr Jasiukajtis 	(void) __swapRD(rd);	/* restore Rounding Direction */
84*25c28e83SPiotr Jasiukajtis 	switch (rd) {
85*25c28e83SPiotr Jasiukajtis 	case fp_nearest:
86*25c28e83SPiotr Jasiukajtis 		if (x < 2147483647.5L && x >= -2147483648.5L)
87*25c28e83SPiotr Jasiukajtis 			return ((int)rintl(x));
88*25c28e83SPiotr Jasiukajtis 		break;
89*25c28e83SPiotr Jasiukajtis 	case fp_tozero:
90*25c28e83SPiotr Jasiukajtis 		if (x < 2147483648.0L && x > -2147483649.0L)
91*25c28e83SPiotr Jasiukajtis 			return ((int)rintl(x));
92*25c28e83SPiotr Jasiukajtis 		break;
93*25c28e83SPiotr Jasiukajtis 	case fp_positive:
94*25c28e83SPiotr Jasiukajtis 		if (x <= 2147483647.0L && x > -2147483649.0L)
95*25c28e83SPiotr Jasiukajtis 			return ((int)rintl(x));
96*25c28e83SPiotr Jasiukajtis 		break;
97*25c28e83SPiotr Jasiukajtis 	case fp_negative:
98*25c28e83SPiotr Jasiukajtis 		if (x < 2147483648.0L && x >= -2147483648.0L)
99*25c28e83SPiotr Jasiukajtis 			return ((int)rintl(x));
100*25c28e83SPiotr Jasiukajtis 		break;
101*25c28e83SPiotr Jasiukajtis 	}
102*25c28e83SPiotr Jasiukajtis 	return ((int)copysignl(1.0e100L, x));
103*25c28e83SPiotr Jasiukajtis }
104*25c28e83SPiotr Jasiukajtis 
105*25c28e83SPiotr Jasiukajtis int
nintl(long double x)106*25c28e83SPiotr Jasiukajtis nintl(long double x) {
107*25c28e83SPiotr Jasiukajtis 	if ((x < 2147483647.5L) && (x > -2147483648.5L))
108*25c28e83SPiotr Jasiukajtis 		return ((int)anintl(x));
109*25c28e83SPiotr Jasiukajtis 	else
110*25c28e83SPiotr Jasiukajtis 		return ((int)copysignl(1.0e100L, x));
111*25c28e83SPiotr Jasiukajtis }
112