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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* IEEE754 floating point arithmetic
  2.  * double precision: common utilities
  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 "ieee754dp.h"
  27. int ieee754dp_class(ieee754dp x)
  28. {
  29. COMPXDP;
  30. EXPLODEXDP;
  31. return xc;
  32. }
  33. int ieee754dp_isnan(ieee754dp x)
  34. {
  35. return ieee754dp_class(x) >= IEEE754_CLASS_SNAN;
  36. }
  37. int ieee754dp_issnan(ieee754dp x)
  38. {
  39. assert(ieee754dp_isnan(x));
  40. return ((DPMANT(x) & DP_MBIT(DP_MBITS-1)) == DP_MBIT(DP_MBITS-1));
  41. }
  42. ieee754dp ieee754dp_xcpt(ieee754dp r, const char *op, ...)
  43. {
  44. struct ieee754xctx ax;
  45. if (!TSTX())
  46. return r;
  47. ax.op = op;
  48. ax.rt = IEEE754_RT_DP;
  49. ax.rv.dp = r;
  50. va_start(ax.ap, op);
  51. ieee754_xcpt(&ax);
  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 (!SETCX(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. return ax.rv.dp;
  74. }
  75. ieee754dp ieee754dp_bestnan(ieee754dp x, ieee754dp y)
  76. {
  77. assert(ieee754dp_isnan(x));
  78. assert(ieee754dp_isnan(y));
  79. if (DPMANT(x) > DPMANT(y))
  80. return x;
  81. else
  82. return y;
  83. }
  84. static u64 get_rounding(int sn, u64 xm)
  85. {
  86. /* inexact must round of 3 bits
  87.  */
  88. if (xm & (DP_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. ieee754dp ieee754dp_format(int sn, int xe, u64 xm)
  114. {
  115. assert(xm); /* we dont gen exact zeros (probably should) */
  116. assert((xm >> (DP_MBITS + 1 + 3)) == 0); /* no execess */
  117. assert(xm & (DP_HIDDEN_BIT << 3));
  118. if (xe < DP_EMIN) {
  119. /* strip lower bits */
  120. int es = DP_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 ieee754dp_zero(sn);
  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. }