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 /*
23*5b2ba9d3SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24*5b2ba9d3SPiotr Jasiukajtis */
25*5b2ba9d3SPiotr Jasiukajtis /*
26*5b2ba9d3SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27*5b2ba9d3SPiotr Jasiukajtis * Use is subject to license terms.
28*5b2ba9d3SPiotr Jasiukajtis */
29*5b2ba9d3SPiotr Jasiukajtis
30*5b2ba9d3SPiotr Jasiukajtis #pragma weak nexttowardf = __nexttowardf
31*5b2ba9d3SPiotr Jasiukajtis
32*5b2ba9d3SPiotr Jasiukajtis #include "libm.h"
33*5b2ba9d3SPiotr Jasiukajtis
34*5b2ba9d3SPiotr Jasiukajtis static union {
35*5b2ba9d3SPiotr Jasiukajtis unsigned i;
36*5b2ba9d3SPiotr Jasiukajtis float f;
37*5b2ba9d3SPiotr Jasiukajtis } C[] = {
38*5b2ba9d3SPiotr Jasiukajtis 0x00800000,
39*5b2ba9d3SPiotr Jasiukajtis 0x7f000000,
40*5b2ba9d3SPiotr Jasiukajtis 0x7fffffff
41*5b2ba9d3SPiotr Jasiukajtis };
42*5b2ba9d3SPiotr Jasiukajtis
43*5b2ba9d3SPiotr Jasiukajtis #define tiny C[0].f
44*5b2ba9d3SPiotr Jasiukajtis #define huge C[1].f
45*5b2ba9d3SPiotr Jasiukajtis #define qnan C[2].f
46*5b2ba9d3SPiotr Jasiukajtis
47*5b2ba9d3SPiotr Jasiukajtis #if defined(__sparc)
48*5b2ba9d3SPiotr Jasiukajtis
49*5b2ba9d3SPiotr Jasiukajtis enum fcc_type {
50*5b2ba9d3SPiotr Jasiukajtis fcc_equal = 0,
51*5b2ba9d3SPiotr Jasiukajtis fcc_less = 1,
52*5b2ba9d3SPiotr Jasiukajtis fcc_greater = 2,
53*5b2ba9d3SPiotr Jasiukajtis fcc_unordered = 3
54*5b2ba9d3SPiotr Jasiukajtis };
55*5b2ba9d3SPiotr Jasiukajtis
56*5b2ba9d3SPiotr Jasiukajtis #ifdef __sparcv9
57*5b2ba9d3SPiotr Jasiukajtis #define _Q_cmp _Qp_cmp
58*5b2ba9d3SPiotr Jasiukajtis #endif
59*5b2ba9d3SPiotr Jasiukajtis
60*5b2ba9d3SPiotr Jasiukajtis extern enum fcc_type _Q_cmp(const long double *, const long double *);
61*5b2ba9d3SPiotr Jasiukajtis
62*5b2ba9d3SPiotr Jasiukajtis float
__nexttowardf(float x,long double y)63*5b2ba9d3SPiotr Jasiukajtis __nexttowardf(float x, long double y) {
64*5b2ba9d3SPiotr Jasiukajtis union {
65*5b2ba9d3SPiotr Jasiukajtis unsigned i;
66*5b2ba9d3SPiotr Jasiukajtis float f;
67*5b2ba9d3SPiotr Jasiukajtis } xx;
68*5b2ba9d3SPiotr Jasiukajtis union {
69*5b2ba9d3SPiotr Jasiukajtis unsigned i[4];
70*5b2ba9d3SPiotr Jasiukajtis long double q;
71*5b2ba9d3SPiotr Jasiukajtis } yy;
72*5b2ba9d3SPiotr Jasiukajtis long double lx;
73*5b2ba9d3SPiotr Jasiukajtis unsigned hx;
74*5b2ba9d3SPiotr Jasiukajtis volatile float dummy;
75*5b2ba9d3SPiotr Jasiukajtis enum fcc_type rel;
76*5b2ba9d3SPiotr Jasiukajtis
77*5b2ba9d3SPiotr Jasiukajtis /*
78*5b2ba9d3SPiotr Jasiukajtis * It would be somewhat more efficient to check for NaN and
79*5b2ba9d3SPiotr Jasiukajtis * zero operands before converting x to long double and then
80*5b2ba9d3SPiotr Jasiukajtis * to code the comparison in line rather than calling _Q_cmp.
81*5b2ba9d3SPiotr Jasiukajtis * However, since this code probably won't get used much,
82*5b2ba9d3SPiotr Jasiukajtis * I'm opting in favor of simplicity instead.
83*5b2ba9d3SPiotr Jasiukajtis */
84*5b2ba9d3SPiotr Jasiukajtis lx = xx.f = x;
85*5b2ba9d3SPiotr Jasiukajtis hx = xx.i & ~0x80000000;
86*5b2ba9d3SPiotr Jasiukajtis
87*5b2ba9d3SPiotr Jasiukajtis /* check for each of four possible orderings */
88*5b2ba9d3SPiotr Jasiukajtis rel = _Q_cmp(&lx, &y);
89*5b2ba9d3SPiotr Jasiukajtis if (rel == fcc_unordered)
90*5b2ba9d3SPiotr Jasiukajtis return (qnan);
91*5b2ba9d3SPiotr Jasiukajtis
92*5b2ba9d3SPiotr Jasiukajtis if (rel == fcc_equal) {
93*5b2ba9d3SPiotr Jasiukajtis if (hx == 0) { /* x is zero; return zero with y's sign */
94*5b2ba9d3SPiotr Jasiukajtis yy.q = y;
95*5b2ba9d3SPiotr Jasiukajtis xx.i = yy.i[0];
96*5b2ba9d3SPiotr Jasiukajtis return (xx.f);
97*5b2ba9d3SPiotr Jasiukajtis }
98*5b2ba9d3SPiotr Jasiukajtis return (x);
99*5b2ba9d3SPiotr Jasiukajtis }
100*5b2ba9d3SPiotr Jasiukajtis
101*5b2ba9d3SPiotr Jasiukajtis if (rel == fcc_less) {
102*5b2ba9d3SPiotr Jasiukajtis if (hx == 0) /* x is zero */
103*5b2ba9d3SPiotr Jasiukajtis xx.i = 0x00000001;
104*5b2ba9d3SPiotr Jasiukajtis else if ((int) xx.i >= 0) /* x is positive */
105*5b2ba9d3SPiotr Jasiukajtis xx.i++;
106*5b2ba9d3SPiotr Jasiukajtis else
107*5b2ba9d3SPiotr Jasiukajtis xx.i--;
108*5b2ba9d3SPiotr Jasiukajtis } else {
109*5b2ba9d3SPiotr Jasiukajtis if (hx == 0) /* x is zero */
110*5b2ba9d3SPiotr Jasiukajtis xx.i = 0x80000001;
111*5b2ba9d3SPiotr Jasiukajtis else if ((int) xx.i >= 0) /* x is positive */
112*5b2ba9d3SPiotr Jasiukajtis xx.i--;
113*5b2ba9d3SPiotr Jasiukajtis else
114*5b2ba9d3SPiotr Jasiukajtis xx.i++;
115*5b2ba9d3SPiotr Jasiukajtis }
116*5b2ba9d3SPiotr Jasiukajtis
117*5b2ba9d3SPiotr Jasiukajtis /* raise exceptions as needed */
118*5b2ba9d3SPiotr Jasiukajtis hx = xx.i & ~0x80000000;
119*5b2ba9d3SPiotr Jasiukajtis if (hx == 0x7f800000) {
120*5b2ba9d3SPiotr Jasiukajtis dummy = huge;
121*5b2ba9d3SPiotr Jasiukajtis dummy *= huge;
122*5b2ba9d3SPiotr Jasiukajtis } else if (hx < 0x00800000) {
123*5b2ba9d3SPiotr Jasiukajtis dummy = tiny;
124*5b2ba9d3SPiotr Jasiukajtis dummy *= tiny;
125*5b2ba9d3SPiotr Jasiukajtis }
126*5b2ba9d3SPiotr Jasiukajtis
127*5b2ba9d3SPiotr Jasiukajtis return (xx.f);
128*5b2ba9d3SPiotr Jasiukajtis }
129*5b2ba9d3SPiotr Jasiukajtis
130*5b2ba9d3SPiotr Jasiukajtis #elif defined(__x86)
131*5b2ba9d3SPiotr Jasiukajtis
132*5b2ba9d3SPiotr Jasiukajtis float
__nexttowardf(float x,long double y)133*5b2ba9d3SPiotr Jasiukajtis __nexttowardf(float x, long double y) {
134*5b2ba9d3SPiotr Jasiukajtis union {
135*5b2ba9d3SPiotr Jasiukajtis unsigned i;
136*5b2ba9d3SPiotr Jasiukajtis float f;
137*5b2ba9d3SPiotr Jasiukajtis } xx;
138*5b2ba9d3SPiotr Jasiukajtis unsigned hx;
139*5b2ba9d3SPiotr Jasiukajtis long double lx;
140*5b2ba9d3SPiotr Jasiukajtis volatile float dummy;
141*5b2ba9d3SPiotr Jasiukajtis
142*5b2ba9d3SPiotr Jasiukajtis lx = xx.f = x;
143*5b2ba9d3SPiotr Jasiukajtis hx = xx.i & ~0x80000000;
144*5b2ba9d3SPiotr Jasiukajtis
145*5b2ba9d3SPiotr Jasiukajtis /* check for each of four possible orderings */
146*5b2ba9d3SPiotr Jasiukajtis if (isunordered(lx, y))
147*5b2ba9d3SPiotr Jasiukajtis return ((float) (lx + y));
148*5b2ba9d3SPiotr Jasiukajtis
149*5b2ba9d3SPiotr Jasiukajtis if (lx == y)
150*5b2ba9d3SPiotr Jasiukajtis return ((float) y);
151*5b2ba9d3SPiotr Jasiukajtis
152*5b2ba9d3SPiotr Jasiukajtis if (lx < y) {
153*5b2ba9d3SPiotr Jasiukajtis if (hx == 0) /* x is zero */
154*5b2ba9d3SPiotr Jasiukajtis xx.i = 0x00000001;
155*5b2ba9d3SPiotr Jasiukajtis else if ((int) xx.i >= 0) /* x is positive */
156*5b2ba9d3SPiotr Jasiukajtis xx.i++;
157*5b2ba9d3SPiotr Jasiukajtis else
158*5b2ba9d3SPiotr Jasiukajtis xx.i--;
159*5b2ba9d3SPiotr Jasiukajtis } else {
160*5b2ba9d3SPiotr Jasiukajtis if (hx == 0) /* x is zero */
161*5b2ba9d3SPiotr Jasiukajtis xx.i = 0x80000001;
162*5b2ba9d3SPiotr Jasiukajtis else if ((int) xx.i >= 0) /* x is positive */
163*5b2ba9d3SPiotr Jasiukajtis xx.i--;
164*5b2ba9d3SPiotr Jasiukajtis else
165*5b2ba9d3SPiotr Jasiukajtis xx.i++;
166*5b2ba9d3SPiotr Jasiukajtis }
167*5b2ba9d3SPiotr Jasiukajtis
168*5b2ba9d3SPiotr Jasiukajtis /* raise exceptions as needed */
169*5b2ba9d3SPiotr Jasiukajtis hx = xx.i & ~0x80000000;
170*5b2ba9d3SPiotr Jasiukajtis if (hx == 0x7f800000) {
171*5b2ba9d3SPiotr Jasiukajtis dummy = huge;
172*5b2ba9d3SPiotr Jasiukajtis dummy *= huge;
173*5b2ba9d3SPiotr Jasiukajtis } else if (hx < 0x00800000) {
174*5b2ba9d3SPiotr Jasiukajtis dummy = tiny;
175*5b2ba9d3SPiotr Jasiukajtis dummy *= tiny;
176*5b2ba9d3SPiotr Jasiukajtis }
177*5b2ba9d3SPiotr Jasiukajtis
178*5b2ba9d3SPiotr Jasiukajtis return (xx.f);
179*5b2ba9d3SPiotr Jasiukajtis }
180*5b2ba9d3SPiotr Jasiukajtis
181*5b2ba9d3SPiotr Jasiukajtis #else
182*5b2ba9d3SPiotr Jasiukajtis #error Unknown architecture
183*5b2ba9d3SPiotr Jasiukajtis #endif
184