sp_fdp.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* IEEE754 floating point arithmetic
  3. * single precision
  4. */
  5. /*
  6. * MIPS floating point support
  7. * Copyright (C) 1994-2000 Algorithmics Ltd.
  8. */
  9. #include "ieee754sp.h"
  10. #include "ieee754dp.h"
  11. static inline union ieee754sp ieee754sp_nan_fdp(int xs, u64 xm)
  12. {
  13. return buildsp(xs, SP_EMAX + 1 + SP_EBIAS,
  14. xm >> (DP_FBITS - SP_FBITS));
  15. }
  16. union ieee754sp ieee754sp_fdp(union ieee754dp x)
  17. {
  18. union ieee754sp y;
  19. u32 rm;
  20. COMPXDP;
  21. COMPYSP;
  22. EXPLODEXDP;
  23. ieee754_clearcx();
  24. FLUSHXDP;
  25. switch (xc) {
  26. case IEEE754_CLASS_SNAN:
  27. x = ieee754dp_nanxcpt(x);
  28. EXPLODEXDP;
  29. fallthrough;
  30. case IEEE754_CLASS_QNAN:
  31. y = ieee754sp_nan_fdp(xs, xm);
  32. if (!ieee754_csr.nan2008) {
  33. EXPLODEYSP;
  34. if (!ieee754_class_nan(yc))
  35. y = ieee754sp_indef();
  36. }
  37. return y;
  38. case IEEE754_CLASS_INF:
  39. return ieee754sp_inf(xs);
  40. case IEEE754_CLASS_ZERO:
  41. return ieee754sp_zero(xs);
  42. case IEEE754_CLASS_DNORM:
  43. /* can't possibly be sp representable */
  44. ieee754_setcx(IEEE754_UNDERFLOW);
  45. ieee754_setcx(IEEE754_INEXACT);
  46. if ((ieee754_csr.rm == FPU_CSR_RU && !xs) ||
  47. (ieee754_csr.rm == FPU_CSR_RD && xs))
  48. return ieee754sp_mind(xs);
  49. return ieee754sp_zero(xs);
  50. case IEEE754_CLASS_NORM:
  51. break;
  52. }
  53. /*
  54. * Convert from DP_FBITS to SP_FBITS+3 with sticky right shift.
  55. */
  56. rm = (xm >> (DP_FBITS - (SP_FBITS + 3))) |
  57. ((xm << (64 - (DP_FBITS - (SP_FBITS + 3)))) != 0);
  58. return ieee754sp_format(xs, xe, rm);
  59. }