sp_tlong.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. } else {
  68. residue = xm << (32 - SP_MBITS + xe);
  69. round = (residue >> 31) != 0;
  70. sticky = (residue << 1) != 0;
  71. xm >>= SP_MBITS - xe;
  72. }
  73. odd = (xm & 0x1) != 0x0;
  74. switch (ieee754_csr.rm) {
  75. case IEEE754_RN:
  76. if (round && (sticky || odd))
  77. xm++;
  78. break;
  79. case IEEE754_RZ:
  80. break;
  81. case IEEE754_RU: /* toward +Infinity */
  82. if ((round || sticky) && !xs)
  83. xm++;
  84. break;
  85. case IEEE754_RD: /* toward -Infinity */
  86. if ((round || sticky) && xs)
  87. xm++;
  88. break;
  89. }
  90. if ((xm >> 63) != 0) {
  91. /* This can happen after rounding */
  92. SETCX(IEEE754_INVALID_OPERATION);
  93. return ieee754di_xcpt(ieee754di_indef(), "sp_tlong", x);
  94. }
  95. if (round || sticky)
  96. SETCX(IEEE754_INEXACT);
  97. }
  98. if (xs)
  99. return -xm;
  100. else
  101. return xm;
  102. }
  103. u64 ieee754sp_tulong(ieee754sp x)
  104. {
  105. ieee754sp hb = ieee754sp_1e63();
  106. /* what if x < 0 ?? */
  107. if (ieee754sp_lt(x, hb))
  108. return (u64) ieee754sp_tlong(x);
  109. return (u64) ieee754sp_tlong(ieee754sp_sub(x, hb)) |
  110. (1ULL << 63);
  111. }