[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

llvm-svn: 237161
This commit is contained in:
Ahmed Bougacha 2015-05-12 18:33:42 +00:00
parent 7a38d75bcd
commit f1ac850e77
11 changed files with 470 additions and 5 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}