fpu_div.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* $NetBSD: fpu_div.c,v 1.4 2005/12/11 12:18:42 christos 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. * All advertising materials mentioning features or use of this software
  13. * must display the following acknowledgement:
  14. * This product includes software developed by the University of
  15. * California, Lawrence Berkeley Laboratory.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. * 3. Neither the name of the University nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  30. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  38. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  39. * SUCH DAMAGE.
  40. */
  41. /*
  42. * Perform an FPU divide (return x / y).
  43. */
  44. #include <sys/types.h>
  45. #include <sys/systm.h>
  46. #include <machine/fpu.h>
  47. #include <powerpc/fpu/fpu_arith.h>
  48. #include <powerpc/fpu/fpu_emu.h>
  49. /*
  50. * Division of normal numbers is done as follows:
  51. *
  52. * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
  53. * If X and Y are the mantissas (1.bbbb's), the quotient is then:
  54. *
  55. * q = (X / Y) * 2^((x exponent) - (y exponent))
  56. *
  57. * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
  58. * will be in [0.5,2.0). Moreover, it will be less than 1.0 if and only
  59. * if X < Y. In that case, it will have to be shifted left one bit to
  60. * become a normal number, and the exponent decremented. Thus, the
  61. * desired exponent is:
  62. *
  63. * left_shift = x->fp_mant < y->fp_mant;
  64. * result_exp = x->fp_exp - y->fp_exp - left_shift;
  65. *
  66. * The quotient mantissa X/Y can then be computed one bit at a time
  67. * using the following algorithm:
  68. *
  69. * Q = 0; -- Initial quotient.
  70. * R = X; -- Initial remainder,
  71. * if (left_shift) -- but fixed up in advance.
  72. * R *= 2;
  73. * for (bit = FP_NMANT; --bit >= 0; R *= 2) {
  74. * if (R >= Y) {
  75. * Q |= 1 << bit;
  76. * R -= Y;
  77. * }
  78. * }
  79. *
  80. * The subtraction R -= Y always removes the uppermost bit from R (and
  81. * can sometimes remove additional lower-order 1 bits); this proof is
  82. * left to the reader.
  83. *
  84. * This loop correctly calculates the guard and round bits since they are
  85. * included in the expanded internal representation. The sticky bit
  86. * is to be set if and only if any other bits beyond guard and round
  87. * would be set. From the above it is obvious that this is true if and
  88. * only if the remainder R is nonzero when the loop terminates.
  89. *
  90. * Examining the loop above, we can see that the quotient Q is built
  91. * one bit at a time ``from the top down''. This means that we can
  92. * dispense with the multi-word arithmetic and just build it one word
  93. * at a time, writing each result word when it is done.
  94. *
  95. * Furthermore, since X and Y are both in [1.0,2.0), we know that,
  96. * initially, R >= Y. (Recall that, if X < Y, R is set to X * 2 and
  97. * is therefore at in [2.0,4.0).) Thus Q is sure to have bit FP_NMANT-1
  98. * set, and R can be set initially to either X - Y (when X >= Y) or
  99. * 2X - Y (when X < Y). In addition, comparing R and Y is difficult,
  100. * so we will simply calculate R - Y and see if that underflows.
  101. * This leads to the following revised version of the algorithm:
  102. *
  103. * R = X;
  104. * bit = FP_1;
  105. * D = R - Y;
  106. * if (D >= 0) {
  107. * result_exp = x->fp_exp - y->fp_exp;
  108. * R = D;
  109. * q = bit;
  110. * bit >>= 1;
  111. * } else {
  112. * result_exp = x->fp_exp - y->fp_exp - 1;
  113. * q = 0;
  114. * }
  115. * R <<= 1;
  116. * do {
  117. * D = R - Y;
  118. * if (D >= 0) {
  119. * q |= bit;
  120. * R = D;
  121. * }
  122. * R <<= 1;
  123. * } while ((bit >>= 1) != 0);
  124. * Q[0] = q;
  125. * for (i = 1; i < 4; i++) {
  126. * q = 0, bit = 1 << 31;
  127. * do {
  128. * D = R - Y;
  129. * if (D >= 0) {
  130. * q |= bit;
  131. * R = D;
  132. * }
  133. * R <<= 1;
  134. * } while ((bit >>= 1) != 0);
  135. * Q[i] = q;
  136. * }
  137. *
  138. * This can be refined just a bit further by moving the `R <<= 1'
  139. * calculations to the front of the do-loops and eliding the first one.
  140. * The process can be terminated immediately whenever R becomes 0, but
  141. * this is relatively rare, and we do not bother.
  142. */
  143. struct fpn *
  144. fpu_div(struct fpemu *fe)
  145. {
  146. struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
  147. u_int q, bit;
  148. u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;
  149. FPU_DECL_CARRY
  150. /*
  151. * Since divide is not commutative, we cannot just use ORDER.
  152. * Check either operand for NaN first; if there is at least one,
  153. * order the signalling one (if only one) onto the right, then
  154. * return it. Otherwise we have the following cases:
  155. *
  156. * Inf / Inf = NaN, plus NV exception
  157. * Inf / num = Inf [i.e., return x]
  158. * Inf / 0 = Inf [i.e., return x]
  159. * 0 / Inf = 0 [i.e., return x]
  160. * 0 / num = 0 [i.e., return x]
  161. * 0 / 0 = NaN, plus NV exception
  162. * num / Inf = 0
  163. * num / num = num (do the divide)
  164. * num / 0 = Inf, plus DZ exception
  165. */
  166. DPRINTF(FPE_REG, ("fpu_div:\n"));
  167. DUMPFPN(FPE_REG, x);
  168. DUMPFPN(FPE_REG, y);
  169. DPRINTF(FPE_REG, ("=>\n"));
  170. if (ISNAN(x) || ISNAN(y)) {
  171. ORDER(x, y);
  172. fe->fe_cx |= FPSCR_VXSNAN;
  173. DUMPFPN(FPE_REG, y);
  174. return (y);
  175. }
  176. /*
  177. * Need to split the following out cause they generate different
  178. * exceptions.
  179. */
  180. if (ISINF(x)) {
  181. if (x->fp_class == y->fp_class) {
  182. fe->fe_cx |= FPSCR_VXIDI;
  183. return (fpu_newnan(fe));
  184. }
  185. DUMPFPN(FPE_REG, x);
  186. return (x);
  187. }
  188. if (ISZERO(x)) {
  189. fe->fe_cx |= FPSCR_ZX;
  190. if (x->fp_class == y->fp_class) {
  191. fe->fe_cx |= FPSCR_VXZDZ;
  192. return (fpu_newnan(fe));
  193. }
  194. DUMPFPN(FPE_REG, x);
  195. return (x);
  196. }
  197. /* all results at this point use XOR of operand signs */
  198. x->fp_sign ^= y->fp_sign;
  199. if (ISINF(y)) {
  200. x->fp_class = FPC_ZERO;
  201. DUMPFPN(FPE_REG, x);
  202. return (x);
  203. }
  204. if (ISZERO(y)) {
  205. fe->fe_cx = FPSCR_ZX;
  206. x->fp_class = FPC_INF;
  207. DUMPFPN(FPE_REG, x);
  208. return (x);
  209. }
  210. /*
  211. * Macros for the divide. See comments at top for algorithm.
  212. * Note that we expand R, D, and Y here.
  213. */
  214. #define SUBTRACT /* D = R - Y */ \
  215. FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \
  216. FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
  217. #define NONNEGATIVE /* D >= 0 */ \
  218. ((int)d0 >= 0)
  219. #ifdef FPU_SHL1_BY_ADD
  220. #define SHL1 /* R <<= 1 */ \
  221. FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \
  222. FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
  223. #else
  224. #define SHL1 \
  225. r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
  226. r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1
  227. #endif
  228. #define LOOP /* do ... while (bit >>= 1) */ \
  229. do { \
  230. SHL1; \
  231. SUBTRACT; \
  232. if (NONNEGATIVE) { \
  233. q |= bit; \
  234. r0 = d0, r1 = d1, r2 = d2, r3 = d3; \
  235. } \
  236. } while ((bit >>= 1) != 0)
  237. #define WORD(r, i) /* calculate r->fp_mant[i] */ \
  238. q = 0; \
  239. bit = 1 << 31; \
  240. LOOP; \
  241. (x)->fp_mant[i] = q
  242. /* Setup. Note that we put our result in x. */
  243. r0 = x->fp_mant[0];
  244. r1 = x->fp_mant[1];
  245. r2 = x->fp_mant[2];
  246. r3 = x->fp_mant[3];
  247. y0 = y->fp_mant[0];
  248. y1 = y->fp_mant[1];
  249. y2 = y->fp_mant[2];
  250. y3 = y->fp_mant[3];
  251. bit = FP_1;
  252. SUBTRACT;
  253. if (NONNEGATIVE) {
  254. x->fp_exp -= y->fp_exp;
  255. r0 = d0, r1 = d1, r2 = d2, r3 = d3;
  256. q = bit;
  257. bit >>= 1;
  258. } else {
  259. x->fp_exp -= y->fp_exp + 1;
  260. q = 0;
  261. }
  262. LOOP;
  263. x->fp_mant[0] = q;
  264. WORD(x, 1);
  265. WORD(x, 2);
  266. WORD(x, 3);
  267. x->fp_sticky = r0 | r1 | r2 | r3;
  268. DUMPFPN(FPE_REG, x);
  269. return (x);
  270. }