floatsidf.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //===-- lib/floatsidf.c - integer -> double-precision conversion --*- 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 integer to double-precision conversion for the
  10. // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
  11. // mode.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DOUBLE_PRECISION
  15. #include "fp_lib.h"
  16. #include "int_lib.h"
  17. COMPILER_RT_ABI fp_t __floatsidf(si_int a) {
  18. const int aWidth = sizeof a * CHAR_BIT;
  19. // Handle zero as a special case to protect clz
  20. if (a == 0)
  21. return fromRep(0);
  22. // All other cases begin by extracting the sign and absolute value of a
  23. rep_t sign = 0;
  24. su_int aAbs = (su_int)a;
  25. if (a < 0) {
  26. sign = signBit;
  27. aAbs = -aAbs;
  28. }
  29. // Exponent of (fp_t)a is the width of abs(a).
  30. const int exponent = (aWidth - 1) - clzsi(aAbs);
  31. rep_t result;
  32. // Shift a into the significand field and clear the implicit bit. Extra
  33. // cast to unsigned int is necessary to get the correct behavior for
  34. // the input INT_MIN.
  35. const int shift = significandBits - exponent;
  36. result = (rep_t)aAbs << shift ^ implicitBit;
  37. // Insert the exponent
  38. result += (rep_t)(exponent + exponentBias) << significandBits;
  39. // Insert the sign bit and return
  40. return fromRep(result | sign);
  41. }
  42. #if defined(__ARM_EABI__)
  43. #if defined(COMPILER_RT_ARMHF_TARGET)
  44. AEABI_RTABI fp_t __aeabi_i2d(si_int a) { return __floatsidf(a); }
  45. #else
  46. COMPILER_RT_ALIAS(__floatsidf, __aeabi_i2d)
  47. #endif
  48. #endif