floattitf.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //===-- lib/builtins/ppc/floattitf.c - Convert int128->long double -*-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 a signed 128 bit integer to a 128bit IBM /
  10. // PowerPC long double (double-double) value.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include <stdint.h>
  14. // Conversions from signed and unsigned 64-bit int to long double.
  15. long double __floatditf(int64_t);
  16. long double __floatunditf(uint64_t);
  17. // Convert a signed 128-bit integer to long double.
  18. // This uses the following property: Let hi and lo be 64-bits each,
  19. // and let signed_val_k() and unsigned_val_k() be the value of the
  20. // argument interpreted as a signed or unsigned k-bit integer. Then,
  21. //
  22. // signed_val_128(hi,lo) = signed_val_64(hi) * 2^64 + unsigned_val_64(lo)
  23. // = (long double)hi * 2^64 + (long double)lo,
  24. //
  25. // where (long double)hi and (long double)lo are signed and
  26. // unsigned 64-bit integer to long double conversions, respectively.
  27. long double __floattitf(__int128_t arg) {
  28. // Split the int128 argument into 64-bit high and low int64 parts.
  29. int64_t ArgHiPart = (int64_t)(arg >> 64);
  30. uint64_t ArgLoPart = (uint64_t)arg;
  31. // Convert each 64-bit part into long double. The high part
  32. // must be a signed conversion and the low part an unsigned conversion
  33. // to ensure the correct result.
  34. long double ConvertedHiPart = __floatditf(ArgHiPart);
  35. long double ConvertedLoPart = __floatunditf(ArgLoPart);
  36. // The low bit of ArgHiPart corresponds to the 2^64 bit in arg.
  37. // Multiply the high part by 2^64 to undo the right shift by 64-bits
  38. // done in the splitting. Then, add to the low part to obtain the
  39. // final result.
  40. return ((ConvertedHiPart * 0x1.0p64) + ConvertedLoPart);
  41. }