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. *
  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 "ieee754.h"
  27. #define DP_EBIAS 1023
  28. #define DP_EMIN (-1022)
  29. #define DP_EMAX 1023
  30. #define DP_MBITS 52
  31. #define SP_EBIAS 127
  32. #define SP_EMIN (-126)
  33. #define SP_EMAX 127
  34. #define SP_MBITS 23
  35. #define DP_MBIT(x) ((u64)1 << (x))
  36. #define DP_HIDDEN_BIT DP_MBIT(DP_MBITS)
  37. #define DP_SIGN_BIT DP_MBIT(63)
  38. #define SP_MBIT(x) ((u32)1 << (x))
  39. #define SP_HIDDEN_BIT SP_MBIT(SP_MBITS)
  40. #define SP_SIGN_BIT SP_MBIT(31)
  41. #define SPSIGN(sp) (sp.parts.sign)
  42. #define SPBEXP(sp) (sp.parts.bexp)
  43. #define SPMANT(sp) (sp.parts.mant)
  44. #define DPSIGN(dp) (dp.parts.sign)
  45. #define DPBEXP(dp) (dp.parts.bexp)
  46. #define DPMANT(dp) (dp.parts.mant)
  47. #define CLPAIR(x, y) ((x)*6+(y))
  48. #define CLEARCX \
  49. (ieee754_csr.cx = 0)
  50. #define SETCX(x) \
  51. (ieee754_csr.cx |= (x), ieee754_csr.sx |= (x))
  52. #define SETANDTESTCX(x) \
  53. (SETCX(x), ieee754_csr.mx & (x))
  54. #define TSTX() \
  55. (ieee754_csr.cx & ieee754_csr.mx)
  56. #define COMPXSP \
  57. unsigned xm; int xe; int xs __maybe_unused; int xc
  58. #define COMPYSP \
  59. unsigned ym; int ye; int ys; int yc
  60. #define EXPLODESP(v, vc, vs, ve, vm) \
  61. {\
  62. vs = SPSIGN(v);\
  63. ve = SPBEXP(v);\
  64. vm = SPMANT(v);\
  65. if(ve == SP_EMAX+1+SP_EBIAS){\
  66. if(vm == 0)\
  67. vc = IEEE754_CLASS_INF;\
  68. else if(vm & SP_MBIT(SP_MBITS-1)) \
  69. vc = IEEE754_CLASS_SNAN;\
  70. else \
  71. vc = IEEE754_CLASS_QNAN;\
  72. } else if(ve == SP_EMIN-1+SP_EBIAS) {\
  73. if(vm) {\
  74. ve = SP_EMIN;\
  75. vc = IEEE754_CLASS_DNORM;\
  76. } else\
  77. vc = IEEE754_CLASS_ZERO;\
  78. } else {\
  79. ve -= SP_EBIAS;\
  80. vm |= SP_HIDDEN_BIT;\
  81. vc = IEEE754_CLASS_NORM;\
  82. }\
  83. }
  84. #define EXPLODEXSP EXPLODESP(x, xc, xs, xe, xm)
  85. #define EXPLODEYSP EXPLODESP(y, yc, ys, ye, ym)
  86. #define COMPXDP \
  87. u64 xm; int xe; int xs __maybe_unused; int xc
  88. #define COMPYDP \
  89. u64 ym; int ye; int ys; int yc
  90. #define EXPLODEDP(v, vc, vs, ve, vm) \
  91. {\
  92. vm = DPMANT(v);\
  93. vs = DPSIGN(v);\
  94. ve = DPBEXP(v);\
  95. if(ve == DP_EMAX+1+DP_EBIAS){\
  96. if(vm == 0)\
  97. vc = IEEE754_CLASS_INF;\
  98. else if(vm & DP_MBIT(DP_MBITS-1)) \
  99. vc = IEEE754_CLASS_SNAN;\
  100. else \
  101. vc = IEEE754_CLASS_QNAN;\
  102. } else if(ve == DP_EMIN-1+DP_EBIAS) {\
  103. if(vm) {\
  104. ve = DP_EMIN;\
  105. vc = IEEE754_CLASS_DNORM;\
  106. } else\
  107. vc = IEEE754_CLASS_ZERO;\
  108. } else {\
  109. ve -= DP_EBIAS;\
  110. vm |= DP_HIDDEN_BIT;\
  111. vc = IEEE754_CLASS_NORM;\
  112. }\
  113. }
  114. #define EXPLODEXDP EXPLODEDP(x, xc, xs, xe, xm)
  115. #define EXPLODEYDP EXPLODEDP(y, yc, ys, ye, ym)
  116. #define FLUSHDP(v, vc, vs, ve, vm) \
  117. if(vc==IEEE754_CLASS_DNORM) {\
  118. if(ieee754_csr.nod) {\
  119. SETCX(IEEE754_INEXACT);\
  120. vc = IEEE754_CLASS_ZERO;\
  121. ve = DP_EMIN-1+DP_EBIAS;\
  122. vm = 0;\
  123. v = ieee754dp_zero(vs);\
  124. }\
  125. }
  126. #define FLUSHSP(v, vc, vs, ve, vm) \
  127. if(vc==IEEE754_CLASS_DNORM) {\
  128. if(ieee754_csr.nod) {\
  129. SETCX(IEEE754_INEXACT);\
  130. vc = IEEE754_CLASS_ZERO;\
  131. ve = SP_EMIN-1+SP_EBIAS;\
  132. vm = 0;\
  133. v = ieee754sp_zero(vs);\
  134. }\
  135. }
  136. #define FLUSHXDP FLUSHDP(x, xc, xs, xe, xm)
  137. #define FLUSHYDP FLUSHDP(y, yc, ys, ye, ym)
  138. #define FLUSHXSP FLUSHSP(x, xc, xs, xe, xm)
  139. #define FLUSHYSP FLUSHSP(y, yc, ys, ye, ym)