fixxfti.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //===-- fixxfti.c - Implement __fixxfti -----------------------------------===//
  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 __fixxfti for the compiler_rt library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "int_lib.h"
  13. #ifdef CRT_HAS_128BIT
  14. // Returns: convert a to a signed long long, rounding toward zero.
  15. // Assumption: long double is an intel 80 bit floating point type padded with 6
  16. // bytes ti_int is a 128 bit integral type value in long double is representable
  17. // in ti_int
  18. // gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee
  19. // eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
  20. // mmmm mmmm mmmm
  21. COMPILER_RT_ABI ti_int __fixxfti(xf_float a) {
  22. const ti_int ti_max = (ti_int)((~(tu_int)0) / 2);
  23. const ti_int ti_min = -ti_max - 1;
  24. xf_bits fb;
  25. fb.f = a;
  26. int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
  27. if (e < 0)
  28. return 0;
  29. ti_int s = -(si_int)((fb.u.high.s.low & 0x00008000) >> 15);
  30. ti_int r = fb.u.low.all;
  31. if ((unsigned)e >= sizeof(ti_int) * CHAR_BIT)
  32. return a > 0 ? ti_max : ti_min;
  33. if (e > 63)
  34. r <<= (e - 63);
  35. else
  36. r >>= (63 - e);
  37. return (r ^ s) - s;
  38. }
  39. #endif // CRT_HAS_128BIT