xref: /freebsd/sys/libkern/arm/muldi3.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
103f11029SOlivier Houchard /*	$NetBSD: muldi3.c,v 1.8 2003/08/07 16:32:09 agc Exp $	*/
203f11029SOlivier Houchard 
303f11029SOlivier Houchard /*-
4*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
5*51369649SPedro F. Giffuni  *
603f11029SOlivier Houchard  * Copyright (c) 1992, 1993
703f11029SOlivier Houchard  *	The Regents of the University of California.  All rights reserved.
803f11029SOlivier Houchard  *
903f11029SOlivier Houchard  * This software was developed by the Computer Systems Engineering group
1003f11029SOlivier Houchard  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
1103f11029SOlivier Houchard  * contributed to Berkeley.
1203f11029SOlivier Houchard  *
1303f11029SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
1403f11029SOlivier Houchard  * modification, are permitted provided that the following conditions
1503f11029SOlivier Houchard  * are met:
1603f11029SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
1703f11029SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
1803f11029SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
1903f11029SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
2003f11029SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
2103f11029SOlivier Houchard  * 3. Neither the name of the University nor the names of its contributors
2203f11029SOlivier Houchard  *    may be used to endorse or promote products derived from this software
2303f11029SOlivier Houchard  *    without specific prior written permission.
2403f11029SOlivier Houchard  *
2503f11029SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2603f11029SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2703f11029SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2803f11029SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2903f11029SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3003f11029SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3103f11029SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3203f11029SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3303f11029SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3403f11029SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3503f11029SOlivier Houchard  * SUCH DAMAGE.
3603f11029SOlivier Houchard  */
3703f11029SOlivier Houchard 
3803f11029SOlivier Houchard #include <libkern/quad.h>
3903f11029SOlivier Houchard 
4003f11029SOlivier Houchard /*
4103f11029SOlivier Houchard  * Multiply two quads.
4203f11029SOlivier Houchard  *
4303f11029SOlivier Houchard  * Our algorithm is based on the following.  Split incoming quad values
4403f11029SOlivier Houchard  * u and v (where u,v >= 0) into
4503f11029SOlivier Houchard  *
4603f11029SOlivier Houchard  *	u = 2^n u1  *  u0	(n = number of bits in `u_int', usu. 32)
4703f11029SOlivier Houchard  *
4803f11029SOlivier Houchard  * and
4903f11029SOlivier Houchard  *
5003f11029SOlivier Houchard  *	v = 2^n v1  *  v0
5103f11029SOlivier Houchard  *
5203f11029SOlivier Houchard  * Then
5303f11029SOlivier Houchard  *
5403f11029SOlivier Houchard  *	uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
5503f11029SOlivier Houchard  *	   = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
5603f11029SOlivier Houchard  *
5703f11029SOlivier Houchard  * Now add 2^n u1 v1 to the first term and subtract it from the middle,
5803f11029SOlivier Houchard  * and add 2^n u0 v0 to the last term and subtract it from the middle.
5903f11029SOlivier Houchard  * This gives:
6003f11029SOlivier Houchard  *
6103f11029SOlivier Houchard  *	uv = (2^2n + 2^n) (u1 v1)  +
6203f11029SOlivier Houchard  *	         (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
6303f11029SOlivier Houchard  *	       (2^n + 1)  (u0 v0)
6403f11029SOlivier Houchard  *
6503f11029SOlivier Houchard  * Factoring the middle a bit gives us:
6603f11029SOlivier Houchard  *
6703f11029SOlivier Houchard  *	uv = (2^2n + 2^n) (u1 v1)  +			[u1v1 = high]
6803f11029SOlivier Houchard  *		 (2^n)    (u1 - u0) (v0 - v1)  +	[(u1-u0)... = mid]
6903f11029SOlivier Houchard  *	       (2^n + 1)  (u0 v0)			[u0v0 = low]
7003f11029SOlivier Houchard  *
7103f11029SOlivier Houchard  * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
7203f11029SOlivier Houchard  * in just half the precision of the original.  (Note that either or both
7303f11029SOlivier Houchard  * of (u1 - u0) or (v0 - v1) may be negative.)
7403f11029SOlivier Houchard  *
7503f11029SOlivier Houchard  * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
7603f11029SOlivier Houchard  *
7703f11029SOlivier Houchard  * Since C does not give us a `int * int = quad' operator, we split
7803f11029SOlivier Houchard  * our input quads into two ints, then split the two ints into two
7903f11029SOlivier Houchard  * shorts.  We can then calculate `short * short = int' in native
8003f11029SOlivier Houchard  * arithmetic.
8103f11029SOlivier Houchard  *
8203f11029SOlivier Houchard  * Our product should, strictly speaking, be a `long quad', with 128
8303f11029SOlivier Houchard  * bits, but we are going to discard the upper 64.  In other words,
8403f11029SOlivier Houchard  * we are not interested in uv, but rather in (uv mod 2^2n).  This
8503f11029SOlivier Houchard  * makes some of the terms above vanish, and we get:
8603f11029SOlivier Houchard  *
8703f11029SOlivier Houchard  *	(2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
8803f11029SOlivier Houchard  *
8903f11029SOlivier Houchard  * or
9003f11029SOlivier Houchard  *
9103f11029SOlivier Houchard  *	(2^n)(high + mid + low) + low
9203f11029SOlivier Houchard  *
9303f11029SOlivier Houchard  * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
9403f11029SOlivier Houchard  * of 2^n in either one will also vanish.  Only `low' need be computed
9503f11029SOlivier Houchard  * mod 2^2n, and only because of the final term above.
9603f11029SOlivier Houchard  */
9703f11029SOlivier Houchard static quad_t __lmulq(u_int, u_int);
9803f11029SOlivier Houchard 
9903f11029SOlivier Houchard quad_t __muldi3(quad_t, quad_t);
10003f11029SOlivier Houchard quad_t
__muldi3(quad_t a,quad_t b)10103f11029SOlivier Houchard __muldi3(quad_t a, quad_t b)
10203f11029SOlivier Houchard {
10303f11029SOlivier Houchard 	union uu u, v, low, prod;
10403f11029SOlivier Houchard 	u_int high, mid, udiff, vdiff;
10503f11029SOlivier Houchard 	int negall, negmid;
10603f11029SOlivier Houchard #define	u1	u.ul[H]
10703f11029SOlivier Houchard #define	u0	u.ul[L]
10803f11029SOlivier Houchard #define	v1	v.ul[H]
10903f11029SOlivier Houchard #define	v0	v.ul[L]
11003f11029SOlivier Houchard 
11103f11029SOlivier Houchard 	/*
11203f11029SOlivier Houchard 	 * Get u and v such that u, v >= 0.  When this is finished,
11303f11029SOlivier Houchard 	 * u1, u0, v1, and v0 will be directly accessible through the
11403f11029SOlivier Houchard 	 * int fields.
11503f11029SOlivier Houchard 	 */
11603f11029SOlivier Houchard 	if (a >= 0)
11703f11029SOlivier Houchard 		u.q = a, negall = 0;
11803f11029SOlivier Houchard 	else
11903f11029SOlivier Houchard 		u.q = -a, negall = 1;
12003f11029SOlivier Houchard 	if (b >= 0)
12103f11029SOlivier Houchard 		v.q = b;
12203f11029SOlivier Houchard 	else
12303f11029SOlivier Houchard 		v.q = -b, negall ^= 1;
12403f11029SOlivier Houchard 
12503f11029SOlivier Houchard 	if (u1 == 0 && v1 == 0) {
12603f11029SOlivier Houchard 		/*
12703f11029SOlivier Houchard 		 * An (I hope) important optimization occurs when u1 and v1
12803f11029SOlivier Houchard 		 * are both 0.  This should be common since most numbers
12903f11029SOlivier Houchard 		 * are small.  Here the product is just u0*v0.
13003f11029SOlivier Houchard 		 */
13103f11029SOlivier Houchard 		prod.q = __lmulq(u0, v0);
13203f11029SOlivier Houchard 	} else {
13303f11029SOlivier Houchard 		/*
13403f11029SOlivier Houchard 		 * Compute the three intermediate products, remembering
13503f11029SOlivier Houchard 		 * whether the middle term is negative.  We can discard
13603f11029SOlivier Houchard 		 * any upper bits in high and mid, so we can use native
13703f11029SOlivier Houchard 		 * u_int * u_int => u_int arithmetic.
13803f11029SOlivier Houchard 		 */
13903f11029SOlivier Houchard 		low.q = __lmulq(u0, v0);
14003f11029SOlivier Houchard 
14103f11029SOlivier Houchard 		if (u1 >= u0)
14203f11029SOlivier Houchard 			negmid = 0, udiff = u1 - u0;
14303f11029SOlivier Houchard 		else
14403f11029SOlivier Houchard 			negmid = 1, udiff = u0 - u1;
14503f11029SOlivier Houchard 		if (v0 >= v1)
14603f11029SOlivier Houchard 			vdiff = v0 - v1;
14703f11029SOlivier Houchard 		else
14803f11029SOlivier Houchard 			vdiff = v1 - v0, negmid ^= 1;
14903f11029SOlivier Houchard 		mid = udiff * vdiff;
15003f11029SOlivier Houchard 
15103f11029SOlivier Houchard 		high = u1 * v1;
15203f11029SOlivier Houchard 
15303f11029SOlivier Houchard 		/*
15403f11029SOlivier Houchard 		 * Assemble the final product.
15503f11029SOlivier Houchard 		 */
15603f11029SOlivier Houchard 		prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
15703f11029SOlivier Houchard 		    low.ul[H];
15803f11029SOlivier Houchard 		prod.ul[L] = low.ul[L];
15903f11029SOlivier Houchard 	}
16003f11029SOlivier Houchard 	return (negall ? -prod.q : prod.q);
16103f11029SOlivier Houchard #undef u1
16203f11029SOlivier Houchard #undef u0
16303f11029SOlivier Houchard #undef v1
16403f11029SOlivier Houchard #undef v0
16503f11029SOlivier Houchard }
16603f11029SOlivier Houchard 
16703f11029SOlivier Houchard /*
16803f11029SOlivier Houchard  * Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half
16903f11029SOlivier Houchard  * the number of bits in an int (whatever that is---the code below
17003f11029SOlivier Houchard  * does not care as long as quad.h does its part of the bargain---but
17103f11029SOlivier Houchard  * typically N==16).
17203f11029SOlivier Houchard  *
17303f11029SOlivier Houchard  * We use the same algorithm from Knuth, but this time the modulo refinement
17403f11029SOlivier Houchard  * does not apply.  On the other hand, since N is half the size of an int,
17503f11029SOlivier Houchard  * we can get away with native multiplication---none of our input terms
17603f11029SOlivier Houchard  * exceeds (UINT_MAX >> 1).
17703f11029SOlivier Houchard  *
17803f11029SOlivier Houchard  * Note that, for u_int l, the quad-precision result
17903f11029SOlivier Houchard  *
18003f11029SOlivier Houchard  *	l << N
18103f11029SOlivier Houchard  *
18203f11029SOlivier Houchard  * splits into high and low ints as HHALF(l) and LHUP(l) respectively.
18303f11029SOlivier Houchard  */
18403f11029SOlivier Houchard static quad_t
__lmulq(u_int u,u_int v)18503f11029SOlivier Houchard __lmulq(u_int u, u_int v)
18603f11029SOlivier Houchard {
18703f11029SOlivier Houchard 	u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low;
18803f11029SOlivier Houchard 	u_int prodh, prodl, was;
18903f11029SOlivier Houchard 	union uu prod;
19003f11029SOlivier Houchard 	int neg;
19103f11029SOlivier Houchard 
19203f11029SOlivier Houchard 	u1 = HHALF(u);
19303f11029SOlivier Houchard 	u0 = LHALF(u);
19403f11029SOlivier Houchard 	v1 = HHALF(v);
19503f11029SOlivier Houchard 	v0 = LHALF(v);
19603f11029SOlivier Houchard 
19703f11029SOlivier Houchard 	low = u0 * v0;
19803f11029SOlivier Houchard 
19903f11029SOlivier Houchard 	/* This is the same small-number optimization as before. */
20003f11029SOlivier Houchard 	if (u1 == 0 && v1 == 0)
20103f11029SOlivier Houchard 		return (low);
20203f11029SOlivier Houchard 
20303f11029SOlivier Houchard 	if (u1 >= u0)
20403f11029SOlivier Houchard 		udiff = u1 - u0, neg = 0;
20503f11029SOlivier Houchard 	else
20603f11029SOlivier Houchard 		udiff = u0 - u1, neg = 1;
20703f11029SOlivier Houchard 	if (v0 >= v1)
20803f11029SOlivier Houchard 		vdiff = v0 - v1;
20903f11029SOlivier Houchard 	else
21003f11029SOlivier Houchard 		vdiff = v1 - v0, neg ^= 1;
21103f11029SOlivier Houchard 	mid = udiff * vdiff;
21203f11029SOlivier Houchard 
21303f11029SOlivier Houchard 	high = u1 * v1;
21403f11029SOlivier Houchard 
21503f11029SOlivier Houchard 	/* prod = (high << 2N) + (high << N); */
21603f11029SOlivier Houchard 	prodh = high + HHALF(high);
21703f11029SOlivier Houchard 	prodl = LHUP(high);
21803f11029SOlivier Houchard 
21903f11029SOlivier Houchard 	/* if (neg) prod -= mid << N; else prod += mid << N; */
22003f11029SOlivier Houchard 	if (neg) {
22103f11029SOlivier Houchard 		was = prodl;
22203f11029SOlivier Houchard 		prodl -= LHUP(mid);
22303f11029SOlivier Houchard 		prodh -= HHALF(mid) + (prodl > was);
22403f11029SOlivier Houchard 	} else {
22503f11029SOlivier Houchard 		was = prodl;
22603f11029SOlivier Houchard 		prodl += LHUP(mid);
22703f11029SOlivier Houchard 		prodh += HHALF(mid) + (prodl < was);
22803f11029SOlivier Houchard 	}
22903f11029SOlivier Houchard 
23003f11029SOlivier Houchard 	/* prod += low << N */
23103f11029SOlivier Houchard 	was = prodl;
23203f11029SOlivier Houchard 	prodl += LHUP(low);
23303f11029SOlivier Houchard 	prodh += HHALF(low) + (prodl < was);
23403f11029SOlivier Houchard 	/* ... + low; */
23503f11029SOlivier Houchard 	if ((prodl += low) < low)
23603f11029SOlivier Houchard 		prodh++;
23703f11029SOlivier Houchard 
23803f11029SOlivier Houchard 	/* return 4N-bit product */
23903f11029SOlivier Houchard 	prod.ul[H] = prodh;
24003f11029SOlivier Houchard 	prod.ul[L] = prodl;
24103f11029SOlivier Houchard 	return (prod.q);
24203f11029SOlivier Houchard }
243