ieee754dp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* IEEE754 floating point arithmetic
  2. * double precision: common utilities
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. *
  8. * This program is free software; you can distribute it and/or modify it
  9. * under the terms of the GNU General Public License (Version 2) as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include <linux/compiler.h>
  22. #include "ieee754dp.h"
  23. int ieee754dp_class(union ieee754dp x)
  24. {
  25. COMPXDP;
  26. EXPLODEXDP;
  27. return xc;
  28. }
  29. int ieee754dp_isnan(union ieee754dp x)
  30. {
  31. return ieee754dp_class(x) >= IEEE754_CLASS_SNAN;
  32. }
  33. static inline int ieee754dp_issnan(union ieee754dp x)
  34. {
  35. assert(ieee754dp_isnan(x));
  36. return ((DPMANT(x) & DP_MBIT(DP_FBITS-1)) == DP_MBIT(DP_FBITS-1));
  37. }
  38. union ieee754dp __cold ieee754dp_nanxcpt(union ieee754dp r)
  39. {
  40. assert(ieee754dp_isnan(r));
  41. if (!ieee754dp_issnan(r)) /* QNAN does not cause invalid op !! */
  42. return r;
  43. if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION)) {
  44. /* not enabled convert to a quiet NaN */
  45. DPMANT(r) &= (~DP_MBIT(DP_FBITS-1));
  46. if (ieee754dp_isnan(r))
  47. return r;
  48. else
  49. return ieee754dp_indef();
  50. }
  51. return r;
  52. }
  53. static u64 ieee754dp_get_rounding(int sn, u64 xm)
  54. {
  55. /* inexact must round of 3 bits
  56. */
  57. if (xm & (DP_MBIT(3) - 1)) {
  58. switch (ieee754_csr.rm) {
  59. case FPU_CSR_RZ:
  60. break;
  61. case FPU_CSR_RN:
  62. xm += 0x3 + ((xm >> 3) & 1);
  63. /* xm += (xm&0x8)?0x4:0x3 */
  64. break;
  65. case FPU_CSR_RU: /* toward +Infinity */
  66. if (!sn) /* ?? */
  67. xm += 0x8;
  68. break;
  69. case FPU_CSR_RD: /* toward -Infinity */
  70. if (sn) /* ?? */
  71. xm += 0x8;
  72. break;
  73. }
  74. }
  75. return xm;
  76. }
  77. /* generate a normal/denormal number with over,under handling
  78. * sn is sign
  79. * xe is an unbiased exponent
  80. * xm is 3bit extended precision value.
  81. */
  82. union ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
  83. {
  84. assert(xm); /* we don't gen exact zeros (probably should) */
  85. assert((xm >> (DP_FBITS + 1 + 3)) == 0); /* no execess */
  86. assert(xm & (DP_HIDDEN_BIT << 3));
  87. if (xe < DP_EMIN) {
  88. /* strip lower bits */
  89. int es = DP_EMIN - xe;
  90. if (ieee754_csr.nod) {
  91. ieee754_setcx(IEEE754_UNDERFLOW);
  92. ieee754_setcx(IEEE754_INEXACT);
  93. switch(ieee754_csr.rm) {
  94. case FPU_CSR_RN:
  95. case FPU_CSR_RZ:
  96. return ieee754dp_zero(sn);
  97. case FPU_CSR_RU: /* toward +Infinity */
  98. if (sn == 0)
  99. return ieee754dp_min(0);
  100. else
  101. return ieee754dp_zero(1);
  102. case FPU_CSR_RD: /* toward -Infinity */
  103. if (sn == 0)
  104. return ieee754dp_zero(0);
  105. else
  106. return ieee754dp_min(1);
  107. }
  108. }
  109. if (xe == DP_EMIN - 1 &&
  110. ieee754dp_get_rounding(sn, xm) >> (DP_FBITS + 1 + 3))
  111. {
  112. /* Not tiny after rounding */
  113. ieee754_setcx(IEEE754_INEXACT);
  114. xm = ieee754dp_get_rounding(sn, xm);
  115. xm >>= 1;
  116. /* Clear grs bits */
  117. xm &= ~(DP_MBIT(3) - 1);
  118. xe++;
  119. }
  120. else {
  121. /* sticky right shift es bits
  122. */
  123. xm = XDPSRS(xm, es);
  124. xe += es;
  125. assert((xm & (DP_HIDDEN_BIT << 3)) == 0);
  126. assert(xe == DP_EMIN);
  127. }
  128. }
  129. if (xm & (DP_MBIT(3) - 1)) {
  130. ieee754_setcx(IEEE754_INEXACT);
  131. if ((xm & (DP_HIDDEN_BIT << 3)) == 0) {
  132. ieee754_setcx(IEEE754_UNDERFLOW);
  133. }
  134. /* inexact must round of 3 bits
  135. */
  136. xm = ieee754dp_get_rounding(sn, xm);
  137. /* adjust exponent for rounding add overflowing
  138. */
  139. if (xm >> (DP_FBITS + 3 + 1)) {
  140. /* add causes mantissa overflow */
  141. xm >>= 1;
  142. xe++;
  143. }
  144. }
  145. /* strip grs bits */
  146. xm >>= 3;
  147. assert((xm >> (DP_FBITS + 1)) == 0); /* no execess */
  148. assert(xe >= DP_EMIN);
  149. if (xe > DP_EMAX) {
  150. ieee754_setcx(IEEE754_OVERFLOW);
  151. ieee754_setcx(IEEE754_INEXACT);
  152. /* -O can be table indexed by (rm,sn) */
  153. switch (ieee754_csr.rm) {
  154. case FPU_CSR_RN:
  155. return ieee754dp_inf(sn);
  156. case FPU_CSR_RZ:
  157. return ieee754dp_max(sn);
  158. case FPU_CSR_RU: /* toward +Infinity */
  159. if (sn == 0)
  160. return ieee754dp_inf(0);
  161. else
  162. return ieee754dp_max(1);
  163. case FPU_CSR_RD: /* toward -Infinity */
  164. if (sn == 0)
  165. return ieee754dp_max(0);
  166. else
  167. return ieee754dp_inf(1);
  168. }
  169. }
  170. /* gen norm/denorm/zero */
  171. if ((xm & DP_HIDDEN_BIT) == 0) {
  172. /* we underflow (tiny/zero) */
  173. assert(xe == DP_EMIN);
  174. if (ieee754_csr.mx & IEEE754_UNDERFLOW)
  175. ieee754_setcx(IEEE754_UNDERFLOW);
  176. return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm);
  177. } else {
  178. assert((xm >> (DP_FBITS + 1)) == 0); /* no execess */
  179. assert(xm & DP_HIDDEN_BIT);
  180. return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
  181. }
  182. }