mirror of
git://sourceware.org/git/glibc.git
synced 2025-04-18 14:30:43 +08:00
C23 adds various <math.h> function families originally defined in TS 18661-4. Add the powr functions, which are like pow, but with simpler handling of special cases (based on exp(y*log(x)), so negative x and 0^0 are domain errors, powers of -0 are always +0 or +Inf never -0 or -Inf, and 1^+-Inf and Inf^0 are also domain errors, while NaN^0 and 1^NaN are NaN). The test inputs are taken from those for pow, with appropriate adjustments (including removing all tests that would be domain errors from those in auto-libm-test-in and adding some more such tests in libm-test-powr.inc). The underlying implementation uses __ieee754_pow functions after dealing with all special cases that need to be handled differently. It might be a little faster (avoiding a wrapper and redundant checks for special cases) to have an underlying implementation built separately for both pow and powr with compile-time conditionals for special-case handling, but I expect the benefit of that would be limited given that both functions will end up needing to use the same logic for computing pow outside of special cases. My understanding is that powr(negative, qNaN) should raise "invalid": that the rule on "invalid" for an argument outside the domain of the function takes precedence over a quiet NaN argument producing a quiet NaN result with no exceptions raised (for rootn it's explicit that the 0th root of qNaN raises "invalid"). I've raised this on the WG14 reflector to confirm the intent. Tested for x86_64 and x86, and with build-many-glibcs.py.
51 lines
1.5 KiB
C
51 lines
1.5 KiB
C
/* Return X^Y, with special cases based on exp(Y*log(X)).
|
|
Copyright (C) 2025 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_private.h>
|
|
|
|
FLOAT
|
|
M_DECL_FUNC (__powr) (FLOAT x, FLOAT y)
|
|
{
|
|
if (isless (x, M_LIT (0.0))
|
|
|| (x == M_LIT (0.0) && y == M_LIT (0.0))
|
|
|| (x == M_LIT (1.0) && isinf (y))
|
|
|| (isinf (x) && y == M_LIT (0.0)))
|
|
{
|
|
__set_errno (EDOM);
|
|
return (x - x) / (x - x);
|
|
}
|
|
if (isnan (x) || isnan (y))
|
|
return x + y;
|
|
x = M_FABS (x);
|
|
FLOAT ret = M_SUF (__ieee754_pow) (x, y);
|
|
if (!isfinite (ret))
|
|
{
|
|
if (isfinite (x) && isfinite (y))
|
|
__set_errno (ERANGE);
|
|
}
|
|
else if (ret == 0
|
|
&& isfinite (x)
|
|
&& x != 0
|
|
&& isfinite (y))
|
|
__set_errno (ERANGE);
|
|
return ret;
|
|
}
|
|
declare_mgen_alias (__powr, powr);
|