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