muldi3.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*-
  2. * Copyright (c) 1992, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This software was developed by the Computer Systems Engineering group
  6. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  7. * contributed to Berkeley.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * From:
  34. * @(#)muldi3.c 8.1 (Berkeley) 6/4/93
  35. * NetBSD: muldi3.c,v 1.1 2005/12/20 19:28:51 christos Exp
  36. */
  37. #include "longlong.h"
  38. /*
  39. * Multiply two long longs.
  40. *
  41. * Our algorithm is based on the following. Split incoming long long
  42. * values u and v (where u,v >= 0) into
  43. *
  44. * u = 2^n u1 * u0 (n = number of bits in `unsigned int', usu. 32)
  45. *
  46. * and
  47. *
  48. * v = 2^n v1 * v0
  49. *
  50. * Then
  51. *
  52. * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
  53. * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
  54. *
  55. * Now add 2^n u1 v1 to the first term and subtract it from the middle,
  56. * and add 2^n u0 v0 to the last term and subtract it from the middle.
  57. * This gives:
  58. *
  59. * uv = (2^2n + 2^n) (u1 v1) +
  60. * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
  61. * (2^n + 1) (u0 v0)
  62. *
  63. * Factoring the middle a bit gives us:
  64. *
  65. * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
  66. * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
  67. * (2^n + 1) (u0 v0) [u0v0 = low]
  68. *
  69. * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
  70. * in just half the precision of the original. (Note that either or both
  71. * of (u1 - u0) or (v0 - v1) may be negative.)
  72. *
  73. * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
  74. *
  75. * Since C does not give us a `int * int = long long' operator, we split
  76. * our input long longs into two ints, then split the two ints into two
  77. * shorts. We can then calculate `short * short = int' in native
  78. * arithmetic.
  79. *
  80. * Our product should, strictly speaking, be a `long long long', with
  81. * 128 bits, but we are going to discard the upper 64. In other words,
  82. * we are not interested in uv, but rather in (uv mod 2^2n). This
  83. * makes some of the terms above vanish, and we get:
  84. *
  85. * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
  86. *
  87. * or
  88. *
  89. * (2^n)(high + mid + low) + low
  90. *
  91. * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
  92. * of 2^n in either one will also vanish. Only `low' need be computed
  93. * mod 2^2n, and only because of the final term above.
  94. */
  95. static long long __lmulq(unsigned int, unsigned int);
  96. long long
  97. __muldi3(long long a, long long b)
  98. {
  99. union uu u, v, low, prod;
  100. unsigned int high, mid, udiff, vdiff;
  101. int negall, negmid;
  102. #define u1 u.ui[H]
  103. #define u0 u.ui[L]
  104. #define v1 v.ui[H]
  105. #define v0 v.ui[L]
  106. /*
  107. * Get u and v such that u, v >= 0. When this is finished,
  108. * u1, u0, v1, and v0 will be directly accessible through the
  109. * int fields.
  110. */
  111. if (a >= 0)
  112. u.ll = a, negall = 0;
  113. else
  114. u.ll = -a, negall = 1;
  115. if (b >= 0)
  116. v.ll = b;
  117. else
  118. v.ll = -b, negall ^= 1;
  119. if (u1 == 0 && v1 == 0) {
  120. /*
  121. * An (I hope) important optimization occurs when u1 and v1
  122. * are both 0. This should be common since most numbers
  123. * are small. Here the product is just u0*v0.
  124. */
  125. prod.ll = __lmulq(u0, v0);
  126. } else {
  127. /*
  128. * Compute the three intermediate products, remembering
  129. * whether the middle term is negative. We can discard
  130. * any upper bits in high and mid, so we can use native
  131. * unsigned int * unsigned int => unsigned int arithmetic.
  132. */
  133. low.ll = __lmulq(u0, v0);
  134. if (u1 >= u0)
  135. negmid = 0, udiff = u1 - u0;
  136. else
  137. negmid = 1, udiff = u0 - u1;
  138. if (v0 >= v1)
  139. vdiff = v0 - v1;
  140. else
  141. vdiff = v1 - v0, negmid ^= 1;
  142. mid = udiff * vdiff;
  143. high = u1 * v1;
  144. /*
  145. * Assemble the final product.
  146. */
  147. prod.ui[H] = high + (negmid ? -mid : mid) + low.ui[L] +
  148. low.ui[H];
  149. prod.ui[L] = low.ui[L];
  150. }
  151. return (negall ? -prod.ll : prod.ll);
  152. #undef u1
  153. #undef u0
  154. #undef v1
  155. #undef v0
  156. }
  157. /*
  158. * Multiply two 2N-bit ints to produce a 4N-bit long long, where N is
  159. * half the number of bits in an int (whatever that is---the code
  160. * below does not care as long as the header file does its part of the
  161. * bargain---but typically N==16).
  162. *
  163. * We use the same algorithm from Knuth, but this time the modulo refinement
  164. * does not apply. On the other hand, since N is half the size of an int,
  165. * we can get away with native multiplication---none of our input terms
  166. * exceeds (UINT_MAX >> 1).
  167. *
  168. * Note that, for unsigned int l, the quad-precision (long long) result
  169. *
  170. * l << N
  171. *
  172. * splits into high and low ints as HHALF(l) and LHUP(l) respectively.
  173. */
  174. static long long
  175. __lmulq(unsigned int u, unsigned int v)
  176. {
  177. unsigned int u1, u0, v1, v0, udiff, vdiff, high, mid, low;
  178. unsigned int prodh, prodl, was;
  179. union uu prod;
  180. int neg;
  181. u1 = HHALF(u);
  182. u0 = LHALF(u);
  183. v1 = HHALF(v);
  184. v0 = LHALF(v);
  185. low = u0 * v0;
  186. /* This is the same small-number optimization as before. */
  187. if (u1 == 0 && v1 == 0)
  188. return (low);
  189. if (u1 >= u0)
  190. udiff = u1 - u0, neg = 0;
  191. else
  192. udiff = u0 - u1, neg = 1;
  193. if (v0 >= v1)
  194. vdiff = v0 - v1;
  195. else
  196. vdiff = v1 - v0, neg ^= 1;
  197. mid = udiff * vdiff;
  198. high = u1 * v1;
  199. /* prod = (high << 2N) + (high << N); */
  200. prodh = high + HHALF(high);
  201. prodl = LHUP(high);
  202. /* if (neg) prod -= mid << N; else prod += mid << N; */
  203. if (neg) {
  204. was = prodl;
  205. prodl -= LHUP(mid);
  206. prodh -= HHALF(mid) + (prodl > was);
  207. } else {
  208. was = prodl;
  209. prodl += LHUP(mid);
  210. prodh += HHALF(mid) + (prodl < was);
  211. }
  212. /* prod += low << N */
  213. was = prodl;
  214. prodl += LHUP(low);
  215. prodh += HHALF(low) + (prodl < was);
  216. /* ... + low; */
  217. if ((prodl += low) < low)
  218. prodh++;
  219. /* return 4N-bit product */
  220. prod.ui[H] = prodh;
  221. prod.ui[L] = prodl;
  222. return (prod.ll);
  223. }