153430Sbostic /*- 253430Sbostic * Copyright (c) 1992 The Regents of the University of California. 353430Sbostic * All rights reserved. 453430Sbostic * 553794Sbostic * This software was developed by the Computer Systems Engineering group 653794Sbostic * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 753794Sbostic * contributed to Berkeley. 853794Sbostic * 953430Sbostic * %sccs.include.redist.c% 1053430Sbostic */ 1153430Sbostic 1253430Sbostic #if defined(LIBC_SCCS) && !defined(lint) 13*54431Sbostic static char sccsid[] = "@(#)muldi3.c 5.8 (Berkeley) 06/25/92"; 1453430Sbostic #endif /* LIBC_SCCS and not lint */ 1553430Sbostic 1653794Sbostic #include "quad.h" 1753459Sbostic 1853794Sbostic /* 1953794Sbostic * Multiply two quads. 2053794Sbostic * 2153794Sbostic * Our algorithm is based on the following. Split incoming quad values 2253794Sbostic * u and v (where u,v >= 0) into 2353794Sbostic * 2453794Sbostic * u = 2^n u1 * u0 (n = number of bits in `u_long', usu. 32) 2553794Sbostic * 2653794Sbostic * and 2753794Sbostic * 2853794Sbostic * v = 2^n v1 * v0 2953794Sbostic * 3053794Sbostic * Then 3153794Sbostic * 3253794Sbostic * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0 3353794Sbostic * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0 3453794Sbostic * 3553794Sbostic * Now add 2^n u1 v1 to the first term and subtract it from the middle, 3653794Sbostic * and add 2^n u0 v0 to the last term and subtract it from the middle. 3753794Sbostic * This gives: 3853794Sbostic * 3953794Sbostic * uv = (2^2n + 2^n) (u1 v1) + 4053794Sbostic * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) + 4153794Sbostic * (2^n + 1) (u0 v0) 4253794Sbostic * 4353794Sbostic * Factoring the middle a bit gives us: 4453794Sbostic * 4553794Sbostic * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high] 4653794Sbostic * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid] 4753794Sbostic * (2^n + 1) (u0 v0) [u0v0 = low] 4853794Sbostic * 4953794Sbostic * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done 5053794Sbostic * in just half the precision of the original. (Note that either or both 5153794Sbostic * of (u1 - u0) or (v0 - v1) may be negative.) 5253794Sbostic * 5353794Sbostic * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278. 5453794Sbostic * 5553794Sbostic * Since C does not give us a `long * long = quad' operator, we split 5653794Sbostic * our input quads into two longs, then split the two longs into two 5753794Sbostic * shorts. We can then calculate `short * short = long' in native 5853794Sbostic * arithmetic. 5953794Sbostic * 6053794Sbostic * Our product should, strictly speaking, be a `long quad', with 128 6153794Sbostic * bits, but we are going to discard the upper 64. In other words, 6253794Sbostic * we are not interested in uv, but rather in (uv mod 2^2n). This 6353794Sbostic * makes some of the terms above vanish, and we get: 6453794Sbostic * 6553794Sbostic * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low) 6653794Sbostic * 6753794Sbostic * or 6853794Sbostic * 6953794Sbostic * (2^n)(high + mid + low) + low 7053794Sbostic * 7153794Sbostic * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor 7253794Sbostic * of 2^n in either one will also vanish. Only `low' need be computed 7353794Sbostic * mod 2^2n, and only because of the final term above. 7453794Sbostic */ 75*54431Sbostic static quad_t __lmulq(u_long, u_long); 7653459Sbostic 77*54431Sbostic quad_t 78*54431Sbostic __muldi3(a, b) 79*54431Sbostic quad_t a, b; 8053794Sbostic { 8153794Sbostic union uu u, v, low, prod; 8253794Sbostic register u_long high, mid, udiff, vdiff; 8353794Sbostic register int negall, negmid; 8453794Sbostic #define u1 u.ul[H] 8553794Sbostic #define u0 u.ul[L] 8653794Sbostic #define v1 v.ul[H] 8753794Sbostic #define v0 v.ul[L] 8853459Sbostic 8953794Sbostic /* 9053794Sbostic * Get u and v such that u, v >= 0. When this is finished, 9153794Sbostic * u1, u0, v1, and v0 will be directly accessible through the 9253794Sbostic * longword fields. 9353794Sbostic */ 9453794Sbostic if (a >= 0) 9553794Sbostic u.q = a, negall = 0; 9653794Sbostic else 9753794Sbostic u.q = -a, negall = 1; 9853794Sbostic if (b >= 0) 9953794Sbostic v.q = b; 10053794Sbostic else 10153794Sbostic v.q = -b, negall ^= 1; 10253459Sbostic 10353794Sbostic if (u1 == 0 && v1 == 0) { 10453794Sbostic /* 10553794Sbostic * An (I hope) important optimization occurs when u1 and v1 10653794Sbostic * are both 0. This should be common since most numbers 10753794Sbostic * are small. Here the product is just u0*v0. 10853794Sbostic */ 10953794Sbostic prod.q = __lmulq(u0, v0); 11053794Sbostic } else { 11153794Sbostic /* 11253794Sbostic * Compute the three intermediate products, remembering 11353794Sbostic * whether the middle term is negative. We can discard 11453794Sbostic * any upper bits in high and mid, so we can use native 11553794Sbostic * u_long * u_long => u_long arithmetic. 11653794Sbostic */ 11753794Sbostic low.q = __lmulq(u0, v0); 11853459Sbostic 11953794Sbostic if (u1 >= u0) 12053794Sbostic negmid = 0, udiff = u1 - u0; 12153794Sbostic else 12253794Sbostic negmid = 1, udiff = u0 - u1; 12353794Sbostic if (v0 >= v1) 12453794Sbostic vdiff = v0 - v1; 12553794Sbostic else 12653794Sbostic vdiff = v1 - v0, negmid ^= 1; 12753794Sbostic mid = udiff * vdiff; 12853459Sbostic 12953794Sbostic high = u1 * v1; 13051749Smckusick 13153794Sbostic /* 13253794Sbostic * Assemble the final product. 13353794Sbostic */ 13453794Sbostic prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] + 13553794Sbostic low.ul[H]; 13653794Sbostic prod.ul[L] = low.ul[L]; 13753794Sbostic } 13853794Sbostic return (negall ? -prod.q : prod.q); 13953794Sbostic #undef u1 14053794Sbostic #undef u0 14153794Sbostic #undef v1 14253794Sbostic #undef v0 14353794Sbostic } 14451749Smckusick 14553794Sbostic /* 14653794Sbostic * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half 14753794Sbostic * the number of bits in a long (whatever that is---the code below 14853794Sbostic * does not care as long as quad.h does its part of the bargain---but 14953794Sbostic * typically N==16). 15053794Sbostic * 15153794Sbostic * We use the same algorithm from Knuth, but this time the modulo refinement 15253794Sbostic * does not apply. On the other hand, since N is half the size of a long, 15353794Sbostic * we can get away with native multiplication---none of our input terms 15453794Sbostic * exceeds (ULONG_MAX >> 1). 15553794Sbostic * 15653794Sbostic * Note that, for u_long l, the quad-precision result 15753794Sbostic * 15853794Sbostic * l << N 15953794Sbostic * 16053794Sbostic * splits into high and low longs as HHALF(l) and LHUP(l) respectively. 16153794Sbostic */ 162*54431Sbostic static quad_t 16353794Sbostic __lmulq(u_long u, u_long v) 16451749Smckusick { 16553794Sbostic u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low; 16653794Sbostic u_long prodh, prodl, was; 16753794Sbostic union uu prod; 16853794Sbostic int neg; 16951749Smckusick 17053794Sbostic u1 = HHALF(u); 17153794Sbostic u0 = LHALF(u); 17253794Sbostic v1 = HHALF(v); 17353794Sbostic v0 = LHALF(v); 17451749Smckusick 17553794Sbostic low = u0 * v0; 17651749Smckusick 17753794Sbostic /* This is the same small-number optimization as before. */ 17853794Sbostic if (u1 == 0 && v1 == 0) 17953794Sbostic return (low); 18051749Smckusick 18153794Sbostic if (u1 >= u0) 18253794Sbostic udiff = u1 - u0, neg = 0; 18353794Sbostic else 18453794Sbostic udiff = u0 - u1, neg = 1; 18553794Sbostic if (v0 >= v1) 18653794Sbostic vdiff = v0 - v1; 18753794Sbostic else 18853794Sbostic vdiff = v1 - v0, neg ^= 1; 18953794Sbostic mid = udiff * vdiff; 19051749Smckusick 19153794Sbostic high = u1 * v1; 19251749Smckusick 19353794Sbostic /* prod = (high << 2N) + (high << N); */ 19453794Sbostic prodh = high + HHALF(high); 19553794Sbostic prodl = LHUP(high); 19651749Smckusick 19753794Sbostic /* if (neg) prod -= mid << N; else prod += mid << N; */ 19853794Sbostic if (neg) { 19953794Sbostic was = prodl; 20053794Sbostic prodl -= LHUP(mid); 20153794Sbostic prodh -= HHALF(mid) + (prodl > was); 20253794Sbostic } else { 20353794Sbostic was = prodl; 20453794Sbostic prodl += LHUP(mid); 20553824Storek prodh += HHALF(mid) + (prodl < was); 20653794Sbostic } 20751750Smckusick 20853794Sbostic /* prod += low << N */ 20953794Sbostic was = prodl; 21053794Sbostic prodl += LHUP(low); 21153794Sbostic prodh += HHALF(low) + (prodl < was); 21253794Sbostic /* ... + low; */ 21353794Sbostic if ((prodl += low) < low) 21453794Sbostic prodh++; 21553794Sbostic 21653794Sbostic /* return 4N-bit product */ 21753794Sbostic prod.ul[H] = prodh; 21853794Sbostic prod.ul[L] = prodl; 21953794Sbostic return (prod.q); 22051749Smckusick } 221