/*-1* Copyright (c) 1992, 19932* The Regents of the University of California. All rights reserved.3*4* This software was developed by the Computer Systems Engineering group5* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and6* contributed to Berkeley.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*32* From:33* @(#)muldi3.c 8.1 (Berkeley) 6/4/9334* NetBSD: muldi3.c,v 1.1 2005/12/20 19:28:51 christos Exp35*/3637#include "longlong.h"3839/*40* Multiply two long longs.41*42* Our algorithm is based on the following. Split incoming long long43* values u and v (where u,v >= 0) into44*45* u = 2^n u1 * u0 (n = number of bits in `unsigned int', usu. 32)46*47* and48*49* v = 2^n v1 * v050*51* Then52*53* uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v054* = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v055*56* Now add 2^n u1 v1 to the first term and subtract it from the middle,57* and add 2^n u0 v0 to the last term and subtract it from the middle.58* This gives:59*60* uv = (2^2n + 2^n) (u1 v1) +61* (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +62* (2^n + 1) (u0 v0)63*64* Factoring the middle a bit gives us:65*66* uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]67* (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]68* (2^n + 1) (u0 v0) [u0v0 = low]69*70* The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done71* in just half the precision of the original. (Note that either or both72* of (u1 - u0) or (v0 - v1) may be negative.)73*74* This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.75*76* Since C does not give us a `int * int = long long' operator, we split77* our input long longs into two ints, then split the two ints into two78* shorts. We can then calculate `short * short = int' in native79* arithmetic.80*81* Our product should, strictly speaking, be a `long long long', with82* 128 bits, but we are going to discard the upper 64. In other words,83* we are not interested in uv, but rather in (uv mod 2^2n). This84* makes some of the terms above vanish, and we get:85*86* (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)87*88* or89*90* (2^n)(high + mid + low) + low91*92* Furthermore, `high' and `mid' can be computed mod 2^n, as any factor93* of 2^n in either one will also vanish. Only `low' need be computed94* mod 2^2n, and only because of the final term above.95*/96static long long __lmulq(unsigned int, unsigned int);9798long long99__muldi3(long long a, long long b)100{101union uu u, v, low, prod;102unsigned int high, mid, udiff, vdiff;103int negall, negmid;104#define u1 u.ui[H]105#define u0 u.ui[L]106#define v1 v.ui[H]107#define v0 v.ui[L]108109/*110* Get u and v such that u, v >= 0. When this is finished,111* u1, u0, v1, and v0 will be directly accessible through the112* int fields.113*/114if (a >= 0)115u.ll = a, negall = 0;116else117u.ll = -a, negall = 1;118if (b >= 0)119v.ll = b;120else121v.ll = -b, negall ^= 1;122123if (u1 == 0 && v1 == 0) {124/*125* An (I hope) important optimization occurs when u1 and v1126* are both 0. This should be common since most numbers127* are small. Here the product is just u0*v0.128*/129prod.ll = __lmulq(u0, v0);130} else {131/*132* Compute the three intermediate products, remembering133* whether the middle term is negative. We can discard134* any upper bits in high and mid, so we can use native135* unsigned int * unsigned int => unsigned int arithmetic.136*/137low.ll = __lmulq(u0, v0);138139if (u1 >= u0)140negmid = 0, udiff = u1 - u0;141else142negmid = 1, udiff = u0 - u1;143if (v0 >= v1)144vdiff = v0 - v1;145else146vdiff = v1 - v0, negmid ^= 1;147mid = udiff * vdiff;148149high = u1 * v1;150151/*152* Assemble the final product.153*/154prod.ui[H] = high + (negmid ? -mid : mid) + low.ui[L] +155low.ui[H];156prod.ui[L] = low.ui[L];157}158return (negall ? -prod.ll : prod.ll);159#undef u1160#undef u0161#undef v1162#undef v0163}164165/*166* Multiply two 2N-bit ints to produce a 4N-bit long long, where N is167* half the number of bits in an int (whatever that is---the code168* below does not care as long as the header file does its part of the169* bargain---but typically N==16).170*171* We use the same algorithm from Knuth, but this time the modulo refinement172* does not apply. On the other hand, since N is half the size of an int,173* we can get away with native multiplication---none of our input terms174* exceeds (UINT_MAX >> 1).175*176* Note that, for unsigned int l, the quad-precision (long long) result177*178* l << N179*180* splits into high and low ints as HHALF(l) and LHUP(l) respectively.181*/182static long long183__lmulq(unsigned int u, unsigned int v)184{185unsigned int u1, u0, v1, v0, udiff, vdiff, high, mid, low;186unsigned int prodh, prodl, was;187union uu prod;188int neg;189190u1 = HHALF(u);191u0 = LHALF(u);192v1 = HHALF(v);193v0 = LHALF(v);194195low = u0 * v0;196197/* This is the same small-number optimization as before. */198if (u1 == 0 && v1 == 0)199return (low);200201if (u1 >= u0)202udiff = u1 - u0, neg = 0;203else204udiff = u0 - u1, neg = 1;205if (v0 >= v1)206vdiff = v0 - v1;207else208vdiff = v1 - v0, neg ^= 1;209mid = udiff * vdiff;210211high = u1 * v1;212213/* prod = (high << 2N) + (high << N); */214prodh = high + HHALF(high);215prodl = LHUP(high);216217/* if (neg) prod -= mid << N; else prod += mid << N; */218if (neg) {219was = prodl;220prodl -= LHUP(mid);221prodh -= HHALF(mid) + (prodl > was);222} else {223was = prodl;224prodl += LHUP(mid);225prodh += HHALF(mid) + (prodl < was);226}227228/* prod += low << N */229was = prodl;230prodl += LHUP(low);231prodh += HHALF(low) + (prodl < was);232/* ... + low; */233if ((prodl += low) < low)234prodh++;235236/* return 4N-bit product */237prod.ui[H] = prodh;238prod.ui[L] = prodl;239return (prod.ll);240}241242243