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