Index: compiler-rt/trunk/lib/builtins/floatsitf.c =================================================================== --- compiler-rt/trunk/lib/builtins/floatsitf.c +++ compiler-rt/trunk/lib/builtins/floatsitf.c @@ -0,0 +1,52 @@ +//===-- 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. +// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) +COMPILER_RT_ABI 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; + unsigned aAbs = (unsigned)a; + if (a < 0) { + sign = signBit; + aAbs += 0x80000000; + } + + // 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)aAbs << shift ^ implicitBit; + + // Insert the exponent + result += (rep_t)(exponent + exponentBias) << significandBits; + // Insert the sign bit and return + return fromRep(result | sign); +} + +#endif Index: compiler-rt/trunk/lib/builtins/floatunsitf.c =================================================================== --- compiler-rt/trunk/lib/builtins/floatunsitf.c +++ compiler-rt/trunk/lib/builtins/floatunsitf.c @@ -0,0 +1,40 @@ +//===-- 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. +// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) +COMPILER_RT_ABI 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: compiler-rt/trunk/test/builtins/Unit/floatsitf_test.c =================================================================== --- compiler-rt/trunk/test/builtins/Unit/floatsitf_test.c +++ compiler-rt/trunk/test/builtins/Unit/floatsitf_test.c @@ -0,0 +1,58 @@ +//===--------------- 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 __LDBL_MANT_DIG__ == 113 + +#include "fp_test.h" + +long double __floatsitf(int a); + +int test__floatsitf(int a, uint64_t expectedHi, uint64_t expectedLo) +{ + long double x = __floatsitf(a); + int ret = compareResultLD(x, expectedHi, expectedLo); + + if (ret) + { + printf("error in test__floatsitf(%d) = %.20Lf, " + "expected %.20Lf\n", a, x, fromRep128(expectedHi, expectedLo)); + } + return ret; +} + +char assumption_1[sizeof(long double) * CHAR_BIT == 128] = {0}; + +#endif + +int main() +{ +#if __LDBL_MANT_DIG__ == 113 + if (test__floatsitf(0x7fffffff, UINT64_C(0x401dfffffffc0000), UINT64_C(0x0))) + return 1; + if (test__floatsitf(0, UINT64_C(0x0), UINT64_C(0x0))) + return 1; + if (test__floatsitf(0xffffffff, UINT64_C(0xbfff000000000000), UINT64_C(0x0))) + return 1; + if (test__floatsitf(0x12345678, UINT64_C(0x401b234567800000), UINT64_C(0x0))) + return 1; + if (test__floatsitf(-0x12345678, UINT64_C(0xc01b234567800000), UINT64_C(0x0))) + return 1; + +#else + printf("skipped\n"); + +#endif + return 0; +} Index: compiler-rt/trunk/test/builtins/Unit/floatunsitf_test.c =================================================================== --- compiler-rt/trunk/test/builtins/Unit/floatunsitf_test.c +++ compiler-rt/trunk/test/builtins/Unit/floatunsitf_test.c @@ -0,0 +1,55 @@ +//===--------------- 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 __LDBL_MANT_DIG__ == 113 + +#include "fp_test.h" + +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 = compareResultLD(x, expectedHi, expectedLo); + + if (ret){ + printf("error in test__floatunsitf(%u) = %.20Lf, " + "expected %.20Lf\n", a, x, fromRep128(expectedHi, expectedLo)); + } + return ret; +} + +char assumption_1[sizeof(long double) * CHAR_BIT == 128] = {0}; + +#endif + +int main() +{ +#if __LDBL_MANT_DIG__ == 113 + if (test__floatunsitf(0x7fffffff, UINT64_C(0x401dfffffffc0000), UINT64_C(0x0))) + return 1; + if (test__floatunsitf(0, UINT64_C(0x0), UINT64_C(0x0))) + return 1; + if (test__floatunsitf(0xffffffff, UINT64_C(0x401efffffffe0000), UINT64_C(0x0))) + return 1; + if (test__floatunsitf(0x12345678, UINT64_C(0x401b234567800000), UINT64_C(0x0))) + return 1; + +#else + printf("skipped\n"); + +#endif + return 0; +}