ieee754sp.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* IEEE754 floating point arithmetic
  2.  * single precision
  3.  */
  4. /*
  5.  * MIPS floating point support
  6.  * Copyright (C) 1994-2000 Algorithmics Ltd.  All rights reserved.
  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. return ax.rv.sp;
  53. }
  54. ieee754sp ieee754sp_nanxcpt(ieee754sp r, const char *op, ...)
  55. {
  56. struct ieee754xctx ax;
  57. assert(ieee754sp_isnan(r));
  58. if (!ieee754sp_issnan(r)) /* QNAN does not cause invalid op !! */
  59. return r;
  60. if (!SETCX(IEEE754_INVALID_OPERATION)) {
  61. /* not enabled convert to a quiet NaN */
  62. SPMANT(r) &= (~SP_MBIT(SP_MBITS-1));
  63. if (ieee754sp_isnan(r))
  64. return r;
  65. else
  66. return ieee754sp_indef();
  67. }
  68. ax.op = op;
  69. ax.rt = 0;
  70. ax.rv.sp = r;
  71. va_start(ax.ap, op);
  72. ieee754_xcpt(&ax);
  73. return ax.rv.sp;
  74. }
  75. ieee754sp ieee754sp_bestnan(ieee754sp x, ieee754sp y)
  76. {
  77. assert(ieee754sp_isnan(x));
  78. assert(ieee754sp_isnan(y));
  79. if (SPMANT(x) > SPMANT(y))
  80. return x;
  81. else
  82. return y;
  83. }
  84. static unsigned get_rounding(int sn, unsigned xm)
  85. {
  86. /* inexact must round of 3 bits
  87.  */
  88. if (xm & (SP_MBIT(3) - 1)) {
  89. switch (ieee754_csr.rm) {
  90. case IEEE754_RZ:
  91. break;
  92. case IEEE754_RN:
  93. xm += 0x3 + ((xm >> 3) & 1);
  94. /* xm += (xm&0x8)?0x4:0x3 */
  95. break;
  96. case IEEE754_RU: /* toward +Infinity */
  97. if (!sn) /* ?? */
  98. xm += 0x8;
  99. break;
  100. case IEEE754_RD: /* toward -Infinity */
  101. if (sn) /* ?? */
  102. xm += 0x8;
  103. break;
  104. }
  105. }
  106. return xm;
  107. }
  108. /* generate a normal/denormal number with over,under handeling
  109.  * sn is sign
  110.  * xe is an unbiased exponent
  111.  * xm is 3bit extended precision value.
  112.  */
  113. ieee754sp ieee754sp_format(int sn, int xe, unsigned xm)
  114. {
  115. assert(xm); /* we dont gen exact zeros (probably should) */
  116. assert((xm >> (SP_MBITS + 1 + 3)) == 0); /* no execess */
  117. assert(xm & (SP_HIDDEN_BIT << 3));
  118. if (xe < SP_EMIN) {
  119. /* strip lower bits */
  120. int es = SP_EMIN - xe;
  121. if (ieee754_csr.nod) {
  122. SETCX(IEEE754_UNDERFLOW);
  123. SETCX(IEEE754_INEXACT);
  124. switch(ieee754_csr.rm) {
  125. case IEEE754_RN:
  126. return ieee754sp_zero(sn);
  127. case IEEE754_RZ:
  128. return ieee754sp_zero(sn);
  129. case IEEE754_RU:      /* toward +Infinity */
  130. if(sn == 0)
  131. return ieee754sp_min(0);
  132. else
  133. return ieee754sp_zero(1);
  134. case IEEE754_RD:      /* toward -Infinity */
  135. if(sn == 0)
  136. return ieee754sp_zero(0);
  137. else
  138. return ieee754sp_min(1);
  139. }
  140. }
  141. if (xe == SP_EMIN - 1
  142. && get_rounding(sn, xm) >> (SP_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 &= ~(SP_MBIT(3) - 1);
  150. xe++;
  151. }
  152. else {
  153. /* sticky right shift es bits
  154.  */
  155. SPXSRSXn(es);
  156. assert((xm & (SP_HIDDEN_BIT << 3)) == 0);
  157. assert(xe == SP_EMIN);
  158. }
  159. }
  160. if (xm & (SP_MBIT(3) - 1)) {
  161. SETCX(IEEE754_INEXACT);
  162. if ((xm & (SP_HIDDEN_BIT << 3)) == 0) {
  163. SETCX(IEEE754_UNDERFLOW);
  164. }
  165. /* inexact must round of 3 bits
  166.  */
  167. xm = get_rounding(sn, xm);
  168. /* adjust exponent for rounding add overflowing
  169.  */
  170. if (xm >> (SP_MBITS + 1 + 3)) {
  171. /* add causes mantissa overflow */
  172. xm >>= 1;
  173. xe++;
  174. }
  175. }
  176. /* strip grs bits */
  177. xm >>= 3;
  178. assert((xm >> (SP_MBITS + 1)) == 0); /* no execess */
  179. assert(xe >= SP_EMIN);
  180. if (xe > SP_EMAX) {
  181. SETCX(IEEE754_OVERFLOW);
  182. SETCX(IEEE754_INEXACT);
  183. /* -O can be table indexed by (rm,sn) */
  184. switch (ieee754_csr.rm) {
  185. case IEEE754_RN:
  186. return ieee754sp_inf(sn);
  187. case IEEE754_RZ:
  188. return ieee754sp_max(sn);
  189. case IEEE754_RU: /* toward +Infinity */
  190. if (sn == 0)
  191. return ieee754sp_inf(0);
  192. else
  193. return ieee754sp_max(1);
  194. case IEEE754_RD: /* toward -Infinity */
  195. if (sn == 0)
  196. return ieee754sp_max(0);
  197. else
  198. return ieee754sp_inf(1);
  199. }
  200. }
  201. /* gen norm/denorm/zero */
  202. if ((xm & SP_HIDDEN_BIT) == 0) {
  203. /* we underflow (tiny/zero) */
  204. assert(xe == SP_EMIN);
  205. if (ieee754_csr.mx & IEEE754_UNDERFLOW)
  206. SETCX(IEEE754_UNDERFLOW);
  207. return buildsp(sn, SP_EMIN - 1 + SP_EBIAS, xm);
  208. } else {
  209. assert((xm >> (SP_MBITS + 1)) == 0); /* no execess */
  210. assert(xm & SP_HIDDEN_BIT);
  211. return buildsp(sn, xe + SP_EBIAS, xm & ~SP_HIDDEN_BIT);
  212. }
  213. }