Index: lib/builtins/floatsitf.c =================================================================== --- /dev/null +++ lib/builtins/floatsitf.c @@ -0,0 +1,53 @@ +//===-- lib/floatsitf.c - integer -> quad-precision 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. +// +//===----------------------------------------------------------------------===// +// +// This file implements integer to quad-precision conversion for the +// compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even +// mode. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" +#ifdef CRT_HAS_128BIT + +#define QUAD_PRECISION +#include "fp_lib.h" + +fp_t __floatsitf(int a) { + + const int aWidth = sizeof a * CHAR_BIT; + + // Handle zero as a special case to protect clz + if (a == 0) + return fromRep(0); + + // All other cases begin by extracting the sign and absolute value of a + rep_t sign = 0; + if (a < 0) { + sign = signBit; + a = -a; + } + + // Exponent of (fp_t)a is the width of abs(a). + const int exponent = (aWidth - 1) - __builtin_clz(a); + rep_t result; + + // Shift a into the significand field and clear the implicit bit. Extra + // cast to unsigned int is necessary to get the correct behavior for + // the input INT_MIN. + const int shift = significandBits - exponent; + result = (rep_t)(unsigned int)a << shift ^ implicitBit; + + // Insert the exponent + result += (rep_t)(exponent + exponentBias) << significandBits; + // Insert the sign bit and return + return fromRep(result | sign); +} + +#endif Index: lib/builtins/floatunsitf.c =================================================================== --- /dev/null +++ lib/builtins/floatunsitf.c @@ -0,0 +1,42 @@ +//===-- lib/floatunsitf.c - uint -> quad-precision 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. +// +//===----------------------------------------------------------------------===// +// +// This file implements unsigned integer to quad-precision conversion for the +// compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even +// mode. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" +#ifdef CRT_HAS_128BIT + +#define QUAD_PRECISION +#include "fp_lib.h" + +fp_t __floatunsitf(unsigned int a) { + + const int aWidth = sizeof a * CHAR_BIT; + + // Handle zero as a special case to protect clz + if (a == 0) return fromRep(0); + + // Exponent of (fp_t)a is the width of abs(a). + const int exponent = (aWidth - 1) - __builtin_clz(a); + rep_t result; + + // Shift a into the significand field and clear the implicit bit. + const int shift = significandBits - exponent; + result = (rep_t)a << shift ^ implicitBit; + + // Insert the exponent + result += (rep_t)(exponent + exponentBias) << significandBits; + return fromRep(result); +} + +#endif Index: test/builtins/Unit/floatsitf_test.c =================================================================== --- /dev/null +++ test/builtins/Unit/floatsitf_test.c @@ -0,0 +1,96 @@ +//===--------------- floatsitf_test.c - Test __floatsitf ------------------===// +// +// 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 __floatsitf for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include + +#if __LP64__ + +#include +#include + +static inline long double fromRep(uint64_t hi, uint64_t lo) +{ + __uint128_t x = ((__uint128_t)hi << 64) + lo; + const union {long double f; __uint128_t i; } rep = {.i = x}; + return rep.f; +} + +static inline __uint128_t toRep(long double x) +{ + const union {long double f; __uint128_t i;} rep = {.f = x}; + return rep.i; +} + +// return 0 if equal +// use two 64-bit integers intead of one 128-bit integer +// because 128-bit integer constant can't be assigned directly +static inline int compareResult(long double result, + uint64_t expectedHi, + uint64_t expectedLo) +{ + __uint128_t rep = toRep(result); + uint64_t hi = rep >> 64; + uint64_t lo = rep; + + if (hi == expectedHi && lo == expectedLo){ + return 0; + } + // test other posible NaN representation(signal NaN) + else if (expectedHi == 0x7fff800000000000UL && expectedLo == 0x0UL){ + if ((hi & 0x7fff000000000000UL) == 0x7fff000000000000UL && + ((hi & 0xffffffffffff) > 0 || lo > 0)){ + return 0; + } + } + return 1; +} + +long double __floatsitf(int a); + +int test__floatsitf(int a, uint64_t expectedHi, uint64_t expectedLo) +{ + long double x = __floatsitf(a); + int ret = compareResult(x, expectedHi, expectedLo); + + if (ret) + { + printf("error in test__floatsitf(%d) = %.20Lf, " + "expected %.20Lf\n", a, x, fromRep(expectedHi, expectedLo)); + } + return ret; +} + +char assumption_1[sizeof(long double) * CHAR_BIT == 128] = {0}; + +#endif + +int main() +{ +#if __LP64__ + if (test__floatsitf(0x7fffffff, 0x401dfffffffc0000UL, 0x0UL)) + return 1; + if (test__floatsitf(0, 0x0UL, 0x0UL)) + return 1; + if (test__floatsitf(0xffffffff, 0xbfff000000000000UL, 0x0UL)) + return 1; + if (test__floatsitf(0x12345678, 0x401b234567800000UL, 0x0UL)) + return 1; + if (test__floatsitf(-0x12345678, 0xc01b234567800000UL, 0x0UL)) + return 1; + +#else + printf("skipped\n"); + +#endif + return 0; +} Index: test/builtins/Unit/floatunsitf_test.c =================================================================== --- /dev/null +++ test/builtins/Unit/floatunsitf_test.c @@ -0,0 +1,93 @@ +//===--------------- floatunsitf_test.c - Test __floatunsitf --------------===// +// +// 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 __floatunsitf for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include + +#if __LP64__ + +#include +#include + +static inline long double fromRep(uint64_t hi, uint64_t lo) +{ + __uint128_t x = ((__uint128_t)hi << 64) + lo; + const union {long double f; __uint128_t i; } rep = {.i = x}; + return rep.f; +} + +static inline __uint128_t toRep(long double x) +{ + const union {long double f; __uint128_t i;} rep = {.f = x}; + return rep.i; +} + +// return 0 if equal +// use two 64-bit integers intead of one 128-bit integer +// because 128-bit integer constant can't be assigned directly +static inline int compareResult(long double result, + uint64_t expectedHi, + uint64_t expectedLo) +{ + __uint128_t rep = toRep(result); + uint64_t hi = rep >> 64; + uint64_t lo = rep; + + if (hi == expectedHi && lo == expectedLo){ + return 0; + } + // test other posible NaN representation(signal NaN) + else if (expectedHi == 0x7fff800000000000UL && expectedLo == 0x0UL){ + if ((hi & 0x7fff000000000000UL) == 0x7fff000000000000UL && + ((hi & 0xffffffffffff) > 0 || lo > 0)){ + return 0; + } + } + return 1; +} + +long double __floatunsitf(unsigned int a); + +int test__floatunsitf(unsigned int a, uint64_t expectedHi, uint64_t expectedLo) +{ + long double x = __floatunsitf(a); + int ret = compareResult(x, expectedHi, expectedLo); + + if (ret){ + printf("error in test__floatunsitf(%u) = %.20Lf, " + "expected %.20Lf\n", a, x, fromRep(expectedHi, expectedLo)); + } + return ret; +} + +char assumption_1[sizeof(long double) * CHAR_BIT == 128] = {0}; + +#endif + +int main() +{ +#if __LP64__ + if (test__floatunsitf(0x7fffffff, 0x401dfffffffc0000UL, 0x0UL)) + return 1; + if (test__floatunsitf(0, 0x0UL, 0x0UL)) + return 1; + if (test__floatunsitf(0xffffffff, 0x401efffffffe0000UL, 0x0UL)) + return 1; + if (test__floatunsitf(0x12345678, 0x401b234567800000UL, 0x0UL)) + return 1; + +#else + printf("skipped\n"); + +#endif + return 0; +}