/* $NetBSD: muldi3.c,v 1.8 2003/08/07 16:32:09 agc Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1992, 19936* The Regents of the University of California. All rights reserved.7*8* This software was developed by the Computer Systems Engineering group9* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and10* contributed to Berkeley.11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20* 3. Neither the name of the University nor the names of its contributors21* may be used to endorse or promote products derived from this software22* without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND25* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE28* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL29* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS30* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT32* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY33* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF34* SUCH DAMAGE.35*/3637#include <libkern/quad.h>3839/*40* Multiply two quads.41*42* Our algorithm is based on the following. Split incoming quad values43* u and v (where u,v >= 0) into44*45* u = 2^n u1 * u0 (n = number of bits in `u_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 = quad' operator, we split77* our input quads 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 quad', with 12882* 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 quad_t __lmulq(u_int, u_int);9798quad_t __muldi3(quad_t, quad_t);99quad_t100__muldi3(quad_t a, quad_t b)101{102union uu u, v, low, prod;103u_int high, mid, udiff, vdiff;104int negall, negmid;105#define u1 u.ul[H]106#define u0 u.ul[L]107#define v1 v.ul[H]108#define v0 v.ul[L]109110/*111* Get u and v such that u, v >= 0. When this is finished,112* u1, u0, v1, and v0 will be directly accessible through the113* int fields.114*/115if (a >= 0)116u.q = a, negall = 0;117else118u.q = -a, negall = 1;119if (b >= 0)120v.q = b;121else122v.q = -b, negall ^= 1;123124if (u1 == 0 && v1 == 0) {125/*126* An (I hope) important optimization occurs when u1 and v1127* are both 0. This should be common since most numbers128* are small. Here the product is just u0*v0.129*/130prod.q = __lmulq(u0, v0);131} else {132/*133* Compute the three intermediate products, remembering134* whether the middle term is negative. We can discard135* any upper bits in high and mid, so we can use native136* u_int * u_int => u_int arithmetic.137*/138low.q = __lmulq(u0, v0);139140if (u1 >= u0)141negmid = 0, udiff = u1 - u0;142else143negmid = 1, udiff = u0 - u1;144if (v0 >= v1)145vdiff = v0 - v1;146else147vdiff = v1 - v0, negmid ^= 1;148mid = udiff * vdiff;149150high = u1 * v1;151152/*153* Assemble the final product.154*/155prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +156low.ul[H];157prod.ul[L] = low.ul[L];158}159return (negall ? -prod.q : prod.q);160#undef u1161#undef u0162#undef v1163#undef v0164}165166/*167* Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half168* the number of bits in an int (whatever that is---the code below169* does not care as long as quad.h does its part of the bargain---but170* typically N==16).171*172* We use the same algorithm from Knuth, but this time the modulo refinement173* does not apply. On the other hand, since N is half the size of an int,174* we can get away with native multiplication---none of our input terms175* exceeds (UINT_MAX >> 1).176*177* Note that, for u_int l, the quad-precision result178*179* l << N180*181* splits into high and low ints as HHALF(l) and LHUP(l) respectively.182*/183static quad_t184__lmulq(u_int u, u_int v)185{186u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low;187u_int prodh, prodl, was;188union uu prod;189int neg;190191u1 = HHALF(u);192u0 = LHALF(u);193v1 = HHALF(v);194v0 = LHALF(v);195196low = u0 * v0;197198/* This is the same small-number optimization as before. */199if (u1 == 0 && v1 == 0)200return (low);201202if (u1 >= u0)203udiff = u1 - u0, neg = 0;204else205udiff = u0 - u1, neg = 1;206if (v0 >= v1)207vdiff = v0 - v1;208else209vdiff = v1 - v0, neg ^= 1;210mid = udiff * vdiff;211212high = u1 * v1;213214/* prod = (high << 2N) + (high << N); */215prodh = high + HHALF(high);216prodl = LHUP(high);217218/* if (neg) prod -= mid << N; else prod += mid << N; */219if (neg) {220was = prodl;221prodl -= LHUP(mid);222prodh -= HHALF(mid) + (prodl > was);223} else {224was = prodl;225prodl += LHUP(mid);226prodh += HHALF(mid) + (prodl < was);227}228229/* prod += low << N */230was = prodl;231prodl += LHUP(low);232prodh += HHALF(low) + (prodl < was);233/* ... + low; */234if ((prodl += low) < low)235prodh++;236237/* return 4N-bit product */238prod.ul[H] = prodh;239prod.ul[L] = prodl;240return (prod.q);241}242243244