fpu_subr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* $NetBSD: fpu_subr.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. * FPU subroutines.
  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. * Shift the given number right rsh bits. Any bits that `fall off' will get
  51. * shoved into the sticky field; we return the resulting sticky. Note that
  52. * shifting NaNs is legal (this will never shift all bits out); a NaN's
  53. * sticky field is ignored anyway.
  54. */
  55. int
  56. fpu_shr(struct fpn *fp, int rsh)
  57. {
  58. u_int m0, m1, m2, m3, s;
  59. int lsh;
  60. #ifdef DIAGNOSTIC
  61. if (rsh <= 0 || (fp->fp_class != FPC_NUM && !ISNAN(fp)))
  62. panic("fpu_rightshift 1");
  63. #endif
  64. m0 = fp->fp_mant[0];
  65. m1 = fp->fp_mant[1];
  66. m2 = fp->fp_mant[2];
  67. m3 = fp->fp_mant[3];
  68. /* If shifting all the bits out, take a shortcut. */
  69. if (rsh >= FP_NMANT) {
  70. #ifdef DIAGNOSTIC
  71. if ((m0 | m1 | m2 | m3) == 0)
  72. panic("fpu_rightshift 2");
  73. #endif
  74. fp->fp_mant[0] = 0;
  75. fp->fp_mant[1] = 0;
  76. fp->fp_mant[2] = 0;
  77. fp->fp_mant[3] = 0;
  78. #ifdef notdef
  79. if ((m0 | m1 | m2 | m3) == 0)
  80. fp->fp_class = FPC_ZERO;
  81. else
  82. #endif
  83. fp->fp_sticky = 1;
  84. return (1);
  85. }
  86. /* Squish out full words. */
  87. s = fp->fp_sticky;
  88. if (rsh >= 32 * 3) {
  89. s |= m3 | m2 | m1;
  90. m3 = m0, m2 = 0, m1 = 0, m0 = 0;
  91. } else if (rsh >= 32 * 2) {
  92. s |= m3 | m2;
  93. m3 = m1, m2 = m0, m1 = 0, m0 = 0;
  94. } else if (rsh >= 32) {
  95. s |= m3;
  96. m3 = m2, m2 = m1, m1 = m0, m0 = 0;
  97. }
  98. /* Handle any remaining partial word. */
  99. if ((rsh &= 31) != 0) {
  100. lsh = 32 - rsh;
  101. s |= m3 << lsh;
  102. m3 = (m3 >> rsh) | (m2 << lsh);
  103. m2 = (m2 >> rsh) | (m1 << lsh);
  104. m1 = (m1 >> rsh) | (m0 << lsh);
  105. m0 >>= rsh;
  106. }
  107. fp->fp_mant[0] = m0;
  108. fp->fp_mant[1] = m1;
  109. fp->fp_mant[2] = m2;
  110. fp->fp_mant[3] = m3;
  111. fp->fp_sticky = s;
  112. return (s);
  113. }
  114. /*
  115. * Force a number to be normal, i.e., make its fraction have all zero
  116. * bits before FP_1, then FP_1, then all 1 bits. This is used for denorms
  117. * and (sometimes) for intermediate results.
  118. *
  119. * Internally, this may use a `supernormal' -- a number whose fp_mant
  120. * is greater than or equal to 2.0 -- so as a side effect you can hand it
  121. * a supernormal and it will fix it (provided fp->fp_mant[3] == 0).
  122. */
  123. void
  124. fpu_norm(struct fpn *fp)
  125. {
  126. u_int m0, m1, m2, m3, top, sup, nrm;
  127. int lsh, rsh, exp;
  128. exp = fp->fp_exp;
  129. m0 = fp->fp_mant[0];
  130. m1 = fp->fp_mant[1];
  131. m2 = fp->fp_mant[2];
  132. m3 = fp->fp_mant[3];
  133. /* Handle severe subnormals with 32-bit moves. */
  134. if (m0 == 0) {
  135. if (m1)
  136. m0 = m1, m1 = m2, m2 = m3, m3 = 0, exp -= 32;
  137. else if (m2)
  138. m0 = m2, m1 = m3, m2 = 0, m3 = 0, exp -= 2 * 32;
  139. else if (m3)
  140. m0 = m3, m1 = 0, m2 = 0, m3 = 0, exp -= 3 * 32;
  141. else {
  142. fp->fp_class = FPC_ZERO;
  143. return;
  144. }
  145. }
  146. /* Now fix any supernormal or remaining subnormal. */
  147. nrm = FP_1;
  148. sup = nrm << 1;
  149. if (m0 >= sup) {
  150. /*
  151. * We have a supernormal number. We need to shift it right.
  152. * We may assume m3==0.
  153. */
  154. for (rsh = 1, top = m0 >> 1; top >= sup; rsh++) /* XXX slow */
  155. top >>= 1;
  156. exp += rsh;
  157. lsh = 32 - rsh;
  158. m3 = m2 << lsh;
  159. m2 = (m2 >> rsh) | (m1 << lsh);
  160. m1 = (m1 >> rsh) | (m0 << lsh);
  161. m0 = top;
  162. } else if (m0 < nrm) {
  163. /*
  164. * We have a regular denorm (a subnormal number), and need
  165. * to shift it left.
  166. */
  167. for (lsh = 1, top = m0 << 1; top < nrm; lsh++) /* XXX slow */
  168. top <<= 1;
  169. exp -= lsh;
  170. rsh = 32 - lsh;
  171. m0 = top | (m1 >> rsh);
  172. m1 = (m1 << lsh) | (m2 >> rsh);
  173. m2 = (m2 << lsh) | (m3 >> rsh);
  174. m3 <<= lsh;
  175. }
  176. fp->fp_exp = exp;
  177. fp->fp_mant[0] = m0;
  178. fp->fp_mant[1] = m1;
  179. fp->fp_mant[2] = m2;
  180. fp->fp_mant[3] = m3;
  181. }
  182. /*
  183. * Concoct a `fresh' Quiet NaN per Appendix N.
  184. * As a side effect, we set NV (invalid) for the current exceptions.
  185. */
  186. struct fpn *
  187. fpu_newnan(struct fpemu *fe)
  188. {
  189. struct fpn *fp;
  190. fe->fe_cx |= FPSCR_VXSNAN;
  191. fp = &fe->fe_f3;
  192. fp->fp_class = FPC_QNAN;
  193. fp->fp_sign = 0;
  194. fp->fp_mant[0] = FP_1 - 1;
  195. fp->fp_mant[1] = fp->fp_mant[2] = fp->fp_mant[3] = ~0;
  196. DUMPFPN(FPE_REG, fp);
  197. return (fp);
  198. }