Cherry-pick fix to FP trunctation routine
Bug: 7342860
Bug: 20822382
Cherry-pick r240450 that fixes a bug in FP truncation where
2^{bit-width-of-dst-type} does not get converted to +inf.
Change-Id: Ia6732923383c0ebd49f16512a12ace59cf78fec5
diff --git a/lib/builtins/fp_trunc_impl.inc b/lib/builtins/fp_trunc_impl.inc
index 21bffae..372e8d6 100644
--- a/lib/builtins/fp_trunc_impl.inc
+++ b/lib/builtins/fp_trunc_impl.inc
@@ -99,7 +99,7 @@
absResult |= dstQNaN;
absResult |= ((aAbs & srcNaNCode) >> (srcSigBits - dstSigBits)) & dstNaNCode;
}
- else if (aAbs > overflow) {
+ else if (aAbs >= overflow) {
// a overflows to infinity.
absResult = (dst_rep_t)dstInfExp << dstSigBits;
}
diff --git a/test/builtins/Unit/fp_test.h b/test/builtins/Unit/fp_test.h
index 95740b6..3bf9cb5 100644
--- a/test/builtins/Unit/fp_test.h
+++ b/test/builtins/Unit/fp_test.h
@@ -14,6 +14,7 @@
#include <stdlib.h>
#include <limits.h>
#include <string.h>
+#include <stdint.h>
enum EXPECTED_RESULT {
LESS_0, LESS_EQUAL_0, EQUAL_0, GREATER_0, GREATER_EQUAL_0, NEQUAL_0
diff --git a/test/builtins/Unit/truncdfhf2_test.c b/test/builtins/Unit/truncdfhf2_test.c
index 98c5c9d..6627a00 100644
--- a/test/builtins/Unit/truncdfhf2_test.c
+++ b/test/builtins/Unit/truncdfhf2_test.c
@@ -107,5 +107,8 @@
if (test__truncdfhf2(-65520.0,
UINT16_C(0xfc00)))
return 1;
+ if (test__truncdfhf2(65536.0,
+ UINT16_C(0x7c00)))
+ return 1;
return 0;
}
diff --git a/test/builtins/Unit/truncdfsf2_test.c b/test/builtins/Unit/truncdfsf2_test.c
new file mode 100644
index 0000000..a208a3a
--- /dev/null
+++ b/test/builtins/Unit/truncdfsf2_test.c
@@ -0,0 +1,38 @@
+//===--------------- truncdfsf2_test.c - Test __truncdfsf2 ----------------===//
+//
+// 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 tests __truncdfsf2 for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdio.h>
+
+#include "fp_test.h"
+
+float __truncdfsf2(double a);
+
+int test__truncdfsf2(double a)
+{
+ float actual = __truncdfsf2(a);
+ float expected = a;
+
+ if (actual != expected) {
+ printf("error in test__truncdfsf2(%lf) = %f, "
+ "expected %f\n", a, actual, expected);
+ return 1;
+ }
+ return 0;
+}
+
+int main()
+{
+ if (test__truncdfsf2(340282366920938463463374607431768211456.0))
+ return 1;
+ return 0;
+}
diff --git a/test/builtins/Unit/truncsfhf2_test.c b/test/builtins/Unit/truncsfhf2_test.c
index 2024605..5bc3c8e 100644
--- a/test/builtins/Unit/truncsfhf2_test.c
+++ b/test/builtins/Unit/truncsfhf2_test.c
@@ -104,6 +104,9 @@
if (test__truncsfhf2(65520.0f,
UINT16_C(0x7c00)))
return 1;
+ if (test__truncsfhf2(65536.0f,
+ UINT16_C(0x7c00)))
+ return 1;
if (test__truncsfhf2(-65520.0f,
UINT16_C(0xfc00)))
return 1;