floatsitf.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //===-- lib/floatsitf.c - integer -> quad-precision conversion ----*- C -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements integer to quad-precision conversion for the
  11. // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
  12. // mode.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #define QUAD_PRECISION
  16. #include "fp_lib.h"
  17. #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
  18. COMPILER_RT_ABI fp_t __floatsitf(int a) {
  19. const int aWidth = sizeof a * CHAR_BIT;
  20. // Handle zero as a special case to protect clz
  21. if (a == 0)
  22. return fromRep(0);
  23. // All other cases begin by extracting the sign and absolute value of a
  24. rep_t sign = 0;
  25. unsigned aAbs = (unsigned)a;
  26. if (a < 0) {
  27. sign = signBit;
  28. aAbs += 0x80000000;
  29. }
  30. // Exponent of (fp_t)a is the width of abs(a).
  31. const int exponent = (aWidth - 1) - __builtin_clz(a);
  32. rep_t result;
  33. // Shift a into the significand field and clear the implicit bit. Extra
  34. // cast to unsigned int is necessary to get the correct behavior for
  35. // the input INT_MIN.
  36. const int shift = significandBits - exponent;
  37. result = (rep_t)aAbs << shift ^ implicitBit;
  38. // Insert the exponent
  39. result += (rep_t)(exponent + exponentBias) << significandBits;
  40. // Insert the sign bit and return
  41. return fromRep(result | sign);
  42. }
  43. #endif