floatunsisf.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //===-- lib/floatunsisf.c - uint -> single-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 unsigned integer to single-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 SINGLE_PRECISION
  15. #include "fp_lib.h"
  16. #include "int_lib.h"
  17. COMPILER_RT_ABI fp_t __floatunsisf(su_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. // Exponent of (fp_t)a is the width of abs(a).
  23. const int exponent = (aWidth - 1) - clzsi(a);
  24. rep_t result;
  25. // Shift a into the significand field, rounding if it is a right-shift
  26. if (exponent <= significandBits) {
  27. const int shift = significandBits - exponent;
  28. result = (rep_t)a << shift ^ implicitBit;
  29. } else {
  30. const int shift = exponent - significandBits;
  31. result = (rep_t)a >> shift ^ implicitBit;
  32. rep_t round = (rep_t)a << (typeWidth - shift);
  33. if (round > signBit)
  34. result++;
  35. if (round == signBit)
  36. result += result & 1;
  37. }
  38. // Insert the exponent
  39. result += (rep_t)(exponent + exponentBias) << significandBits;
  40. return fromRep(result);
  41. }
  42. #if defined(__ARM_EABI__)
  43. #if defined(COMPILER_RT_ARMHF_TARGET)
  44. AEABI_RTABI fp_t __aeabi_ui2f(unsigned int a) { return __floatunsisf(a); }
  45. #else
  46. COMPILER_RT_ALIAS(__floatunsisf, __aeabi_ui2f)
  47. #endif
  48. #endif