sp_fdp.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. static inline union ieee754sp ieee754sp_nan_fdp(int xs, u64 xm)
  24. {
  25. return buildsp(xs, SP_EMAX + 1 + SP_EBIAS,
  26. xm >> (DP_FBITS - SP_FBITS));
  27. }
  28. union ieee754sp ieee754sp_fdp(union ieee754dp x)
  29. {
  30. union ieee754sp y;
  31. u32 rm;
  32. COMPXDP;
  33. COMPYSP;
  34. EXPLODEXDP;
  35. ieee754_clearcx();
  36. FLUSHXDP;
  37. switch (xc) {
  38. case IEEE754_CLASS_SNAN:
  39. return ieee754sp_nanxcpt(ieee754sp_nan_fdp(xs, xm));
  40. case IEEE754_CLASS_QNAN:
  41. y = ieee754sp_nan_fdp(xs, xm);
  42. EXPLODEYSP;
  43. if (!ieee754_class_nan(yc))
  44. y = ieee754sp_indef();
  45. return y;
  46. case IEEE754_CLASS_INF:
  47. return ieee754sp_inf(xs);
  48. case IEEE754_CLASS_ZERO:
  49. return ieee754sp_zero(xs);
  50. case IEEE754_CLASS_DNORM:
  51. /* can't possibly be sp representable */
  52. ieee754_setcx(IEEE754_UNDERFLOW);
  53. ieee754_setcx(IEEE754_INEXACT);
  54. if ((ieee754_csr.rm == FPU_CSR_RU && !xs) ||
  55. (ieee754_csr.rm == FPU_CSR_RD && xs))
  56. return ieee754sp_mind(xs);
  57. return ieee754sp_zero(xs);
  58. case IEEE754_CLASS_NORM:
  59. break;
  60. }
  61. /*
  62. * Convert from DP_FBITS to SP_FBITS+3 with sticky right shift.
  63. */
  64. rm = (xm >> (DP_FBITS - (SP_FBITS + 3))) |
  65. ((xm << (64 - (DP_FBITS - (SP_FBITS + 3)))) != 0);
  66. return ieee754sp_format(xs, xe, rm);
  67. }