muldi3.c 7.0 KB

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