fixtfdi.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  2. // See https://llvm.org/LICENSE.txt for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // int64_t __fixunstfdi(long double x);
  5. // This file implements the PowerPC 128-bit double-double -> int64_t conversion
  6. #include "../int_math.h"
  7. #include "DD.h"
  8. uint64_t __fixtfdi(long double input) {
  9. const DD x = {.ld = input};
  10. const doublebits hibits = {.d = x.s.hi};
  11. const uint32_t absHighWord =
  12. (uint32_t)(hibits.x >> 32) & UINT32_C(0x7fffffff);
  13. const uint32_t absHighWordMinusOne = absHighWord - UINT32_C(0x3ff00000);
  14. // If (1.0 - tiny) <= input < 0x1.0p63:
  15. if (UINT32_C(0x03f00000) > absHighWordMinusOne) {
  16. // Do an unsigned conversion of the absolute value, then restore the sign.
  17. const int unbiasedHeadExponent = absHighWordMinusOne >> 20;
  18. int64_t result = hibits.x & INT64_C(0x000fffffffffffff); // mantissa(hi)
  19. result |= INT64_C(0x0010000000000000); // matissa(hi) with implicit bit
  20. result <<= 10; // mantissa(hi) with one zero preceding bit.
  21. const int64_t hiNegationMask = ((int64_t)(hibits.x)) >> 63;
  22. // If the tail is non-zero, we need to patch in the tail bits.
  23. if (0.0 != x.s.lo) {
  24. const doublebits lobits = {.d = x.s.lo};
  25. int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
  26. tailMantissa |= INT64_C(0x0010000000000000);
  27. // At this point we have the mantissa of |tail|
  28. // We need to negate it if head and tail have different signs.
  29. const int64_t loNegationMask = ((int64_t)(lobits.x)) >> 63;
  30. const int64_t negationMask = loNegationMask ^ hiNegationMask;
  31. tailMantissa = (tailMantissa ^ negationMask) - negationMask;
  32. // Now we have the mantissa of tail as a signed 2s-complement integer
  33. const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
  34. // Shift the tail mantissa into the right position, accounting for the
  35. // bias of 10 that we shifted the head mantissa by.
  36. tailMantissa >>=
  37. (unbiasedHeadExponent - (biasedTailExponent - (1023 - 10)));
  38. result += tailMantissa;
  39. }
  40. result >>= (62 - unbiasedHeadExponent);
  41. // Restore the sign of the result and return
  42. result = (result ^ hiNegationMask) - hiNegationMask;
  43. return result;
  44. }
  45. // Edge cases handled here:
  46. // |x| < 1, result is zero.
  47. if (1.0 > crt_fabs(x.s.hi))
  48. return INT64_C(0);
  49. // x very close to INT64_MIN, care must be taken to see which side we are on.
  50. if (x.s.hi == -0x1.0p63) {
  51. int64_t result = INT64_MIN;
  52. if (0.0 < x.s.lo) {
  53. // If the tail is positive, the correct result is something other than
  54. // INT64_MIN. we'll need to figure out what it is.
  55. const doublebits lobits = {.d = x.s.lo};
  56. int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
  57. tailMantissa |= INT64_C(0x0010000000000000);
  58. // Now we negate the tailMantissa
  59. tailMantissa = (tailMantissa ^ INT64_C(-1)) + INT64_C(1);
  60. // And shift it by the appropriate amount
  61. const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
  62. tailMantissa >>= 1075 - biasedTailExponent;
  63. result -= tailMantissa;
  64. }
  65. return result;
  66. }
  67. // Signed overflows, infinities, and NaNs
  68. if (x.s.hi > 0.0)
  69. return INT64_MAX;
  70. else
  71. return INT64_MIN;
  72. }