ieee754dp.c 4.6 KB

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