xref: /freebsd/lib/libc/quad/muldi3.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro 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 #include "quad.h"
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes /*
3958f0484fSRodney W. Grimes  * Multiply two quads.
4058f0484fSRodney W. Grimes  *
4158f0484fSRodney W. Grimes  * Our algorithm is based on the following.  Split incoming quad values
4258f0484fSRodney W. Grimes  * u and v (where u,v >= 0) into
4358f0484fSRodney W. Grimes  *
4458f0484fSRodney W. Grimes  *	u = 2^n u1  *  u0	(n = number of bits in `u_long', usu. 32)
4558f0484fSRodney W. Grimes  *
4658f0484fSRodney W. Grimes  * and
4758f0484fSRodney W. Grimes  *
4858f0484fSRodney W. Grimes  *	v = 2^n v1  *  v0
4958f0484fSRodney W. Grimes  *
5058f0484fSRodney W. Grimes  * Then
5158f0484fSRodney W. Grimes  *
5258f0484fSRodney W. Grimes  *	uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
5358f0484fSRodney W. Grimes  *	   = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
5458f0484fSRodney W. Grimes  *
5558f0484fSRodney W. Grimes  * Now add 2^n u1 v1 to the first term and subtract it from the middle,
5658f0484fSRodney W. Grimes  * and add 2^n u0 v0 to the last term and subtract it from the middle.
5758f0484fSRodney W. Grimes  * This gives:
5858f0484fSRodney W. Grimes  *
5958f0484fSRodney W. Grimes  *	uv = (2^2n + 2^n) (u1 v1)  +
6058f0484fSRodney W. Grimes  *	         (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
6158f0484fSRodney W. Grimes  *	       (2^n + 1)  (u0 v0)
6258f0484fSRodney W. Grimes  *
6358f0484fSRodney W. Grimes  * Factoring the middle a bit gives us:
6458f0484fSRodney W. Grimes  *
6558f0484fSRodney W. Grimes  *	uv = (2^2n + 2^n) (u1 v1)  +			[u1v1 = high]
6658f0484fSRodney W. Grimes  *		 (2^n)    (u1 - u0) (v0 - v1)  +	[(u1-u0)... = mid]
6758f0484fSRodney W. Grimes  *	       (2^n + 1)  (u0 v0)			[u0v0 = low]
6858f0484fSRodney W. Grimes  *
6958f0484fSRodney W. Grimes  * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
7058f0484fSRodney W. Grimes  * in just half the precision of the original.  (Note that either or both
7158f0484fSRodney W. Grimes  * of (u1 - u0) or (v0 - v1) may be negative.)
7258f0484fSRodney W. Grimes  *
7358f0484fSRodney W. Grimes  * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
7458f0484fSRodney W. Grimes  *
7558f0484fSRodney W. Grimes  * Since C does not give us a `long * long = quad' operator, we split
7658f0484fSRodney W. Grimes  * our input quads into two longs, then split the two longs into two
7758f0484fSRodney W. Grimes  * shorts.  We can then calculate `short * short = long' in native
7858f0484fSRodney W. Grimes  * arithmetic.
7958f0484fSRodney W. Grimes  *
8058f0484fSRodney W. Grimes  * Our product should, strictly speaking, be a `long quad', with 128
8158f0484fSRodney W. Grimes  * bits, but we are going to discard the upper 64.  In other words,
8258f0484fSRodney W. Grimes  * we are not interested in uv, but rather in (uv mod 2^2n).  This
8358f0484fSRodney W. Grimes  * makes some of the terms above vanish, and we get:
8458f0484fSRodney W. Grimes  *
8558f0484fSRodney W. Grimes  *	(2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
8658f0484fSRodney W. Grimes  *
8758f0484fSRodney W. Grimes  * or
8858f0484fSRodney W. Grimes  *
8958f0484fSRodney W. Grimes  *	(2^n)(high + mid + low) + low
9058f0484fSRodney W. Grimes  *
9158f0484fSRodney W. Grimes  * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
9258f0484fSRodney W. Grimes  * of 2^n in either one will also vanish.  Only `low' need be computed
9358f0484fSRodney W. Grimes  * mod 2^2n, and only because of the final term above.
9458f0484fSRodney W. Grimes  */
9558f0484fSRodney W. Grimes static quad_t __lmulq(u_long, u_long);
9658f0484fSRodney W. Grimes 
9758f0484fSRodney W. Grimes quad_t
__muldi3(quad_t a,quad_t b)98*6492be46SEd Maste __muldi3(quad_t a, quad_t b)
9958f0484fSRodney W. Grimes {
10058f0484fSRodney W. Grimes 	union uu u, v, low, prod;
1018fb3f3f6SDavid E. O'Brien 	u_long high, mid, udiff, vdiff;
1028fb3f3f6SDavid E. O'Brien 	int negall, negmid;
10358f0484fSRodney W. Grimes #define	u1	u.ul[H]
10458f0484fSRodney W. Grimes #define	u0	u.ul[L]
10558f0484fSRodney W. Grimes #define	v1	v.ul[H]
10658f0484fSRodney W. Grimes #define	v0	v.ul[L]
10758f0484fSRodney W. Grimes 
10858f0484fSRodney W. Grimes 	/*
10958f0484fSRodney W. Grimes 	 * Get u and v such that u, v >= 0.  When this is finished,
11058f0484fSRodney W. Grimes 	 * u1, u0, v1, and v0 will be directly accessible through the
11158f0484fSRodney W. Grimes 	 * longword fields.
11258f0484fSRodney W. Grimes 	 */
11358f0484fSRodney W. Grimes 	if (a >= 0)
11458f0484fSRodney W. Grimes 		u.q = a, negall = 0;
11558f0484fSRodney W. Grimes 	else
11658f0484fSRodney W. Grimes 		u.q = -a, negall = 1;
11758f0484fSRodney W. Grimes 	if (b >= 0)
11858f0484fSRodney W. Grimes 		v.q = b;
11958f0484fSRodney W. Grimes 	else
12058f0484fSRodney W. Grimes 		v.q = -b, negall ^= 1;
12158f0484fSRodney W. Grimes 
12258f0484fSRodney W. Grimes 	if (u1 == 0 && v1 == 0) {
12358f0484fSRodney W. Grimes 		/*
12458f0484fSRodney W. Grimes 		 * An (I hope) important optimization occurs when u1 and v1
12558f0484fSRodney W. Grimes 		 * are both 0.  This should be common since most numbers
12658f0484fSRodney W. Grimes 		 * are small.  Here the product is just u0*v0.
12758f0484fSRodney W. Grimes 		 */
12858f0484fSRodney W. Grimes 		prod.q = __lmulq(u0, v0);
12958f0484fSRodney W. Grimes 	} else {
13058f0484fSRodney W. Grimes 		/*
13158f0484fSRodney W. Grimes 		 * Compute the three intermediate products, remembering
13258f0484fSRodney W. Grimes 		 * whether the middle term is negative.  We can discard
13358f0484fSRodney W. Grimes 		 * any upper bits in high and mid, so we can use native
13458f0484fSRodney W. Grimes 		 * u_long * u_long => u_long arithmetic.
13558f0484fSRodney W. Grimes 		 */
13658f0484fSRodney W. Grimes 		low.q = __lmulq(u0, v0);
13758f0484fSRodney W. Grimes 
13858f0484fSRodney W. Grimes 		if (u1 >= u0)
13958f0484fSRodney W. Grimes 			negmid = 0, udiff = u1 - u0;
14058f0484fSRodney W. Grimes 		else
14158f0484fSRodney W. Grimes 			negmid = 1, udiff = u0 - u1;
14258f0484fSRodney W. Grimes 		if (v0 >= v1)
14358f0484fSRodney W. Grimes 			vdiff = v0 - v1;
14458f0484fSRodney W. Grimes 		else
14558f0484fSRodney W. Grimes 			vdiff = v1 - v0, negmid ^= 1;
14658f0484fSRodney W. Grimes 		mid = udiff * vdiff;
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes 		high = u1 * v1;
14958f0484fSRodney W. Grimes 
15058f0484fSRodney W. Grimes 		/*
15158f0484fSRodney W. Grimes 		 * Assemble the final product.
15258f0484fSRodney W. Grimes 		 */
15358f0484fSRodney W. Grimes 		prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
15458f0484fSRodney W. Grimes 		    low.ul[H];
15558f0484fSRodney W. Grimes 		prod.ul[L] = low.ul[L];
15658f0484fSRodney W. Grimes 	}
15758f0484fSRodney W. Grimes 	return (negall ? -prod.q : prod.q);
15858f0484fSRodney W. Grimes #undef u1
15958f0484fSRodney W. Grimes #undef u0
16058f0484fSRodney W. Grimes #undef v1
16158f0484fSRodney W. Grimes #undef v0
16258f0484fSRodney W. Grimes }
16358f0484fSRodney W. Grimes 
16458f0484fSRodney W. Grimes /*
16558f0484fSRodney W. Grimes  * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
16658f0484fSRodney W. Grimes  * the number of bits in a long (whatever that is---the code below
16758f0484fSRodney W. Grimes  * does not care as long as quad.h does its part of the bargain---but
16858f0484fSRodney W. Grimes  * typically N==16).
16958f0484fSRodney W. Grimes  *
17058f0484fSRodney W. Grimes  * We use the same algorithm from Knuth, but this time the modulo refinement
17158f0484fSRodney W. Grimes  * does not apply.  On the other hand, since N is half the size of a long,
17258f0484fSRodney W. Grimes  * we can get away with native multiplication---none of our input terms
17358f0484fSRodney W. Grimes  * exceeds (ULONG_MAX >> 1).
17458f0484fSRodney W. Grimes  *
17558f0484fSRodney W. Grimes  * Note that, for u_long l, the quad-precision result
17658f0484fSRodney W. Grimes  *
17758f0484fSRodney W. Grimes  *	l << N
17858f0484fSRodney W. Grimes  *
17958f0484fSRodney W. Grimes  * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
18058f0484fSRodney W. Grimes  */
18158f0484fSRodney W. Grimes static quad_t
__lmulq(u_long u,u_long v)18258f0484fSRodney W. Grimes __lmulq(u_long u, u_long v)
18358f0484fSRodney W. Grimes {
18458f0484fSRodney W. Grimes 	u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low;
18558f0484fSRodney W. Grimes 	u_long prodh, prodl, was;
18658f0484fSRodney W. Grimes 	union uu prod;
18758f0484fSRodney W. Grimes 	int neg;
18858f0484fSRodney W. Grimes 
18958f0484fSRodney W. Grimes 	u1 = HHALF(u);
19058f0484fSRodney W. Grimes 	u0 = LHALF(u);
19158f0484fSRodney W. Grimes 	v1 = HHALF(v);
19258f0484fSRodney W. Grimes 	v0 = LHALF(v);
19358f0484fSRodney W. Grimes 
19458f0484fSRodney W. Grimes 	low = u0 * v0;
19558f0484fSRodney W. Grimes 
19658f0484fSRodney W. Grimes 	/* This is the same small-number optimization as before. */
19758f0484fSRodney W. Grimes 	if (u1 == 0 && v1 == 0)
19858f0484fSRodney W. Grimes 		return (low);
19958f0484fSRodney W. Grimes 
20058f0484fSRodney W. Grimes 	if (u1 >= u0)
20158f0484fSRodney W. Grimes 		udiff = u1 - u0, neg = 0;
20258f0484fSRodney W. Grimes 	else
20358f0484fSRodney W. Grimes 		udiff = u0 - u1, neg = 1;
20458f0484fSRodney W. Grimes 	if (v0 >= v1)
20558f0484fSRodney W. Grimes 		vdiff = v0 - v1;
20658f0484fSRodney W. Grimes 	else
20758f0484fSRodney W. Grimes 		vdiff = v1 - v0, neg ^= 1;
20858f0484fSRodney W. Grimes 	mid = udiff * vdiff;
20958f0484fSRodney W. Grimes 
21058f0484fSRodney W. Grimes 	high = u1 * v1;
21158f0484fSRodney W. Grimes 
21258f0484fSRodney W. Grimes 	/* prod = (high << 2N) + (high << N); */
21358f0484fSRodney W. Grimes 	prodh = high + HHALF(high);
21458f0484fSRodney W. Grimes 	prodl = LHUP(high);
21558f0484fSRodney W. Grimes 
21658f0484fSRodney W. Grimes 	/* if (neg) prod -= mid << N; else prod += mid << N; */
21758f0484fSRodney W. Grimes 	if (neg) {
21858f0484fSRodney W. Grimes 		was = prodl;
21958f0484fSRodney W. Grimes 		prodl -= LHUP(mid);
22058f0484fSRodney W. Grimes 		prodh -= HHALF(mid) + (prodl > was);
22158f0484fSRodney W. Grimes 	} else {
22258f0484fSRodney W. Grimes 		was = prodl;
22358f0484fSRodney W. Grimes 		prodl += LHUP(mid);
22458f0484fSRodney W. Grimes 		prodh += HHALF(mid) + (prodl < was);
22558f0484fSRodney W. Grimes 	}
22658f0484fSRodney W. Grimes 
22758f0484fSRodney W. Grimes 	/* prod += low << N */
22858f0484fSRodney W. Grimes 	was = prodl;
22958f0484fSRodney W. Grimes 	prodl += LHUP(low);
23058f0484fSRodney W. Grimes 	prodh += HHALF(low) + (prodl < was);
23158f0484fSRodney W. Grimes 	/* ... + low; */
23258f0484fSRodney W. Grimes 	if ((prodl += low) < low)
23358f0484fSRodney W. Grimes 		prodh++;
23458f0484fSRodney W. Grimes 
23558f0484fSRodney W. Grimes 	/* return 4N-bit product */
23658f0484fSRodney W. Grimes 	prod.ul[H] = prodh;
23758f0484fSRodney W. Grimes 	prod.ul[L] = prodl;
23858f0484fSRodney W. Grimes 	return (prod.q);
23958f0484fSRodney W. Grimes }
240