muldi3.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include "quad.h"
  34. /*
  35. * Multiply two quads.
  36. *
  37. * Our algorithm is based on the following. Split incoming quad values
  38. * u and v (where u,v >= 0) into
  39. *
  40. * u = 2^n u1 * u0 (n = number of bits in `u_int', usu. 32)
  41. *
  42. * and
  43. *
  44. * v = 2^n v1 * v0
  45. *
  46. * Then
  47. *
  48. * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
  49. * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
  50. *
  51. * Now add 2^n u1 v1 to the first term and subtract it from the middle,
  52. * and add 2^n u0 v0 to the last term and subtract it from the middle.
  53. * This gives:
  54. *
  55. * uv = (2^2n + 2^n) (u1 v1) +
  56. * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
  57. * (2^n + 1) (u0 v0)
  58. *
  59. * Factoring the middle a bit gives us:
  60. *
  61. * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
  62. * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
  63. * (2^n + 1) (u0 v0) [u0v0 = low]
  64. *
  65. * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
  66. * in just half the precision of the original. (Note that either or both
  67. * of (u1 - u0) or (v0 - v1) may be negative.)
  68. *
  69. * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
  70. *
  71. * Since C does not give us a `int * int = quad' operator, we split
  72. * our input quads into two ints, then split the two ints into two
  73. * shorts. We can then calculate `short * short = int' in native
  74. * arithmetic.
  75. *
  76. * Our product should, strictly speaking, be a `long quad', with 128
  77. * bits, but we are going to discard the upper 64. In other words,
  78. * we are not interested in uv, but rather in (uv mod 2^2n). This
  79. * makes some of the terms above vanish, and we get:
  80. *
  81. * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
  82. *
  83. * or
  84. *
  85. * (2^n)(high + mid + low) + low
  86. *
  87. * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
  88. * of 2^n in either one will also vanish. Only `low' need be computed
  89. * mod 2^2n, and only because of the final term above.
  90. */
  91. static quad_t __lmulq(u_int, u_int);
  92. quad_t
  93. __muldi3(a, b)
  94. quad_t a, b;
  95. {
  96. union uu u, v, low, prod;
  97. u_int high, mid, udiff, vdiff;
  98. int negall, negmid;
  99. #define u1 u.ul[H]
  100. #define u0 u.ul[L]
  101. #define v1 v.ul[H]
  102. #define v0 v.ul[L]
  103. /*
  104. * Get u and v such that u, v >= 0. When this is finished,
  105. * u1, u0, v1, and v0 will be directly accessible through the
  106. * int fields.
  107. */
  108. if (a >= 0)
  109. u.q = a, negall = 0;
  110. else
  111. u.q = -a, negall = 1;
  112. if (b >= 0)
  113. v.q = b;
  114. else
  115. v.q = -b, negall ^= 1;
  116. if (u1 == 0 && v1 == 0) {
  117. /*
  118. * An (I hope) important optimization occurs when u1 and v1
  119. * are both 0. This should be common since most numbers
  120. * are small. Here the product is just u0*v0.
  121. */
  122. prod.q = __lmulq(u0, v0);
  123. } else {
  124. /*
  125. * Compute the three intermediate products, remembering
  126. * whether the middle term is negative. We can discard
  127. * any upper bits in high and mid, so we can use native
  128. * u_int * u_int => u_int arithmetic.
  129. */
  130. low.q = __lmulq(u0, v0);
  131. if (u1 >= u0)
  132. negmid = 0, udiff = u1 - u0;
  133. else
  134. negmid = 1, udiff = u0 - u1;
  135. if (v0 >= v1)
  136. vdiff = v0 - v1;
  137. else
  138. vdiff = v1 - v0, negmid ^= 1;
  139. mid = udiff * vdiff;
  140. high = u1 * v1;
  141. /*
  142. * Assemble the final product.
  143. */
  144. prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
  145. low.ul[H];
  146. prod.ul[L] = low.ul[L];
  147. }
  148. return (negall ? -prod.q : prod.q);
  149. #undef u1
  150. #undef u0
  151. #undef v1
  152. #undef v0
  153. }
  154. /*
  155. * Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half
  156. * the number of bits in an int (whatever that is---the code below
  157. * does not care as long as quad.h does its part of the bargain---but
  158. * typically N==16).
  159. *
  160. * We use the same algorithm from Knuth, but this time the modulo refinement
  161. * does not apply. On the other hand, since N is half the size of an int,
  162. * we can get away with native multiplication---none of our input terms
  163. * exceeds (UINT_MAX >> 1).
  164. *
  165. * Note that, for u_int l, the quad-precision result
  166. *
  167. * l << N
  168. *
  169. * splits into high and low ints as HHALF(l) and LHUP(l) respectively.
  170. */
  171. static quad_t
  172. __lmulq(u_int u, u_int v)
  173. {
  174. u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low;
  175. u_int prodh, prodl, was;
  176. union uu prod;
  177. int neg;
  178. u1 = HHALF(u);
  179. u0 = LHALF(u);
  180. v1 = HHALF(v);
  181. v0 = LHALF(v);
  182. low = u0 * v0;
  183. /* This is the same small-number optimization as before. */
  184. if (u1 == 0 && v1 == 0)
  185. return (low);
  186. if (u1 >= u0)
  187. udiff = u1 - u0, neg = 0;
  188. else
  189. udiff = u0 - u1, neg = 1;
  190. if (v0 >= v1)
  191. vdiff = v0 - v1;
  192. else
  193. vdiff = v1 - v0, neg ^= 1;
  194. mid = udiff * vdiff;
  195. high = u1 * v1;
  196. /* prod = (high << 2N) + (high << N); */
  197. prodh = high + HHALF(high);
  198. prodl = LHUP(high);
  199. /* if (neg) prod -= mid << N; else prod += mid << N; */
  200. if (neg) {
  201. was = prodl;
  202. prodl -= LHUP(mid);
  203. prodh -= HHALF(mid) + (prodl > was);
  204. } else {
  205. was = prodl;
  206. prodl += LHUP(mid);
  207. prodh += HHALF(mid) + (prodl < was);
  208. }
  209. /* prod += low << N */
  210. was = prodl;
  211. prodl += LHUP(low);
  212. prodh += HHALF(low) + (prodl < was);
  213. /* ... + low; */
  214. if ((prodl += low) < low)
  215. prodh++;
  216. /* return 4N-bit product */
  217. prod.ul[H] = prodh;
  218. prod.ul[L] = prodl;
  219. return (prod.q);
  220. }