xref: /illumos-gate/usr/src/lib/libm/common/m9x/nearbyint.c (revision b515258426fed6c7311fd3f1dea697cfbd4085c6)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #if defined(ELFOBJ)
31 #pragma weak nearbyint = __nearbyint
32 #endif
33 
34 /*
35  * nearbyint(x) returns the nearest fp integer to x in the direction
36  * corresponding to the current rounding direction without raising
37  * the inexact exception.
38  *
39  * nearbyint(x) is x unchanged if x is +/-0 or +/-inf.  If x is NaN,
40  * nearbyint(x) is also NaN.
41  */
42 
43 #include "libm.h"
44 #include "fenv_synonyms.h"
45 #include <fenv.h>
46 
47 double
48 __nearbyint(double x) {
49 	union {
50 		unsigned i[2];
51 		double d;
52 	} xx;
53 	unsigned hx, sx, i, frac;
54 	int rm, j;
55 
56 	xx.d = x;
57 	sx = xx.i[HIWORD] & 0x80000000;
58 	hx = xx.i[HIWORD] & ~0x80000000;
59 
60 	/* handle trivial cases */
61 	if (hx >= 0x43300000) {	/* x is nan, inf, or already integral */
62 		if (hx >= 0x7ff00000)	/* x is inf or nan */
63 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
64 			return (hx >= 0x7ff80000 ? x : x + x);
65 			/* assumes sparc-like QNaN */
66 #else
67 			return (x + x);
68 #endif
69 		return (x);
70 	} else if ((hx | xx.i[LOWORD]) == 0)	/* x is zero */
71 		return (x);
72 
73 	/* get the rounding mode */
74 	rm = fegetround();
75 
76 	/* flip the sense of directed roundings if x is negative */
77 	if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD))
78 		rm = (FE_UPWARD + FE_DOWNWARD) - rm;
79 
80 	/* handle |x| < 1 */
81 	if (hx < 0x3ff00000) {
82 		if (rm == FE_UPWARD || (rm == FE_TONEAREST &&
83 			(hx >= 0x3fe00000 && ((hx & 0xfffff) | xx.i[LOWORD]))))
84 			xx.i[HIWORD] = sx | 0x3ff00000;
85 		else
86 			xx.i[HIWORD] = sx;
87 		xx.i[LOWORD] = 0;
88 		return (xx.d);
89 	}
90 
91 	/* round x at the integer bit */
92 	j = 0x433 - (hx >> 20);
93 	if (j >= 32) {
94 		i = 1 << (j - 32);
95 		frac = ((xx.i[HIWORD] << 1) << (63 - j)) |
96 			(xx.i[LOWORD] >> (j - 32));
97 		if (xx.i[LOWORD] & (i - 1))
98 			frac |= 1;
99 		if (!frac)
100 			return (x);
101 		xx.i[LOWORD] = 0;
102 		xx.i[HIWORD] &= ~(i - 1);
103 		if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
104 			((frac > 0x80000000u) || ((frac == 0x80000000) &&
105 			(xx.i[HIWORD] & i)))))
106 			xx.i[HIWORD] += i;
107 	} else {
108 		i = 1 << j;
109 		frac = (xx.i[LOWORD] << 1) << (31 - j);
110 		if (!frac)
111 			return (x);
112 		xx.i[LOWORD] &= ~(i - 1);
113 		if ((rm == FE_UPWARD) || ((rm == FE_TONEAREST) &&
114 			(frac > 0x80000000u || ((frac == 0x80000000) &&
115 			(xx.i[LOWORD] & i))))) {
116 			xx.i[LOWORD] += i;
117 			if (xx.i[LOWORD] == 0)
118 				xx.i[HIWORD]++;
119 		}
120 	}
121 	return (xx.d);
122 }
123