From d982553daf42bbedf3373c35d8db2d3fd323f9b0 Mon Sep 17 00:00:00 2001 From: Ahmed Bougacha Date: Tue, 12 May 2015 18:33:42 +0000 Subject: [Builtins] Implement half-precision conversions. Mostly uninteresting, except: - in __extendXfYf2, when checking if the number is normal, the old code relied on the unsignedness of src_rep_t, which is a problem when sizeof(src_rep_t) < sizeof(int): the result gets promoted to int, the signedness of which breaks the comparison. I added an explicit cast; it shouldn't affect other types. - we can't pass __fp16, so src_t and src_rep_t are the same. - the gnu_*_ieee symbols are simply duplicated definitions, as aliases are problematic on mach-o (where only weak aliases are supported; that's not what we want). Differential Revision: http://reviews.llvm.org/D9693 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@237161 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/builtins/CMakeLists.txt | 3 +++ lib/builtins/extendhfsf2.c | 21 +++++++++++++++++++++ lib/builtins/fp_extend.h | 19 ++++++++++++++++--- lib/builtins/fp_extend_impl.inc | 4 +++- lib/builtins/fp_trunc.h | 14 +++++++++++++- lib/builtins/truncdfhf2.c | 16 ++++++++++++++++ lib/builtins/truncsfhf2.c | 20 ++++++++++++++++++++ 7 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 lib/builtins/extendhfsf2.c create mode 100644 lib/builtins/truncdfhf2.c create mode 100644 lib/builtins/truncsfhf2.c (limited to 'lib') 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); +} -- cgit v1.2.3