floatditf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //===-- lib/floatditf.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 di_int 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 __floatditf(di_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. du_int aAbs = (du_int)a;
  26. if (a < 0) {
  27. sign = signBit;
  28. aAbs = ~(du_int)a + 1U;
  29. }
  30. // Exponent of (fp_t)a is the width of abs(a).
  31. const int exponent = (aWidth - 1) - __builtin_clzll(aAbs);
  32. rep_t result;
  33. // Shift a into the significand field, rounding if it is a right-shift
  34. const int shift = significandBits - exponent;
  35. result = (rep_t)aAbs << shift ^ implicitBit;
  36. // Insert the exponent
  37. result += (rep_t)(exponent + exponentBias) << significandBits;
  38. // Insert the sign bit and return
  39. return fromRep(result | sign);
  40. }
  41. #endif