ieee754sp.c 5.3 KB

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