Index: compiler-rt/trunk/lib/builtins/CMakeLists.txt =================================================================== --- compiler-rt/trunk/lib/builtins/CMakeLists.txt +++ compiler-rt/trunk/lib/builtins/CMakeLists.txt @@ -44,6 +44,7 @@ enable_execute_stack.c eprintf.c extendsfdf2.c + extendhfsf2.c ffsdi2.c ffsti2.c fixdfdi.c @@ -123,7 +124,9 @@ subvti3.c subtf3.c trampoline_setup.c + truncdfhf2.c truncdfsf2.c + truncsfhf2.c ucmpdi2.c ucmpti2.c udivdi3.c Index: compiler-rt/trunk/lib/builtins/extendhfsf2.c =================================================================== --- compiler-rt/trunk/lib/builtins/extendhfsf2.c +++ compiler-rt/trunk/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); +} Index: compiler-rt/trunk/lib/builtins/fp_extend.h =================================================================== --- compiler-rt/trunk/lib/builtins/fp_extend.h +++ compiler-rt/trunk/lib/builtins/fp_extend.h @@ -39,11 +39,24 @@ #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 @@ 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 Index: compiler-rt/trunk/lib/builtins/fp_extend_impl.inc =================================================================== --- compiler-rt/trunk/lib/builtins/fp_extend_impl.inc +++ compiler-rt/trunk/lib/builtins/fp_extend_impl.inc @@ -66,7 +66,9 @@ 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. Index: compiler-rt/trunk/lib/builtins/fp_trunc.h =================================================================== --- compiler-rt/trunk/lib/builtins/fp_trunc.h +++ compiler-rt/trunk/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 @@ #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 Index: compiler-rt/trunk/lib/builtins/truncdfhf2.c =================================================================== --- compiler-rt/trunk/lib/builtins/truncdfhf2.c +++ compiler-rt/trunk/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); +} Index: compiler-rt/trunk/lib/builtins/truncsfhf2.c =================================================================== --- compiler-rt/trunk/lib/builtins/truncsfhf2.c +++ compiler-rt/trunk/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); +} Index: compiler-rt/trunk/test/builtins/Unit/extendhfsf2_test.c =================================================================== --- compiler-rt/trunk/test/builtins/Unit/extendhfsf2_test.c +++ compiler-rt/trunk/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 + +#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; +} Index: compiler-rt/trunk/test/builtins/Unit/fp_test.h =================================================================== --- compiler-rt/trunk/test/builtins/Unit/fp_test.h +++ compiler-rt/trunk/test/builtins/Unit/fp_test.h @@ -19,6 +19,11 @@ 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 @@ 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 @@ 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 @@ return ""; } +static inline uint16_t makeQNaN16() +{ + return fromRep16(0x7e00U); +} + static inline float makeQNaN32() { return fromRep32(0x7fc00000U); @@ -192,6 +225,11 @@ 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 @@ return fromRep128(0x7fff000000000000UL | (rand & 0xffffffffffffUL), 0x0UL); } +static inline uint16_t makeInf16() +{ + return fromRep16(0x7c00U); +} + static inline float makeInf32() { return fromRep32(0x7f800000U); Index: compiler-rt/trunk/test/builtins/Unit/truncdfhf2_test.c =================================================================== --- compiler-rt/trunk/test/builtins/Unit/truncdfhf2_test.c +++ compiler-rt/trunk/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 + +#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; +} Index: compiler-rt/trunk/test/builtins/Unit/truncsfhf2_test.c =================================================================== --- compiler-rt/trunk/test/builtins/Unit/truncsfhf2_test.c +++ compiler-rt/trunk/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 + +#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; +}