1200576Sroberto/*	$NetBSD: fpu_mul.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */
2181834Sroberto
3181834Sroberto/*
4200576Sroberto * SPDX-License-Identifier: BSD-3-Clause
5181834Sroberto *
6181834Sroberto * Copyright (c) 1992, 1993
7181834Sroberto *	The Regents of the University of California.  All rights reserved.
8181834Sroberto *
9181834Sroberto * This software was developed by the Computer Systems Engineering group
10181834Sroberto * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11181834Sroberto * contributed to Berkeley.
12181834Sroberto *
13181834Sroberto * All advertising materials mentioning features or use of this software
14181834Sroberto * must display the following acknowledgement:
15181834Sroberto *	This product includes software developed by the University of
16181834Sroberto *	California, Lawrence Berkeley Laboratory.
17181834Sroberto *
18181834Sroberto * Redistribution and use in source and binary forms, with or without
19181834Sroberto * modification, are permitted provided that the following conditions
20181834Sroberto * are met:
21181834Sroberto * 1. Redistributions of source code must retain the above copyright
22181834Sroberto *    notice, this list of conditions and the following disclaimer.
23181834Sroberto * 2. Redistributions in binary form must reproduce the above copyright
24181834Sroberto *    notice, this list of conditions and the following disclaimer in the
25181834Sroberto *    documentation and/or other materials provided with the distribution.
26181834Sroberto * 3. Neither the name of the University nor the names of its contributors
27181834Sroberto *    may be used to endorse or promote products derived from this software
28181834Sroberto *    without specific prior written permission.
29181834Sroberto *
30181834Sroberto * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31181834Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32181834Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33181834Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34181834Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35181834Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36181834Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37181834Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38181834Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39181834Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40181834Sroberto * SUCH DAMAGE.
41181834Sroberto */
42181834Sroberto
43181834Sroberto/*
44181834Sroberto * Perform an FPU multiply (return x * y).
45181834Sroberto */
46181834Sroberto
47181834Sroberto#include <sys/types.h>
48181834Sroberto#include <sys/systm.h>
49181834Sroberto
50181834Sroberto#include <machine/fpu.h>
51181834Sroberto
52181834Sroberto#include <powerpc/fpu/fpu_arith.h>
53181834Sroberto#include <powerpc/fpu/fpu_emu.h>
54181834Sroberto
55181834Sroberto/*
56181834Sroberto * The multiplication algorithm for normal numbers is as follows:
57181834Sroberto *
58181834Sroberto * The fraction of the product is built in the usual stepwise fashion.
59181834Sroberto * Each step consists of shifting the accumulator right one bit
60181834Sroberto * (maintaining any guard bits) and, if the next bit in y is set,
61181834Sroberto * adding the multiplicand (x) to the accumulator.  Then, in any case,
62181834Sroberto * we advance one bit leftward in y.  Algorithmically:
63181834Sroberto *
64181834Sroberto *	A = 0;
65181834Sroberto *	for (bit = 0; bit < FP_NMANT; bit++) {
66181834Sroberto *		sticky |= A & 1, A >>= 1;
67181834Sroberto *		if (Y & (1 << bit))
68181834Sroberto *			A += X;
69181834Sroberto *	}
70181834Sroberto *
71181834Sroberto * (X and Y here represent the mantissas of x and y respectively.)
72181834Sroberto * The resultant accumulator (A) is the product's mantissa.  It may
73181834Sroberto * be as large as 11.11111... in binary and hence may need to be
74181834Sroberto * shifted right, but at most one bit.
75181834Sroberto *
76181834Sroberto * Since we do not have efficient multiword arithmetic, we code the
77181834Sroberto * accumulator as four separate words, just like any other mantissa.
78181834Sroberto * We use local variables in the hope that this is faster than memory.
79181834Sroberto * We keep x->fp_mant in locals for the same reason.
80181834Sroberto *
81181834Sroberto * In the algorithm above, the bits in y are inspected one at a time.
82181834Sroberto * We will pick them up 32 at a time and then deal with those 32, one
83181834Sroberto * at a time.  Note, however, that we know several things about y:
84181834Sroberto *
85181834Sroberto *    - the guard and round bits at the bottom are sure to be zero;
86181834Sroberto *
87181834Sroberto *    - often many low bits are zero (y is often from a single or double
88181834Sroberto *	precision source);
89181834Sroberto *
90181834Sroberto *    - bit FP_NMANT-1 is set, and FP_1*2 fits in a word.
91181834Sroberto *
92181834Sroberto * We can also test for 32-zero-bits swiftly.  In this case, the center
93181834Sroberto * part of the loop---setting sticky, shifting A, and not adding---will
94181834Sroberto * run 32 times without adding X to A.  We can do a 32-bit shift faster
95181834Sroberto * by simply moving words.  Since zeros are common, we optimize this case.
96181834Sroberto * Furthermore, since A is initially zero, we can omit the shift as well
97181834Sroberto * until we reach a nonzero word.
98181834Sroberto */
99181834Srobertostruct fpn *
100181834Srobertofpu_mul(struct fpemu *fe)
101181834Sroberto{
102181834Sroberto	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
103181834Sroberto	u_int a3, a2, a1, a0, x3, x2, x1, x0, bit, m;
104181834Sroberto	int sticky;
105181834Sroberto	FPU_DECL_CARRY;
106181834Sroberto
107181834Sroberto	/*
108181834Sroberto	 * Put the `heavier' operand on the right (see fpu_emu.h).
109181834Sroberto	 * Then we will have one of the following cases, taken in the
110181834Sroberto	 * following order:
111181834Sroberto	 *
112181834Sroberto	 *  - y = NaN.  Implied: if only one is a signalling NaN, y is.
113181834Sroberto	 *	The result is y.
114181834Sroberto	 *  - y = Inf.  Implied: x != NaN (is 0, number, or Inf: the NaN
115181834Sroberto	 *    case was taken care of earlier).
116181834Sroberto	 *	If x = 0, the result is NaN.  Otherwise the result
117181834Sroberto	 *	is y, with its sign reversed if x is negative.
118181834Sroberto	 *  - x = 0.  Implied: y is 0 or number.
119181834Sroberto	 *	The result is 0 (with XORed sign as usual).
120181834Sroberto	 *  - other.  Implied: both x and y are numbers.
121181834Sroberto	 *	The result is x * y (XOR sign, multiply bits, add exponents).
122181834Sroberto	 */
123181834Sroberto	DPRINTF(FPE_REG, ("fpu_mul:\n"));
124181834Sroberto	DUMPFPN(FPE_REG, x);
125181834Sroberto	DUMPFPN(FPE_REG, y);
126181834Sroberto	DPRINTF(FPE_REG, ("=>\n"));
127181834Sroberto
128181834Sroberto	ORDER(x, y);
129181834Sroberto	if (ISNAN(y)) {
130181834Sroberto		y->fp_sign ^= x->fp_sign;
131181834Sroberto		fe->fe_cx |= FPSCR_VXSNAN;
132181834Sroberto		DUMPFPN(FPE_REG, y);
133181834Sroberto		return (y);
134181834Sroberto	}
135181834Sroberto	if (ISINF(y)) {
136181834Sroberto		if (ISZERO(x)) {
137181834Sroberto			fe->fe_cx |= FPSCR_VXIMZ;
138181834Sroberto			return (fpu_newnan(fe));
139181834Sroberto		}
140181834Sroberto		y->fp_sign ^= x->fp_sign;
141181834Sroberto			DUMPFPN(FPE_REG, y);
142181834Sroberto		return (y);
143181834Sroberto	}
144181834Sroberto	if (ISZERO(x)) {
145181834Sroberto		x->fp_sign ^= y->fp_sign;
146181834Sroberto		DUMPFPN(FPE_REG, x);
147181834Sroberto		return (x);
148181834Sroberto	}
149181834Sroberto
150181834Sroberto	/*
151181834Sroberto	 * Setup.  In the code below, the mask `m' will hold the current
152181834Sroberto	 * mantissa byte from y.  The variable `bit' denotes the bit
153181834Sroberto	 * within m.  We also define some macros to deal with everything.
154181834Sroberto	 */
155181834Sroberto	x3 = x->fp_mant[3];
156181834Sroberto	x2 = x->fp_mant[2];
157181834Sroberto	x1 = x->fp_mant[1];
158181834Sroberto	x0 = x->fp_mant[0];
159181834Sroberto	sticky = a3 = a2 = a1 = a0 = 0;
160181834Sroberto
161181834Sroberto#define	ADD	/* A += X */ \
162181834Sroberto	FPU_ADDS(a3, a3, x3); \
163181834Sroberto	FPU_ADDCS(a2, a2, x2); \
164181834Sroberto	FPU_ADDCS(a1, a1, x1); \
165181834Sroberto	FPU_ADDC(a0, a0, x0)
166181834Sroberto
167181834Sroberto#define	SHR1	/* A >>= 1, with sticky */ \
168181834Sroberto	sticky |= a3 & 1, a3 = (a3 >> 1) | (a2 << 31), \
169181834Sroberto	a2 = (a2 >> 1) | (a1 << 31), a1 = (a1 >> 1) | (a0 << 31), a0 >>= 1
170181834Sroberto
171181834Sroberto#define	SHR32	/* A >>= 32, with sticky */ \
172181834Sroberto	sticky |= a3, a3 = a2, a2 = a1, a1 = a0, a0 = 0
173181834Sroberto
174181834Sroberto#define	STEP	/* each 1-bit step of the multiplication */ \
175181834Sroberto	SHR1; if (bit & m) { ADD; }; bit <<= 1
176181834Sroberto
177181834Sroberto	/*
178181834Sroberto	 * We are ready to begin.  The multiply loop runs once for each
179181834Sroberto	 * of the four 32-bit words.  Some words, however, are special.
180181834Sroberto	 * As noted above, the low order bits of Y are often zero.  Even
181181834Sroberto	 * if not, the first loop can certainly skip the guard bits.
182181834Sroberto	 * The last word of y has its highest 1-bit in position FP_NMANT-1,
183181834Sroberto	 * so we stop the loop when we move past that bit.
184181834Sroberto	 */
185181834Sroberto	if ((m = y->fp_mant[3]) == 0) {
186181834Sroberto		/* SHR32; */			/* unneeded since A==0 */
187181834Sroberto	} else {
188181834Sroberto		bit = 1 << FP_NG;
189181834Sroberto		do {
190181834Sroberto			STEP;
191181834Sroberto		} while (bit != 0);
192181834Sroberto	}
193181834Sroberto	if ((m = y->fp_mant[2]) == 0) {
194181834Sroberto		SHR32;
195181834Sroberto	} else {
196181834Sroberto		bit = 1;
197181834Sroberto		do {
198181834Sroberto			STEP;
199181834Sroberto		} while (bit != 0);
200181834Sroberto	}
201181834Sroberto	if ((m = y->fp_mant[1]) == 0) {
202181834Sroberto		SHR32;
203181834Sroberto	} else {
204181834Sroberto		bit = 1;
205181834Sroberto		do {
206181834Sroberto			STEP;
207181834Sroberto		} while (bit != 0);
208181834Sroberto	}
209181834Sroberto	m = y->fp_mant[0];		/* definitely != 0 */
210181834Sroberto	bit = 1;
211181834Sroberto	do {
212181834Sroberto		STEP;
213181834Sroberto	} while (bit <= m);
214181834Sroberto
215181834Sroberto	/*
216181834Sroberto	 * Done with mantissa calculation.  Get exponent and handle
217181834Sroberto	 * 11.111...1 case, then put result in place.  We reuse x since
218181834Sroberto	 * it already has the right class (FP_NUM).
219181834Sroberto	 */
220181834Sroberto	m = x->fp_exp + y->fp_exp;
221181834Sroberto	if (a0 >= FP_2) {
222181834Sroberto		SHR1;
223181834Sroberto		m++;
224181834Sroberto	}
225181834Sroberto	x->fp_sign ^= y->fp_sign;
226181834Sroberto	x->fp_exp = m;
227181834Sroberto	x->fp_sticky = sticky;
228181834Sroberto	x->fp_mant[3] = a3;
229181834Sroberto	x->fp_mant[2] = a2;
230181834Sroberto	x->fp_mant[1] = a1;
231181834Sroberto	x->fp_mant[0] = a0;
232181834Sroberto
233181834Sroberto	DUMPFPN(FPE_REG, x);
234181834Sroberto	return (x);
235181834Sroberto}
236181834Sroberto