fp_fixuint_impl.inc 1.4 KB

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