1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1994-1997, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * This file contains __quad_mag_add and __quad_mag_sub, the core
29*7c478bd9Sstevel@tonic-gate * of the quad precision add and subtract operations.
30*7c478bd9Sstevel@tonic-gate */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include "quad.h"
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate * __quad_mag_add(x, y, z, fsr)
36*7c478bd9Sstevel@tonic-gate *
37*7c478bd9Sstevel@tonic-gate * Sets *z = *x + *y, rounded according to the rounding mode in *fsr,
38*7c478bd9Sstevel@tonic-gate * and updates the current exceptions in *fsr. This routine assumes
39*7c478bd9Sstevel@tonic-gate * *x and *y are finite, with the same sign (i.e., an addition of
40*7c478bd9Sstevel@tonic-gate * magnitudes), |*x| >= |*y|, and *z already has its sign bit set.
41*7c478bd9Sstevel@tonic-gate */
42*7c478bd9Sstevel@tonic-gate void
__quad_mag_add(const union longdouble * x,const union longdouble * y,union longdouble * z,unsigned int * fsr)43*7c478bd9Sstevel@tonic-gate __quad_mag_add(const union longdouble *x, const union longdouble *y,
44*7c478bd9Sstevel@tonic-gate union longdouble *z, unsigned int *fsr)
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate unsigned int lx, ly, ex, ey, frac2, frac3, frac4;
47*7c478bd9Sstevel@tonic-gate unsigned int round, sticky, carry, rm;
48*7c478bd9Sstevel@tonic-gate int e, uflo;
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate /* get the leading significand words and exponents */
51*7c478bd9Sstevel@tonic-gate ex = (x->l.msw & 0x7fffffff) >> 16;
52*7c478bd9Sstevel@tonic-gate lx = x->l.msw & 0xffff;
53*7c478bd9Sstevel@tonic-gate if (ex == 0)
54*7c478bd9Sstevel@tonic-gate ex = 1;
55*7c478bd9Sstevel@tonic-gate else
56*7c478bd9Sstevel@tonic-gate lx |= 0x10000;
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate ey = (y->l.msw & 0x7fffffff) >> 16;
59*7c478bd9Sstevel@tonic-gate ly = y->l.msw & 0xffff;
60*7c478bd9Sstevel@tonic-gate if (ey == 0)
61*7c478bd9Sstevel@tonic-gate ey = 1;
62*7c478bd9Sstevel@tonic-gate else
63*7c478bd9Sstevel@tonic-gate ly |= 0x10000;
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate /* prenormalize y */
66*7c478bd9Sstevel@tonic-gate e = (int) ex - (int) ey;
67*7c478bd9Sstevel@tonic-gate round = sticky = 0;
68*7c478bd9Sstevel@tonic-gate if (e >= 114) {
69*7c478bd9Sstevel@tonic-gate frac2 = x->l.frac2;
70*7c478bd9Sstevel@tonic-gate frac3 = x->l.frac3;
71*7c478bd9Sstevel@tonic-gate frac4 = x->l.frac4;
72*7c478bd9Sstevel@tonic-gate sticky = ly | y->l.frac2 | y->l.frac3 | y->l.frac4;
73*7c478bd9Sstevel@tonic-gate } else {
74*7c478bd9Sstevel@tonic-gate frac2 = y->l.frac2;
75*7c478bd9Sstevel@tonic-gate frac3 = y->l.frac3;
76*7c478bd9Sstevel@tonic-gate frac4 = y->l.frac4;
77*7c478bd9Sstevel@tonic-gate if (e >= 96) {
78*7c478bd9Sstevel@tonic-gate sticky = frac4 | frac3 | (frac2 & 0x7fffffff);
79*7c478bd9Sstevel@tonic-gate round = frac2 & 0x80000000;
80*7c478bd9Sstevel@tonic-gate frac4 = ly;
81*7c478bd9Sstevel@tonic-gate frac3 = frac2 = ly = 0;
82*7c478bd9Sstevel@tonic-gate e -= 96;
83*7c478bd9Sstevel@tonic-gate } else if (e >= 64) {
84*7c478bd9Sstevel@tonic-gate sticky = frac4 | (frac3 & 0x7fffffff);
85*7c478bd9Sstevel@tonic-gate round = frac3 & 0x80000000;
86*7c478bd9Sstevel@tonic-gate frac4 = frac2;
87*7c478bd9Sstevel@tonic-gate frac3 = ly;
88*7c478bd9Sstevel@tonic-gate frac2 = ly = 0;
89*7c478bd9Sstevel@tonic-gate e -= 64;
90*7c478bd9Sstevel@tonic-gate } else if (e >= 32) {
91*7c478bd9Sstevel@tonic-gate sticky = frac4 & 0x7fffffff;
92*7c478bd9Sstevel@tonic-gate round = frac4 & 0x80000000;
93*7c478bd9Sstevel@tonic-gate frac4 = frac3;
94*7c478bd9Sstevel@tonic-gate frac3 = frac2;
95*7c478bd9Sstevel@tonic-gate frac2 = ly;
96*7c478bd9Sstevel@tonic-gate ly = 0;
97*7c478bd9Sstevel@tonic-gate e -= 32;
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate if (e) {
100*7c478bd9Sstevel@tonic-gate sticky |= round | (frac4 & ((1 << (e - 1)) - 1));
101*7c478bd9Sstevel@tonic-gate round = frac4 & (1 << (e - 1));
102*7c478bd9Sstevel@tonic-gate frac4 = (frac4 >> e) | (frac3 << (32 - e));
103*7c478bd9Sstevel@tonic-gate frac3 = (frac3 >> e) | (frac2 << (32 - e));
104*7c478bd9Sstevel@tonic-gate frac2 = (frac2 >> e) | (ly << (32 - e));
105*7c478bd9Sstevel@tonic-gate ly >>= e;
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate /* add, propagating carries */
109*7c478bd9Sstevel@tonic-gate frac4 += x->l.frac4;
110*7c478bd9Sstevel@tonic-gate carry = (frac4 < x->l.frac4);
111*7c478bd9Sstevel@tonic-gate frac3 += x->l.frac3;
112*7c478bd9Sstevel@tonic-gate if (carry) {
113*7c478bd9Sstevel@tonic-gate frac3++;
114*7c478bd9Sstevel@tonic-gate carry = (frac3 <= x->l.frac3);
115*7c478bd9Sstevel@tonic-gate } else {
116*7c478bd9Sstevel@tonic-gate carry = (frac3 < x->l.frac3);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate frac2 += x->l.frac2;
119*7c478bd9Sstevel@tonic-gate if (carry) {
120*7c478bd9Sstevel@tonic-gate frac2++;
121*7c478bd9Sstevel@tonic-gate carry = (frac2 <= x->l.frac2);
122*7c478bd9Sstevel@tonic-gate } else {
123*7c478bd9Sstevel@tonic-gate carry = (frac2 < x->l.frac2);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate lx += ly;
126*7c478bd9Sstevel@tonic-gate if (carry)
127*7c478bd9Sstevel@tonic-gate lx++;
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate /* postnormalize */
130*7c478bd9Sstevel@tonic-gate if (lx >= 0x20000) {
131*7c478bd9Sstevel@tonic-gate sticky |= round;
132*7c478bd9Sstevel@tonic-gate round = frac4 & 1;
133*7c478bd9Sstevel@tonic-gate frac4 = (frac4 >> 1) | (frac3 << 31);
134*7c478bd9Sstevel@tonic-gate frac3 = (frac3 >> 1) | (frac2 << 31);
135*7c478bd9Sstevel@tonic-gate frac2 = (frac2 >> 1) | (lx << 31);
136*7c478bd9Sstevel@tonic-gate lx >>= 1;
137*7c478bd9Sstevel@tonic-gate ex++;
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate /* keep track of whether the result before rounding is tiny */
142*7c478bd9Sstevel@tonic-gate uflo = (lx < 0x10000);
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate /* get the rounding mode, fudging directed rounding modes */
145*7c478bd9Sstevel@tonic-gate /* as though the result were positive */
146*7c478bd9Sstevel@tonic-gate rm = *fsr >> 30;
147*7c478bd9Sstevel@tonic-gate if (z->l.msw)
148*7c478bd9Sstevel@tonic-gate rm ^= (rm >> 1);
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate /* see if we need to round */
151*7c478bd9Sstevel@tonic-gate if (round | sticky) {
152*7c478bd9Sstevel@tonic-gate *fsr |= FSR_NXC;
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate /* round up if necessary */
155*7c478bd9Sstevel@tonic-gate if (rm == FSR_RP || (rm == FSR_RN && round &&
156*7c478bd9Sstevel@tonic-gate (sticky || (frac4 & 1)))) {
157*7c478bd9Sstevel@tonic-gate if (++frac4 == 0)
158*7c478bd9Sstevel@tonic-gate if (++frac3 == 0)
159*7c478bd9Sstevel@tonic-gate if (++frac2 == 0)
160*7c478bd9Sstevel@tonic-gate if (++lx >= 0x20000) {
161*7c478bd9Sstevel@tonic-gate lx >>= 1;
162*7c478bd9Sstevel@tonic-gate ex++;
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate
167*7c478bd9Sstevel@tonic-gate /* check for overflow */
168*7c478bd9Sstevel@tonic-gate if (ex >= 0x7fff) {
169*7c478bd9Sstevel@tonic-gate /* store the default overflowed result */
170*7c478bd9Sstevel@tonic-gate *fsr |= FSR_OFC | FSR_NXC;
171*7c478bd9Sstevel@tonic-gate if (rm == FSR_RN || rm == FSR_RP) {
172*7c478bd9Sstevel@tonic-gate z->l.msw |= 0x7fff0000;
173*7c478bd9Sstevel@tonic-gate z->l.frac2 = z->l.frac3 = z->l.frac4 = 0;
174*7c478bd9Sstevel@tonic-gate } else {
175*7c478bd9Sstevel@tonic-gate z->l.msw |= 0x7ffeffff;
176*7c478bd9Sstevel@tonic-gate z->l.frac2 = z->l.frac3 = z->l.frac4 = 0xffffffff;
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate } else {
179*7c478bd9Sstevel@tonic-gate /* store the result */
180*7c478bd9Sstevel@tonic-gate if (lx >= 0x10000)
181*7c478bd9Sstevel@tonic-gate z->l.msw |= (ex << 16);
182*7c478bd9Sstevel@tonic-gate z->l.msw |= (lx & 0xffff);
183*7c478bd9Sstevel@tonic-gate z->l.frac2 = frac2;
184*7c478bd9Sstevel@tonic-gate z->l.frac3 = frac3;
185*7c478bd9Sstevel@tonic-gate z->l.frac4 = frac4;
186*7c478bd9Sstevel@tonic-gate
187*7c478bd9Sstevel@tonic-gate /* if the pre-rounded result was tiny and underflow trapping */
188*7c478bd9Sstevel@tonic-gate /* is enabled, simulate underflow */
189*7c478bd9Sstevel@tonic-gate if (uflo && (*fsr & FSR_UFM))
190*7c478bd9Sstevel@tonic-gate *fsr |= FSR_UFC;
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate /*
195*7c478bd9Sstevel@tonic-gate * __quad_mag_sub(x, y, z, fsr)
196*7c478bd9Sstevel@tonic-gate *
197*7c478bd9Sstevel@tonic-gate * Sets *z = *x - *y, rounded according to the rounding mode in *fsr,
198*7c478bd9Sstevel@tonic-gate * and updates the current exceptions in *fsr. This routine assumes
199*7c478bd9Sstevel@tonic-gate * *x and *y are finite, with opposite signs (i.e., a subtraction of
200*7c478bd9Sstevel@tonic-gate * magnitudes), |*x| >= |*y|, and *z already has its sign bit set.
201*7c478bd9Sstevel@tonic-gate */
202*7c478bd9Sstevel@tonic-gate void
__quad_mag_sub(const union longdouble * x,const union longdouble * y,union longdouble * z,unsigned int * fsr)203*7c478bd9Sstevel@tonic-gate __quad_mag_sub(const union longdouble *x, const union longdouble *y,
204*7c478bd9Sstevel@tonic-gate union longdouble *z, unsigned int *fsr)
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate unsigned int lx, ly, ex, ey, frac2, frac3, frac4;
207*7c478bd9Sstevel@tonic-gate unsigned int guard, round, sticky, borrow, rm;
208*7c478bd9Sstevel@tonic-gate int e;
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate /* get the leading significand words and exponents */
211*7c478bd9Sstevel@tonic-gate ex = (x->l.msw & 0x7fffffff) >> 16;
212*7c478bd9Sstevel@tonic-gate lx = x->l.msw & 0xffff;
213*7c478bd9Sstevel@tonic-gate if (ex == 0)
214*7c478bd9Sstevel@tonic-gate ex = 1;
215*7c478bd9Sstevel@tonic-gate else
216*7c478bd9Sstevel@tonic-gate lx |= 0x10000;
217*7c478bd9Sstevel@tonic-gate
218*7c478bd9Sstevel@tonic-gate ey = (y->l.msw & 0x7fffffff) >> 16;
219*7c478bd9Sstevel@tonic-gate ly = y->l.msw & 0xffff;
220*7c478bd9Sstevel@tonic-gate if (ey == 0)
221*7c478bd9Sstevel@tonic-gate ey = 1;
222*7c478bd9Sstevel@tonic-gate else
223*7c478bd9Sstevel@tonic-gate ly |= 0x10000;
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate /* prenormalize y */
226*7c478bd9Sstevel@tonic-gate e = (int) ex - (int) ey;
227*7c478bd9Sstevel@tonic-gate guard = round = sticky = 0;
228*7c478bd9Sstevel@tonic-gate if (e > 114) {
229*7c478bd9Sstevel@tonic-gate sticky = ly | y->l.frac2 | y->l.frac3 | y->l.frac4;
230*7c478bd9Sstevel@tonic-gate ly = frac2 = frac3 = frac4 = 0;
231*7c478bd9Sstevel@tonic-gate } else {
232*7c478bd9Sstevel@tonic-gate frac2 = y->l.frac2;
233*7c478bd9Sstevel@tonic-gate frac3 = y->l.frac3;
234*7c478bd9Sstevel@tonic-gate frac4 = y->l.frac4;
235*7c478bd9Sstevel@tonic-gate if (e >= 96) {
236*7c478bd9Sstevel@tonic-gate sticky = frac4 | frac3 | (frac2 & 0x3fffffff);
237*7c478bd9Sstevel@tonic-gate round = frac2 & 0x40000000;
238*7c478bd9Sstevel@tonic-gate guard = frac2 & 0x80000000;
239*7c478bd9Sstevel@tonic-gate frac4 = ly;
240*7c478bd9Sstevel@tonic-gate frac3 = frac2 = ly = 0;
241*7c478bd9Sstevel@tonic-gate e -= 96;
242*7c478bd9Sstevel@tonic-gate } else if (e >= 64) {
243*7c478bd9Sstevel@tonic-gate sticky = frac4 | (frac3 & 0x3fffffff);
244*7c478bd9Sstevel@tonic-gate round = frac3 & 0x40000000;
245*7c478bd9Sstevel@tonic-gate guard = frac3 & 0x80000000;
246*7c478bd9Sstevel@tonic-gate frac4 = frac2;
247*7c478bd9Sstevel@tonic-gate frac3 = ly;
248*7c478bd9Sstevel@tonic-gate frac2 = ly = 0;
249*7c478bd9Sstevel@tonic-gate e -= 64;
250*7c478bd9Sstevel@tonic-gate } else if (e >= 32) {
251*7c478bd9Sstevel@tonic-gate sticky = frac4 & 0x3fffffff;
252*7c478bd9Sstevel@tonic-gate round = frac4 & 0x40000000;
253*7c478bd9Sstevel@tonic-gate guard = frac4 & 0x80000000;
254*7c478bd9Sstevel@tonic-gate frac4 = frac3;
255*7c478bd9Sstevel@tonic-gate frac3 = frac2;
256*7c478bd9Sstevel@tonic-gate frac2 = ly;
257*7c478bd9Sstevel@tonic-gate ly = 0;
258*7c478bd9Sstevel@tonic-gate e -= 32;
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate if (e > 1) {
261*7c478bd9Sstevel@tonic-gate sticky |= guard | round |
262*7c478bd9Sstevel@tonic-gate (frac4 & ((1 << (e - 2)) - 1));
263*7c478bd9Sstevel@tonic-gate round = frac4 & (1 << (e - 2));
264*7c478bd9Sstevel@tonic-gate guard = frac4 & (1 << (e - 1));
265*7c478bd9Sstevel@tonic-gate frac4 = (frac4 >> e) | (frac3 << (32 - e));
266*7c478bd9Sstevel@tonic-gate frac3 = (frac3 >> e) | (frac2 << (32 - e));
267*7c478bd9Sstevel@tonic-gate frac2 = (frac2 >> e) | (ly << (32 - e));
268*7c478bd9Sstevel@tonic-gate ly >>= e;
269*7c478bd9Sstevel@tonic-gate } else if (e == 1) {
270*7c478bd9Sstevel@tonic-gate sticky |= round;
271*7c478bd9Sstevel@tonic-gate round = guard;
272*7c478bd9Sstevel@tonic-gate guard = frac4 & 1;
273*7c478bd9Sstevel@tonic-gate frac4 = (frac4 >> 1) | (frac3 << 31);
274*7c478bd9Sstevel@tonic-gate frac3 = (frac3 >> 1) | (frac2 << 31);
275*7c478bd9Sstevel@tonic-gate frac2 = (frac2 >> 1) | (ly << 31);
276*7c478bd9Sstevel@tonic-gate ly >>= 1;
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate
280*7c478bd9Sstevel@tonic-gate /* complement guard, round, and sticky as need be */
281*7c478bd9Sstevel@tonic-gate if (sticky) {
282*7c478bd9Sstevel@tonic-gate round = !round;
283*7c478bd9Sstevel@tonic-gate guard = !guard;
284*7c478bd9Sstevel@tonic-gate } else if (round) {
285*7c478bd9Sstevel@tonic-gate guard = !guard;
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate borrow = (guard | round | sticky);
288*7c478bd9Sstevel@tonic-gate
289*7c478bd9Sstevel@tonic-gate /* subtract, propagating borrows */
290*7c478bd9Sstevel@tonic-gate frac4 = x->l.frac4 - frac4;
291*7c478bd9Sstevel@tonic-gate if (borrow) {
292*7c478bd9Sstevel@tonic-gate frac4--;
293*7c478bd9Sstevel@tonic-gate borrow = (frac4 >= x->l.frac4);
294*7c478bd9Sstevel@tonic-gate } else {
295*7c478bd9Sstevel@tonic-gate borrow = (frac4 > x->l.frac4);
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate frac3 = x->l.frac3 - frac3;
298*7c478bd9Sstevel@tonic-gate if (borrow) {
299*7c478bd9Sstevel@tonic-gate frac3--;
300*7c478bd9Sstevel@tonic-gate borrow = (frac3 >= x->l.frac3);
301*7c478bd9Sstevel@tonic-gate } else {
302*7c478bd9Sstevel@tonic-gate borrow = (frac3 > x->l.frac3);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate frac2 = x->l.frac2 - frac2;
305*7c478bd9Sstevel@tonic-gate if (borrow) {
306*7c478bd9Sstevel@tonic-gate frac2--;
307*7c478bd9Sstevel@tonic-gate borrow = (frac2 >= x->l.frac2);
308*7c478bd9Sstevel@tonic-gate } else {
309*7c478bd9Sstevel@tonic-gate borrow = (frac2 > x->l.frac2);
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate lx -= ly;
312*7c478bd9Sstevel@tonic-gate if (borrow)
313*7c478bd9Sstevel@tonic-gate lx--;
314*7c478bd9Sstevel@tonic-gate
315*7c478bd9Sstevel@tonic-gate /* get the rounding mode */
316*7c478bd9Sstevel@tonic-gate rm = *fsr >> 30;
317*7c478bd9Sstevel@tonic-gate
318*7c478bd9Sstevel@tonic-gate /* handle zero result */
319*7c478bd9Sstevel@tonic-gate if (!(lx | frac2 | frac3 | frac4 | guard)) {
320*7c478bd9Sstevel@tonic-gate z->l.msw = ((rm == FSR_RM)? 0x80000000 : 0);
321*7c478bd9Sstevel@tonic-gate z->l.frac2 = z->l.frac3 = z->l.frac4 = 0;
322*7c478bd9Sstevel@tonic-gate return;
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate
325*7c478bd9Sstevel@tonic-gate /* postnormalize */
326*7c478bd9Sstevel@tonic-gate if (lx < 0x10000) {
327*7c478bd9Sstevel@tonic-gate /* if cancellation occurred or the exponent is 1, */
328*7c478bd9Sstevel@tonic-gate /* the result is exact */
329*7c478bd9Sstevel@tonic-gate if (lx < 0x8000 || ex == 1) {
330*7c478bd9Sstevel@tonic-gate while ((lx | (frac2 & 0xfffe0000)) == 0 && ex > 32) {
331*7c478bd9Sstevel@tonic-gate lx = frac2;
332*7c478bd9Sstevel@tonic-gate frac2 = frac3;
333*7c478bd9Sstevel@tonic-gate frac3 = frac4;
334*7c478bd9Sstevel@tonic-gate frac4 = ((guard)? 0x80000000 : 0);
335*7c478bd9Sstevel@tonic-gate guard = 0;
336*7c478bd9Sstevel@tonic-gate ex -= 32;
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate while (lx < 0x10000 && ex > 1) {
339*7c478bd9Sstevel@tonic-gate lx = (lx << 1) | (frac2 >> 31);
340*7c478bd9Sstevel@tonic-gate frac2 = (frac2 << 1) | (frac3 >> 31);
341*7c478bd9Sstevel@tonic-gate frac3 = (frac3 << 1) | (frac4 >> 31);
342*7c478bd9Sstevel@tonic-gate frac4 <<= 1;
343*7c478bd9Sstevel@tonic-gate if (guard) {
344*7c478bd9Sstevel@tonic-gate frac4 |= 1;
345*7c478bd9Sstevel@tonic-gate guard = 0;
346*7c478bd9Sstevel@tonic-gate }
347*7c478bd9Sstevel@tonic-gate ex--;
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate if (lx >= 0x10000)
350*7c478bd9Sstevel@tonic-gate z->l.msw |= (ex << 16);
351*7c478bd9Sstevel@tonic-gate z->l.msw |= (lx & 0xffff);
352*7c478bd9Sstevel@tonic-gate z->l.frac2 = frac2;
353*7c478bd9Sstevel@tonic-gate z->l.frac3 = frac3;
354*7c478bd9Sstevel@tonic-gate z->l.frac4 = frac4;
355*7c478bd9Sstevel@tonic-gate
356*7c478bd9Sstevel@tonic-gate /* if the result is tiny and underflow trapping is */
357*7c478bd9Sstevel@tonic-gate /* enabled, simulate underflow */
358*7c478bd9Sstevel@tonic-gate if (lx < 0x10000 && (*fsr & FSR_UFM))
359*7c478bd9Sstevel@tonic-gate *fsr |= FSR_UFC;
360*7c478bd9Sstevel@tonic-gate return;
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate
363*7c478bd9Sstevel@tonic-gate /* otherwise we only borrowed one place */
364*7c478bd9Sstevel@tonic-gate lx = (lx << 1) | (frac2 >> 31);
365*7c478bd9Sstevel@tonic-gate frac2 = (frac2 << 1) | (frac3 >> 31);
366*7c478bd9Sstevel@tonic-gate frac3 = (frac3 << 1) | (frac4 >> 31);
367*7c478bd9Sstevel@tonic-gate frac4 <<= 1;
368*7c478bd9Sstevel@tonic-gate if (guard)
369*7c478bd9Sstevel@tonic-gate frac4 |= 1;
370*7c478bd9Sstevel@tonic-gate ex--;
371*7c478bd9Sstevel@tonic-gate } else {
372*7c478bd9Sstevel@tonic-gate sticky |= round;
373*7c478bd9Sstevel@tonic-gate round = guard;
374*7c478bd9Sstevel@tonic-gate }
375*7c478bd9Sstevel@tonic-gate
376*7c478bd9Sstevel@tonic-gate /* fudge directed rounding modes as though the result were positive */
377*7c478bd9Sstevel@tonic-gate if (z->l.msw)
378*7c478bd9Sstevel@tonic-gate rm ^= (rm >> 1);
379*7c478bd9Sstevel@tonic-gate
380*7c478bd9Sstevel@tonic-gate /* see if we need to round */
381*7c478bd9Sstevel@tonic-gate if (round | sticky) {
382*7c478bd9Sstevel@tonic-gate *fsr |= FSR_NXC;
383*7c478bd9Sstevel@tonic-gate
384*7c478bd9Sstevel@tonic-gate /* round up if necessary */
385*7c478bd9Sstevel@tonic-gate if (rm == FSR_RP || (rm == FSR_RN && round &&
386*7c478bd9Sstevel@tonic-gate (sticky || (frac4 & 1)))) {
387*7c478bd9Sstevel@tonic-gate if (++frac4 == 0)
388*7c478bd9Sstevel@tonic-gate if (++frac3 == 0)
389*7c478bd9Sstevel@tonic-gate if (++frac2 == 0)
390*7c478bd9Sstevel@tonic-gate if (++lx >= 0x20000) {
391*7c478bd9Sstevel@tonic-gate lx >>= 1;
392*7c478bd9Sstevel@tonic-gate ex++;
393*7c478bd9Sstevel@tonic-gate }
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate }
396*7c478bd9Sstevel@tonic-gate
397*7c478bd9Sstevel@tonic-gate /* store the result */
398*7c478bd9Sstevel@tonic-gate z->l.msw |= (ex << 16) | (lx & 0xffff);
399*7c478bd9Sstevel@tonic-gate z->l.frac2 = frac2;
400*7c478bd9Sstevel@tonic-gate z->l.frac3 = frac3;
401*7c478bd9Sstevel@tonic-gate z->l.frac4 = frac4;
402*7c478bd9Sstevel@tonic-gate }
403