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