summaryrefslogtreecommitdiff
path: root/lib/builtins/fp_fixint_impl.inc
diff options
context:
space:
mode:
authorJoerg Sonnenberger <joerg@bec.de>2015-03-11 21:13:56 +0000
committerJoerg Sonnenberger <joerg@bec.de>2015-03-11 21:13:56 +0000
commit6c015134aead1e20986ebbda8d75e9125e0eeed9 (patch)
tree087bc7b4667f73e29e18876318fc024c41091346 /lib/builtins/fp_fixint_impl.inc
parent4779d3501762b1c89b54dd852787e410d1dc3e5e (diff)
Refactor float to integer conversion to share the same code.
80bit Intel/PPC long double is excluded due to lacking support for the abstraction. Consistently provide saturation logic. Extend to long double on 128bit IEEE extended platforms. Initial patch with test cases from GuanHong Liu. Reviewed by Steve Canon. Differential Revision: http://reviews.llvm.org/D2804 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@231965 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/builtins/fp_fixint_impl.inc')
-rw-r--r--lib/builtins/fp_fixint_impl.inc41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/builtins/fp_fixint_impl.inc b/lib/builtins/fp_fixint_impl.inc
new file mode 100644
index 000000000..035e87ca1
--- /dev/null
+++ b/lib/builtins/fp_fixint_impl.inc
@@ -0,0 +1,41 @@
+//===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements float to integer conversion for the
+// compiler-rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include "fp_lib.h"
+
+static inline fixint_t __fixint(fp_t a) {
+ const fixint_t fixint_max = (fixint_t)((~(fixuint_t)0) / 2);
+ const fixint_t fixint_min = -fixint_max - 1;
+ // Break a into sign, exponent, significand
+ const rep_t aRep = toRep(a);
+ const rep_t aAbs = aRep & absMask;
+ const fixint_t sign = aRep & signBit ? -1 : 1;
+ const int exponent = (aAbs >> significandBits) - exponentBias;
+ const rep_t significand = (aAbs & significandMask) | implicitBit;
+
+ // If exponent is negative, the result is zero.
+ if (exponent < 0)
+ return 0;
+
+ // If the value is too large for the integer type, saturate.
+ if ((unsigned)exponent >= sizeof(fixint_t) * CHAR_BIT)
+ return sign == 1 ? fixint_max : fixint_min;
+
+ // If 0 <= exponent < significandBits, right shift to get the result.
+ // Otherwise, shift left.
+ if (exponent < significandBits)
+ return sign * (significand >> (significandBits - exponent));
+ else
+ return sign * ((fixint_t)significand << (exponent - significandBits));
+}