Index: lib/builtins/CMakeLists.txt =================================================================== --- lib/builtins/CMakeLists.txt +++ lib/builtins/CMakeLists.txt @@ -51,6 +51,9 @@ cmpti2.c comparedf2.c comparesf2.c + compiler_rt_logb.c + compiler_rt_logbf.c + compiler_rt_logbl.c ctzdi2.c ctzsi2.c ctzti2.c Index: lib/builtins/compiler_rt_logb.c =================================================================== --- /dev/null +++ lib/builtins/compiler_rt_logb.c @@ -0,0 +1,20 @@ +//===-- lib/builtins/compiler_rt_logb.c - floating point logb -----*- 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 double precision soft-float logb method for IEEE-754. +// This avoids pulling in a libm dependency from compiler-rt, but is not meant +// to replace it (i.e. code calling logb() should get the one from libm, not +// this), hence the __compiler_rt prefix. +// +//===----------------------------------------------------------------------===// +#define DOUBLE_PRECISION +#include "fp_logb_impl.inc" + +COMPILER_RT_ABI fp_t __compiler_rt_logb(fp_t x) { + return __compiler_rt_logbX(x); +} Index: lib/builtins/compiler_rt_logbf.c =================================================================== --- /dev/null +++ lib/builtins/compiler_rt_logbf.c @@ -0,0 +1,21 @@ +//===-- lib/builtins/compiler_rt_logbf.c - floating point logb ----*- 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 single precision soft-float logbf method for IEEE-754. +// This avoids pulling in a libm dependency from compiler-rt, but is not meant +// to replace it (i.e. code calling logbf() should get the one from libm, not +// this), hence the __compiler_rt prefix. +// +//===----------------------------------------------------------------------===// + +#define SINGLE_PRECISION +#include "fp_logb_impl.inc" + +COMPILER_RT_ABI fp_t __compiler_rt_logbf(fp_t x) { + return __compiler_rt_logbX(x); +} Index: lib/builtins/compiler_rt_logbl.c =================================================================== --- /dev/null +++ lib/builtins/compiler_rt_logbl.c @@ -0,0 +1,36 @@ +//===-- lib/builtins/compiler_rt_logbl.c - floating point logb ----*- 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 long double precision soft-float logbl method for +// IEEE-754. This avoids pulling in a libm dependency from compiler-rt, but is +// not meant to replace it (i.e. code calling logbl() should get the one from +// libm, not this), hence the __compiler_rt prefix. +// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include "fp_lib.h" + +#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) +#include "fp_logb_impl.inc" + +COMPILER_RT_ABI fp_t __compiler_rt_logbl(fp_t x) { + return __compiler_rt_logbX(x); +} + +#else // !(defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)) +// Rely on the libm implementation, for now. +COMPILER_RT_ABI long double __compiler_rt_logbl(long double x) { +#if defined(_MSC_VER) && !defined(__clang__) + return logbl(x) +#else // !(defined(_MSC_VER) && !defined(__clang__)) + return __builtin_logbl(x); +#endif +} +#endif Index: lib/builtins/divdc3.c =================================================================== --- lib/builtins/divdc3.c +++ lib/builtins/divdc3.c @@ -12,6 +12,8 @@ * ===----------------------------------------------------------------------=== */ +#define DOUBLE_PRECISION +#include "fp_lib.h" #include "int_lib.h" #include "int_math.h" @@ -21,7 +23,7 @@ __divdc3(double __a, double __b, double __c, double __d) { int __ilogbw = 0; - double __logbw = crt_logb(crt_fmax(crt_fabs(__c), crt_fabs(__d))); + double __logbw = __compiler_rt_logb(crt_fmax(crt_fabs(__c), crt_fabs(__d))); if (crt_isfinite(__logbw)) { __ilogbw = (int)__logbw; Index: lib/builtins/divsc3.c =================================================================== --- lib/builtins/divsc3.c +++ lib/builtins/divsc3.c @@ -12,6 +12,8 @@ *===----------------------------------------------------------------------=== */ +#define SINGLE_PRECISION +#include "fp_lib.h" #include "int_lib.h" #include "int_math.h" @@ -21,7 +23,8 @@ __divsc3(float __a, float __b, float __c, float __d) { int __ilogbw = 0; - float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d))); + float __logbw = + __compiler_rt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d))); if (crt_isfinite(__logbw)) { __ilogbw = (int)__logbw; Index: lib/builtins/divtc3.c =================================================================== --- lib/builtins/divtc3.c +++ lib/builtins/divtc3.c @@ -12,6 +12,8 @@ *===----------------------------------------------------------------------=== */ +#define QUAD_PRECISION +#include "fp_lib.h" #include "int_lib.h" #include "int_math.h" @@ -21,7 +23,8 @@ __divtc3(long double __a, long double __b, long double __c, long double __d) { int __ilogbw = 0; - long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d))); + long double __logbw = + __compiler_rt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d))); if (crt_isfinite(__logbw)) { __ilogbw = (int)__logbw; Index: lib/builtins/divxc3.c =================================================================== --- lib/builtins/divxc3.c +++ lib/builtins/divxc3.c @@ -13,6 +13,8 @@ #if !_ARCH_PPC +#define QUAD_PRECISION +#include "fp_lib.h" #include "int_lib.h" #include "int_math.h" @@ -22,7 +24,8 @@ __divxc3(long double __a, long double __b, long double __c, long double __d) { int __ilogbw = 0; - long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d))); + long double __logbw = + __compiler_rt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d))); if (crt_isfinite(__logbw)) { __ilogbw = (int)__logbw; Index: lib/builtins/fp_lib.h =================================================================== --- lib/builtins/fp_lib.h +++ lib/builtins/fp_lib.h @@ -57,6 +57,7 @@ *lo = product; } COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b); +COMPILER_RT_ABI fp_t __compiler_rt_logbf(fp_t x); #elif defined DOUBLE_PRECISION @@ -100,6 +101,7 @@ #undef hiWord COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b); +COMPILER_RT_ABI fp_t __compiler_rt_logb(fp_t x); #elif defined QUAD_PRECISION #if __LDBL_MANT_DIG__ == 113 @@ -207,6 +209,13 @@ #undef Word_HiMask #undef Word_LoMask #undef Word_FullMask + +COMPILER_RT_ABI fp_t __compiler_rt_logbl(fp_t x); + +#else // __LDBL_MANT_DIG__ != 113 +// Manually export as fp_t == long double for currently unsupported archs +// (e.g. PPC where __LDBL_MANT_DIG__ == 106). +COMPILER_RT_ABI long double __compiler_rt_logbl(long double x); #endif // __LDBL_MANT_DIG__ == 113 #else #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined. Index: lib/builtins/fp_logb_impl.inc =================================================================== --- /dev/null +++ lib/builtins/fp_logb_impl.inc @@ -0,0 +1,47 @@ +//===---- lib/fp_logb_impl.inc - floating point logb --------------*- 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 logb methods for IEEE-754. This avoids pulling in a libm +// dependency from compiler-rt, but is not meant to replace it (i.e. code +// calling logb() should get the one from libm, not this), hence the +// __compiler_rt prefix. +// +//===----------------------------------------------------------------------===// + +#include "fp_lib.h" + +static __inline fp_t __compiler_rt_logbX(fp_t x) { + rep_t rep = toRep(x); + int exp = (rep & exponentMask) >> significandBits; + + // Abnormal cases: + // 1) +/- inf returns +inf; NaN returns NaN + // 2) 0.0 returns -inf + if (exp == maxExponent) { + if (((rep & signBit) == 0) || (x != x)) { + return x; // NaN or +inf: return x + } else { + return -x; // -inf: return -x + } + } else if (x == 0.0) { + // 0.0: return -inf + return fromRep(infRep | signBit); + } + + if (exp != 0) { + // Normal number + return exp - exponentBias; // Unbias exponent + } else { + // Subnormal number; normalize and repeat + rep &= absMask; + const int shift = 1 - normalize(&rep); + exp = (rep & exponentMask) >> significandBits; + return exp - exponentBias - shift; // Unbias exponent + } +} Index: lib/builtins/int_math.h =================================================================== --- lib/builtins/int_math.h +++ lib/builtins/int_math.h @@ -91,16 +91,6 @@ #define crt_fmaxl(x, y) __builtin_fmaxl((x), (y)) #endif -#if defined(_MSC_VER) && !defined(__clang__) -#define crt_logb(x) logb((x)) -#define crt_logbf(x) logbf((x)) -#define crt_logbl(x) logbl((x)) -#else -#define crt_logb(x) __builtin_logb((x)) -#define crt_logbf(x) __builtin_logbf((x)) -#define crt_logbl(x) __builtin_logbl((x)) -#endif - #if defined(_MSC_VER) && !defined(__clang__) #define crt_scalbn(x, y) scalbn((x), (y)) #define crt_scalbnf(x, y) scalbnf((x), (y)) Index: lib/builtins/ppc/divtc3.c =================================================================== --- lib/builtins/ppc/divtc3.c +++ lib/builtins/ppc/divtc3.c @@ -4,6 +4,8 @@ #include "DD.h" #include "../int_math.h" +#define QUAD_PRECISION +#include "../fp_lib.h" #if !defined(CRT_INFINITY) && defined(HUGE_VAL) #define CRT_INFINITY HUGE_VAL @@ -21,9 +23,10 @@ DD dDD = { .ld = d }; int ilogbw = 0; - const double logbw = crt_logb(crt_fmax(crt_fabs(cDD.s.hi), crt_fabs(dDD.s.hi) )); - - if (crt_isfinite(logbw)) + const double logbw = __compiler_rt_logbl( + crt_fmax(crt_fabs(cDD.s.hi), crt_fabs(dDD.s.hi))); + + if (crt_isfinite(logbw)) { ilogbw = (int)logbw; Index: test/builtins/Unit/compiler_rt_logb_test.c =================================================================== --- /dev/null +++ test/builtins/Unit/compiler_rt_logb_test.c @@ -0,0 +1,63 @@ +// RUN: %clang_builtins %s %librt -lm -o %t && %run %t +//===-- compiler_rt_logb_test.c - Test __compiler_rt_logb -----------------===// +// +// 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 checks __compiler_rt_logb from the compiler_rt library for +// conformance against libm. +// +//===----------------------------------------------------------------------===// + +#define DOUBLE_PRECISION +#include +#include +#include "fp_lib.h" + +int test__compiler_rt_logb(fp_t x) { + fp_t crt_value = __compiler_rt_logb(x); + fp_t libm_value = logb(x); + // Compare actual rep, e.g. to avoid NaN != the same NaN + if (toRep(crt_value) != toRep(libm_value)) { + printf("error: in __compiler_rt_logb(%a [%lX]) = %a [%lX] != %a [%lX]\n", + x, toRep(x), crt_value, toRep(crt_value), libm_value, + toRep(libm_value)); + return 1; + } + return 0; +} + +double cases[] = { + 1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1, + -0.0, 0.0, 1, -2, 2, -0.5, 0.5, +}; + +int main() { + const unsigned N = sizeof(cases) / sizeof(cases[0]); + unsigned i; + for (i = 0; i < N; ++i) { + if (test__compiler_rt_logb(cases[i])) return 1; + } + + // Test a moving 1 bit, especially to handle denormal values. + // Test the negation as well. + rep_t x = signBit; + while (x) { + if (test__compiler_rt_logb(fromRep(x))) return 1; + if (test__compiler_rt_logb(fromRep(signBit ^ x))) return 1; + x >>= 1; + } + // Also try a couple moving ones + x = signBit | (signBit >> 1) | (signBit >> 2); + while (x) { + if (test__compiler_rt_logb(fromRep(x))) return 1; + if (test__compiler_rt_logb(fromRep(signBit ^ x))) return 1; + x >>= 1; + } + + return 0; +} Index: test/builtins/Unit/compiler_rt_logbf_test.c =================================================================== --- /dev/null +++ test/builtins/Unit/compiler_rt_logbf_test.c @@ -0,0 +1,63 @@ +// RUN: %clang_builtins %s %librt -lm -o %t && %run %t +//===-- compiler_rt_logbf_test.c - Test __compiler_rt_logbf ---------------===// +// +// 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 checks __compiler_rt_logbf from the compiler_rt library for +// conformance against libm. +// +//===----------------------------------------------------------------------===// + +#define SINGLE_PRECISION +#include +#include +#include "fp_lib.h" + +int test__compiler_rt_logbf(fp_t x) { + fp_t crt_value = __compiler_rt_logbf(x); + fp_t libm_value = logbf(x); + // Compare actual rep, e.g. to avoid NaN != the same NaN + if (toRep(crt_value) != toRep(libm_value)) { + printf("error: in __compiler_rt_logb(%a [%X]) = %a [%X] != %a [%X]\n", x, + toRep(x), crt_value, toRep(crt_value), libm_value, + toRep(libm_value)); + return 1; + } + return 0; +} + +double cases[] = { + 1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1, + -0.0, 0.0, 1, -2, 2, -0.5, 0.5, +}; + +int main() { + const unsigned N = sizeof(cases) / sizeof(cases[0]); + unsigned i; + for (i = 0; i < N; ++i) { + if (test__compiler_rt_logbf(cases[i])) return 1; + } + + // Test a moving 1 bit, especially to handle denormal values. + // Test the negation as well. + rep_t x = signBit; + while (x) { + if (test__compiler_rt_logbf(fromRep(x))) return 1; + if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1; + x >>= 1; + } + // Also try a couple moving ones + x = signBit | (signBit >> 1) | (signBit >> 2); + while (x) { + if (test__compiler_rt_logbf(fromRep(x))) return 1; + if (test__compiler_rt_logbf(fromRep(signBit ^ x))) return 1; + x >>= 1; + } + + return 0; +} Index: test/builtins/Unit/compiler_rt_logbl_test.c =================================================================== --- /dev/null +++ test/builtins/Unit/compiler_rt_logbl_test.c @@ -0,0 +1,79 @@ +// RUN: %clang_builtins %s %librt -lm -o %t && %run %t +//===-- compiler_rt_logbl_test.c - Test __compiler_rt_logbl ---------------===// +// +// 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 checks __compiler_rt_logbl from the compiler_rt library for +// conformance against libm. +// +//===----------------------------------------------------------------------===// + +#define QUAD_PRECISION +#include +#include +#include "fp_lib.h" +#include "int_lib.h" + +#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) + +int test__compiler_rt_logbl(fp_t x) { + fp_t crt_value = __compiler_rt_logbl(x); + fp_t libm_value = logbl(x); + // Compare actual rep, e.g. to avoid NaN != the same NaN + if (toRep(crt_value) != toRep(libm_value)) { + // Split expected values into two for printf + twords x_t, crt_value_t, libm_value_t; + x_t.all = toRep(x); + crt_value_t.all = toRep(crt_value); + libm_value_t.all = toRep(libm_value); + printf( + "error: in __compiler_rt_logb(%a [%llX %llX]) = %a [%llX %llX] != %a " + "[%llX %llX]\n", + x, x_t.s.high, x_t.s.low, crt_value, crt_value_t.s.high, + crt_value_t.s.low, libm_value, libm_value_t.s.high, libm_value_t.s.low); + return 1; + } + return 0; +} + +double cases[] = { + 1.e-6, -1.e-6, NAN, -NAN, INFINITY, -INFINITY, -1, + -0.0, 0.0, 1, -2, 2, -0.5, 0.5, +}; + +#endif + +int main() { +#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) + const unsigned N = sizeof(cases) / sizeof(cases[0]); + unsigned i; + for (i = 0; i < N; ++i) { + if (test__compiler_rt_logbl(cases[i])) return 1; + } + + // Test a moving 1 bit, especially to handle denormal values. + // Test the negation as well. + rep_t x = signBit; + while (x) { + if (test__compiler_rt_logbl(fromRep(x))) return 1; + if (test__compiler_rt_logbl(fromRep(signBit ^ x))) return 1; + x >>= 1; + } + // Also try a couple moving ones + x = signBit | (signBit >> 1) | (signBit >> 2); + while (x) { + if (test__compiler_rt_logbl(fromRep(x))) return 1; + if (test__compiler_rt_logbl(fromRep(signBit ^ x))) return 1; + x >>= 1; + } +#else + printf("skipped\n"); +#endif + + return 0; +}