diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -136,6 +136,7 @@ libc.src.math.ldexp libc.src.math.ldexpf libc.src.math.ldexpl + libc.src.math.logf libc.src.math.logb libc.src.math.logbf libc.src.math.logbl diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -139,6 +139,7 @@ libc.src.math.llround libc.src.math.llroundf libc.src.math.llroundl + libc.src.math.logf libc.src.math.logb libc.src.math.logbf libc.src.math.logbl diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt --- a/libc/config/windows/entrypoints.txt +++ b/libc/config/windows/entrypoints.txt @@ -118,6 +118,7 @@ libc.src.math.llround libc.src.math.llroundf libc.src.math.llroundl + libc.src.math.logf libc.src.math.logb libc.src.math.logbf libc.src.math.logbl diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -396,6 +396,8 @@ FunctionSpec<"ldexpf", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"ldexpl", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"logf", RetValSpec, [ArgSpec]>, + FunctionSpec<"logb", RetValSpec, [ArgSpec]>, FunctionSpec<"logbf", RetValSpec, [ArgSpec]>, FunctionSpec<"logbl", RetValSpec, [ArgSpec]>, diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -116,6 +116,8 @@ add_math_entrypoint_object(ldexpf) add_math_entrypoint_object(ldexpl) +add_math_entrypoint_object(logf) + add_math_entrypoint_object(logb) add_math_entrypoint_object(logbf) add_math_entrypoint_object(logbl) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -645,6 +645,18 @@ -O2 ) +add_entrypoint_object( + logf + SRCS + logf.cpp + HDRS + ../logf.h + DEPENDS + libc.src.__support.FPUtil.fputil + COMPILE_OPTIONS + -O3 +) + add_entrypoint_object( logb SRCS diff --git a/libc/src/math/generic/logf.cpp b/libc/src/math/generic/logf.cpp new file mode 100644 --- /dev/null +++ b/libc/src/math/generic/logf.cpp @@ -0,0 +1,202 @@ +//===-- Single-precision log(x) function ----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/logf.h" +#include "src/__support/FPUtil/BasicOperations.h" +#include "src/__support/FPUtil/FMA.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/PolyEval.h" +#include "src/__support/common.h" + +// This is a correctly-rounded algorithm for log(x) in single precision with +// round-to-nearest, tie-to-even mode from the RLIBM project at: +// https://people.cs.rutgers.edu/~sn349/rlibm + +// Step 1 - Range reduction: +// For x = 2^m * 1.mant, log(x) = m * log(2) + log(1.m) +// If x is denormal, we normalize it by multiplying x by 2^23 and subtracting +// m by 23. + +// Step 2 - Another range reduction: +// To compute log(1.mant), let f be the highest 8 bits including the hidden +// bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the +// mantissa. Then we have the following approximation formula: +// log(1.mant) = log(f) + log(1.mant / f) +// = log(f) + log(1 + d/f) +// ~ log(f) + P(d/f) +// since d/f is sufficiently small. +// log(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables. + +// Step 3 - Polynomial approximation: +// To compute P(d/f), we use a single degree-5 polynomial in double precision +// which provides correct rounding for all but few exception values. +// For more detail about how this polynomial is obtained, please refer to the +// paper: +// Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce +// Correctly Rounded Results of an Elementary Function for Multiple +// Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN +// Symposium on Principles of Programming Languages (POPL-2022), Philadelphia, +// USA, January 16-22, 2022. +// https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf + +namespace __llvm_libc { + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++17-extensions" + +// Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127. +static constexpr double LOG_F[128] = { + 0x0.0000000000000p+0, 0x1.fe02a6b106788p-8, 0x1.fc0a8b0fc03e3p-7, + 0x1.7b91b07d5b11ap-6, 0x1.f829b0e783300p-6, 0x1.39e87b9febd5fp-5, + 0x1.77458f632dcfcp-5, 0x1.b42dd711971bep-5, 0x1.f0a30c01162a6p-5, + 0x1.16536eea37ae0p-4, 0x1.341d7961bd1d0p-4, 0x1.51b073f06183fp-4, + 0x1.6f0d28ae56b4bp-4, 0x1.8c345d6319b20p-4, 0x1.a926d3a4ad563p-4, + 0x1.c5e548f5bc743p-4, 0x1.e27076e2af2e5p-4, 0x1.fec9131dbeabap-4, + 0x1.0d77e7cd08e59p-3, 0x1.1b72ad52f67a0p-3, 0x1.29552f81ff523p-3, + 0x1.371fc201e8f74p-3, 0x1.44d2b6ccb7d1ep-3, 0x1.526e5e3a1b437p-3, + 0x1.5ff3070a793d3p-3, 0x1.6d60fe719d21cp-3, 0x1.7ab890210d909p-3, + 0x1.87fa06520c910p-3, 0x1.9525a9cf456b4p-3, 0x1.a23bc1fe2b563p-3, + 0x1.af3c94e80bff2p-3, 0x1.bc286742d8cd6p-3, 0x1.c8ff7c79a9a21p-3, + 0x1.d5c216b4fbb91p-3, 0x1.e27076e2af2e5p-3, 0x1.ef0adcbdc5936p-3, + 0x1.fb9186d5e3e2ap-3, 0x1.0402594b4d040p-2, 0x1.0a324e27390e3p-2, + 0x1.1058bf9ae4ad5p-2, 0x1.1675cababa60ep-2, 0x1.1c898c16999fap-2, + 0x1.22941fbcf7965p-2, 0x1.2895a13de86a3p-2, 0x1.2e8e2bae11d30p-2, + 0x1.347dd9a987d54p-2, 0x1.3a64c556945e9p-2, 0x1.404308686a7e3p-2, + 0x1.4618bc21c5ec2p-2, 0x1.4be5f957778a0p-2, 0x1.51aad872df82dp-2, + 0x1.5767717455a6cp-2, 0x1.5d1bdbf5809cap-2, 0x1.62c82f2b9c795p-2, + 0x1.686c81e9b14aep-2, 0x1.6e08eaa2ba1e3p-2, 0x1.739d7f6bbd006p-2, + 0x1.792a55fdd47a2p-2, 0x1.7eaf83b82afc3p-2, 0x1.842d1da1e8b17p-2, + 0x1.89a3386c1425ap-2, 0x1.8f11e873662c7p-2, 0x1.947941c2116fap-2, + 0x1.99d958117e08ap-2, 0x1.9f323ecbf984bp-2, 0x1.a484090e5bb0ap-2, + 0x1.a9cec9a9a0849p-2, 0x1.af1293247786bp-2, 0x1.b44f77bcc8f62p-2, + 0x1.b9858969310fbp-2, 0x1.beb4d9da71b7bp-2, 0x1.c3dd7a7cdad4dp-2, + 0x1.c8ff7c79a9a21p-2, 0x1.ce1af0b85f3ebp-2, 0x1.d32fe7e00ebd5p-2, + 0x1.d83e7258a2f3ep-2, 0x1.dd46a04c1c4a0p-2, 0x1.e24881a7c6c26p-2, + 0x1.e744261d68787p-2, 0x1.ec399d2468cc0p-2, 0x1.f128f5faf06ecp-2, + 0x1.f6123fa7028acp-2, 0x1.faf588f78f31ep-2, 0x1.ffd2e0857f498p-2, + 0x1.02552a5a5d0fep-1, 0x1.04bdf9da926d2p-1, 0x1.0723e5c1cdf40p-1, + 0x1.0986f4f573520p-1, 0x1.0be72e4252a82p-1, 0x1.0e44985d1cc8bp-1, + 0x1.109f39e2d4c96p-1, 0x1.12f719593efbcp-1, 0x1.154c3d2f4d5e9p-1, + 0x1.179eabbd899a0p-1, 0x1.19ee6b467c96ep-1, 0x1.1c3b81f713c24p-1, + 0x1.1e85f5e7040d0p-1, 0x1.20cdcd192ab6dp-1, 0x1.23130d7bebf42p-1, + 0x1.2555bce98f7cbp-1, 0x1.2795e1289b11ap-1, 0x1.29d37fec2b08ap-1, + 0x1.2c0e9ed448e8bp-1, 0x1.2e47436e40268p-1, 0x1.307d7334f10bep-1, + 0x1.32b1339121d71p-1, 0x1.34e289d9ce1d3p-1, 0x1.37117b54747b5p-1, + 0x1.393e0d3562a19p-1, 0x1.3b68449fffc22p-1, 0x1.3d9026a7156fap-1, + 0x1.3fb5b84d16f42p-1, 0x1.41d8fe84672aep-1, 0x1.43f9fe2f9ce67p-1, + 0x1.4618bc21c5ec2p-1, 0x1.48353d1ea88dfp-1, 0x1.4a4f85db03ebbp-1, + 0x1.4c679afccee39p-1, 0x1.4e7d811b75bb0p-1, 0x1.50913cc01686bp-1, + 0x1.52a2d265bc5aap-1, 0x1.54b2467999497p-1, 0x1.56bf9d5b3f399p-1, + 0x1.58cadb5cd7989p-1, 0x1.5ad404c359f2cp-1, 0x1.5cdb1dc6c1764p-1, + 0x1.5ee02a9241675p-1, 0x1.60e32f44788d8p-1}; + +// Lookup table for (1/f) where f = 1 + n*2^(-7), n = 0..127. +static constexpr double ONE_OVER_F[128] = { + 0x1.0000000000000p+0, 0x1.fc07f01fc07f0p-1, 0x1.f81f81f81f820p-1, + 0x1.f44659e4a4271p-1, 0x1.f07c1f07c1f08p-1, 0x1.ecc07b301ecc0p-1, + 0x1.e9131abf0b767p-1, 0x1.e573ac901e574p-1, 0x1.e1e1e1e1e1e1ep-1, + 0x1.de5d6e3f8868ap-1, 0x1.dae6076b981dbp-1, 0x1.d77b654b82c34p-1, + 0x1.d41d41d41d41dp-1, 0x1.d0cb58f6ec074p-1, 0x1.cd85689039b0bp-1, + 0x1.ca4b3055ee191p-1, 0x1.c71c71c71c71cp-1, 0x1.c3f8f01c3f8f0p-1, + 0x1.c0e070381c0e0p-1, 0x1.bdd2b899406f7p-1, 0x1.bacf914c1bad0p-1, + 0x1.b7d6c3dda338bp-1, 0x1.b4e81b4e81b4fp-1, 0x1.b2036406c80d9p-1, + 0x1.af286bca1af28p-1, 0x1.ac5701ac5701bp-1, 0x1.a98ef606a63bep-1, + 0x1.a6d01a6d01a6dp-1, 0x1.a41a41a41a41ap-1, 0x1.a16d3f97a4b02p-1, + 0x1.9ec8e951033d9p-1, 0x1.9c2d14ee4a102p-1, 0x1.999999999999ap-1, + 0x1.970e4f80cb872p-1, 0x1.948b0fcd6e9e0p-1, 0x1.920fb49d0e229p-1, + 0x1.8f9c18f9c18fap-1, 0x1.8d3018d3018d3p-1, 0x1.8acb90f6bf3aap-1, + 0x1.886e5f0abb04ap-1, 0x1.8618618618618p-1, 0x1.83c977ab2beddp-1, + 0x1.8181818181818p-1, 0x1.7f405fd017f40p-1, 0x1.7d05f417d05f4p-1, + 0x1.7ad2208e0ecc3p-1, 0x1.78a4c8178a4c8p-1, 0x1.767dce434a9b1p-1, + 0x1.745d1745d1746p-1, 0x1.724287f46debcp-1, 0x1.702e05c0b8170p-1, + 0x1.6e1f76b4337c7p-1, 0x1.6c16c16c16c17p-1, 0x1.6a13cd1537290p-1, + 0x1.6816816816817p-1, 0x1.661ec6a5122f9p-1, 0x1.642c8590b2164p-1, + 0x1.623fa77016240p-1, 0x1.6058160581606p-1, 0x1.5e75bb8d015e7p-1, + 0x1.5c9882b931057p-1, 0x1.5ac056b015ac0p-1, 0x1.58ed2308158edp-1, + 0x1.571ed3c506b3ap-1, 0x1.5555555555555p-1, 0x1.5390948f40febp-1, + 0x1.51d07eae2f815p-1, 0x1.5015015015015p-1, 0x1.4e5e0a72f0539p-1, + 0x1.4cab88725af6ep-1, 0x1.4afd6a052bf5bp-1, 0x1.49539e3b2d067p-1, + 0x1.47ae147ae147bp-1, 0x1.460cbc7f5cf9ap-1, 0x1.446f86562d9fbp-1, + 0x1.42d6625d51f87p-1, 0x1.4141414141414p-1, 0x1.3fb013fb013fbp-1, + 0x1.3e22cbce4a902p-1, 0x1.3c995a47babe7p-1, 0x1.3b13b13b13b14p-1, + 0x1.3991c2c187f63p-1, 0x1.3813813813814p-1, 0x1.3698df3de0748p-1, + 0x1.3521cfb2b78c1p-1, 0x1.33ae45b57bcb2p-1, 0x1.323e34a2b10bfp-1, + 0x1.30d190130d190p-1, 0x1.2f684bda12f68p-1, 0x1.2e025c04b8097p-1, + 0x1.2c9fb4d812ca0p-1, 0x1.2b404ad012b40p-1, 0x1.29e4129e4129ep-1, + 0x1.288b01288b013p-1, 0x1.27350b8812735p-1, 0x1.25e22708092f1p-1, + 0x1.2492492492492p-1, 0x1.23456789abcdfp-1, 0x1.21fb78121fb78p-1, + 0x1.20b470c67c0d9p-1, 0x1.1f7047dc11f70p-1, 0x1.1e2ef3b3fb874p-1, + 0x1.1cf06ada2811dp-1, 0x1.1bb4a4046ed29p-1, 0x1.1a7b9611a7b96p-1, + 0x1.19453808ca29cp-1, 0x1.1811811811812p-1, 0x1.16e0689427379p-1, + 0x1.15b1e5f75270dp-1, 0x1.1485f0e0acd3bp-1, 0x1.135c81135c811p-1, + 0x1.12358e75d3033p-1, 0x1.1111111111111p-1, 0x1.0fef010fef011p-1, + 0x1.0ecf56be69c90p-1, 0x1.0db20a88f4696p-1, 0x1.0c9714fbcda3bp-1, + 0x1.0b7e6ec259dc8p-1, 0x1.0a6810a6810a7p-1, 0x1.0953f39010954p-1, + 0x1.0842108421084p-1, 0x1.073260a47f7c6p-1, 0x1.0624dd2f1a9fcp-1, + 0x1.05197f7d73404p-1, 0x1.0410410410410p-1, 0x1.03091b51f5e1ap-1, + 0x1.0204081020408p-1, 0x1.0101010101010p-1}; + +INLINE_FMA +LLVM_LIBC_FUNCTION(float, logf, (float x)) { + constexpr double LOG_2 = 0x1.62e42fefa39efp-1; + using FPBits = typename fputil::FPBits; + FPBits xbits(x); + int m = 0; + + if (xbits.uintval() < FPBits::MIN_NORMAL || + xbits.uintval() > FPBits::MAX_NORMAL) { + if (xbits.is_zero()) { + return static_cast(FPBits::neg_inf()); + } + if (xbits.get_sign() && !xbits.is_nan()) { + return FPBits::build_nan(1 << (fputil::MantissaWidth::VALUE - 1)); + } + if (xbits.is_inf_or_nan()) { + return x; + } + // Normalize denormal inputs. + xbits.val *= 0x1.0p23f; + m = -23; + } + + m += xbits.get_exponent(); + // Set bits to 1.m + xbits.set_unbiased_exponent(0x7F); + int fIndex = xbits.get_mantissa() >> 16; + + FPBits f(xbits.val); + f.bits &= ~0x0000'FFFF; + + double d = static_cast(xbits) - static_cast(f); + d *= ONE_OVER_F[fIndex]; + + double r = __llvm_libc::fputil::polyeval( + d, 0x1.0000000008169p+0, -0x1.0000004f78405p-1, 0x1.555654d2bc769p-2, + -0x1.00a570d090322p-2, 0x1.e158d823f89cap-3); + + double extra_factor = + __llvm_libc::fputil::fma(static_cast(m), LOG_2, LOG_F[fIndex]); + switch (FPBits(x).uintval()) { + case 0x3f80d19f: + return 0x1.a1e82cp-8f; + case 0x41178feb: + return 0x1.1fcbcep+1f; + case 0x4c5d65a5: + return 0x1.1e0696p+4f; + case 0x65d890d3: + return 0x1.a9a3f2p+5f; + case 0x6f31a8ec: + return 0x1.08b512p+6f; + default: + return static_cast(__llvm_libc::fputil::fma(d, r, extra_factor)); + } +} + +#pragma clang diagnostic pop + +} // namespace __llvm_libc diff --git a/libc/src/math/logf.h b/libc/src/math/logf.h new file mode 100644 --- /dev/null +++ b/libc/src/math/logf.h @@ -0,0 +1,18 @@ +//===-- Implementation header for logf --------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_MATH_LOGF_H +#define LLVM_LIBC_SRC_MATH_LOGF_H + +namespace __llvm_libc { + +float logf(float x); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_MATH_LOGF_H diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -1181,6 +1181,19 @@ libc.src.__support.FPUtil.fputil ) +add_fp_unittest( + logf_test + NEED_MPFR + SUITE + libc_math_unittests + SRCS + logf_test.cpp + DEPENDS + libc.include.math + libc.src.math.logf + libc.src.__support.FPUtil.fputil +) + add_subdirectory(generic) add_subdirectory(exhaustive) add_subdirectory(differential_testing) diff --git a/libc/test/src/math/differential_testing/CMakeLists.txt b/libc/test/src/math/differential_testing/CMakeLists.txt --- a/libc/test/src/math/differential_testing/CMakeLists.txt +++ b/libc/test/src/math/differential_testing/CMakeLists.txt @@ -227,6 +227,28 @@ -fno-builtin ) +add_diff_binary( + logf_diff + SRCS + logf_diff.cpp + DEPENDS + .single_input_single_output_diff + libc.src.math.logf + COMPILE_OPTIONS + -fno-builtin +) + +add_diff_binary( + logf_perf + SRCS + logf_perf.cpp + DEPENDS + .single_input_single_output_diff + libc.src.math.logf + COMPILE_OPTIONS + -fno-builtin +) + add_diff_binary( logbf_diff SRCS diff --git a/libc/test/src/math/differential_testing/logf_diff.cpp b/libc/test/src/math/differential_testing/logf_diff.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/differential_testing/logf_diff.cpp @@ -0,0 +1,16 @@ +//===-- Differential test for logf ----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SingleInputSingleOutputDiff.h" + +#include "src/math/logf.h" + +#include + +SINGLE_INPUT_SINGLE_OUTPUT_DIFF(float, __llvm_libc::logf, ::logf, + "logf_diff.log") diff --git a/libc/test/src/math/differential_testing/logf_perf.cpp b/libc/test/src/math/differential_testing/logf_perf.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/differential_testing/logf_perf.cpp @@ -0,0 +1,16 @@ +//===-- Differential test for logf ----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SingleInputSingleOutputDiff.h" + +#include "src/math/logf.h" + +#include + +SINGLE_INPUT_SINGLE_OUTPUT_PERF(float, __llvm_libc::logf, ::logf, + "logf_perf.log") diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt --- a/libc/test/src/math/exhaustive/CMakeLists.txt +++ b/libc/test/src/math/exhaustive/CMakeLists.txt @@ -51,3 +51,16 @@ libc.src.math.expm1f libc.src.__support.FPUtil.fputil ) + +add_fp_unittest( + logf_test + NEED_MPFR + SUITE + libc_math_exhaustive_tests + SRCS + logf_test.cpp + DEPENDS + libc.include.math + libc.src.math.logf + libc.src.__support.FPUtil.fputil +) diff --git a/libc/test/src/math/exhaustive/logf_test.cpp b/libc/test/src/math/exhaustive/logf_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/exhaustive/logf_test.cpp @@ -0,0 +1,26 @@ +//===-- Exhaustive test for logf ------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/__support/FPUtil/FPBits.h" +#include "src/math/logf.h" +#include "utils/MPFRWrapper/MPFRUtils.h" +#include "utils/UnitTest/FPMatcher.h" +#include "utils/UnitTest/Test.h" + +using FPBits = __llvm_libc::fputil::FPBits; + +namespace mpfr = __llvm_libc::testing::mpfr; + +TEST(LlvmLibcLogfExhaustiveTest, AllValues) { + uint32_t bits = 0U; + do { + FPBits xbits(bits); + float x = float(xbits); + EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5); + } while (bits++ < 0x7f7f'ffffU); +} diff --git a/libc/test/src/math/logf_test.cpp b/libc/test/src/math/logf_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/logf_test.cpp @@ -0,0 +1,64 @@ +//===-- Unittests for logf-----------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/__support/FPUtil/FPBits.h" +#include "src/math/logf.h" +#include "utils/MPFRWrapper/MPFRUtils.h" +#include "utils/UnitTest/FPMatcher.h" +#include "utils/UnitTest/Test.h" +#include + +#include +#include + +namespace mpfr = __llvm_libc::testing::mpfr; + +DECLARE_SPECIAL_CONSTANTS(float) + +TEST(LlvmLibcLogfTest, SpecialNumbers) { + EXPECT_FP_EQ(aNaN, __llvm_libc::logf(aNaN)); + EXPECT_FP_EQ(inf, __llvm_libc::logf(inf)); + EXPECT_TRUE(isnan(__llvm_libc::logf(neg_inf))); + EXPECT_FP_EQ(neg_inf, __llvm_libc::logf(0.0f)); + EXPECT_FP_EQ(neg_inf, __llvm_libc::logf(-0.0f)); + EXPECT_TRUE(isnan(__llvm_libc::logf(-1.0f))); + EXPECT_FP_EQ(zero, __llvm_libc::logf(1.0f)); +} + +TEST(LlvmLibcLogfTest, TrickyInputs) { + constexpr int N = 24; + constexpr uint32_t INPUTS[N] = { + 0x3509dcf6U, 0x3bf86ef0U, 0x3ca1c99fU, 0x3d13e105U, 0x3f7ff1f2U, + 0x3f7fffffU, 0x3f800006U, 0x3f800014U, 0x3f80001cU, 0x3f80c777U, + 0x3f80ce72U, 0x3f80d19fU, 0x3f80f7bfU, 0x3f80fcfeU, 0x3f81feb4U, + 0x3f83d731U, 0x3f90cb1dU, 0x3fc55379U, 0x3fd364d7U, 0x41178febU, + 0x4c5d65a5U, 0x4e85f412U, 0x65d890d3U, 0x6f31a8ecU}; + for (int i = 0; i < N; ++i) { + float x = float(FPBits(INPUTS[i])); + EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5); + } +} + +TEST(LlvmLibcLogfTest, InFloatRange) { + constexpr uint32_t count = 1000000; + constexpr uint32_t step = UINT32_MAX / count; + for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) { + float x = float(FPBits(v)); + if (isnan(x) || isinf(x)) + continue; + errno = 0; + float result = __llvm_libc::logf(x); + // If the computation resulted in an error or did not produce valid result + // in the single-precision floating point range, then ignore comparing with + // MPFR result as MPFR can still produce valid results because of its + // wider precision. + if (isnan(result) || isinf(result) || errno != 0) + continue; + ASSERT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5); + } +} diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h --- a/libc/utils/MPFRWrapper/MPFRUtils.h +++ b/libc/utils/MPFRWrapper/MPFRUtils.h @@ -30,6 +30,7 @@ Exp2, Expm1, Floor, + Log, Mod2PI, ModPIOver2, ModPIOver4, diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -165,6 +165,12 @@ return result; } + MPFRNumber log() const { + MPFRNumber result; + mpfr_log(result.value, value, MPFR_RNDN); + return result; + } + MPFRNumber remquo(const MPFRNumber &divisor, int "ient) { MPFRNumber remainder; long q; @@ -385,6 +391,8 @@ return mpfrInput.expm1(); case Operation::Floor: return mpfrInput.floor(); + case Operation::Log: + return mpfrInput.log(); case Operation::Mod2PI: return mpfrInput.mod_2pi(); case Operation::ModPIOver2: