dp_rint.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* IEEE754 floating point arithmetic
  3. * double precision: common utilities
  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 "ieee754dp.h"
  12. union ieee754dp ieee754dp_rint(union ieee754dp x)
  13. {
  14. union ieee754dp ret;
  15. u64 residue;
  16. int sticky;
  17. int round;
  18. int odd;
  19. COMPXDP;
  20. ieee754_clearcx();
  21. EXPLODEXDP;
  22. FLUSHXDP;
  23. if (xc == IEEE754_CLASS_SNAN)
  24. return ieee754dp_nanxcpt(x);
  25. if ((xc == IEEE754_CLASS_QNAN) ||
  26. (xc == IEEE754_CLASS_INF) ||
  27. (xc == IEEE754_CLASS_ZERO))
  28. return x;
  29. if (xe >= DP_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 << (64 - DP_FBITS + xe);
  38. round = (residue >> 63) != 0;
  39. sticky = (residue << 1) != 0;
  40. xm >>= DP_FBITS - xe;
  41. }
  42. odd = (xm & 0x1) != 0x0;
  43. switch (ieee754_csr.rm) {
  44. case FPU_CSR_RN: /* toward nearest */
  45. if (round && (sticky || odd))
  46. xm++;
  47. break;
  48. case FPU_CSR_RZ: /* toward zero */
  49. break;
  50. case FPU_CSR_RU: /* toward +infinity */
  51. if ((round || sticky) && !xs)
  52. xm++;
  53. break;
  54. case FPU_CSR_RD: /* toward -infinity */
  55. if ((round || sticky) && xs)
  56. xm++;
  57. break;
  58. }
  59. if (round || sticky)
  60. ieee754_setcx(IEEE754_INEXACT);
  61. ret = ieee754dp_flong(xm);
  62. DPSIGN(ret) = xs;
  63. return ret;
  64. }