xref: /openbsd-src/lib/libc/arch/sparc64/fpu/fpu_mul.c (revision 6244ddcc7bcc151947d91dad33fcee9e7428fcc3)
1*6244ddccSmiod /*	$OpenBSD: fpu_mul.c,v 1.4 2024/03/29 21:02:11 miod Exp $	*/
202b90beaSjason 
302b90beaSjason /*
402b90beaSjason  * Copyright (c) 1992, 1993
502b90beaSjason  *	The Regents of the University of California.  All rights reserved.
602b90beaSjason  *
702b90beaSjason  * This software was developed by the Computer Systems Engineering group
802b90beaSjason  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
902b90beaSjason  * contributed to Berkeley.
1002b90beaSjason  *
1102b90beaSjason  * All advertising materials mentioning features or use of this software
1202b90beaSjason  * must display the following acknowledgement:
1302b90beaSjason  *	This product includes software developed by the University of
1402b90beaSjason  *	California, Lawrence Berkeley Laboratory.
1502b90beaSjason  *
1602b90beaSjason  * Redistribution and use in source and binary forms, with or without
1702b90beaSjason  * modification, are permitted provided that the following conditions
1802b90beaSjason  * are met:
1902b90beaSjason  * 1. Redistributions of source code must retain the above copyright
2002b90beaSjason  *    notice, this list of conditions and the following disclaimer.
2102b90beaSjason  * 2. Redistributions in binary form must reproduce the above copyright
2202b90beaSjason  *    notice, this list of conditions and the following disclaimer in the
2302b90beaSjason  *    documentation and/or other materials provided with the distribution.
2402b90beaSjason  * 3. All advertising materials mentioning features or use of this software
2502b90beaSjason  *    must display the following acknowledgement:
2602b90beaSjason  *	This product includes software developed by the University of
2702b90beaSjason  *	California, Berkeley and its contributors.
2802b90beaSjason  * 4. Neither the name of the University nor the names of its contributors
2902b90beaSjason  *    may be used to endorse or promote products derived from this software
3002b90beaSjason  *    without specific prior written permission.
3102b90beaSjason  *
3202b90beaSjason  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3302b90beaSjason  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3402b90beaSjason  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3502b90beaSjason  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3602b90beaSjason  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3702b90beaSjason  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3802b90beaSjason  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3902b90beaSjason  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4002b90beaSjason  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4102b90beaSjason  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4202b90beaSjason  * SUCH DAMAGE.
4302b90beaSjason  *
4402b90beaSjason  *	@(#)fpu_mul.c	8.1 (Berkeley) 6/11/93
4502b90beaSjason  *	$NetBSD: fpu_mul.c,v 1.2 1994/11/20 20:52:44 deraadt Exp $
4602b90beaSjason  */
4702b90beaSjason 
4802b90beaSjason /*
4902b90beaSjason  * Perform an FPU multiply (return x * y).
5002b90beaSjason  */
5102b90beaSjason 
5202b90beaSjason #include <sys/types.h>
5302b90beaSjason 
5402b90beaSjason #include "fpu_arith.h"
5502b90beaSjason #include "fpu_emu.h"
5602b90beaSjason #include "fpu_extern.h"
5702b90beaSjason 
5802b90beaSjason /*
5902b90beaSjason  * The multiplication algorithm for normal numbers is as follows:
6002b90beaSjason  *
6102b90beaSjason  * The fraction of the product is built in the usual stepwise fashion.
6202b90beaSjason  * Each step consists of shifting the accumulator right one bit
6302b90beaSjason  * (maintaining any guard bits) and, if the next bit in y is set,
6402b90beaSjason  * adding the multiplicand (x) to the accumulator.  Then, in any case,
6502b90beaSjason  * we advance one bit leftward in y.  Algorithmically:
6602b90beaSjason  *
6702b90beaSjason  *	A = 0;
6802b90beaSjason  *	for (bit = 0; bit < FP_NMANT; bit++) {
6902b90beaSjason  *		sticky |= A & 1, A >>= 1;
7002b90beaSjason  *		if (Y & (1 << bit))
7102b90beaSjason  *			A += X;
7202b90beaSjason  *	}
7302b90beaSjason  *
7402b90beaSjason  * (X and Y here represent the mantissas of x and y respectively.)
7502b90beaSjason  * The resultant accumulator (A) is the product's mantissa.  It may
7602b90beaSjason  * be as large as 11.11111... in binary and hence may need to be
7702b90beaSjason  * shifted right, but at most one bit.
7802b90beaSjason  *
7902b90beaSjason  * Since we do not have efficient multiword arithmetic, we code the
8002b90beaSjason  * accumulator as four separate words, just like any other mantissa.
8102b90beaSjason  * We use local `register' variables in the hope that this is faster
8202b90beaSjason  * than memory.  We keep x->fp_mant in locals for the same reason.
8302b90beaSjason  *
8402b90beaSjason  * In the algorithm above, the bits in y are inspected one at a time.
8502b90beaSjason  * We will pick them up 32 at a time and then deal with those 32, one
8602b90beaSjason  * at a time.  Note, however, that we know several things about y:
8702b90beaSjason  *
8802b90beaSjason  *    - the guard and round bits at the bottom are sure to be zero;
8902b90beaSjason  *
9002b90beaSjason  *    - often many low bits are zero (y is often from a single or double
9102b90beaSjason  *	precision source);
9202b90beaSjason  *
9302b90beaSjason  *    - bit FP_NMANT-1 is set, and FP_1*2 fits in a word.
9402b90beaSjason  *
9502b90beaSjason  * We can also test for 32-zero-bits swiftly.  In this case, the center
9602b90beaSjason  * part of the loop---setting sticky, shifting A, and not adding---will
9702b90beaSjason  * run 32 times without adding X to A.  We can do a 32-bit shift faster
9802b90beaSjason  * by simply moving words.  Since zeros are common, we optimize this case.
9902b90beaSjason  * Furthermore, since A is initially zero, we can omit the shift as well
10002b90beaSjason  * until we reach a nonzero word.
10102b90beaSjason  */
10202b90beaSjason struct fpn *
__fpu_mul(fe)10302b90beaSjason __fpu_mul(fe)
10402b90beaSjason 	struct fpemu *fe;
10502b90beaSjason {
10602b90beaSjason 	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
10702b90beaSjason 	u_int a3, a2, a1, a0, x3, x2, x1, x0, bit, m;
10802b90beaSjason 	int sticky;
10902b90beaSjason 	FPU_DECL_CARRY
11002b90beaSjason 
11102b90beaSjason 	/*
11202b90beaSjason 	 * Put the `heavier' operand on the right (see fpu_emu.h).
11302b90beaSjason 	 * Then we will have one of the following cases, taken in the
11402b90beaSjason 	 * following order:
11502b90beaSjason 	 *
11602b90beaSjason 	 *  - y = NaN.  Implied: if only one is a signalling NaN, y is.
11702b90beaSjason 	 *	The result is y.
11802b90beaSjason 	 *  - y = Inf.  Implied: x != NaN (is 0, number, or Inf: the NaN
11902b90beaSjason 	 *    case was taken care of earlier).
12002b90beaSjason 	 *	If x = 0, the result is NaN.  Otherwise the result
12102b90beaSjason 	 *	is y, with its sign reversed if x is negative.
12202b90beaSjason 	 *  - x = 0.  Implied: y is 0 or number.
12302b90beaSjason 	 *	The result is 0 (with XORed sign as usual).
12402b90beaSjason 	 *  - other.  Implied: both x and y are numbers.
12502b90beaSjason 	 *	The result is x * y (XOR sign, multiply bits, add exponents).
12602b90beaSjason 	 */
12702b90beaSjason 	ORDER(x, y);
12802b90beaSjason 	if (ISNAN(y)) {
12902b90beaSjason 		y->fp_sign ^= x->fp_sign;
13002b90beaSjason 		return (y);
13102b90beaSjason 	}
13202b90beaSjason 	if (ISINF(y)) {
13302b90beaSjason 		if (ISZERO(x))
13402b90beaSjason 			return (__fpu_newnan(fe));
13502b90beaSjason 		y->fp_sign ^= x->fp_sign;
13602b90beaSjason 		return (y);
13702b90beaSjason 	}
13802b90beaSjason 	if (ISZERO(x)) {
13902b90beaSjason 		x->fp_sign ^= y->fp_sign;
14002b90beaSjason 		return (x);
14102b90beaSjason 	}
14202b90beaSjason 
14302b90beaSjason 	/*
14402b90beaSjason 	 * Setup.  In the code below, the mask `m' will hold the current
14502b90beaSjason 	 * mantissa byte from y.  The variable `bit' denotes the bit
14602b90beaSjason 	 * within m.  We also define some macros to deal with everything.
14702b90beaSjason 	 */
14802b90beaSjason 	x3 = x->fp_mant[3];
14902b90beaSjason 	x2 = x->fp_mant[2];
15002b90beaSjason 	x1 = x->fp_mant[1];
15102b90beaSjason 	x0 = x->fp_mant[0];
15202b90beaSjason 	sticky = a3 = a2 = a1 = a0 = 0;
15302b90beaSjason 
15402b90beaSjason #define	ADD	/* A += X */ \
15502b90beaSjason 	FPU_ADDS(a3, a3, x3); \
15602b90beaSjason 	FPU_ADDCS(a2, a2, x2); \
15702b90beaSjason 	FPU_ADDCS(a1, a1, x1); \
15802b90beaSjason 	FPU_ADDC(a0, a0, x0)
15902b90beaSjason 
16002b90beaSjason #define	SHR1	/* A >>= 1, with sticky */ \
16102b90beaSjason 	sticky |= a3 & 1, a3 = (a3 >> 1) | (a2 << 31), \
16202b90beaSjason 	a2 = (a2 >> 1) | (a1 << 31), a1 = (a1 >> 1) | (a0 << 31), a0 >>= 1
16302b90beaSjason 
16402b90beaSjason #define	SHR32	/* A >>= 32, with sticky */ \
16502b90beaSjason 	sticky |= a3, a3 = a2, a2 = a1, a1 = a0, a0 = 0
16602b90beaSjason 
16702b90beaSjason #define	STEP	/* each 1-bit step of the multiplication */ \
16802b90beaSjason 	SHR1; if (bit & m) { ADD; }; bit <<= 1
16902b90beaSjason 
17002b90beaSjason 	/*
17102b90beaSjason 	 * We are ready to begin.  The multiply loop runs once for each
17202b90beaSjason 	 * of the four 32-bit words.  Some words, however, are special.
17302b90beaSjason 	 * As noted above, the low order bits of Y are often zero.  Even
17402b90beaSjason 	 * if not, the first loop can certainly skip the guard bits.
17502b90beaSjason 	 * The last word of y has its highest 1-bit in position FP_NMANT-1,
17602b90beaSjason 	 * so we stop the loop when we move past that bit.
17702b90beaSjason 	 */
17802b90beaSjason 	if ((m = y->fp_mant[3]) == 0) {
17902b90beaSjason 		/* SHR32; */			/* unneeded since A==0 */
18002b90beaSjason 	} else {
18102b90beaSjason 		bit = 1 << FP_NG;
18202b90beaSjason 		do {
18302b90beaSjason 			STEP;
18402b90beaSjason 		} while (bit != 0);
18502b90beaSjason 	}
18602b90beaSjason 	if ((m = y->fp_mant[2]) == 0) {
18702b90beaSjason 		SHR32;
18802b90beaSjason 	} else {
18902b90beaSjason 		bit = 1;
19002b90beaSjason 		do {
19102b90beaSjason 			STEP;
19202b90beaSjason 		} while (bit != 0);
19302b90beaSjason 	}
19402b90beaSjason 	if ((m = y->fp_mant[1]) == 0) {
19502b90beaSjason 		SHR32;
19602b90beaSjason 	} else {
19702b90beaSjason 		bit = 1;
19802b90beaSjason 		do {
19902b90beaSjason 			STEP;
20002b90beaSjason 		} while (bit != 0);
20102b90beaSjason 	}
20202b90beaSjason 	m = y->fp_mant[0];		/* definitely != 0 */
20302b90beaSjason 	bit = 1;
20402b90beaSjason 	do {
20502b90beaSjason 		STEP;
20602b90beaSjason 	} while (bit <= m);
20702b90beaSjason 
20802b90beaSjason 	/*
20902b90beaSjason 	 * Done with mantissa calculation.  Get exponent and handle
21002b90beaSjason 	 * 11.111...1 case, then put result in place.  We reuse x since
21102b90beaSjason 	 * it already has the right class (FP_NUM).
21202b90beaSjason 	 */
21302b90beaSjason 	m = x->fp_exp + y->fp_exp;
21402b90beaSjason 	if (a0 >= FP_2) {
21502b90beaSjason 		SHR1;
21602b90beaSjason 		m++;
21702b90beaSjason 	}
21802b90beaSjason 	x->fp_sign ^= y->fp_sign;
21902b90beaSjason 	x->fp_exp = m;
22002b90beaSjason 	x->fp_sticky = sticky;
22102b90beaSjason 	x->fp_mant[3] = a3;
22202b90beaSjason 	x->fp_mant[2] = a2;
22302b90beaSjason 	x->fp_mant[1] = a1;
22402b90beaSjason 	x->fp_mant[0] = a0;
22502b90beaSjason 	return (x);
22602b90beaSjason }
227