muldi3.c revision 5ce758aedffaa9134685f699eb3d4255274038b6
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync/* $NetBSD: muldi3.c,v 1.10 2005/12/11 12:24:37 christos Exp $ */
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Copyright (c) 1992, 1993
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * The Regents of the University of California. All rights reserved.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * This software was developed by the Computer Systems Engineering group
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * contributed to Berkeley.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Redistribution and use in source and binary forms, with or without
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * modification, are permitted provided that the following conditions
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * 1. Redistributions of source code must retain the above copyright
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * notice, this list of conditions and the following disclaimer.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * 2. Redistributions in binary form must reproduce the above copyright
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * notice, this list of conditions and the following disclaimer in the
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * documentation and/or other materials provided with the distribution.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * 3. Neither the name of the University nor the names of its contributors
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * may be used to endorse or promote products derived from this software
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * without specific prior written permission.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * SUCH DAMAGE.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync#if defined(LIBC_SCCS) && !defined(lint)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsyncstatic char sccsid[] = "@(#)muldi3.c 8.1 (Berkeley) 6/4/93";
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync__RCSID("$NetBSD: muldi3.c,v 1.10 2005/12/11 12:24:37 christos Exp $");
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Multiply two quads.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Our algorithm is based on the following. Split incoming quad values
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * u and v (where u,v >= 0) into
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * u = 2^n u1 * u0 (n = number of bits in `u_int', usu. 32)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * v = 2^n v1 * v0
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Now add 2^n u1 v1 to the first term and subtract it from the middle,
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * and add 2^n u0 v0 to the last term and subtract it from the middle.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * This gives:
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * uv = (2^2n + 2^n) (u1 v1) +
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n + 1) (u0 v0)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Factoring the middle a bit gives us:
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n + 1) (u0 v0) [u0v0 = low]
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * in just half the precision of the original. (Note that either or both
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * of (u1 - u0) or (v0 - v1) may be negative.)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Since C does not give us a `int * int = quad' operator, we split
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * our input quads into two ints, then split the two ints into two
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * shorts. We can then calculate `short * short = int' in native
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * arithmetic.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Our product should, strictly speaking, be a `long quad', with 128
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * bits, but we are going to discard the upper 64. In other words,
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * we are not interested in uv, but rather in (uv mod 2^2n). This
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * makes some of the terms above vanish, and we get:
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n)(high + mid + low) + low
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * of 2^n in either one will also vanish. Only `low' need be computed
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * mod 2^2n, and only because of the final term above.
u.q = a, negall = 0;
* does not care as long as quad.h does its part of the bargain---but
static quad_t
int neg;
return (low);
if (neg) {
prodh++;
return (prod.q);