ieee754int.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * IEEE754 floating point
  3. * common internal header file
  4. */
  5. /*
  6. * MIPS floating point support
  7. * Copyright (C) 1994-2000 Algorithmics Ltd.
  8. * http://www.algor.co.uk
  9. *
  10. * ########################################################################
  11. *
  12. * This program is free software; you can distribute it and/or modify it
  13. * under the terms of the GNU General Public License (Version 2) as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19. * for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  24. *
  25. * ########################################################################
  26. */
  27. #include "ieee754.h"
  28. #define DP_EBIAS 1023
  29. #define DP_EMIN (-1022)
  30. #define DP_EMAX 1023
  31. #define DP_MBITS 52
  32. #define SP_EBIAS 127
  33. #define SP_EMIN (-126)
  34. #define SP_EMAX 127
  35. #define SP_MBITS 23
  36. #define DP_MBIT(x) ((u64)1 << (x))
  37. #define DP_HIDDEN_BIT DP_MBIT(DP_MBITS)
  38. #define DP_SIGN_BIT DP_MBIT(63)
  39. #define SP_MBIT(x) ((u32)1 << (x))
  40. #define SP_HIDDEN_BIT SP_MBIT(SP_MBITS)
  41. #define SP_SIGN_BIT SP_MBIT(31)
  42. #define SPSIGN(sp) (sp.parts.sign)
  43. #define SPBEXP(sp) (sp.parts.bexp)
  44. #define SPMANT(sp) (sp.parts.mant)
  45. #define DPSIGN(dp) (dp.parts.sign)
  46. #define DPBEXP(dp) (dp.parts.bexp)
  47. #define DPMANT(dp) (dp.parts.mant)
  48. #define CLPAIR(x, y) ((x)*6+(y))
  49. #define CLEARCX \
  50. (ieee754_csr.cx = 0)
  51. #define SETCX(x) \
  52. (ieee754_csr.cx |= (x), ieee754_csr.sx |= (x))
  53. #define SETANDTESTCX(x) \
  54. (SETCX(x), ieee754_csr.mx & (x))
  55. #define TSTX() \
  56. (ieee754_csr.cx & ieee754_csr.mx)
  57. #define COMPXSP \
  58. unsigned xm; int xe; int xs; int xc
  59. #define COMPYSP \
  60. unsigned ym; int ye; int ys; int yc
  61. #define EXPLODESP(v, vc, vs, ve, vm) \
  62. {\
  63. vs = SPSIGN(v);\
  64. ve = SPBEXP(v);\
  65. vm = SPMANT(v);\
  66. if(ve == SP_EMAX+1+SP_EBIAS){\
  67. if(vm == 0)\
  68. vc = IEEE754_CLASS_INF;\
  69. else if(vm & SP_MBIT(SP_MBITS-1)) \
  70. vc = IEEE754_CLASS_SNAN;\
  71. else \
  72. vc = IEEE754_CLASS_QNAN;\
  73. } else if(ve == SP_EMIN-1+SP_EBIAS) {\
  74. if(vm) {\
  75. ve = SP_EMIN;\
  76. vc = IEEE754_CLASS_DNORM;\
  77. } else\
  78. vc = IEEE754_CLASS_ZERO;\
  79. } else {\
  80. ve -= SP_EBIAS;\
  81. vm |= SP_HIDDEN_BIT;\
  82. vc = IEEE754_CLASS_NORM;\
  83. }\
  84. }
  85. #define EXPLODEXSP EXPLODESP(x, xc, xs, xe, xm)
  86. #define EXPLODEYSP EXPLODESP(y, yc, ys, ye, ym)
  87. #define COMPXDP \
  88. u64 xm; int xe; int xs; int xc
  89. #define COMPYDP \
  90. u64 ym; int ye; int ys; int yc
  91. #define EXPLODEDP(v, vc, vs, ve, vm) \
  92. {\
  93. vm = DPMANT(v);\
  94. vs = DPSIGN(v);\
  95. ve = DPBEXP(v);\
  96. if(ve == DP_EMAX+1+DP_EBIAS){\
  97. if(vm == 0)\
  98. vc = IEEE754_CLASS_INF;\
  99. else if(vm & DP_MBIT(DP_MBITS-1)) \
  100. vc = IEEE754_CLASS_SNAN;\
  101. else \
  102. vc = IEEE754_CLASS_QNAN;\
  103. } else if(ve == DP_EMIN-1+DP_EBIAS) {\
  104. if(vm) {\
  105. ve = DP_EMIN;\
  106. vc = IEEE754_CLASS_DNORM;\
  107. } else\
  108. vc = IEEE754_CLASS_ZERO;\
  109. } else {\
  110. ve -= DP_EBIAS;\
  111. vm |= DP_HIDDEN_BIT;\
  112. vc = IEEE754_CLASS_NORM;\
  113. }\
  114. }
  115. #define EXPLODEXDP EXPLODEDP(x, xc, xs, xe, xm)
  116. #define EXPLODEYDP EXPLODEDP(y, yc, ys, ye, ym)
  117. #define FLUSHDP(v, vc, vs, ve, vm) \
  118. if(vc==IEEE754_CLASS_DNORM) {\
  119. if(ieee754_csr.nod) {\
  120. SETCX(IEEE754_INEXACT);\
  121. vc = IEEE754_CLASS_ZERO;\
  122. ve = DP_EMIN-1+DP_EBIAS;\
  123. vm = 0;\
  124. v = ieee754dp_zero(vs);\
  125. }\
  126. }
  127. #define FLUSHSP(v, vc, vs, ve, vm) \
  128. if(vc==IEEE754_CLASS_DNORM) {\
  129. if(ieee754_csr.nod) {\
  130. SETCX(IEEE754_INEXACT);\
  131. vc = IEEE754_CLASS_ZERO;\
  132. ve = SP_EMIN-1+SP_EBIAS;\
  133. vm = 0;\
  134. v = ieee754sp_zero(vs);\
  135. }\
  136. }
  137. #define FLUSHXDP FLUSHDP(x, xc, xs, xe, xm)
  138. #define FLUSHYDP FLUSHDP(y, yc, ys, ye, ym)
  139. #define FLUSHXSP FLUSHSP(x, xc, xs, xe, xm)
  140. #define FLUSHYSP FLUSHSP(y, yc, ys, ye, ym)