ieee754.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* ieee754 floating point arithmetic
  2. * single and double precision
  3. *
  4. * BUGS
  5. * not much dp done
  6. * doesn't generate IEEE754_INEXACT
  7. *
  8. */
  9. /*
  10. * MIPS floating point support
  11. * Copyright (C) 1994-2000 Algorithmics Ltd.
  12. * http://www.algor.co.uk
  13. *
  14. * ########################################################################
  15. *
  16. * This program is free software; you can distribute it and/or modify it
  17. * under the terms of the GNU General Public License (Version 2) as
  18. * published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope it will be useful, but WITHOUT
  21. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  22. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23. * for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  28. *
  29. * ########################################################################
  30. */
  31. #include "ieee754int.h"
  32. #include "ieee754sp.h"
  33. #include "ieee754dp.h"
  34. #define DP_EBIAS 1023
  35. #define DP_EMIN (-1022)
  36. #define DP_EMAX 1023
  37. #define SP_EBIAS 127
  38. #define SP_EMIN (-126)
  39. #define SP_EMAX 127
  40. /* special constants
  41. */
  42. #if (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) || defined(__MIPSEL__)
  43. #define SPSTR(s, b, m) {m, b, s}
  44. #define DPSTR(s, b, mh, ml) {ml, mh, b, s}
  45. #endif
  46. #ifdef __MIPSEB__
  47. #define SPSTR(s, b, m) {s, b, m}
  48. #define DPSTR(s, b, mh, ml) {s, b, mh, ml}
  49. #endif
  50. const struct ieee754dp_konst __ieee754dp_spcvals[] = {
  51. DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 0), /* + zero */
  52. DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 0), /* - zero */
  53. DPSTR(0, DP_EBIAS, 0, 0), /* + 1.0 */
  54. DPSTR(1, DP_EBIAS, 0, 0), /* - 1.0 */
  55. DPSTR(0, 3 + DP_EBIAS, 0x40000, 0), /* + 10.0 */
  56. DPSTR(1, 3 + DP_EBIAS, 0x40000, 0), /* - 10.0 */
  57. DPSTR(0, DP_EMAX + 1 + DP_EBIAS, 0, 0), /* + infinity */
  58. DPSTR(1, DP_EMAX + 1 + DP_EBIAS, 0, 0), /* - infinity */
  59. DPSTR(0, DP_EMAX+1+DP_EBIAS, 0x7FFFF, 0xFFFFFFFF), /* + indef quiet Nan */
  60. DPSTR(0, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF), /* + max */
  61. DPSTR(1, DP_EMAX + DP_EBIAS, 0xFFFFF, 0xFFFFFFFF), /* - max */
  62. DPSTR(0, DP_EMIN + DP_EBIAS, 0, 0), /* + min normal */
  63. DPSTR(1, DP_EMIN + DP_EBIAS, 0, 0), /* - min normal */
  64. DPSTR(0, DP_EMIN - 1 + DP_EBIAS, 0, 1), /* + min denormal */
  65. DPSTR(1, DP_EMIN - 1 + DP_EBIAS, 0, 1), /* - min denormal */
  66. DPSTR(0, 31 + DP_EBIAS, 0, 0), /* + 1.0e31 */
  67. DPSTR(0, 63 + DP_EBIAS, 0, 0), /* + 1.0e63 */
  68. };
  69. const struct ieee754sp_konst __ieee754sp_spcvals[] = {
  70. SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 0), /* + zero */
  71. SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 0), /* - zero */
  72. SPSTR(0, SP_EBIAS, 0), /* + 1.0 */
  73. SPSTR(1, SP_EBIAS, 0), /* - 1.0 */
  74. SPSTR(0, 3 + SP_EBIAS, 0x200000), /* + 10.0 */
  75. SPSTR(1, 3 + SP_EBIAS, 0x200000), /* - 10.0 */
  76. SPSTR(0, SP_EMAX + 1 + SP_EBIAS, 0), /* + infinity */
  77. SPSTR(1, SP_EMAX + 1 + SP_EBIAS, 0), /* - infinity */
  78. SPSTR(0, SP_EMAX+1+SP_EBIAS, 0x3FFFFF), /* + indef quiet Nan */
  79. SPSTR(0, SP_EMAX + SP_EBIAS, 0x7FFFFF), /* + max normal */
  80. SPSTR(1, SP_EMAX + SP_EBIAS, 0x7FFFFF), /* - max normal */
  81. SPSTR(0, SP_EMIN + SP_EBIAS, 0), /* + min normal */
  82. SPSTR(1, SP_EMIN + SP_EBIAS, 0), /* - min normal */
  83. SPSTR(0, SP_EMIN - 1 + SP_EBIAS, 1), /* + min denormal */
  84. SPSTR(1, SP_EMIN - 1 + SP_EBIAS, 1), /* - min denormal */
  85. SPSTR(0, 31 + SP_EBIAS, 0), /* + 1.0e31 */
  86. SPSTR(0, 63 + SP_EBIAS, 0), /* + 1.0e63 */
  87. };
  88. int ieee754si_xcpt(int r, const char *op, ...)
  89. {
  90. struct ieee754xctx ax;
  91. if (!TSTX())
  92. return r;
  93. ax.op = op;
  94. ax.rt = IEEE754_RT_SI;
  95. ax.rv.si = r;
  96. va_start(ax.ap, op);
  97. ieee754_xcpt(&ax);
  98. va_end(ax.ap);
  99. return ax.rv.si;
  100. }
  101. s64 ieee754di_xcpt(s64 r, const char *op, ...)
  102. {
  103. struct ieee754xctx ax;
  104. if (!TSTX())
  105. return r;
  106. ax.op = op;
  107. ax.rt = IEEE754_RT_DI;
  108. ax.rv.di = r;
  109. va_start(ax.ap, op);
  110. ieee754_xcpt(&ax);
  111. va_end(ax.ap);
  112. return ax.rv.di;
  113. }