ieee754dp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. static inline int ieee754dp_isnan(union ieee754dp x)
  30. {
  31. return ieee754_class_nan(ieee754dp_class(x));
  32. }
  33. static inline int ieee754dp_issnan(union ieee754dp x)
  34. {
  35. int qbit;
  36. assert(ieee754dp_isnan(x));
  37. qbit = (DPMANT(x) & DP_MBIT(DP_FBITS - 1)) == DP_MBIT(DP_FBITS - 1);
  38. return ieee754_csr.nan2008 ^ qbit;
  39. }
  40. /*
  41. * Raise the Invalid Operation IEEE 754 exception
  42. * and convert the signaling NaN supplied to a quiet NaN.
  43. */
  44. union ieee754dp __cold ieee754dp_nanxcpt(union ieee754dp r)
  45. {
  46. assert(ieee754dp_issnan(r));
  47. ieee754_setcx(IEEE754_INVALID_OPERATION);
  48. if (ieee754_csr.nan2008) {
  49. DPMANT(r) |= DP_MBIT(DP_FBITS - 1);
  50. } else {
  51. DPMANT(r) &= ~DP_MBIT(DP_FBITS - 1);
  52. if (!ieee754dp_isnan(r))
  53. DPMANT(r) |= DP_MBIT(DP_FBITS - 2);
  54. }
  55. return r;
  56. }
  57. static u64 ieee754dp_get_rounding(int sn, u64 xm)
  58. {
  59. /* inexact must round of 3 bits
  60. */
  61. if (xm & (DP_MBIT(3) - 1)) {
  62. switch (ieee754_csr.rm) {
  63. case FPU_CSR_RZ:
  64. break;
  65. case FPU_CSR_RN:
  66. xm += 0x3 + ((xm >> 3) & 1);
  67. /* xm += (xm&0x8)?0x4:0x3 */
  68. break;
  69. case FPU_CSR_RU: /* toward +Infinity */
  70. if (!sn) /* ?? */
  71. xm += 0x8;
  72. break;
  73. case FPU_CSR_RD: /* toward -Infinity */
  74. if (sn) /* ?? */
  75. xm += 0x8;
  76. break;
  77. }
  78. }
  79. return xm;
  80. }
  81. /* generate a normal/denormal number with over,under handling
  82. * sn is sign
  83. * xe is an unbiased exponent
  84. * xm is 3bit extended precision value.
  85. */
  86. union ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
  87. {
  88. assert(xm); /* we don't gen exact zeros (probably should) */
  89. assert((xm >> (DP_FBITS + 1 + 3)) == 0); /* no excess */
  90. assert(xm & (DP_HIDDEN_BIT << 3));
  91. if (xe < DP_EMIN) {
  92. /* strip lower bits */
  93. int es = DP_EMIN - xe;
  94. if (ieee754_csr.nod) {
  95. ieee754_setcx(IEEE754_UNDERFLOW);
  96. ieee754_setcx(IEEE754_INEXACT);
  97. switch(ieee754_csr.rm) {
  98. case FPU_CSR_RN:
  99. case FPU_CSR_RZ:
  100. return ieee754dp_zero(sn);
  101. case FPU_CSR_RU: /* toward +Infinity */
  102. if (sn == 0)
  103. return ieee754dp_min(0);
  104. else
  105. return ieee754dp_zero(1);
  106. case FPU_CSR_RD: /* toward -Infinity */
  107. if (sn == 0)
  108. return ieee754dp_zero(0);
  109. else
  110. return ieee754dp_min(1);
  111. }
  112. }
  113. if (xe == DP_EMIN - 1 &&
  114. ieee754dp_get_rounding(sn, xm) >> (DP_FBITS + 1 + 3))
  115. {
  116. /* Not tiny after rounding */
  117. ieee754_setcx(IEEE754_INEXACT);
  118. xm = ieee754dp_get_rounding(sn, xm);
  119. xm >>= 1;
  120. /* Clear grs bits */
  121. xm &= ~(DP_MBIT(3) - 1);
  122. xe++;
  123. }
  124. else {
  125. /* sticky right shift es bits
  126. */
  127. xm = XDPSRS(xm, es);
  128. xe += es;
  129. assert((xm & (DP_HIDDEN_BIT << 3)) == 0);
  130. assert(xe == DP_EMIN);
  131. }
  132. }
  133. if (xm & (DP_MBIT(3) - 1)) {
  134. ieee754_setcx(IEEE754_INEXACT);
  135. if ((xm & (DP_HIDDEN_BIT << 3)) == 0) {
  136. ieee754_setcx(IEEE754_UNDERFLOW);
  137. }
  138. /* inexact must round of 3 bits
  139. */
  140. xm = ieee754dp_get_rounding(sn, xm);
  141. /* adjust exponent for rounding add overflowing
  142. */
  143. if (xm >> (DP_FBITS + 3 + 1)) {
  144. /* add causes mantissa overflow */
  145. xm >>= 1;
  146. xe++;
  147. }
  148. }
  149. /* strip grs bits */
  150. xm >>= 3;
  151. assert((xm >> (DP_FBITS + 1)) == 0); /* no excess */
  152. assert(xe >= DP_EMIN);
  153. if (xe > DP_EMAX) {
  154. ieee754_setcx(IEEE754_OVERFLOW);
  155. ieee754_setcx(IEEE754_INEXACT);
  156. /* -O can be table indexed by (rm,sn) */
  157. switch (ieee754_csr.rm) {
  158. case FPU_CSR_RN:
  159. return ieee754dp_inf(sn);
  160. case FPU_CSR_RZ:
  161. return ieee754dp_max(sn);
  162. case FPU_CSR_RU: /* toward +Infinity */
  163. if (sn == 0)
  164. return ieee754dp_inf(0);
  165. else
  166. return ieee754dp_max(1);
  167. case FPU_CSR_RD: /* toward -Infinity */
  168. if (sn == 0)
  169. return ieee754dp_max(0);
  170. else
  171. return ieee754dp_inf(1);
  172. }
  173. }
  174. /* gen norm/denorm/zero */
  175. if ((xm & DP_HIDDEN_BIT) == 0) {
  176. /* we underflow (tiny/zero) */
  177. assert(xe == DP_EMIN);
  178. if (ieee754_csr.mx & IEEE754_UNDERFLOW)
  179. ieee754_setcx(IEEE754_UNDERFLOW);
  180. return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm);
  181. } else {
  182. assert((xm >> (DP_FBITS + 1)) == 0); /* no excess */
  183. assert(xm & DP_HIDDEN_BIT);
  184. return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
  185. }
  186. }