fixtfti.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //===--- lib/builtins/ppc/fixtfti.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 a signed 128 bit integer.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "../int_math.h"
  14. // Convert long double into a signed 128-bit integer.
  15. __int128_t __fixtfti(long double input) {
  16. // If we are trying to convert a NaN, return the NaN bit pattern.
  17. if (crt_isnan(input)) {
  18. return ((__uint128_t)0x7FF8000000000000ll) << 64 |
  19. (__uint128_t)0x0000000000000000ll;
  20. }
  21. // Note: overflow is an undefined behavior for this conversion.
  22. // For this reason, overflow is not checked here.
  23. // If the long double is negative, use unsigned conversion from its absolute
  24. // value.
  25. if (input < 0.0) {
  26. __uint128_t result = (__uint128_t)(-input);
  27. return -((__int128_t)result);
  28. }
  29. // Otherwise, use unsigned conversion from the input value.
  30. __uint128_t result = (__uint128_t)input;
  31. return result;
  32. }