sp_fdp.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* IEEE754 floating point arithmetic
  2. * single precision
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. *
  8. * This program is free software; you can distribute it and/or modify it
  9. * under the terms of the GNU General Public License (Version 2) as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "ieee754sp.h"
  22. #include "ieee754dp.h"
  23. union ieee754sp ieee754sp_fdp(union ieee754dp x)
  24. {
  25. u32 rm;
  26. COMPXDP;
  27. union ieee754sp nan;
  28. EXPLODEXDP;
  29. ieee754_clearcx();
  30. FLUSHXDP;
  31. switch (xc) {
  32. case IEEE754_CLASS_SNAN:
  33. ieee754_setcx(IEEE754_INVALID_OPERATION);
  34. return ieee754sp_nanxcpt(ieee754sp_indef());
  35. case IEEE754_CLASS_QNAN:
  36. nan = buildsp(xs, SP_EMAX + 1 + SP_EBIAS, (u32)
  37. (xm >> (DP_FBITS - SP_FBITS)));
  38. if (!ieee754sp_isnan(nan))
  39. nan = ieee754sp_indef();
  40. return ieee754sp_nanxcpt(nan);
  41. case IEEE754_CLASS_INF:
  42. return ieee754sp_inf(xs);
  43. case IEEE754_CLASS_ZERO:
  44. return ieee754sp_zero(xs);
  45. case IEEE754_CLASS_DNORM:
  46. /* can't possibly be sp representable */
  47. ieee754_setcx(IEEE754_UNDERFLOW);
  48. ieee754_setcx(IEEE754_INEXACT);
  49. if ((ieee754_csr.rm == FPU_CSR_RU && !xs) ||
  50. (ieee754_csr.rm == FPU_CSR_RD && xs))
  51. return ieee754sp_mind(xs);
  52. return ieee754sp_zero(xs);
  53. case IEEE754_CLASS_NORM:
  54. break;
  55. }
  56. /*
  57. * Convert from DP_FBITS to SP_FBITS+3 with sticky right shift.
  58. */
  59. rm = (xm >> (DP_FBITS - (SP_FBITS + 3))) |
  60. ((xm << (64 - (DP_FBITS - (SP_FBITS + 3)))) != 0);
  61. return ieee754sp_format(xs, xe, rm);
  62. }