dp_tlong.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. s64 ieee754dp_tlong(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 ieee754di_indef();
  25. case IEEE754_CLASS_INF:
  26. ieee754_setcx(IEEE754_INVALID_OPERATION);
  27. return ieee754di_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 >= 63) {
  35. /* look for valid corner case */
  36. if (xe == 63 && xs && xm == DP_HIDDEN_BIT)
  37. return -0x8000000000000000LL;
  38. /* Set invalid. We will only use overflow for floating
  39. point overflow */
  40. ieee754_setcx(IEEE754_INVALID_OPERATION);
  41. return ieee754di_overflow(xs);
  42. }
  43. /* oh gawd */
  44. if (xe > DP_FBITS) {
  45. xm <<= xe - DP_FBITS;
  46. } else if (xe < DP_FBITS) {
  47. if (xe < -1) {
  48. residue = xm;
  49. round = 0;
  50. sticky = residue != 0;
  51. xm = 0;
  52. } else {
  53. /* Shifting a u64 64 times does not work,
  54. * so we do it in two steps. Be aware that xe
  55. * may be -1 */
  56. residue = xm << (xe + 1);
  57. residue <<= 63 - DP_FBITS;
  58. round = (residue >> 63) != 0;
  59. sticky = (residue << 1) != 0;
  60. xm >>= DP_FBITS - xe;
  61. }
  62. odd = (xm & 0x1) != 0x0;
  63. switch (ieee754_csr.rm) {
  64. case FPU_CSR_RN:
  65. if (round && (sticky || odd))
  66. xm++;
  67. break;
  68. case FPU_CSR_RZ:
  69. break;
  70. case FPU_CSR_RU: /* toward +Infinity */
  71. if ((round || sticky) && !xs)
  72. xm++;
  73. break;
  74. case FPU_CSR_RD: /* toward -Infinity */
  75. if ((round || sticky) && xs)
  76. xm++;
  77. break;
  78. }
  79. if ((xm >> 63) != 0) {
  80. /* This can happen after rounding */
  81. ieee754_setcx(IEEE754_INVALID_OPERATION);
  82. return ieee754di_overflow(xs);
  83. }
  84. if (round || sticky)
  85. ieee754_setcx(IEEE754_INEXACT);
  86. }
  87. if (xs)
  88. return -xm;
  89. else
  90. return xm;
  91. }