Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / platform / pc32 / gnu / fpemul / reg_mul.c
1 /*
2  *  reg_mul.c
3  *
4  * Multiply one FPU_REG by another, put the result in a destination FPU_REG.
5  *
6  *
7  * Copyright (C) 1992,1993,1994
8  *                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
9  *                       Australia.  E-mail   billm@vaxc.cc.monash.edu.au
10  * All rights reserved.
11  *
12  * This copyright notice covers the redistribution and use of the
13  * FPU emulator developed by W. Metzenthen. It covers only its use
14  * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
15  * use is not permitted under this copyright.
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 include information specifying
23  *    that source code for the emulator is freely available and include
24  *    either:
25  *      a) an offer to provide the source code for a nominal distribution
26  *         fee, or
27  *      b) list at least two alternative methods whereby the source
28  *         can be obtained, e.g. a publically accessible bulletin board
29  *         and an anonymous ftp site from which the software can be
30  *         downloaded.
31  * 3. All advertising materials specifically mentioning features or use of
32  *    this emulator must acknowledge that it was developed by W. Metzenthen.
33  * 4. The name of W. Metzenthen may not be used to endorse or promote
34  *    products derived from this software without specific prior written
35  *    permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
38  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
39  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
40  * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  *
49  * The purpose of this copyright, based upon the Berkeley copyright, is to
50  * ensure that the covered software remains freely available to everyone.
51  *
52  * The software (with necessary differences) is also available, but under
53  * the terms of the GNU copyleft, for the Linux operating system and for
54  * the djgpp ms-dos extender.
55  *
56  * W. Metzenthen   June 1994.
57  *
58  *
59  * $FreeBSD: src/sys/gnu/i386/fpemul/reg_mul.c,v 1.8 1999/08/28 00:42:56 peter Exp $
60  *
61  */
62
63 /*---------------------------------------------------------------------------+
64  | The destination may be any FPU_REG, including one of the source FPU_REGs. |
65  +---------------------------------------------------------------------------*/
66
67 #include <gnu/i386/fpemul/reg_constant.h>
68
69
70 /* This routine must be called with non-empty source registers */
71 void
72 reg_mul(FPU_REG * a, FPU_REG * b, FPU_REG * dest, unsigned int control_w)
73 {
74         char    sign = (a->sign ^ b->sign);
75
76         if (!(a->tag | b->tag)) {
77                 /* This should be the most common case */
78                 reg_u_mul(a, b, dest, control_w);
79                 dest->sign = sign;
80                 return;
81         } else
82                 if ((a->tag <= TW_Zero) && (b->tag <= TW_Zero)) {
83 #ifdef DENORM_OPERAND
84                         if (((b->tag == TW_Valid) && (b->exp <= EXP_UNDER)) ||
85                             ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER))) {
86                                 if (denormal_operand())
87                                         return;
88                         }
89 #endif                          /* DENORM_OPERAND */
90                         /* Must have either both arguments == zero, or one
91                          * valid and the other zero. The result is therefore
92                          * zero. */
93                         reg_move(&CONST_Z, dest);
94 #ifdef PECULIAR_486
95                         /* The 80486 book says that the answer is +0, but a
96                          * real 80486 appears to behave this way... */
97                         dest->sign = sign;
98 #endif                          /* PECULIAR_486 */
99                         return;
100                 }
101 #if 0                           /* TW_Denormal is not used yet... perhaps
102                                  * never will be. */
103                 else
104                         if ((a->tag <= TW_Denormal) && (b->tag <= TW_Denormal)) {
105                                 /* One or both arguments are de-normalized */
106                                 /* Internal de-normalized numbers are not
107                                  * supported yet */
108                                 EXCEPTION(EX_INTERNAL | 0x105);
109                                 reg_move(&CONST_Z, dest);
110                         }
111 #endif
112                         else {
113                                 /* Must have infinities, NaNs, etc */
114                                 if ((a->tag == TW_NaN) || (b->tag == TW_NaN)) {
115                                         real_2op_NaN(a, b, dest);
116                                         return;
117                                 } else
118                                         if (a->tag == TW_Infinity) {
119                                                 if (b->tag == TW_Zero) {
120                                                         arith_invalid(dest);
121                                                         return;
122                                                 }
123                                                 /* Zero*Infinity is invalid */
124                                                 else {
125 #ifdef DENORM_OPERAND
126                                                         if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER) &&
127                                                             denormal_operand())
128                                                                 return;
129 #endif                          /* DENORM_OPERAND */
130                                                         reg_move(a, dest);
131                                                         dest->sign = sign;
132                                                 }
133                                                 return;
134                                         } else
135                                                 if (b->tag == TW_Infinity) {
136                                                         if (a->tag == TW_Zero) {
137                                                                 arith_invalid(dest);
138                                                                 return;
139                                                         }
140                                                         /* Zero*Infinity is
141                                                          * invalid */
142                                                         else {
143 #ifdef DENORM_OPERAND
144                                                                 if ((a->tag == TW_Valid) && (a->exp <= EXP_UNDER) &&
145                                                                     denormal_operand())
146                                                                         return;
147 #endif                          /* DENORM_OPERAND */
148                                                                 reg_move(b, dest);
149                                                                 dest->sign = sign;
150                                                         }
151                                                         return;
152                                                 }
153 #ifdef PARANOID
154                                                 else {
155                                                         EXCEPTION(EX_INTERNAL | 0x102);
156                                                 }
157 #endif                          /* PARANOID */
158                         }
159 }