xref: /freebsd/lib/libc/quad/muldi3.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This software was developed by the Computer Systems Engineering group
858f0484fSRodney W. Grimes  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
958f0484fSRodney W. Grimes  * contributed to Berkeley.
1058f0484fSRodney W. Grimes  *
1158f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1258f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1358f0484fSRodney W. Grimes  * are met:
1458f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1658f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1758f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1858f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2058f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2158f0484fSRodney W. Grimes  *    without specific prior written permission.
2258f0484fSRodney W. Grimes  *
2358f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2458f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2758f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes  * SUCH DAMAGE.
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3758f0484fSRodney W. Grimes static char sccsid[] = "@(#)muldi3.c	8.1 (Berkeley) 6/4/93";
3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
398fb3f3f6SDavid E. O'Brien #include <sys/cdefs.h>
408fb3f3f6SDavid E. O'Brien __FBSDID("$FreeBSD$");
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include "quad.h"
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes /*
4558f0484fSRodney W. Grimes  * Multiply two quads.
4658f0484fSRodney W. Grimes  *
4758f0484fSRodney W. Grimes  * Our algorithm is based on the following.  Split incoming quad values
4858f0484fSRodney W. Grimes  * u and v (where u,v >= 0) into
4958f0484fSRodney W. Grimes  *
5058f0484fSRodney W. Grimes  *	u = 2^n u1  *  u0	(n = number of bits in `u_long', usu. 32)
5158f0484fSRodney W. Grimes  *
5258f0484fSRodney W. Grimes  * and
5358f0484fSRodney W. Grimes  *
5458f0484fSRodney W. Grimes  *	v = 2^n v1  *  v0
5558f0484fSRodney W. Grimes  *
5658f0484fSRodney W. Grimes  * Then
5758f0484fSRodney W. Grimes  *
5858f0484fSRodney W. Grimes  *	uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
5958f0484fSRodney W. Grimes  *	   = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
6058f0484fSRodney W. Grimes  *
6158f0484fSRodney W. Grimes  * Now add 2^n u1 v1 to the first term and subtract it from the middle,
6258f0484fSRodney W. Grimes  * and add 2^n u0 v0 to the last term and subtract it from the middle.
6358f0484fSRodney W. Grimes  * This gives:
6458f0484fSRodney W. Grimes  *
6558f0484fSRodney W. Grimes  *	uv = (2^2n + 2^n) (u1 v1)  +
6658f0484fSRodney W. Grimes  *	         (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
6758f0484fSRodney W. Grimes  *	       (2^n + 1)  (u0 v0)
6858f0484fSRodney W. Grimes  *
6958f0484fSRodney W. Grimes  * Factoring the middle a bit gives us:
7058f0484fSRodney W. Grimes  *
7158f0484fSRodney W. Grimes  *	uv = (2^2n + 2^n) (u1 v1)  +			[u1v1 = high]
7258f0484fSRodney W. Grimes  *		 (2^n)    (u1 - u0) (v0 - v1)  +	[(u1-u0)... = mid]
7358f0484fSRodney W. Grimes  *	       (2^n + 1)  (u0 v0)			[u0v0 = low]
7458f0484fSRodney W. Grimes  *
7558f0484fSRodney W. Grimes  * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
7658f0484fSRodney W. Grimes  * in just half the precision of the original.  (Note that either or both
7758f0484fSRodney W. Grimes  * of (u1 - u0) or (v0 - v1) may be negative.)
7858f0484fSRodney W. Grimes  *
7958f0484fSRodney W. Grimes  * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
8058f0484fSRodney W. Grimes  *
8158f0484fSRodney W. Grimes  * Since C does not give us a `long * long = quad' operator, we split
8258f0484fSRodney W. Grimes  * our input quads into two longs, then split the two longs into two
8358f0484fSRodney W. Grimes  * shorts.  We can then calculate `short * short = long' in native
8458f0484fSRodney W. Grimes  * arithmetic.
8558f0484fSRodney W. Grimes  *
8658f0484fSRodney W. Grimes  * Our product should, strictly speaking, be a `long quad', with 128
8758f0484fSRodney W. Grimes  * bits, but we are going to discard the upper 64.  In other words,
8858f0484fSRodney W. Grimes  * we are not interested in uv, but rather in (uv mod 2^2n).  This
8958f0484fSRodney W. Grimes  * makes some of the terms above vanish, and we get:
9058f0484fSRodney W. Grimes  *
9158f0484fSRodney W. Grimes  *	(2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
9258f0484fSRodney W. Grimes  *
9358f0484fSRodney W. Grimes  * or
9458f0484fSRodney W. Grimes  *
9558f0484fSRodney W. Grimes  *	(2^n)(high + mid + low) + low
9658f0484fSRodney W. Grimes  *
9758f0484fSRodney W. Grimes  * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
9858f0484fSRodney W. Grimes  * of 2^n in either one will also vanish.  Only `low' need be computed
9958f0484fSRodney W. Grimes  * mod 2^2n, and only because of the final term above.
10058f0484fSRodney W. Grimes  */
10158f0484fSRodney W. Grimes static quad_t __lmulq(u_long, u_long);
10258f0484fSRodney W. Grimes 
10358f0484fSRodney W. Grimes quad_t
10458f0484fSRodney W. Grimes __muldi3(a, b)
10558f0484fSRodney W. Grimes 	quad_t a, b;
10658f0484fSRodney W. Grimes {
10758f0484fSRodney W. Grimes 	union uu u, v, low, prod;
1088fb3f3f6SDavid E. O'Brien 	u_long high, mid, udiff, vdiff;
1098fb3f3f6SDavid E. O'Brien 	int negall, negmid;
11058f0484fSRodney W. Grimes #define	u1	u.ul[H]
11158f0484fSRodney W. Grimes #define	u0	u.ul[L]
11258f0484fSRodney W. Grimes #define	v1	v.ul[H]
11358f0484fSRodney W. Grimes #define	v0	v.ul[L]
11458f0484fSRodney W. Grimes 
11558f0484fSRodney W. Grimes 	/*
11658f0484fSRodney W. Grimes 	 * Get u and v such that u, v >= 0.  When this is finished,
11758f0484fSRodney W. Grimes 	 * u1, u0, v1, and v0 will be directly accessible through the
11858f0484fSRodney W. Grimes 	 * longword fields.
11958f0484fSRodney W. Grimes 	 */
12058f0484fSRodney W. Grimes 	if (a >= 0)
12158f0484fSRodney W. Grimes 		u.q = a, negall = 0;
12258f0484fSRodney W. Grimes 	else
12358f0484fSRodney W. Grimes 		u.q = -a, negall = 1;
12458f0484fSRodney W. Grimes 	if (b >= 0)
12558f0484fSRodney W. Grimes 		v.q = b;
12658f0484fSRodney W. Grimes 	else
12758f0484fSRodney W. Grimes 		v.q = -b, negall ^= 1;
12858f0484fSRodney W. Grimes 
12958f0484fSRodney W. Grimes 	if (u1 == 0 && v1 == 0) {
13058f0484fSRodney W. Grimes 		/*
13158f0484fSRodney W. Grimes 		 * An (I hope) important optimization occurs when u1 and v1
13258f0484fSRodney W. Grimes 		 * are both 0.  This should be common since most numbers
13358f0484fSRodney W. Grimes 		 * are small.  Here the product is just u0*v0.
13458f0484fSRodney W. Grimes 		 */
13558f0484fSRodney W. Grimes 		prod.q = __lmulq(u0, v0);
13658f0484fSRodney W. Grimes 	} else {
13758f0484fSRodney W. Grimes 		/*
13858f0484fSRodney W. Grimes 		 * Compute the three intermediate products, remembering
13958f0484fSRodney W. Grimes 		 * whether the middle term is negative.  We can discard
14058f0484fSRodney W. Grimes 		 * any upper bits in high and mid, so we can use native
14158f0484fSRodney W. Grimes 		 * u_long * u_long => u_long arithmetic.
14258f0484fSRodney W. Grimes 		 */
14358f0484fSRodney W. Grimes 		low.q = __lmulq(u0, v0);
14458f0484fSRodney W. Grimes 
14558f0484fSRodney W. Grimes 		if (u1 >= u0)
14658f0484fSRodney W. Grimes 			negmid = 0, udiff = u1 - u0;
14758f0484fSRodney W. Grimes 		else
14858f0484fSRodney W. Grimes 			negmid = 1, udiff = u0 - u1;
14958f0484fSRodney W. Grimes 		if (v0 >= v1)
15058f0484fSRodney W. Grimes 			vdiff = v0 - v1;
15158f0484fSRodney W. Grimes 		else
15258f0484fSRodney W. Grimes 			vdiff = v1 - v0, negmid ^= 1;
15358f0484fSRodney W. Grimes 		mid = udiff * vdiff;
15458f0484fSRodney W. Grimes 
15558f0484fSRodney W. Grimes 		high = u1 * v1;
15658f0484fSRodney W. Grimes 
15758f0484fSRodney W. Grimes 		/*
15858f0484fSRodney W. Grimes 		 * Assemble the final product.
15958f0484fSRodney W. Grimes 		 */
16058f0484fSRodney W. Grimes 		prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
16158f0484fSRodney W. Grimes 		    low.ul[H];
16258f0484fSRodney W. Grimes 		prod.ul[L] = low.ul[L];
16358f0484fSRodney W. Grimes 	}
16458f0484fSRodney W. Grimes 	return (negall ? -prod.q : prod.q);
16558f0484fSRodney W. Grimes #undef u1
16658f0484fSRodney W. Grimes #undef u0
16758f0484fSRodney W. Grimes #undef v1
16858f0484fSRodney W. Grimes #undef v0
16958f0484fSRodney W. Grimes }
17058f0484fSRodney W. Grimes 
17158f0484fSRodney W. Grimes /*
17258f0484fSRodney W. Grimes  * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
17358f0484fSRodney W. Grimes  * the number of bits in a long (whatever that is---the code below
17458f0484fSRodney W. Grimes  * does not care as long as quad.h does its part of the bargain---but
17558f0484fSRodney W. Grimes  * typically N==16).
17658f0484fSRodney W. Grimes  *
17758f0484fSRodney W. Grimes  * We use the same algorithm from Knuth, but this time the modulo refinement
17858f0484fSRodney W. Grimes  * does not apply.  On the other hand, since N is half the size of a long,
17958f0484fSRodney W. Grimes  * we can get away with native multiplication---none of our input terms
18058f0484fSRodney W. Grimes  * exceeds (ULONG_MAX >> 1).
18158f0484fSRodney W. Grimes  *
18258f0484fSRodney W. Grimes  * Note that, for u_long l, the quad-precision result
18358f0484fSRodney W. Grimes  *
18458f0484fSRodney W. Grimes  *	l << N
18558f0484fSRodney W. Grimes  *
18658f0484fSRodney W. Grimes  * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
18758f0484fSRodney W. Grimes  */
18858f0484fSRodney W. Grimes static quad_t
18958f0484fSRodney W. Grimes __lmulq(u_long u, u_long v)
19058f0484fSRodney W. Grimes {
19158f0484fSRodney W. Grimes 	u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low;
19258f0484fSRodney W. Grimes 	u_long prodh, prodl, was;
19358f0484fSRodney W. Grimes 	union uu prod;
19458f0484fSRodney W. Grimes 	int neg;
19558f0484fSRodney W. Grimes 
19658f0484fSRodney W. Grimes 	u1 = HHALF(u);
19758f0484fSRodney W. Grimes 	u0 = LHALF(u);
19858f0484fSRodney W. Grimes 	v1 = HHALF(v);
19958f0484fSRodney W. Grimes 	v0 = LHALF(v);
20058f0484fSRodney W. Grimes 
20158f0484fSRodney W. Grimes 	low = u0 * v0;
20258f0484fSRodney W. Grimes 
20358f0484fSRodney W. Grimes 	/* This is the same small-number optimization as before. */
20458f0484fSRodney W. Grimes 	if (u1 == 0 && v1 == 0)
20558f0484fSRodney W. Grimes 		return (low);
20658f0484fSRodney W. Grimes 
20758f0484fSRodney W. Grimes 	if (u1 >= u0)
20858f0484fSRodney W. Grimes 		udiff = u1 - u0, neg = 0;
20958f0484fSRodney W. Grimes 	else
21058f0484fSRodney W. Grimes 		udiff = u0 - u1, neg = 1;
21158f0484fSRodney W. Grimes 	if (v0 >= v1)
21258f0484fSRodney W. Grimes 		vdiff = v0 - v1;
21358f0484fSRodney W. Grimes 	else
21458f0484fSRodney W. Grimes 		vdiff = v1 - v0, neg ^= 1;
21558f0484fSRodney W. Grimes 	mid = udiff * vdiff;
21658f0484fSRodney W. Grimes 
21758f0484fSRodney W. Grimes 	high = u1 * v1;
21858f0484fSRodney W. Grimes 
21958f0484fSRodney W. Grimes 	/* prod = (high << 2N) + (high << N); */
22058f0484fSRodney W. Grimes 	prodh = high + HHALF(high);
22158f0484fSRodney W. Grimes 	prodl = LHUP(high);
22258f0484fSRodney W. Grimes 
22358f0484fSRodney W. Grimes 	/* if (neg) prod -= mid << N; else prod += mid << N; */
22458f0484fSRodney W. Grimes 	if (neg) {
22558f0484fSRodney W. Grimes 		was = prodl;
22658f0484fSRodney W. Grimes 		prodl -= LHUP(mid);
22758f0484fSRodney W. Grimes 		prodh -= HHALF(mid) + (prodl > was);
22858f0484fSRodney W. Grimes 	} else {
22958f0484fSRodney W. Grimes 		was = prodl;
23058f0484fSRodney W. Grimes 		prodl += LHUP(mid);
23158f0484fSRodney W. Grimes 		prodh += HHALF(mid) + (prodl < was);
23258f0484fSRodney W. Grimes 	}
23358f0484fSRodney W. Grimes 
23458f0484fSRodney W. Grimes 	/* prod += low << N */
23558f0484fSRodney W. Grimes 	was = prodl;
23658f0484fSRodney W. Grimes 	prodl += LHUP(low);
23758f0484fSRodney W. Grimes 	prodh += HHALF(low) + (prodl < was);
23858f0484fSRodney W. Grimes 	/* ... + low; */
23958f0484fSRodney W. Grimes 	if ((prodl += low) < low)
24058f0484fSRodney W. Grimes 		prodh++;
24158f0484fSRodney W. Grimes 
24258f0484fSRodney W. Grimes 	/* return 4N-bit product */
24358f0484fSRodney W. Grimes 	prod.ul[H] = prodh;
24458f0484fSRodney W. Grimes 	prod.ul[L] = prodl;
24558f0484fSRodney W. Grimes 	return (prod.q);
24658f0484fSRodney W. Grimes }
247