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.
62 /*---------------------------------------------------------------------------+
63 | For each function, the destination may be any FPU_REG, including one of |
64 | the source FPU_REGs. |
65 +---------------------------------------------------------------------------*/
67 #include <gnu/i386/fpemul/reg_constant.h>
68 #include <gnu/i386/fpemul/control_w.h>
72 reg_add(FPU_REG * a, FPU_REG * b, FPU_REG * dest, int control_w)
76 if (!(a->tag | b->tag)) {
77 /* Both registers are valid */
78 if (!(a->sign ^ b->sign)) {
79 /* signs are the same */
80 reg_u_add(a, b, dest, control_w);
84 /* The signs are different, so do a subtraction */
85 diff = a->exp - b->exp;
87 diff = a->sigh - b->sigh; /* Works only if ms bits
90 diff = a->sigl > b->sigl;
92 diff = -(a->sigl < b->sigl);
96 reg_u_sub(a, b, dest, control_w);
100 reg_move(&CONST_Z, dest);
101 /* sign depends upon rounding mode */
102 dest->sign = ((control_w & CW_RC) != RC_DOWN)
103 ? SIGN_POS : SIGN_NEG;
105 reg_u_sub(b, a, dest, control_w);
106 dest->sign = b->sign;
110 if ((a->tag == TW_NaN) || (b->tag == TW_NaN)) {
111 real_2op_NaN(a, b, dest);
114 if (a->tag == TW_Zero) {
115 if (b->tag == TW_Zero) {
116 char different_signs = a->sign ^ b->sign;
117 /* Both are zero, result will be zero. */
119 if (different_signs) {
120 /* Signs are different. */
121 /* Sign of answer depends upon
123 dest->sign = ((control_w & CW_RC) != RC_DOWN)
124 ? SIGN_POS : SIGN_NEG;
127 #ifdef DENORM_OPERAND
128 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
131 #endif /* DENORM_OPERAND */
136 if (b->tag == TW_Zero) {
137 #ifdef DENORM_OPERAND
138 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
141 #endif /* DENORM_OPERAND */
145 if (a->tag == TW_Infinity) {
146 if (b->tag != TW_Infinity) {
147 #ifdef DENORM_OPERAND
148 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
151 #endif /* DENORM_OPERAND */
155 if (a->sign == b->sign) {
156 /* They are both + or
161 arith_invalid(dest); /* Infinity-Infinity is
165 if (b->tag == TW_Infinity) {
166 #ifdef DENORM_OPERAND
167 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
170 #endif /* DENORM_OPERAND */
176 EXCEPTION(EX_INTERNAL | 0x101);
181 /* Subtract b from a. (a-b) -> dest */
183 reg_sub(FPU_REG * a, FPU_REG * b, FPU_REG * dest, int control_w)
187 if (!(a->tag | b->tag)) {
188 /* Both registers are valid */
189 diff = a->exp - b->exp;
191 diff = a->sigh - b->sigh; /* Works only if ms bits
194 diff = a->sigl > b->sigl;
196 diff = -(a->sigl < b->sigl);
199 switch (a->sign * 2 + b->sign) {
203 reg_u_sub(a, b, dest, control_w);
204 dest->sign = a->sign;
207 #ifdef DENORM_OPERAND
208 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
211 #endif /* DENORM_OPERAND */
212 reg_move(&CONST_Z, dest);
213 /* sign depends upon rounding mode */
214 dest->sign = ((control_w & CW_RC) != RC_DOWN)
215 ? SIGN_POS : SIGN_NEG;
217 reg_u_sub(b, a, dest, control_w);
218 dest->sign = a->sign ^ SIGN_POS ^ SIGN_NEG;
222 reg_u_add(a, b, dest, control_w);
223 dest->sign = SIGN_POS;
226 reg_u_add(a, b, dest, control_w);
227 dest->sign = SIGN_NEG;
231 if ((a->tag == TW_NaN) || (b->tag == TW_NaN)) {
232 real_2op_NaN(a, b, dest);
235 if (b->tag == TW_Zero) {
236 if (a->tag == TW_Zero) {
237 char same_signs = !(a->sign ^ b->sign);
238 /* Both are zero, result will be zero. */
239 reg_move(a, dest); /* Answer for different
242 /* Sign depends upon rounding
244 dest->sign = ((control_w & CW_RC) != RC_DOWN)
245 ? SIGN_POS : SIGN_NEG;
248 #ifdef DENORM_OPERAND
249 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
252 #endif /* DENORM_OPERAND */
257 if (a->tag == TW_Zero) {
258 #ifdef DENORM_OPERAND
259 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
262 #endif /* DENORM_OPERAND */
264 dest->sign ^= SIGN_POS ^ SIGN_NEG;
267 if (a->tag == TW_Infinity) {
268 if (b->tag != TW_Infinity) {
269 #ifdef DENORM_OPERAND
270 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
273 #endif /* DENORM_OPERAND */
277 /* Both args are Infinity */
278 if (a->sign == b->sign) {
279 arith_invalid(dest); /* Infinity-Infinity is
286 if (b->tag == TW_Infinity) {
287 #ifdef DENORM_OPERAND
288 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
291 #endif /* DENORM_OPERAND */
293 dest->sign ^= SIGN_POS ^ SIGN_NEG;
298 EXCEPTION(EX_INTERNAL | 0x110);