Deleted Added
full compact
fpu_add.c (92986) fpu_add.c (95587)
1/*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)fpu_add.c 8.1 (Berkeley) 6/11/93
43 * $NetBSD: fpu_add.c,v 1.3 1996/03/14 19:41:52 christos Exp $
44 */
45
46#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)fpu_add.c 8.1 (Berkeley) 6/11/93
43 * $NetBSD: fpu_add.c,v 1.3 1996/03/14 19:41:52 christos Exp $
44 */
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/lib/libc/sparc64/fpu/fpu_add.c 92986 2002-03-22 21:53:29Z obrien $");
47__FBSDID("$FreeBSD: head/lib/libc/sparc64/fpu/fpu_add.c 95587 2002-04-27 21:56:28Z jake $");
48
49/*
50 * Perform an FPU add (return x + y).
51 *
52 * To subtract, negate y and call add.
53 */
54
55#include <sys/param.h>
56
57#include <machine/frame.h>
58#include <machine/fp.h>
59#include <machine/fsr.h>
60#include <machine/instr.h>
61
62#include "fpu_arith.h"
63#include "fpu_emu.h"
64#include "fpu_extern.h"
48
49/*
50 * Perform an FPU add (return x + y).
51 *
52 * To subtract, negate y and call add.
53 */
54
55#include <sys/param.h>
56
57#include <machine/frame.h>
58#include <machine/fp.h>
59#include <machine/fsr.h>
60#include <machine/instr.h>
61
62#include "fpu_arith.h"
63#include "fpu_emu.h"
64#include "fpu_extern.h"
65#include "__sparc_utrap_private.h"
65
66struct fpn *
67__fpu_add(fe)
68 struct fpemu *fe;
69{
70 struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r;
71 u_int r0, r1, r2, r3;
72 int rd;
73
74 /*
75 * Put the `heavier' operand on the right (see fpu_emu.h).
76 * Then we will have one of the following cases, taken in the
77 * following order:
78 *
79 * - y = NaN. Implied: if only one is a signalling NaN, y is.
80 * The result is y.
81 * - y = Inf. Implied: x != NaN (is 0, number, or Inf: the NaN
82 * case was taken care of earlier).
83 * If x = -y, the result is NaN. Otherwise the result
84 * is y (an Inf of whichever sign).
85 * - y is 0. Implied: x = 0.
86 * If x and y differ in sign (one positive, one negative),
87 * the result is +0 except when rounding to -Inf. If same:
88 * +0 + +0 = +0; -0 + -0 = -0.
89 * - x is 0. Implied: y != 0.
90 * Result is y.
91 * - other. Implied: both x and y are numbers.
92 * Do addition a la Hennessey & Patterson.
93 */
94 ORDER(x, y);
95 if (ISNAN(y))
96 return (y);
97 if (ISINF(y)) {
98 if (ISINF(x) && x->fp_sign != y->fp_sign)
99 return (__fpu_newnan(fe));
100 return (y);
101 }
102 rd = FSR_GET_RD(fe->fe_fsr);
103 if (ISZERO(y)) {
104 if (rd != FSR_RD_NINF) /* only -0 + -0 gives -0 */
105 y->fp_sign &= x->fp_sign;
106 else /* any -0 operand gives -0 */
107 y->fp_sign |= x->fp_sign;
108 return (y);
109 }
110 if (ISZERO(x))
111 return (y);
112 /*
113 * We really have two numbers to add, although their signs may
114 * differ. Make the exponents match, by shifting the smaller
115 * number right (e.g., 1.011 => 0.1011) and increasing its
116 * exponent (2^3 => 2^4). Note that we do not alter the exponents
117 * of x and y here.
118 */
119 r = &fe->fe_f3;
120 r->fp_class = FPC_NUM;
121 if (x->fp_exp == y->fp_exp) {
122 r->fp_exp = x->fp_exp;
123 r->fp_sticky = 0;
124 } else {
125 if (x->fp_exp < y->fp_exp) {
126 /*
127 * Try to avoid subtract case iii (see below).
128 * This also guarantees that x->fp_sticky = 0.
129 */
130 SWAP(x, y);
131 }
132 /* now x->fp_exp > y->fp_exp */
133 r->fp_exp = x->fp_exp;
134 r->fp_sticky = __fpu_shr(y, x->fp_exp - y->fp_exp);
135 }
136 r->fp_sign = x->fp_sign;
137 if (x->fp_sign == y->fp_sign) {
138 FPU_DECL_CARRY
139
140 /*
141 * The signs match, so we simply add the numbers. The result
142 * may be `supernormal' (as big as 1.111...1 + 1.111...1, or
143 * 11.111...0). If so, a single bit shift-right will fix it
144 * (but remember to adjust the exponent).
145 */
146 /* r->fp_mant = x->fp_mant + y->fp_mant */
147 FPU_ADDS(r->fp_mant[3], x->fp_mant[3], y->fp_mant[3]);
148 FPU_ADDCS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]);
149 FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]);
150 FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]);
151 if ((r->fp_mant[0] = r0) >= FP_2) {
152 (void) __fpu_shr(r, 1);
153 r->fp_exp++;
154 }
155 } else {
156 FPU_DECL_CARRY
157
158 /*
159 * The signs differ, so things are rather more difficult.
160 * H&P would have us negate the negative operand and add;
161 * this is the same as subtracting the negative operand.
162 * This is quite a headache. Instead, we will subtract
163 * y from x, regardless of whether y itself is the negative
164 * operand. When this is done one of three conditions will
165 * hold, depending on the magnitudes of x and y:
166 * case i) |x| > |y|. The result is just x - y,
167 * with x's sign, but it may need to be normalized.
168 * case ii) |x| = |y|. The result is 0 (maybe -0)
169 * so must be fixed up.
170 * case iii) |x| < |y|. We goofed; the result should
171 * be (y - x), with the same sign as y.
172 * We could compare |x| and |y| here and avoid case iii,
173 * but that would take just as much work as the subtract.
174 * We can tell case iii has occurred by an overflow.
175 *
176 * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0.
177 */
178 /* r->fp_mant = x->fp_mant - y->fp_mant */
179 FPU_SET_CARRY(y->fp_sticky);
180 FPU_SUBCS(r3, x->fp_mant[3], y->fp_mant[3]);
181 FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]);
182 FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]);
183 FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]);
184 if (r0 < FP_2) {
185 /* cases i and ii */
186 if ((r0 | r1 | r2 | r3) == 0) {
187 /* case ii */
188 r->fp_class = FPC_ZERO;
189 r->fp_sign = rd == FSR_RD_NINF;
190 return (r);
191 }
192 } else {
193 /*
194 * Oops, case iii. This can only occur when the
195 * exponents were equal, in which case neither
196 * x nor y have sticky bits set. Flip the sign
197 * (to y's sign) and negate the result to get y - x.
198 */
199#ifdef DIAGNOSTIC
200 if (x->fp_exp != y->fp_exp || r->fp_sticky)
66
67struct fpn *
68__fpu_add(fe)
69 struct fpemu *fe;
70{
71 struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r;
72 u_int r0, r1, r2, r3;
73 int rd;
74
75 /*
76 * Put the `heavier' operand on the right (see fpu_emu.h).
77 * Then we will have one of the following cases, taken in the
78 * following order:
79 *
80 * - y = NaN. Implied: if only one is a signalling NaN, y is.
81 * The result is y.
82 * - y = Inf. Implied: x != NaN (is 0, number, or Inf: the NaN
83 * case was taken care of earlier).
84 * If x = -y, the result is NaN. Otherwise the result
85 * is y (an Inf of whichever sign).
86 * - y is 0. Implied: x = 0.
87 * If x and y differ in sign (one positive, one negative),
88 * the result is +0 except when rounding to -Inf. If same:
89 * +0 + +0 = +0; -0 + -0 = -0.
90 * - x is 0. Implied: y != 0.
91 * Result is y.
92 * - other. Implied: both x and y are numbers.
93 * Do addition a la Hennessey & Patterson.
94 */
95 ORDER(x, y);
96 if (ISNAN(y))
97 return (y);
98 if (ISINF(y)) {
99 if (ISINF(x) && x->fp_sign != y->fp_sign)
100 return (__fpu_newnan(fe));
101 return (y);
102 }
103 rd = FSR_GET_RD(fe->fe_fsr);
104 if (ISZERO(y)) {
105 if (rd != FSR_RD_NINF) /* only -0 + -0 gives -0 */
106 y->fp_sign &= x->fp_sign;
107 else /* any -0 operand gives -0 */
108 y->fp_sign |= x->fp_sign;
109 return (y);
110 }
111 if (ISZERO(x))
112 return (y);
113 /*
114 * We really have two numbers to add, although their signs may
115 * differ. Make the exponents match, by shifting the smaller
116 * number right (e.g., 1.011 => 0.1011) and increasing its
117 * exponent (2^3 => 2^4). Note that we do not alter the exponents
118 * of x and y here.
119 */
120 r = &fe->fe_f3;
121 r->fp_class = FPC_NUM;
122 if (x->fp_exp == y->fp_exp) {
123 r->fp_exp = x->fp_exp;
124 r->fp_sticky = 0;
125 } else {
126 if (x->fp_exp < y->fp_exp) {
127 /*
128 * Try to avoid subtract case iii (see below).
129 * This also guarantees that x->fp_sticky = 0.
130 */
131 SWAP(x, y);
132 }
133 /* now x->fp_exp > y->fp_exp */
134 r->fp_exp = x->fp_exp;
135 r->fp_sticky = __fpu_shr(y, x->fp_exp - y->fp_exp);
136 }
137 r->fp_sign = x->fp_sign;
138 if (x->fp_sign == y->fp_sign) {
139 FPU_DECL_CARRY
140
141 /*
142 * The signs match, so we simply add the numbers. The result
143 * may be `supernormal' (as big as 1.111...1 + 1.111...1, or
144 * 11.111...0). If so, a single bit shift-right will fix it
145 * (but remember to adjust the exponent).
146 */
147 /* r->fp_mant = x->fp_mant + y->fp_mant */
148 FPU_ADDS(r->fp_mant[3], x->fp_mant[3], y->fp_mant[3]);
149 FPU_ADDCS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]);
150 FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]);
151 FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]);
152 if ((r->fp_mant[0] = r0) >= FP_2) {
153 (void) __fpu_shr(r, 1);
154 r->fp_exp++;
155 }
156 } else {
157 FPU_DECL_CARRY
158
159 /*
160 * The signs differ, so things are rather more difficult.
161 * H&P would have us negate the negative operand and add;
162 * this is the same as subtracting the negative operand.
163 * This is quite a headache. Instead, we will subtract
164 * y from x, regardless of whether y itself is the negative
165 * operand. When this is done one of three conditions will
166 * hold, depending on the magnitudes of x and y:
167 * case i) |x| > |y|. The result is just x - y,
168 * with x's sign, but it may need to be normalized.
169 * case ii) |x| = |y|. The result is 0 (maybe -0)
170 * so must be fixed up.
171 * case iii) |x| < |y|. We goofed; the result should
172 * be (y - x), with the same sign as y.
173 * We could compare |x| and |y| here and avoid case iii,
174 * but that would take just as much work as the subtract.
175 * We can tell case iii has occurred by an overflow.
176 *
177 * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0.
178 */
179 /* r->fp_mant = x->fp_mant - y->fp_mant */
180 FPU_SET_CARRY(y->fp_sticky);
181 FPU_SUBCS(r3, x->fp_mant[3], y->fp_mant[3]);
182 FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]);
183 FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]);
184 FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]);
185 if (r0 < FP_2) {
186 /* cases i and ii */
187 if ((r0 | r1 | r2 | r3) == 0) {
188 /* case ii */
189 r->fp_class = FPC_ZERO;
190 r->fp_sign = rd == FSR_RD_NINF;
191 return (r);
192 }
193 } else {
194 /*
195 * Oops, case iii. This can only occur when the
196 * exponents were equal, in which case neither
197 * x nor y have sticky bits set. Flip the sign
198 * (to y's sign) and negate the result to get y - x.
199 */
200#ifdef DIAGNOSTIC
201 if (x->fp_exp != y->fp_exp || r->fp_sticky)
201 __fpu_panic("fpu_add");
202 __utrap_panic("fpu_add");
202#endif
203 r->fp_sign = y->fp_sign;
204 FPU_SUBS(r3, 0, r3);
205 FPU_SUBCS(r2, 0, r2);
206 FPU_SUBCS(r1, 0, r1);
207 FPU_SUBC(r0, 0, r0);
208 }
209 r->fp_mant[3] = r3;
210 r->fp_mant[2] = r2;
211 r->fp_mant[1] = r1;
212 r->fp_mant[0] = r0;
213 if (r0 < FP_1)
214 __fpu_norm(r);
215 }
216 return (r);
217}
203#endif
204 r->fp_sign = y->fp_sign;
205 FPU_SUBS(r3, 0, r3);
206 FPU_SUBCS(r2, 0, r2);
207 FPU_SUBCS(r1, 0, r1);
208 FPU_SUBC(r0, 0, r0);
209 }
210 r->fp_mant[3] = r3;
211 r->fp_mant[2] = r2;
212 r->fp_mant[1] = r1;
213 r->fp_mant[0] = r0;
214 if (r0 < FP_1)
215 __fpu_norm(r);
216 }
217 return (r);
218}