dp_tint.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. */
  9. #include "ieee754dp.h"
  10. int ieee754dp_tint(union ieee754dp x)
  11. {
  12. u64 residue;
  13. int round;
  14. int sticky;
  15. int odd;
  16. COMPXDP;
  17. ieee754_clearcx();
  18. EXPLODEXDP;
  19. FLUSHXDP;
  20. switch (xc) {
  21. case IEEE754_CLASS_SNAN:
  22. case IEEE754_CLASS_QNAN:
  23. ieee754_setcx(IEEE754_INVALID_OPERATION);
  24. return ieee754si_indef();
  25. case IEEE754_CLASS_INF:
  26. ieee754_setcx(IEEE754_INVALID_OPERATION);
  27. return ieee754si_overflow(xs);
  28. case IEEE754_CLASS_ZERO:
  29. return 0;
  30. case IEEE754_CLASS_DNORM:
  31. case IEEE754_CLASS_NORM:
  32. break;
  33. }
  34. if (xe > 31) {
  35. /* Set invalid. We will only use overflow for floating
  36. point overflow */
  37. ieee754_setcx(IEEE754_INVALID_OPERATION);
  38. return ieee754si_overflow(xs);
  39. }
  40. /* oh gawd */
  41. if (xe > DP_FBITS) {
  42. xm <<= xe - DP_FBITS;
  43. } else if (xe < DP_FBITS) {
  44. if (xe < -1) {
  45. residue = xm;
  46. round = 0;
  47. sticky = residue != 0;
  48. xm = 0;
  49. } else {
  50. residue = xm << (64 - DP_FBITS + xe);
  51. round = (residue >> 63) != 0;
  52. sticky = (residue << 1) != 0;
  53. xm >>= DP_FBITS - xe;
  54. }
  55. /* Note: At this point upper 32 bits of xm are guaranteed
  56. to be zero */
  57. odd = (xm & 0x1) != 0x0;
  58. switch (ieee754_csr.rm) {
  59. case FPU_CSR_RN:
  60. if (round && (sticky || odd))
  61. xm++;
  62. break;
  63. case FPU_CSR_RZ:
  64. break;
  65. case FPU_CSR_RU: /* toward +Infinity */
  66. if ((round || sticky) && !xs)
  67. xm++;
  68. break;
  69. case FPU_CSR_RD: /* toward -Infinity */
  70. if ((round || sticky) && xs)
  71. xm++;
  72. break;
  73. }
  74. /* look for valid corner case 0x80000000 */
  75. if ((xm >> 31) != 0 && (xs == 0 || xm != 0x80000000)) {
  76. /* This can happen after rounding */
  77. ieee754_setcx(IEEE754_INVALID_OPERATION);
  78. return ieee754si_overflow(xs);
  79. }
  80. if (round || sticky)
  81. ieee754_setcx(IEEE754_INEXACT);
  82. }
  83. if (xs)
  84. return -xm;
  85. else
  86. return xm;
  87. }