4 * Functions to add or subtract two registers and put the result in a third.
6 * Copyright (C) 1992,1993,1994
7 * W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
8 * Australia. E-mail billm@vaxc.cc.monash.edu.au
11 * This copyright notice covers the redistribution and use of the
12 * FPU emulator developed by W. Metzenthen. It covers only its use
13 * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
14 * use is not permitted under this copyright.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must include information specifying
22 * that source code for the emulator is freely available and include
24 * a) an offer to provide the source code for a nominal distribution
26 * b) list at least two alternative methods whereby the source
27 * can be obtained, e.g. a publically accessible bulletin board
28 * and an anonymous ftp site from which the software can be
30 * 3. All advertising materials specifically mentioning features or use of
31 * this emulator must acknowledge that it was developed by W. Metzenthen.
32 * 4. The name of W. Metzenthen may not be used to endorse or promote
33 * products derived from this software without specific prior written
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
37 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
38 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
39 * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 * The purpose of this copyright, based upon the Berkeley copyright, is to
49 * ensure that the covered software remains freely available to everyone.
51 * The software (with necessary differences) is also available, but under
52 * the terms of the GNU copyleft, for the Linux operating system and for
53 * the djgpp ms-dos extender.
55 * W. Metzenthen June 1994.
58 * $FreeBSD: src/sys/gnu/i386/fpemul/reg_add_sub.c,v 1.8 1999/08/28 00:42:55 peter Exp $
59 * $DragonFly: src/sys/i386/gnu/fpemul/Attic/reg_add_sub.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
63 /*---------------------------------------------------------------------------+
64 | For each function, the destination may be any FPU_REG, including one of |
65 | the source FPU_REGs. |
66 +---------------------------------------------------------------------------*/
68 #include "reg_constant.h"
69 #include "control_w.h"
73 reg_add(FPU_REG * a, FPU_REG * b, FPU_REG * dest, int control_w)
77 if (!(a->tag | b->tag)) {
78 /* Both registers are valid */
79 if (!(a->sign ^ b->sign)) {
80 /* signs are the same */
81 reg_u_add(a, b, dest, control_w);
85 /* The signs are different, so do a subtraction */
86 diff = a->exp - b->exp;
88 diff = a->sigh - b->sigh; /* Works only if ms bits
91 diff = a->sigl > b->sigl;
93 diff = -(a->sigl < b->sigl);
97 reg_u_sub(a, b, dest, control_w);
101 reg_move(&CONST_Z, dest);
102 /* sign depends upon rounding mode */
103 dest->sign = ((control_w & CW_RC) != RC_DOWN)
104 ? SIGN_POS : SIGN_NEG;
106 reg_u_sub(b, a, dest, control_w);
107 dest->sign = b->sign;
111 if ((a->tag == TW_NaN) || (b->tag == TW_NaN)) {
112 real_2op_NaN(a, b, dest);
115 if (a->tag == TW_Zero) {
116 if (b->tag == TW_Zero) {
117 char different_signs = a->sign ^ b->sign;
118 /* Both are zero, result will be zero. */
120 if (different_signs) {
121 /* Signs are different. */
122 /* Sign of answer depends upon
124 dest->sign = ((control_w & CW_RC) != RC_DOWN)
125 ? SIGN_POS : SIGN_NEG;
128 #ifdef DENORM_OPERAND
129 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
132 #endif /* DENORM_OPERAND */
137 if (b->tag == TW_Zero) {
138 #ifdef DENORM_OPERAND
139 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
142 #endif /* DENORM_OPERAND */
146 if (a->tag == TW_Infinity) {
147 if (b->tag != TW_Infinity) {
148 #ifdef DENORM_OPERAND
149 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
152 #endif /* DENORM_OPERAND */
156 if (a->sign == b->sign) {
157 /* They are both + or
162 arith_invalid(dest); /* Infinity-Infinity is
166 if (b->tag == TW_Infinity) {
167 #ifdef DENORM_OPERAND
168 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
171 #endif /* DENORM_OPERAND */
177 EXCEPTION(EX_INTERNAL | 0x101);
182 /* Subtract b from a. (a-b) -> dest */
184 reg_sub(FPU_REG * a, FPU_REG * b, FPU_REG * dest, int control_w)
188 if (!(a->tag | b->tag)) {
189 /* Both registers are valid */
190 diff = a->exp - b->exp;
192 diff = a->sigh - b->sigh; /* Works only if ms bits
195 diff = a->sigl > b->sigl;
197 diff = -(a->sigl < b->sigl);
200 switch (a->sign * 2 + b->sign) {
204 reg_u_sub(a, b, dest, control_w);
205 dest->sign = a->sign;
208 #ifdef DENORM_OPERAND
209 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
212 #endif /* DENORM_OPERAND */
213 reg_move(&CONST_Z, dest);
214 /* sign depends upon rounding mode */
215 dest->sign = ((control_w & CW_RC) != RC_DOWN)
216 ? SIGN_POS : SIGN_NEG;
218 reg_u_sub(b, a, dest, control_w);
219 dest->sign = a->sign ^ SIGN_POS ^ SIGN_NEG;
223 reg_u_add(a, b, dest, control_w);
224 dest->sign = SIGN_POS;
227 reg_u_add(a, b, dest, control_w);
228 dest->sign = SIGN_NEG;
232 if ((a->tag == TW_NaN) || (b->tag == TW_NaN)) {
233 real_2op_NaN(a, b, dest);
236 if (b->tag == TW_Zero) {
237 if (a->tag == TW_Zero) {
238 char same_signs = !(a->sign ^ b->sign);
239 /* Both are zero, result will be zero. */
240 reg_move(a, dest); /* Answer for different
243 /* Sign depends upon rounding
245 dest->sign = ((control_w & CW_RC) != RC_DOWN)
246 ? SIGN_POS : SIGN_NEG;
249 #ifdef DENORM_OPERAND
250 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
253 #endif /* DENORM_OPERAND */
258 if (a->tag == TW_Zero) {
259 #ifdef DENORM_OPERAND
260 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
263 #endif /* DENORM_OPERAND */
265 dest->sign ^= SIGN_POS ^ SIGN_NEG;
268 if (a->tag == TW_Infinity) {
269 if (b->tag != TW_Infinity) {
270 #ifdef DENORM_OPERAND
271 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
274 #endif /* DENORM_OPERAND */
278 /* Both args are Infinity */
279 if (a->sign == b->sign) {
280 arith_invalid(dest); /* Infinity-Infinity is
287 if (b->tag == TW_Infinity) {
288 #ifdef DENORM_OPERAND
289 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
292 #endif /* DENORM_OPERAND */
294 dest->sign ^= SIGN_POS ^ SIGN_NEG;
299 EXCEPTION(EX_INTERNAL | 0x110);