sp_fdp.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. x = ieee754dp_nanxcpt(x);
  40. EXPLODEXDP;
  41. /* Fall through. */
  42. case IEEE754_CLASS_QNAN:
  43. y = ieee754sp_nan_fdp(xs, xm);
  44. if (!ieee754_csr.nan2008) {
  45. EXPLODEYSP;
  46. if (!ieee754_class_nan(yc))
  47. y = ieee754sp_indef();
  48. }
  49. return y;
  50. case IEEE754_CLASS_INF:
  51. return ieee754sp_inf(xs);
  52. case IEEE754_CLASS_ZERO:
  53. return ieee754sp_zero(xs);
  54. case IEEE754_CLASS_DNORM:
  55. /* can't possibly be sp representable */
  56. ieee754_setcx(IEEE754_UNDERFLOW);
  57. ieee754_setcx(IEEE754_INEXACT);
  58. if ((ieee754_csr.rm == FPU_CSR_RU && !xs) ||
  59. (ieee754_csr.rm == FPU_CSR_RD && xs))
  60. return ieee754sp_mind(xs);
  61. return ieee754sp_zero(xs);
  62. case IEEE754_CLASS_NORM:
  63. break;
  64. }
  65. /*
  66. * Convert from DP_FBITS to SP_FBITS+3 with sticky right shift.
  67. */
  68. rm = (xm >> (DP_FBITS - (SP_FBITS + 3))) |
  69. ((xm << (64 - (DP_FBITS - (SP_FBITS + 3)))) != 0);
  70. return ieee754sp_format(xs, xe, rm);
  71. }