ieee754dp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * ########################################################################
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * ########################################################################
  24. */
  25. #include "ieee754dp.h"
  26. int ieee754dp_class(ieee754dp x)
  27. {
  28. COMPXDP;
  29. EXPLODEXDP;
  30. return xc;
  31. }
  32. int ieee754dp_isnan(ieee754dp x)
  33. {
  34. return ieee754dp_class(x) >= IEEE754_CLASS_SNAN;
  35. }
  36. int ieee754dp_issnan(ieee754dp x)
  37. {
  38. assert(ieee754dp_isnan(x));
  39. return ((DPMANT(x) & DP_MBIT(DP_MBITS-1)) == DP_MBIT(DP_MBITS-1));
  40. }
  41. ieee754dp ieee754dp_xcpt(ieee754dp r, const char *op, ...)
  42. {
  43. struct ieee754xctx ax;
  44. if (!TSTX())
  45. return r;
  46. ax.op = op;
  47. ax.rt = IEEE754_RT_DP;
  48. ax.rv.dp = r;
  49. va_start(ax.ap, op);
  50. ieee754_xcpt(&ax);
  51. va_end(ax.ap);
  52. return ax.rv.dp;
  53. }
  54. ieee754dp ieee754dp_nanxcpt(ieee754dp r, const char *op, ...)
  55. {
  56. struct ieee754xctx ax;
  57. assert(ieee754dp_isnan(r));
  58. if (!ieee754dp_issnan(r)) /* QNAN does not cause invalid op !! */
  59. return r;
  60. if (!SETANDTESTCX(IEEE754_INVALID_OPERATION)) {
  61. /* not enabled convert to a quiet NaN */
  62. DPMANT(r) &= (~DP_MBIT(DP_MBITS-1));
  63. if (ieee754dp_isnan(r))
  64. return r;
  65. else
  66. return ieee754dp_indef();
  67. }
  68. ax.op = op;
  69. ax.rt = 0;
  70. ax.rv.dp = r;
  71. va_start(ax.ap, op);
  72. ieee754_xcpt(&ax);
  73. va_end(ax.ap);
  74. return ax.rv.dp;
  75. }
  76. ieee754dp ieee754dp_bestnan(ieee754dp x, ieee754dp y)
  77. {
  78. assert(ieee754dp_isnan(x));
  79. assert(ieee754dp_isnan(y));
  80. if (DPMANT(x) > DPMANT(y))
  81. return x;
  82. else
  83. return y;
  84. }
  85. static u64 get_rounding(int sn, u64 xm)
  86. {
  87. /* inexact must round of 3 bits
  88. */
  89. if (xm & (DP_MBIT(3) - 1)) {
  90. switch (ieee754_csr.rm) {
  91. case IEEE754_RZ:
  92. break;
  93. case IEEE754_RN:
  94. xm += 0x3 + ((xm >> 3) & 1);
  95. /* xm += (xm&0x8)?0x4:0x3 */
  96. break;
  97. case IEEE754_RU: /* toward +Infinity */
  98. if (!sn) /* ?? */
  99. xm += 0x8;
  100. break;
  101. case IEEE754_RD: /* toward -Infinity */
  102. if (sn) /* ?? */
  103. xm += 0x8;
  104. break;
  105. }
  106. }
  107. return xm;
  108. }
  109. /* generate a normal/denormal number with over,under handling
  110. * sn is sign
  111. * xe is an unbiased exponent
  112. * xm is 3bit extended precision value.
  113. */
  114. ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
  115. {
  116. assert(xm); /* we don't gen exact zeros (probably should) */
  117. assert((xm >> (DP_MBITS + 1 + 3)) == 0); /* no execess */
  118. assert(xm & (DP_HIDDEN_BIT << 3));
  119. if (xe < DP_EMIN) {
  120. /* strip lower bits */
  121. int es = DP_EMIN - xe;
  122. if (ieee754_csr.nod) {
  123. SETCX(IEEE754_UNDERFLOW);
  124. SETCX(IEEE754_INEXACT);
  125. switch(ieee754_csr.rm) {
  126. case IEEE754_RN:
  127. case IEEE754_RZ:
  128. return ieee754dp_zero(sn);
  129. case IEEE754_RU: /* toward +Infinity */
  130. if(sn == 0)
  131. return ieee754dp_min(0);
  132. else
  133. return ieee754dp_zero(1);
  134. case IEEE754_RD: /* toward -Infinity */
  135. if(sn == 0)
  136. return ieee754dp_zero(0);
  137. else
  138. return ieee754dp_min(1);
  139. }
  140. }
  141. if (xe == DP_EMIN - 1
  142. && get_rounding(sn, xm) >> (DP_MBITS + 1 + 3))
  143. {
  144. /* Not tiny after rounding */
  145. SETCX(IEEE754_INEXACT);
  146. xm = get_rounding(sn, xm);
  147. xm >>= 1;
  148. /* Clear grs bits */
  149. xm &= ~(DP_MBIT(3) - 1);
  150. xe++;
  151. }
  152. else {
  153. /* sticky right shift es bits
  154. */
  155. xm = XDPSRS(xm, es);
  156. xe += es;
  157. assert((xm & (DP_HIDDEN_BIT << 3)) == 0);
  158. assert(xe == DP_EMIN);
  159. }
  160. }
  161. if (xm & (DP_MBIT(3) - 1)) {
  162. SETCX(IEEE754_INEXACT);
  163. if ((xm & (DP_HIDDEN_BIT << 3)) == 0) {
  164. SETCX(IEEE754_UNDERFLOW);
  165. }
  166. /* inexact must round of 3 bits
  167. */
  168. xm = get_rounding(sn, xm);
  169. /* adjust exponent for rounding add overflowing
  170. */
  171. if (xm >> (DP_MBITS + 3 + 1)) {
  172. /* add causes mantissa overflow */
  173. xm >>= 1;
  174. xe++;
  175. }
  176. }
  177. /* strip grs bits */
  178. xm >>= 3;
  179. assert((xm >> (DP_MBITS + 1)) == 0); /* no execess */
  180. assert(xe >= DP_EMIN);
  181. if (xe > DP_EMAX) {
  182. SETCX(IEEE754_OVERFLOW);
  183. SETCX(IEEE754_INEXACT);
  184. /* -O can be table indexed by (rm,sn) */
  185. switch (ieee754_csr.rm) {
  186. case IEEE754_RN:
  187. return ieee754dp_inf(sn);
  188. case IEEE754_RZ:
  189. return ieee754dp_max(sn);
  190. case IEEE754_RU: /* toward +Infinity */
  191. if (sn == 0)
  192. return ieee754dp_inf(0);
  193. else
  194. return ieee754dp_max(1);
  195. case IEEE754_RD: /* toward -Infinity */
  196. if (sn == 0)
  197. return ieee754dp_max(0);
  198. else
  199. return ieee754dp_inf(1);
  200. }
  201. }
  202. /* gen norm/denorm/zero */
  203. if ((xm & DP_HIDDEN_BIT) == 0) {
  204. /* we underflow (tiny/zero) */
  205. assert(xe == DP_EMIN);
  206. if (ieee754_csr.mx & IEEE754_UNDERFLOW)
  207. SETCX(IEEE754_UNDERFLOW);
  208. return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm);
  209. } else {
  210. assert((xm >> (DP_MBITS + 1)) == 0); /* no execess */
  211. assert(xm & DP_HIDDEN_BIT);
  212. return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
  213. }
  214. }