fp_fixuint_impl.inc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //===-- lib/fixdfsi.c - Double-precision -> integer 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 float to unsigned integer conversion for the
  11. // compiler-rt library.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "fp_lib.h"
  15. static __inline fixuint_t __fixuint(fp_t a) {
  16. // Break a into sign, exponent, significand
  17. const rep_t aRep = toRep(a);
  18. const rep_t aAbs = aRep & absMask;
  19. const int sign = aRep & signBit ? -1 : 1;
  20. const int exponent = (aAbs >> significandBits) - exponentBias;
  21. const rep_t significand = (aAbs & significandMask) | implicitBit;
  22. // If either the value or the exponent is negative, the result is zero.
  23. if (sign == -1 || exponent < 0)
  24. return 0;
  25. // If the value is too large for the integer type, saturate.
  26. if ((unsigned)exponent >= sizeof(fixuint_t) * CHAR_BIT)
  27. return ~(fixuint_t)0;
  28. // If 0 <= exponent < significandBits, right shift to get the result.
  29. // Otherwise, shift left.
  30. if (exponent < significandBits)
  31. return significand >> (significandBits - exponent);
  32. else
  33. return (fixuint_t)significand << (exponent - significandBits);
  34. }