floatsidf.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. if (a < 0) {
  25. sign = signBit;
  26. a = -a;
  27. }
  28. // Exponent of (fp_t)a is the width of abs(a).
  29. const int exponent = (aWidth - 1) - clzsi(a);
  30. rep_t result;
  31. // Shift a into the significand field and clear the implicit bit. Extra
  32. // cast to unsigned int is necessary to get the correct behavior for
  33. // the input INT_MIN.
  34. const int shift = significandBits - exponent;
  35. result = (rep_t)(su_int)a << 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. #if defined(__ARM_EABI__)
  42. #if defined(COMPILER_RT_ARMHF_TARGET)
  43. AEABI_RTABI fp_t __aeabi_i2d(si_int a) { return __floatsidf(a); }
  44. #else
  45. COMPILER_RT_ALIAS(__floatsidf, __aeabi_i2d)
  46. #endif
  47. #endif