glibc/math/s_log2p1_template.c
Joseph Myers 79c52daf47 Implement C23 log2p1
C23 adds various <math.h> function families originally defined in TS
18661-4.  Add the log2p1 functions (log2(1+x): like log1p, but for
base-2 logarithms).

This illustrates the intended structure of implementations of all
these function families: define them initially with a type-generic
template implementation.  If someone wishes to add type-specific
implementations, it is likely such implementations can be both faster
and more accurate than the type-generic one and can then override it
for types for which they are implemented (adding benchmarks would be
desirable in such cases to demonstrate that a new implementation is
indeed faster).

The test inputs are copied from those for log1p.  Note that these
changes make gen-auto-libm-tests depend on MPFR 4.2 (or later).

The bulk of the changes are fairly generic for any such new function.
(sysdeps/powerpc/nofpu/Makefile only needs changing for those
type-generic templates that use fabs.)

Tested for x86_64 and x86, and with build-many-glibcs.py.
2024-05-20 13:41:39 +00:00

44 lines
1.4 KiB
C

/* Return base-2 logarithm of 1 + X.
Copyright (C) 2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <math.h>
#include <math-underflow.h>
FLOAT
M_DECL_FUNC (__log2p1) (FLOAT x)
{
if (__glibc_unlikely (islessequal (x, M_LIT (-1.0))))
{
if (x == -1)
__set_errno (ERANGE);
else
__set_errno (EDOM);
}
if (isless (M_FABS (x), M_EPSILON / 4))
{
/* Avoid spurious underflows when log1p underflows but log2p1
should not. */
FLOAT ret = M_MLIT (M_LOG2E) * x;
math_check_force_underflow (ret);
return ret;
}
return M_MLIT (M_LOG2E) * M_SUF (__log1p) (x);
}
declare_mgen_alias (__log2p1, log2p1);