sp_rint.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * Copyright (C) 2017 Imagination Technologies, Ltd.
  9. * Author: Aleksandar Markovic <[email protected]>
  10. */
  11. #include "ieee754sp.h"
  12. union ieee754sp ieee754sp_rint(union ieee754sp x)
  13. {
  14. union ieee754sp ret;
  15. u32 residue;
  16. int sticky;
  17. int round;
  18. int odd;
  19. COMPXDP; /* <-- DP needed for 64-bit mantissa tmp */
  20. ieee754_clearcx();
  21. EXPLODEXSP;
  22. FLUSHXSP;
  23. if (xc == IEEE754_CLASS_SNAN)
  24. return ieee754sp_nanxcpt(x);
  25. if ((xc == IEEE754_CLASS_QNAN) ||
  26. (xc == IEEE754_CLASS_INF) ||
  27. (xc == IEEE754_CLASS_ZERO))
  28. return x;
  29. if (xe >= SP_FBITS)
  30. return x;
  31. if (xe < -1) {
  32. residue = xm;
  33. round = 0;
  34. sticky = residue != 0;
  35. xm = 0;
  36. } else {
  37. residue = xm << (xe + 1);
  38. residue <<= 31 - SP_FBITS;
  39. round = (residue >> 31) != 0;
  40. sticky = (residue << 1) != 0;
  41. xm >>= SP_FBITS - xe;
  42. }
  43. odd = (xm & 0x1) != 0x0;
  44. switch (ieee754_csr.rm) {
  45. case FPU_CSR_RN: /* toward nearest */
  46. if (round && (sticky || odd))
  47. xm++;
  48. break;
  49. case FPU_CSR_RZ: /* toward zero */
  50. break;
  51. case FPU_CSR_RU: /* toward +infinity */
  52. if ((round || sticky) && !xs)
  53. xm++;
  54. break;
  55. case FPU_CSR_RD: /* toward -infinity */
  56. if ((round || sticky) && xs)
  57. xm++;
  58. break;
  59. }
  60. if (round || sticky)
  61. ieee754_setcx(IEEE754_INEXACT);
  62. ret = ieee754sp_flong(xm);
  63. SPSIGN(ret) = xs;
  64. return ret;
  65. }