sp_tlong.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* IEEE754 floating point arithmetic
  2. * single precision
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  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. s64 ieee754sp_tlong(ieee754sp x)
  28. {
  29. COMPXDP; /* <-- need 64-bit mantissa tmp */
  30. CLEARCX;
  31. EXPLODEXSP;
  32. FLUSHXSP;
  33. switch (xc) {
  34. case IEEE754_CLASS_SNAN:
  35. case IEEE754_CLASS_QNAN:
  36. case IEEE754_CLASS_INF:
  37. SETCX(IEEE754_INVALID_OPERATION);
  38. return ieee754di_xcpt(ieee754di_indef(), "sp_tlong", x);
  39. case IEEE754_CLASS_ZERO:
  40. return 0;
  41. case IEEE754_CLASS_DNORM:
  42. case IEEE754_CLASS_NORM:
  43. break;
  44. }
  45. if (xe >= 63) {
  46. /* look for valid corner case */
  47. if (xe == 63 && xs && xm == SP_HIDDEN_BIT)
  48. return -0x8000000000000000LL;
  49. /* Set invalid. We will only use overflow for floating
  50. point overflow */
  51. SETCX(IEEE754_INVALID_OPERATION);
  52. return ieee754di_xcpt(ieee754di_indef(), "sp_tlong", x);
  53. }
  54. /* oh gawd */
  55. if (xe > SP_MBITS) {
  56. xm <<= xe - SP_MBITS;
  57. } else if (xe < SP_MBITS) {
  58. u32 residue;
  59. int round;
  60. int sticky;
  61. int odd;
  62. if (xe < -1) {
  63. residue = xm;
  64. round = 0;
  65. sticky = residue != 0;
  66. xm = 0;
  67. }
  68. else {
  69. residue = xm << (32 - SP_MBITS + xe);
  70. round = (residue >> 31) != 0;
  71. sticky = (residue << 1) != 0;
  72. xm >>= SP_MBITS - xe;
  73. }
  74. odd = (xm & 0x1) != 0x0;
  75. switch (ieee754_csr.rm) {
  76. case IEEE754_RN:
  77. if (round && (sticky || odd))
  78. xm++;
  79. break;
  80. case IEEE754_RZ:
  81. break;
  82. case IEEE754_RU: /* toward +Infinity */
  83. if ((round || sticky) && !xs)
  84. xm++;
  85. break;
  86. case IEEE754_RD: /* toward -Infinity */
  87. if ((round || sticky) && xs)
  88. xm++;
  89. break;
  90. }
  91. if ((xm >> 63) != 0) {
  92. /* This can happen after rounding */
  93. SETCX(IEEE754_INVALID_OPERATION);
  94. return ieee754di_xcpt(ieee754di_indef(), "sp_tlong", x);
  95. }
  96. if (round || sticky)
  97. SETCX(IEEE754_INEXACT);
  98. }
  99. if (xs)
  100. return -xm;
  101. else
  102. return xm;
  103. }
  104. u64 ieee754sp_tulong(ieee754sp x)
  105. {
  106. ieee754sp hb = ieee754sp_1e63();
  107. /* what if x < 0 ?? */
  108. if (ieee754sp_lt(x, hb))
  109. return (u64) ieee754sp_tlong(x);
  110. return (u64) ieee754sp_tlong(ieee754sp_sub(x, hb)) |
  111. (1ULL << 63);
  112. }