fixunstfti.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===-- lib/builtins/ppc/fixunstfti.c - Convert long double->int128 *-C -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements converting the 128bit IBM/PowerPC long double (double-
  10. // double) data type to an unsigned 128 bit integer.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "../int_math.h"
  14. #define BIAS 1023
  15. // Convert long double into an unsigned 128-bit integer.
  16. __uint128_t __fixunstfti(long double input) {
  17. // If we are trying to convert a NaN, return the NaN bit pattern.
  18. if (crt_isnan(input)) {
  19. return ((__uint128_t)0x7FF8000000000000ll) << 64 |
  20. (__uint128_t)0x0000000000000000ll;
  21. }
  22. __uint128_t result, hiResult, loResult;
  23. int hiExponent, loExponent, shift;
  24. // The long double representation, with the high and low portions of
  25. // the long double, and the corresponding bit patterns of each double.
  26. union {
  27. long double ld;
  28. double d[2]; // [0] is the high double, [1] is the low double.
  29. unsigned long long ull[2]; // High and low doubles as 64-bit integers.
  30. } ldUnion;
  31. // If the long double is less than 1.0 or negative,
  32. // return 0.
  33. if (input < 1.0)
  34. return 0;
  35. // Retrieve the 64-bit patterns of high and low doubles.
  36. // Compute the unbiased exponent of both high and low doubles by
  37. // removing the signs, isolating the exponent, and subtracting
  38. // the bias from it.
  39. ldUnion.ld = input;
  40. hiExponent = ((ldUnion.ull[0] & 0x7FFFFFFFFFFFFFFFll) >> 52) - BIAS;
  41. loExponent = ((ldUnion.ull[1] & 0x7FFFFFFFFFFFFFFFll) >> 52) - BIAS;
  42. // Convert each double into int64; they will be added to the int128 result.
  43. // CASE 1: High or low double fits in int64
  44. // - Convert the each double normally into int64.
  45. //
  46. // CASE 2: High or low double does not fit in int64
  47. // - Scale the double to fit within a 64-bit integer
  48. // - Calculate the shift (amount to scale the double by in the int128)
  49. // - Clear all the bits of the exponent (with 0x800FFFFFFFFFFFFF)
  50. // - Add BIAS+53 (0x4350000000000000) to exponent to correct the value
  51. // - Scale (move) the double to the correct place in the int128
  52. // (Move it by 2^53 places)
  53. //
  54. // Note: If the high double is assumed to be positive, an unsigned conversion
  55. // from long double to 64-bit integer is needed. The low double can be either
  56. // positive or negative, so a signed conversion is needed to retain the result
  57. // of the low double and to ensure it does not simply get converted to 0.
  58. // CASE 1 - High double fits in int64.
  59. if (hiExponent < 63) {
  60. hiResult = (unsigned long long)ldUnion.d[0];
  61. } else if (hiExponent < 128) {
  62. // CASE 2 - High double does not fit in int64, scale and convert it.
  63. shift = hiExponent - 54;
  64. ldUnion.ull[0] &= 0x800FFFFFFFFFFFFFll;
  65. ldUnion.ull[0] |= 0x4350000000000000ll;
  66. hiResult = (unsigned long long)ldUnion.d[0];
  67. hiResult <<= shift;
  68. } else {
  69. // Detect cases for overflow. When the exponent of the high
  70. // double is greater than 128 bits and when the long double
  71. // input is positive, return the max 128-bit integer.
  72. // For negative inputs with exponents > 128, return 1, like gcc.
  73. if (ldUnion.d[0] > 0) {
  74. return ((__uint128_t)0xFFFFFFFFFFFFFFFFll) << 64 |
  75. (__uint128_t)0xFFFFFFFFFFFFFFFFll;
  76. } else {
  77. return ((__uint128_t)0x0000000000000000ll) << 64 |
  78. (__uint128_t)0x0000000000000001ll;
  79. }
  80. }
  81. // CASE 1 - Low double fits in int64.
  82. if (loExponent < 63) {
  83. loResult = (long long)ldUnion.d[1];
  84. } else {
  85. // CASE 2 - Low double does not fit in int64, scale and convert it.
  86. shift = loExponent - 54;
  87. ldUnion.ull[1] &= 0x800FFFFFFFFFFFFFll;
  88. ldUnion.ull[1] |= 0x4350000000000000ll;
  89. loResult = (long long)ldUnion.d[1];
  90. loResult <<= shift;
  91. }
  92. // If the low double is negative, it may change the integer value of the
  93. // whole number if the absolute value of its fractional part is bigger than
  94. // the fractional part of the high double. Because both doubles cannot
  95. // overlap, this situation only occurs when the high double has no
  96. // fractional part.
  97. ldUnion.ld = input;
  98. if ((ldUnion.d[0] == (double)hiResult) &&
  99. (ldUnion.d[1] < (double)((__int128_t)loResult)))
  100. loResult--;
  101. // Add the high and low doublewords together to form a 128 bit integer.
  102. result = loResult + hiResult;
  103. return result;
  104. }