summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/builtins/CMakeLists.txt3
-rw-r--r--lib/builtins/extendhfsf2.c21
-rw-r--r--lib/builtins/fp_extend.h19
-rw-r--r--lib/builtins/fp_extend_impl.inc4
-rw-r--r--lib/builtins/fp_trunc.h14
-rw-r--r--lib/builtins/truncdfhf2.c16
-rw-r--r--lib/builtins/truncsfhf2.c20
-rw-r--r--test/builtins/Unit/extendhfsf2_test.c113
-rw-r--r--test/builtins/Unit/fp_test.h43
-rw-r--r--test/builtins/Unit/truncdfhf2_test.c111
-rw-r--r--test/builtins/Unit/truncsfhf2_test.c111
11 files changed, 470 insertions, 5 deletions
diff --git a/lib/builtins/CMakeLists.txt b/lib/builtins/CMakeLists.txt
index 4d102c615..d79feae13 100644
--- a/lib/builtins/CMakeLists.txt
+++ b/lib/builtins/CMakeLists.txt
@@ -44,6 +44,7 @@ set(GENERIC_SOURCES
enable_execute_stack.c
eprintf.c
extendsfdf2.c
+ extendhfsf2.c
ffsdi2.c
ffsti2.c
fixdfdi.c
@@ -123,7 +124,9 @@ set(GENERIC_SOURCES
subvti3.c
subtf3.c
trampoline_setup.c
+ truncdfhf2.c
truncdfsf2.c
+ truncsfhf2.c
ucmpdi2.c
ucmpti2.c
udivdi3.c
diff --git a/lib/builtins/extendhfsf2.c b/lib/builtins/extendhfsf2.c
new file mode 100644
index 000000000..f0b3c37c3
--- /dev/null
+++ b/lib/builtins/extendhfsf2.c
@@ -0,0 +1,21 @@
+//===-- lib/extendhfsf2.c - half -> single 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.
+//
+//===----------------------------------------------------------------------===//
+//
+
+#define SRC_HALF
+#define DST_SINGLE
+#include "fp_extend_impl.inc"
+
+COMPILER_RT_ABI float __extendhfsf2(uint16_t a) {
+ return __extendXfYf2__(a);
+}
+
+COMPILER_RT_ABI float __gnu_h2f_ieee(uint16_t a) {
+ return __extendXfYf2__(a);
+}
diff --git a/lib/builtins/fp_extend.h b/lib/builtins/fp_extend.h
index fff676eec..5c2b92310 100644
--- a/lib/builtins/fp_extend.h
+++ b/lib/builtins/fp_extend.h
@@ -39,11 +39,24 @@ static inline int src_rep_t_clz(src_rep_t a) {
#endif
}
+#elif defined SRC_HALF
+typedef uint16_t src_t;
+typedef uint16_t src_rep_t;
+#define SRC_REP_C UINT16_C
+static const int srcSigBits = 10;
+#define src_rep_t_clz __builtin_clz
+
#else
-#error Source should be single precision or double precision!
+#error Source should be half, single, or double precision!
#endif //end source precision
-#if defined DST_DOUBLE
+#if defined DST_SINGLE
+typedef float dst_t;
+typedef uint32_t dst_rep_t;
+#define DST_REP_C UINT32_C
+static const int dstSigBits = 23;
+
+#elif defined DST_DOUBLE
typedef double dst_t;
typedef uint64_t dst_rep_t;
#define DST_REP_C UINT64_C
@@ -56,7 +69,7 @@ typedef __uint128_t dst_rep_t;
static const int dstSigBits = 112;
#else
-#error Destination should be double precision or quad precision!
+#error Destination should be single, double, or quad precision!
#endif //end destination precision
// End of specialization parameters. Two helper routines for conversion to and
diff --git a/lib/builtins/fp_extend_impl.inc b/lib/builtins/fp_extend_impl.inc
index f6953ff4b..edcfa8d23 100644
--- a/lib/builtins/fp_extend_impl.inc
+++ b/lib/builtins/fp_extend_impl.inc
@@ -66,7 +66,9 @@ static inline dst_t __extendXfYf2__(src_t a) {
const src_rep_t sign = aRep & srcSignMask;
dst_rep_t absResult;
- if (aAbs - srcMinNormal < srcInfinity - srcMinNormal) {
+ // If sizeof(src_rep_t) < sizeof(int), the subtraction result is promoted
+ // to (signed) int. To avoid that, explicitly cast to src_rep_t.
+ if ((src_rep_t)(aAbs - srcMinNormal) < srcInfinity - srcMinNormal) {
// a is a normal number.
// Extend to the destination type by shifting the significand and
// exponent into the proper position and rebiasing the exponent.
diff --git a/lib/builtins/fp_trunc.h b/lib/builtins/fp_trunc.h
index 49a9aebbc..373ba1b04 100644
--- a/lib/builtins/fp_trunc.h
+++ b/lib/builtins/fp_trunc.h
@@ -16,7 +16,13 @@
#include "int_lib.h"
-#if defined SRC_DOUBLE
+#if defined SRC_SINGLE
+typedef float src_t;
+typedef uint32_t src_rep_t;
+#define SRC_REP_C UINT32_C
+static const int srcSigBits = 23;
+
+#elif defined SRC_DOUBLE
typedef double src_t;
typedef uint64_t src_rep_t;
#define SRC_REP_C UINT64_C
@@ -44,6 +50,12 @@ typedef uint32_t dst_rep_t;
#define DST_REP_C UINT32_C
static const int dstSigBits = 23;
+#elif defined DST_HALF
+typedef uint16_t dst_t;
+typedef uint16_t dst_rep_t;
+#define DST_REP_C UINT16_C
+static const int dstSigBits = 10;
+
#else
#error Destination should be single precision or double precision!
#endif //end destination precision
diff --git a/lib/builtins/truncdfhf2.c b/lib/builtins/truncdfhf2.c
new file mode 100644
index 000000000..0852df369
--- /dev/null
+++ b/lib/builtins/truncdfhf2.c
@@ -0,0 +1,16 @@
+//===-- lib/truncdfhf2.c - double -> half 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.
+//
+//===----------------------------------------------------------------------===//
+
+#define SRC_DOUBLE
+#define DST_HALF
+#include "fp_trunc_impl.inc"
+
+COMPILER_RT_ABI uint16_t __truncdfhf2(double a) {
+ return __truncXfYf2__(a);
+}
diff --git a/lib/builtins/truncsfhf2.c b/lib/builtins/truncsfhf2.c
new file mode 100644
index 000000000..a25dd83d8
--- /dev/null
+++ b/lib/builtins/truncsfhf2.c
@@ -0,0 +1,20 @@
+//===-- lib/truncsfhf2.c - single -> half 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.
+//
+//===----------------------------------------------------------------------===//
+
+#define SRC_SINGLE
+#define DST_HALF
+#include "fp_trunc_impl.inc"
+
+COMPILER_RT_ABI uint16_t __truncsfhf2(float a) {
+ return __truncXfYf2__(a);
+}
+
+COMPILER_RT_ABI uint16_t __gnu_f2h_ieee(float a) {
+ return __truncXfYf2__(a);
+}
diff --git a/test/builtins/Unit/extendhfsf2_test.c b/test/builtins/Unit/extendhfsf2_test.c
new file mode 100644
index 000000000..5dd994cae
--- /dev/null
+++ b/test/builtins/Unit/extendhfsf2_test.c
@@ -0,0 +1,113 @@
+//===--------------- extendhfsf2_test.c - Test __extendhfsf2 --------------===//
+//
+// 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 __extendhfsf2 for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdio.h>
+
+#include "fp_test.h"
+
+float __extendhfsf2(uint16_t a);
+
+int test__extendhfsf2(uint16_t a, float expected)
+{
+ float x = __extendhfsf2(a);
+ int ret = compareResultH(x, expected);
+
+ if (ret){
+ printf("error in test__extendhfsf2(%#.4x) = %f, "
+ "expected %f\n", a, x, expected);
+ }
+ return ret;
+}
+
+char assumption_1[sizeof(__fp16) * CHAR_BIT == 16] = {0};
+
+int main()
+{
+ // qNaN
+ if (test__extendhfsf2(UINT16_C(0x7e00),
+ makeQNaN32()))
+ return 1;
+ // NaN
+ if (test__extendhfsf2(UINT16_C(0x7e00),
+ makeNaN32(UINT32_C(0x8000))))
+ return 1;
+ // inf
+ if (test__extendhfsf2(UINT16_C(0x7c00),
+ makeInf32()))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0xfc00),
+ -makeInf32()))
+ return 1;
+ // zero
+ if (test__extendhfsf2(UINT16_C(0x0),
+ 0.0f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x8000),
+ -0.0f))
+ return 1;
+
+ if (test__extendhfsf2(UINT16_C(0x4248),
+ 3.1415926535f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0xc248),
+ -3.1415926535f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x7c00),
+ 0x1.987124876876324p+100f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x6e62),
+ 0x1.988p+12f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x3c00),
+ 0x1.0p+0f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x0400),
+ 0x1.0p-14f))
+ return 1;
+ // denormal
+ if (test__extendhfsf2(UINT16_C(0x0010),
+ 0x1.0p-20f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x0001),
+ 0x1.0p-24f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x8001),
+ -0x1.0p-24f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x0001),
+ 0x1.5p-25f))
+ return 1;
+ // and back to zero
+ if (test__extendhfsf2(UINT16_C(0x0000),
+ 0x1.0p-25f))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0x8000),
+ -0x1.0p-25f))
+ return 1;
+ // max (precise)
+ if (test__extendhfsf2(UINT16_C(0x7bff),
+ 65504.0f))
+ return 1;
+ // max (rounded)
+ if (test__extendhfsf2(UINT16_C(0x7bff),
+ 65504.0f))
+ return 1;
+ // max (to +inf)
+ if (test__extendhfsf2(UINT16_C(0x7c00),
+ makeInf32()))
+ return 1;
+ if (test__extendhfsf2(UINT16_C(0xfc00),
+ -makeInf32()))
+ return 1;
+ return 0;
+}
diff --git a/test/builtins/Unit/fp_test.h b/test/builtins/Unit/fp_test.h
index da58ca989..95740b68c 100644
--- a/test/builtins/Unit/fp_test.h
+++ b/test/builtins/Unit/fp_test.h
@@ -19,6 +19,11 @@ enum EXPECTED_RESULT {
LESS_0, LESS_EQUAL_0, EQUAL_0, GREATER_0, GREATER_EQUAL_0, NEQUAL_0
};
+static inline uint16_t fromRep16(uint16_t x)
+{
+ return x;
+}
+
static inline float fromRep32(uint32_t x)
{
float ret;
@@ -41,6 +46,11 @@ static inline long double fromRep128(uint64_t hi, uint64_t lo)
return ret;
}
+static inline uint16_t toRep16(uint16_t x)
+{
+ return x;
+}
+
static inline uint32_t toRep32(float x)
{
uint32_t ret;
@@ -62,6 +72,24 @@ static inline __uint128_t toRep128(long double x)
return ret;
}
+static inline int compareResultH(uint16_t result,
+ uint16_t expected)
+{
+ uint16_t rep = toRep16(result);
+
+ if (rep == expected){
+ return 0;
+ }
+ // test other posible NaN representation(signal NaN)
+ else if (expected == 0x7e00U){
+ if ((rep & 0x7c00U) == 0x7c00U &&
+ (rep & 0x3ffU) > 0){
+ return 0;
+ }
+ }
+ return 1;
+}
+
static inline int compareResultF(float result,
uint32_t expected)
{
@@ -177,6 +205,11 @@ static inline char *expectedStr(enum EXPECTED_RESULT expected)
return "";
}
+static inline uint16_t makeQNaN16()
+{
+ return fromRep16(0x7e00U);
+}
+
static inline float makeQNaN32()
{
return fromRep32(0x7fc00000U);
@@ -192,6 +225,11 @@ static inline long double makeQNaN128()
return fromRep128(0x7fff800000000000UL, 0x0UL);
}
+static inline uint16_t makeNaN16(uint16_t rand)
+{
+ return fromRep16(0x7c00U | (rand & 0x7fffU));
+}
+
static inline float makeNaN32(uint32_t rand)
{
return fromRep32(0x7f800000U | (rand & 0x7fffffU));
@@ -207,6 +245,11 @@ static inline long double makeNaN128(uint64_t rand)
return fromRep128(0x7fff000000000000UL | (rand & 0xffffffffffffUL), 0x0UL);
}
+static inline uint16_t makeInf16()
+{
+ return fromRep16(0x7c00U);
+}
+
static inline float makeInf32()
{
return fromRep32(0x7f800000U);
diff --git a/test/builtins/Unit/truncdfhf2_test.c b/test/builtins/Unit/truncdfhf2_test.c
new file mode 100644
index 000000000..98c5c9dac
--- /dev/null
+++ b/test/builtins/Unit/truncdfhf2_test.c
@@ -0,0 +1,111 @@
+//===--------------- truncdfhf2_test.c - Test __truncdfhf2 ----------------===//
+//
+// 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 __truncdfhf2 for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdio.h>
+
+#include "fp_test.h"
+
+uint16_t __truncdfhf2(double a);
+
+int test__truncdfhf2(double a, uint16_t expected)
+{
+ uint16_t x = __truncdfhf2(a);
+ int ret = compareResultH(x, expected);
+
+ if (ret){
+ printf("error in test__truncdfhf2(%f) = %#.4x, "
+ "expected %#.4x\n", a, x, fromRep16(expected));
+ }
+ return ret;
+}
+
+char assumption_1[sizeof(__fp16) * CHAR_BIT == 16] = {0};
+
+int main()
+{
+ // qNaN
+ if (test__truncdfhf2(makeQNaN64(),
+ UINT16_C(0x7e00)))
+ return 1;
+ // NaN
+ if (test__truncdfhf2(makeNaN64(UINT64_C(0x8000)),
+ UINT16_C(0x7e00)))
+ return 1;
+ // inf
+ if (test__truncdfhf2(makeInf64(),
+ UINT16_C(0x7c00)))
+ return 1;
+ if (test__truncdfhf2(-makeInf64(),
+ UINT16_C(0xfc00)))
+ return 1;
+ // zero
+ if (test__truncdfhf2(0.0, UINT16_C(0x0)))
+ return 1;
+ if (test__truncdfhf2(-0.0, UINT16_C(0x8000)))
+ return 1;
+
+ if (test__truncdfhf2(3.1415926535,
+ UINT16_C(0x4248)))
+ return 1;
+ if (test__truncdfhf2(-3.1415926535,
+ UINT16_C(0xc248)))
+ return 1;
+ if (test__truncdfhf2(0x1.987124876876324p+1000,
+ UINT16_C(0x7c00)))
+ return 1;
+ if (test__truncdfhf2(0x1.987124876876324p+12,
+ UINT16_C(0x6e62)))
+ return 1;
+ if (test__truncdfhf2(0x1.0p+0,
+ UINT16_C(0x3c00)))
+ return 1;
+ if (test__truncdfhf2(0x1.0p-14,
+ UINT16_C(0x0400)))
+ return 1;
+ // denormal
+ if (test__truncdfhf2(0x1.0p-20,
+ UINT16_C(0x0010)))
+ return 1;
+ if (test__truncdfhf2(0x1.0p-24,
+ UINT16_C(0x0001)))
+ return 1;
+ if (test__truncdfhf2(-0x1.0p-24,
+ UINT16_C(0x8001)))
+ return 1;
+ if (test__truncdfhf2(0x1.5p-25,
+ UINT16_C(0x0001)))
+ return 1;
+ // and back to zero
+ if (test__truncdfhf2(0x1.0p-25,
+ UINT16_C(0x0000)))
+ return 1;
+ if (test__truncdfhf2(-0x1.0p-25,
+ UINT16_C(0x8000)))
+ return 1;
+ // max (precise)
+ if (test__truncdfhf2(65504.0,
+ UINT16_C(0x7bff)))
+ return 1;
+ // max (rounded)
+ if (test__truncdfhf2(65519.0,
+ UINT16_C(0x7bff)))
+ return 1;
+ // max (to +inf)
+ if (test__truncdfhf2(65520.0,
+ UINT16_C(0x7c00)))
+ return 1;
+ if (test__truncdfhf2(-65520.0,
+ UINT16_C(0xfc00)))
+ return 1;
+ return 0;
+}
diff --git a/test/builtins/Unit/truncsfhf2_test.c b/test/builtins/Unit/truncsfhf2_test.c
new file mode 100644
index 000000000..2024605d0
--- /dev/null
+++ b/test/builtins/Unit/truncsfhf2_test.c
@@ -0,0 +1,111 @@
+//===--------------- truncsfhf2_test.c - Test __truncsfhf2 ----------------===//
+//
+// 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 __truncsfhf2 for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdio.h>
+
+#include "fp_test.h"
+
+uint16_t __truncsfhf2(float a);
+
+int test__truncsfhf2(float a, uint16_t expected)
+{
+ uint16_t x = __truncsfhf2(a);
+ int ret = compareResultH(x, expected);
+
+ if (ret){
+ printf("error in test__truncsfhf2(%f) = %#.4x, "
+ "expected %#.4x\n", a, x, fromRep16(expected));
+ }
+ return ret;
+}
+
+char assumption_1[sizeof(__fp16) * CHAR_BIT == 16] = {0};
+
+int main()
+{
+ // qNaN
+ if (test__truncsfhf2(makeQNaN32(),
+ UINT16_C(0x7e00)))
+ return 1;
+ // NaN
+ if (test__truncsfhf2(makeNaN32(UINT32_C(0x8000)),
+ UINT16_C(0x7e00)))
+ return 1;
+ // inf
+ if (test__truncsfhf2(makeInf32(),
+ UINT16_C(0x7c00)))
+ return 1;
+ if (test__truncsfhf2(-makeInf32(),
+ UINT16_C(0xfc00)))
+ return 1;
+ // zero
+ if (test__truncsfhf2(0.0f, UINT16_C(0x0)))
+ return 1;
+ if (test__truncsfhf2(-0.0f, UINT16_C(0x8000)))
+ return 1;
+
+ if (test__truncsfhf2(3.1415926535f,
+ UINT16_C(0x4248)))
+ return 1;
+ if (test__truncsfhf2(-3.1415926535f,
+ UINT16_C(0xc248)))
+ return 1;
+ if (test__truncsfhf2(0x1.987124876876324p+100f,
+ UINT16_C(0x7c00)))
+ return 1;
+ if (test__truncsfhf2(0x1.987124876876324p+12f,
+ UINT16_C(0x6e62)))
+ return 1;
+ if (test__truncsfhf2(0x1.0p+0f,
+ UINT16_C(0x3c00)))
+ return 1;
+ if (test__truncsfhf2(0x1.0p-14f,
+ UINT16_C(0x0400)))
+ return 1;
+ // denormal
+ if (test__truncsfhf2(0x1.0p-20f,
+ UINT16_C(0x0010)))
+ return 1;
+ if (test__truncsfhf2(0x1.0p-24f,
+ UINT16_C(0x0001)))
+ return 1;
+ if (test__truncsfhf2(-0x1.0p-24f,
+ UINT16_C(0x8001)))
+ return 1;
+ if (test__truncsfhf2(0x1.5p-25f,
+ UINT16_C(0x0001)))
+ return 1;
+ // and back to zero
+ if (test__truncsfhf2(0x1.0p-25f,
+ UINT16_C(0x0000)))
+ return 1;
+ if (test__truncsfhf2(-0x1.0p-25f,
+ UINT16_C(0x8000)))
+ return 1;
+ // max (precise)
+ if (test__truncsfhf2(65504.0f,
+ UINT16_C(0x7bff)))
+ return 1;
+ // max (rounded)
+ if (test__truncsfhf2(65519.0f,
+ UINT16_C(0x7bff)))
+ return 1;
+ // max (to +inf)
+ if (test__truncsfhf2(65520.0f,
+ UINT16_C(0x7c00)))
+ return 1;
+ if (test__truncsfhf2(-65520.0f,
+ UINT16_C(0xfc00)))
+ return 1;
+ return 0;
+}