1*2c7a42e9Smiod /* $OpenBSD: fpu_mul.c,v 1.4 2024/03/29 21:07:11 miod Exp $ */
2839f47eaSjason /* $NetBSD: fpu_mul.c,v 1.2 1994/11/20 20:52:44 deraadt Exp $ */
3839f47eaSjason
4839f47eaSjason /*
5839f47eaSjason * Copyright (c) 1992, 1993
6839f47eaSjason * The Regents of the University of California. All rights reserved.
7839f47eaSjason *
8839f47eaSjason * This software was developed by the Computer Systems Engineering group
9839f47eaSjason * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10839f47eaSjason * contributed to Berkeley.
11839f47eaSjason *
12839f47eaSjason * All advertising materials mentioning features or use of this software
13839f47eaSjason * must display the following acknowledgement:
14839f47eaSjason * This product includes software developed by the University of
15839f47eaSjason * California, Lawrence Berkeley Laboratory.
16839f47eaSjason *
17839f47eaSjason * Redistribution and use in source and binary forms, with or without
18839f47eaSjason * modification, are permitted provided that the following conditions
19839f47eaSjason * are met:
20839f47eaSjason * 1. Redistributions of source code must retain the above copyright
21839f47eaSjason * notice, this list of conditions and the following disclaimer.
22839f47eaSjason * 2. Redistributions in binary form must reproduce the above copyright
23839f47eaSjason * notice, this list of conditions and the following disclaimer in the
24839f47eaSjason * documentation and/or other materials provided with the distribution.
2529295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
26839f47eaSjason * may be used to endorse or promote products derived from this software
27839f47eaSjason * without specific prior written permission.
28839f47eaSjason *
29839f47eaSjason * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30839f47eaSjason * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31839f47eaSjason * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32839f47eaSjason * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33839f47eaSjason * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34839f47eaSjason * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35839f47eaSjason * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36839f47eaSjason * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37839f47eaSjason * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38839f47eaSjason * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39839f47eaSjason * SUCH DAMAGE.
40839f47eaSjason *
41839f47eaSjason * @(#)fpu_mul.c 8.1 (Berkeley) 6/11/93
42839f47eaSjason */
43839f47eaSjason
44839f47eaSjason /*
45839f47eaSjason * Perform an FPU multiply (return x * y).
46839f47eaSjason */
47839f47eaSjason
48839f47eaSjason #include <sys/types.h>
49839f47eaSjason
50839f47eaSjason #include <machine/reg.h>
51839f47eaSjason
52839f47eaSjason #include <sparc64/fpu/fpu_arith.h>
53839f47eaSjason #include <sparc64/fpu/fpu_emu.h>
54839f47eaSjason
55839f47eaSjason /*
56839f47eaSjason * The multiplication algorithm for normal numbers is as follows:
57839f47eaSjason *
58839f47eaSjason * The fraction of the product is built in the usual stepwise fashion.
59839f47eaSjason * Each step consists of shifting the accumulator right one bit
60839f47eaSjason * (maintaining any guard bits) and, if the next bit in y is set,
61839f47eaSjason * adding the multiplicand (x) to the accumulator. Then, in any case,
62839f47eaSjason * we advance one bit leftward in y. Algorithmically:
63839f47eaSjason *
64839f47eaSjason * A = 0;
65839f47eaSjason * for (bit = 0; bit < FP_NMANT; bit++) {
66839f47eaSjason * sticky |= A & 1, A >>= 1;
67839f47eaSjason * if (Y & (1 << bit))
68839f47eaSjason * A += X;
69839f47eaSjason * }
70839f47eaSjason *
71839f47eaSjason * (X and Y here represent the mantissas of x and y respectively.)
72839f47eaSjason * The resultant accumulator (A) is the product's mantissa. It may
73839f47eaSjason * be as large as 11.11111... in binary and hence may need to be
74839f47eaSjason * shifted right, but at most one bit.
75839f47eaSjason *
76839f47eaSjason * Since we do not have efficient multiword arithmetic, we code the
77839f47eaSjason * accumulator as four separate words, just like any other mantissa.
78839f47eaSjason *
79839f47eaSjason * In the algorithm above, the bits in y are inspected one at a time.
80839f47eaSjason * We will pick them up 32 at a time and then deal with those 32, one
81839f47eaSjason * at a time. Note, however, that we know several things about y:
82839f47eaSjason *
83839f47eaSjason * - the guard and round bits at the bottom are sure to be zero;
84839f47eaSjason *
85839f47eaSjason * - often many low bits are zero (y is often from a single or double
86839f47eaSjason * precision source);
87839f47eaSjason *
88839f47eaSjason * - bit FP_NMANT-1 is set, and FP_1*2 fits in a word.
89839f47eaSjason *
90839f47eaSjason * We can also test for 32-zero-bits swiftly. In this case, the center
91839f47eaSjason * part of the loop---setting sticky, shifting A, and not adding---will
92839f47eaSjason * run 32 times without adding X to A. We can do a 32-bit shift faster
93839f47eaSjason * by simply moving words. Since zeros are common, we optimize this case.
94839f47eaSjason * Furthermore, since A is initially zero, we can omit the shift as well
95839f47eaSjason * until we reach a nonzero word.
96839f47eaSjason */
97839f47eaSjason struct fpn *
fpu_mul(struct fpemu * fe)98*2c7a42e9Smiod fpu_mul(struct fpemu *fe)
99839f47eaSjason {
100*2c7a42e9Smiod struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
101*2c7a42e9Smiod u_int a3, a2, a1, a0, x3, x2, x1, x0, bit, m;
102*2c7a42e9Smiod int sticky;
103839f47eaSjason FPU_DECL_CARRY
104839f47eaSjason
105839f47eaSjason /*
106839f47eaSjason * Put the `heavier' operand on the right (see fpu_emu.h).
107839f47eaSjason * Then we will have one of the following cases, taken in the
108839f47eaSjason * following order:
109839f47eaSjason *
110839f47eaSjason * - y = NaN. Implied: if only one is a signalling NaN, y is.
111839f47eaSjason * The result is y.
112839f47eaSjason * - y = Inf. Implied: x != NaN (is 0, number, or Inf: the NaN
113839f47eaSjason * case was taken care of earlier).
114839f47eaSjason * If x = 0, the result is NaN. Otherwise the result
115839f47eaSjason * is y, with its sign reversed if x is negative.
116839f47eaSjason * - x = 0. Implied: y is 0 or number.
117839f47eaSjason * The result is 0 (with XORed sign as usual).
118839f47eaSjason * - other. Implied: both x and y are numbers.
119839f47eaSjason * The result is x * y (XOR sign, multiply bits, add exponents).
120839f47eaSjason */
121839f47eaSjason ORDER(x, y);
122839f47eaSjason if (ISNAN(y)) {
123839f47eaSjason y->fp_sign ^= x->fp_sign;
124839f47eaSjason return (y);
125839f47eaSjason }
126839f47eaSjason if (ISINF(y)) {
127839f47eaSjason if (ISZERO(x))
128839f47eaSjason return (fpu_newnan(fe));
129839f47eaSjason y->fp_sign ^= x->fp_sign;
130839f47eaSjason return (y);
131839f47eaSjason }
132839f47eaSjason if (ISZERO(x)) {
133839f47eaSjason x->fp_sign ^= y->fp_sign;
134839f47eaSjason return (x);
135839f47eaSjason }
136839f47eaSjason
137839f47eaSjason /*
138839f47eaSjason * Setup. In the code below, the mask `m' will hold the current
139839f47eaSjason * mantissa byte from y. The variable `bit' denotes the bit
140839f47eaSjason * within m. We also define some macros to deal with everything.
141839f47eaSjason */
142839f47eaSjason x3 = x->fp_mant[3];
143839f47eaSjason x2 = x->fp_mant[2];
144839f47eaSjason x1 = x->fp_mant[1];
145839f47eaSjason x0 = x->fp_mant[0];
146839f47eaSjason sticky = a3 = a2 = a1 = a0 = 0;
147839f47eaSjason
148839f47eaSjason #define ADD /* A += X */ \
149839f47eaSjason FPU_ADDS(a3, a3, x3); \
150839f47eaSjason FPU_ADDCS(a2, a2, x2); \
151839f47eaSjason FPU_ADDCS(a1, a1, x1); \
152839f47eaSjason FPU_ADDC(a0, a0, x0)
153839f47eaSjason
154839f47eaSjason #define SHR1 /* A >>= 1, with sticky */ \
155839f47eaSjason sticky |= a3 & 1, a3 = (a3 >> 1) | (a2 << 31), \
156839f47eaSjason a2 = (a2 >> 1) | (a1 << 31), a1 = (a1 >> 1) | (a0 << 31), a0 >>= 1
157839f47eaSjason
158839f47eaSjason #define SHR32 /* A >>= 32, with sticky */ \
159839f47eaSjason sticky |= a3, a3 = a2, a2 = a1, a1 = a0, a0 = 0
160839f47eaSjason
161839f47eaSjason #define STEP /* each 1-bit step of the multiplication */ \
162839f47eaSjason SHR1; if (bit & m) { ADD; }; bit <<= 1
163839f47eaSjason
164839f47eaSjason /*
165839f47eaSjason * We are ready to begin. The multiply loop runs once for each
166839f47eaSjason * of the four 32-bit words. Some words, however, are special.
167839f47eaSjason * As noted above, the low order bits of Y are often zero. Even
168839f47eaSjason * if not, the first loop can certainly skip the guard bits.
169839f47eaSjason * The last word of y has its highest 1-bit in position FP_NMANT-1,
170839f47eaSjason * so we stop the loop when we move past that bit.
171839f47eaSjason */
172839f47eaSjason if ((m = y->fp_mant[3]) == 0) {
173839f47eaSjason /* SHR32; */ /* unneeded since A==0 */
174839f47eaSjason } else {
175839f47eaSjason bit = 1 << FP_NG;
176839f47eaSjason do {
177839f47eaSjason STEP;
178839f47eaSjason } while (bit != 0);
179839f47eaSjason }
180839f47eaSjason if ((m = y->fp_mant[2]) == 0) {
181839f47eaSjason SHR32;
182839f47eaSjason } else {
183839f47eaSjason bit = 1;
184839f47eaSjason do {
185839f47eaSjason STEP;
186839f47eaSjason } while (bit != 0);
187839f47eaSjason }
188839f47eaSjason if ((m = y->fp_mant[1]) == 0) {
189839f47eaSjason SHR32;
190839f47eaSjason } else {
191839f47eaSjason bit = 1;
192839f47eaSjason do {
193839f47eaSjason STEP;
194839f47eaSjason } while (bit != 0);
195839f47eaSjason }
196839f47eaSjason m = y->fp_mant[0]; /* definitely != 0 */
197839f47eaSjason bit = 1;
198839f47eaSjason do {
199839f47eaSjason STEP;
200839f47eaSjason } while (bit <= m);
201839f47eaSjason
202839f47eaSjason /*
203839f47eaSjason * Done with mantissa calculation. Get exponent and handle
204839f47eaSjason * 11.111...1 case, then put result in place. We reuse x since
205839f47eaSjason * it already has the right class (FP_NUM).
206839f47eaSjason */
207839f47eaSjason m = x->fp_exp + y->fp_exp;
208839f47eaSjason if (a0 >= FP_2) {
209839f47eaSjason SHR1;
210839f47eaSjason m++;
211839f47eaSjason }
212839f47eaSjason x->fp_sign ^= y->fp_sign;
213839f47eaSjason x->fp_exp = m;
214839f47eaSjason x->fp_sticky = sticky;
215839f47eaSjason x->fp_mant[3] = a3;
216839f47eaSjason x->fp_mant[2] = a2;
217839f47eaSjason x->fp_mant[1] = a1;
218839f47eaSjason x->fp_mant[0] = a0;
219839f47eaSjason return (x);
220839f47eaSjason }
221