sp_rint.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* IEEE754 floating point arithmetic
  2. * single precision
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. * Copyright (C) 2017 Imagination Technologies, Ltd.
  8. * Author: Aleksandar Markovic <aleksandar.markovic@imgtec.com>
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program.
  21. */
  22. #include "ieee754sp.h"
  23. union ieee754sp ieee754sp_rint(union ieee754sp x)
  24. {
  25. union ieee754sp ret;
  26. u32 residue;
  27. int sticky;
  28. int round;
  29. int odd;
  30. COMPXDP; /* <-- DP needed for 64-bit mantissa tmp */
  31. ieee754_clearcx();
  32. EXPLODEXSP;
  33. FLUSHXSP;
  34. if (xc == IEEE754_CLASS_SNAN)
  35. return ieee754sp_nanxcpt(x);
  36. if ((xc == IEEE754_CLASS_QNAN) ||
  37. (xc == IEEE754_CLASS_INF) ||
  38. (xc == IEEE754_CLASS_ZERO))
  39. return x;
  40. if (xe >= SP_FBITS)
  41. return x;
  42. if (xe < -1) {
  43. residue = xm;
  44. round = 0;
  45. sticky = residue != 0;
  46. xm = 0;
  47. } else {
  48. residue = xm << (xe + 1);
  49. residue <<= 31 - SP_FBITS;
  50. round = (residue >> 31) != 0;
  51. sticky = (residue << 1) != 0;
  52. xm >>= SP_FBITS - xe;
  53. }
  54. odd = (xm & 0x1) != 0x0;
  55. switch (ieee754_csr.rm) {
  56. case FPU_CSR_RN: /* toward nearest */
  57. if (round && (sticky || odd))
  58. xm++;
  59. break;
  60. case FPU_CSR_RZ: /* toward zero */
  61. break;
  62. case FPU_CSR_RU: /* toward +infinity */
  63. if ((round || sticky) && !xs)
  64. xm++;
  65. break;
  66. case FPU_CSR_RD: /* toward -infinity */
  67. if ((round || sticky) && xs)
  68. xm++;
  69. break;
  70. }
  71. if (round || sticky)
  72. ieee754_setcx(IEEE754_INEXACT);
  73. ret = ieee754sp_flong(xm);
  74. SPSIGN(ret) = xs;
  75. return ret;
  76. }