[PATCH 3/3] math: Consolidate coshf and sinhf internal tables

Andreas K. Huettel dilfridge@gentoo.org
Tue Feb 11 19:53:06 GMT 2025


Am Montag, 3. Februar 2025, 22:24:19 Mitteleuropäische Normalzeit schrieb Adhemerval Zanella:
> The libm size improvement built with "--enable-stack-protector=strong
> --enable-bind-now=yes --enable-fortify-source=2":
> 
> Before:
> 
>    text    data     bss     dec     hex filename
>  585192     860      12  586064   8f150 aarch64-linux-gnu/math/libm.so
>  960775    1068      12  961855   ead3f x86_64-linux-gnu/math/libm.so
> 1189174    5544     368 1195086  123c4e powerpc64le-linux-gnu/math/libm.so
> 
> After:
> 
>    text    data     bss     dec     hex filename
>  584952     860      12  585824   8f060 aarch64-linux-gnu/math/libm.so
>  960615    1068      12  961695   eac9f x86_64-linux-gnu/math/libm.so
> 1189078    5544     368 1194990  123bee powerpc64le-linux-gnu/math/libm.so
> 
> The are small code changes for x86_64 and powerpc64le, which do not
> affect performance; but on aarch64 with gcc-14 I see a slight better
> code generation due the usage of ldq for floating point constant loading.

LGTM

Reviewed-by: Andreas K. Huettel <dilfridge@gentoo.org>

> ---
>  math/Makefile                            |  1 +
>  sysdeps/ieee754/flt-32/e_coshf.c         | 38 ++++--------------
>  sysdeps/ieee754/flt-32/e_sincoshf_data.c | 49 ++++++++++++++++++++++++
>  sysdeps/ieee754/flt-32/e_sincoshf_data.h | 36 +++++++++++++++++
>  sysdeps/ieee754/flt-32/e_sinhf.c         | 37 ++++--------------
>  5 files changed, 101 insertions(+), 60 deletions(-)
>  create mode 100644 sysdeps/ieee754/flt-32/e_sincoshf_data.c
>  create mode 100644 sysdeps/ieee754/flt-32/e_sincoshf_data.h
> 
> diff --git a/math/Makefile b/math/Makefile
> index febeb253f6..a591cd59cb 100644
> --- a/math/Makefile
> +++ b/math/Makefile
> @@ -363,6 +363,7 @@ type-double-routines := \
>  type-float-suffix := f
>  type-float-routines := \
>    e_asincosf_data \
> +  e_sincoshf_data \
>    e_exp2f_data \
>    e_log2f_data \
>    e_logf_data \

ok

> diff --git a/sysdeps/ieee754/flt-32/e_coshf.c b/sysdeps/ieee754/flt-32/e_coshf.c
> index 602d2b413f..5f6ff8c29f 100644
> --- a/sysdeps/ieee754/flt-32/e_coshf.c
> +++ b/sysdeps/ieee754/flt-32/e_coshf.c
> @@ -28,34 +28,11 @@ SOFTWARE.
>  #include <math.h>
>  #include <libm-alias-finite.h>
>  #include "math_config.h"
> +#include "e_sincoshf_data.h"
>  
>  float
>  __ieee754_coshf (float x)
>  {
> -  static const double c[] =
> -    {
> -      1, 0x1.62e42fef4c4e7p-6, 0x1.ebfd1b232f475p-13, 0x1.c6b19384ecd93p-20
> -    };
> -  static const double ch[] =
> -    {
> -      1,                     0x1.62e42fefa39efp-6,  0x1.ebfbdff82c58fp-13,
> -      0x1.c6b08d702e0edp-20, 0x1.3b2ab6fb92e5ep-27, 0x1.5d886e6d54203p-35,
> -      0x1.430976b8ce6efp-43
> -    };
> -  static const uint64_t tb[] =
> -    {
> -      0x3fe0000000000000, 0x3fe059b0d3158574, 0x3fe0b5586cf9890f,
> -      0x3fe11301d0125b51, 0x3fe172b83c7d517b, 0x3fe1d4873168b9aa,
> -      0x3fe2387a6e756238, 0x3fe29e9df51fdee1, 0x3fe306fe0a31b715,
> -      0x3fe371a7373aa9cb, 0x3fe3dea64c123422, 0x3fe44e086061892d,
> -      0x3fe4bfdad5362a27, 0x3fe5342b569d4f82, 0x3fe5ab07dd485429,
> -      0x3fe6247eb03a5585, 0x3fe6a09e667f3bcd, 0x3fe71f75e8ec5f74,
> -      0x3fe7a11473eb0187, 0x3fe82589994cce13, 0x3fe8ace5422aa0db,
> -      0x3fe93737b0cdc5e5, 0x3fe9c49182a3f090, 0x3fea5503b23e255d,
> -      0x3feae89f995ad3ad, 0x3feb7f76f2fb5e47, 0x3fec199bdd85529c,
> -      0x3fecb720dcef9069, 0x3fed5818dcfba487, 0x3fedfc97337b9b5f,
> -      0x3feea4afa2a490da, 0x3fef50765b6e4540
> -    };
>    const double iln2 = 0x1.71547652b82fep+5;
>    double z = x;
>    uint32_t ax = asuint (x) << 1;

ok

> @@ -92,10 +69,10 @@ __ieee754_coshf (float x)
>    double h2 = h * h;
>    int64_t jp = asuint64 (ia + 0x1.8p52);
>    int64_t jm = -jp;
> -  double sp = asdouble (tb[jp & 31] + ((jp >> 5) << 52));
> -  double sm = asdouble (tb[jm & 31] + ((jm >> 5) << 52));
> -  double te = c[0] + h2 * c[2];
> -  double to = (c[1] + h2 * c[3]);
> +  double sp = asdouble (TB[jp & 31] + ((jp >> 5) << 52));
> +  double sm = asdouble (TB[jm & 31] + ((jm >> 5) << 52));
> +  double te = C[0] + h2 * C[2];
> +  double to = (C[1] + h2 * C[3]);
>    double rp = sp * (te + h * to);
>    double rm = sm * (te - h * to);
>    double r = rp + rm;

ok

> @@ -107,8 +84,9 @@ __ieee754_coshf (float x)
>        const double iln2l = 0x1.5c17f0bbbe88p-26;
>        h = (iln2h * z - ia) + iln2l * z;
>        h2 = h * h;
> -      te = ch[0] + h2 * ch[2] + (h2 * h2) * (ch[4] + h2 * ch[6]);
> -      to = ch[1] + h2 * (ch[3] + h2 * ch[5]);
> +      te = CH[0] + h2 * CH[2] + (h2 * h2) * (CH[4] + h2 * CH[6]);
> +      to = CH[1] + h2 * (CH[3] + h2
> +				     * CH[5]);
>        r = sp * (te + h * to) + sm * (te - h * to);
>        ub = r;
>      }

why the extra line?

> diff --git a/sysdeps/ieee754/flt-32/e_sincoshf_data.c b/sysdeps/ieee754/flt-32/e_sincoshf_data.c
> new file mode 100644
> index 0000000000..c1a34d1ab3
> --- /dev/null
> +++ b/sysdeps/ieee754/flt-32/e_sincoshf_data.c
> @@ -0,0 +1,49 @@
> +/* Common data for sinhf/coshf implementations.
> +
> +Copyright (c) 2022-2025 Alexei Sibidanov.
> +
> +Permission is hereby granted, free of charge, to any person obtaining a copy
> +of this software and associated documentation files (the "Software"), to deal
> +in the Software without restriction, including without limitation the rights
> +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> +copies of the Software, and to permit persons to whom the Software is
> +furnished to do so, subject to the following conditions:
> +
> +The above copyright notice and this permission notice shall be included in all
> +copies or substantial portions of the Software.
> +
> +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> +SOFTWARE.
> +*/
> +
> +#include "e_sincoshf_data.h"
> +
> +const double __sincoshf_c[] =
> +  {
> +    1, 0x1.62e42fef4c4e7p-6, 0x1.ebfd1b232f475p-13, 0x1.c6b19384ecd93p-20
> +  };

ok

> +const double __sincoshf_ch[] =
> +  {
> +    1,                     0x1.62e42fefa39efp-6,  0x1.ebfbdff82c58fp-13,
> +    0x1.c6b08d702e0edp-20, 0x1.3b2ab6fb92e5ep-27, 0x1.5d886e6d54203p-35,
> +    0x1.430976b8ce6efp-43
> +  };

ok (spot check)

> +const uint64_t __sincoshf_tb[] =
> +  {
> +    0x3fe0000000000000, 0x3fe059b0d3158574, 0x3fe0b5586cf9890f,
> +    0x3fe11301d0125b51, 0x3fe172b83c7d517b, 0x3fe1d4873168b9aa,
> +    0x3fe2387a6e756238, 0x3fe29e9df51fdee1, 0x3fe306fe0a31b715,
> +    0x3fe371a7373aa9cb, 0x3fe3dea64c123422, 0x3fe44e086061892d,
> +    0x3fe4bfdad5362a27, 0x3fe5342b569d4f82, 0x3fe5ab07dd485429,
> +    0x3fe6247eb03a5585, 0x3fe6a09e667f3bcd, 0x3fe71f75e8ec5f74,
> +    0x3fe7a11473eb0187, 0x3fe82589994cce13, 0x3fe8ace5422aa0db,
> +    0x3fe93737b0cdc5e5, 0x3fe9c49182a3f090, 0x3fea5503b23e255d,
> +    0x3feae89f995ad3ad, 0x3feb7f76f2fb5e47, 0x3fec199bdd85529c,
> +    0x3fecb720dcef9069, 0x3fed5818dcfba487, 0x3fedfc97337b9b5f,
> +    0x3feea4afa2a490da, 0x3fef50765b6e4540
> +  };

ok (spot check)

> diff --git a/sysdeps/ieee754/flt-32/e_sincoshf_data.h b/sysdeps/ieee754/flt-32/e_sincoshf_data.h
> new file mode 100644
> index 0000000000..ea4b38089f
> --- /dev/null
> +++ b/sysdeps/ieee754/flt-32/e_sincoshf_data.h
> @@ -0,0 +1,36 @@
> +/* Common data for sinhf/coshf implementations.
> +
> +Copyright (c) 2022-2025 Alexei Sibidanov.
> +
> +Permission is hereby granted, free of charge, to any person obtaining a copy
> +of this software and associated documentation files (the "Software"), to deal
> +in the Software without restriction, including without limitation the rights
> +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> +copies of the Software, and to permit persons to whom the Software is
> +furnished to do so, subject to the following conditions:
> +
> +The above copyright notice and this permission notice shall be included in all
> +copies or substantial portions of the Software.
> +
> +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> +SOFTWARE.
> +*/
> +
> +#ifndef _SINCOSHF_H
> +#define _SINCOSHF_H
> +
> +#include <stdint.h>
> +
> +extern const double __sincoshf_c[] attribute_hidden;
> +#define C __sincoshf_c
> +extern const double __sincoshf_ch[] attribute_hidden;
> +#define CH __sincoshf_ch
> +extern const uint64_t __sincoshf_tb[] attribute_hidden;
> +#define TB __sincoshf_tb
> +
> +#endif

ok

> diff --git a/sysdeps/ieee754/flt-32/e_sinhf.c b/sysdeps/ieee754/flt-32/e_sinhf.c
> index dee96fc7cb..754b84abe0 100644
> --- a/sysdeps/ieee754/flt-32/e_sinhf.c
> +++ b/sysdeps/ieee754/flt-32/e_sinhf.c
> @@ -29,34 +29,11 @@ SOFTWARE.
>  #include <libm-alias-finite.h>
>  #include <math-narrow-eval.h>
>  #include "math_config.h"
> +#include "e_sincoshf_data.h"
>  
>  float
>  __ieee754_sinhf (float x)
>  {
> -  static const double c[] =
> -    {
> -      1, 0x1.62e42fef4c4e7p-6, 0x1.ebfd1b232f475p-13, 0x1.c6b19384ecd93p-20
> -    };

ok

> -  static const double ch[] =
> -    {
> -      1,                     0x1.62e42fefa39efp-6,  0x1.ebfbdff82c58fp-13,
> -      0x1.c6b08d702e0edp-20, 0x1.3b2ab6fb92e5ep-27, 0x1.5d886e6d54203p-35,
> -      0x1.430976b8ce6efp-43
> -    };

ok (spot check)

> -  static const uint64_t tb[] =
> -    {
> -      0x3fe0000000000000, 0x3fe059b0d3158574, 0x3fe0b5586cf9890f,
> -      0x3fe11301d0125b51, 0x3fe172b83c7d517b, 0x3fe1d4873168b9aa,
> -      0x3fe2387a6e756238, 0x3fe29e9df51fdee1, 0x3fe306fe0a31b715,
> -      0x3fe371a7373aa9cb, 0x3fe3dea64c123422, 0x3fe44e086061892d,
> -      0x3fe4bfdad5362a27, 0x3fe5342b569d4f82, 0x3fe5ab07dd485429,
> -      0x3fe6247eb03a5585, 0x3fe6a09e667f3bcd, 0x3fe71f75e8ec5f74,
> -      0x3fe7a11473eb0187, 0x3fe82589994cce13, 0x3fe8ace5422aa0db,
> -      0x3fe93737b0cdc5e5, 0x3fe9c49182a3f090, 0x3fea5503b23e255d,
> -      0x3feae89f995ad3ad, 0x3feb7f76f2fb5e47, 0x3fec199bdd85529c,
> -      0x3fecb720dcef9069, 0x3fed5818dcfba487, 0x3fedfc97337b9b5f,
> -      0x3feea4afa2a490da, 0x3fef50765b6e4540
> -    };

ok (spot check)

>    static const struct
>    {
>      uint32_t uarg;
> @@ -106,10 +83,10 @@ __ieee754_sinhf (float x)
>    double h2 = h * h;
>    int64_t jp = asuint64 (ia + 0x1.8p52);
>    int64_t jm = -jp;
> -  double sp = asdouble (tb[jp & 31] + ((jp >> 5) << 52));
> -  double sm = asdouble (tb[jm & 31] + ((jm >> 5) << 52));
> -  double te = c[0] + h2 * c[2];
> -  double to = (c[1] + h2 * c[3]);
> +  double sp = asdouble (TB[jp & 31] + ((jp >> 5) << 52));
> +  double sm = asdouble (TB[jm & 31] + ((jm >> 5) << 52));
> +  double te = C[0] + h2 * C[2];
> +  double to = (C[1] + h2 * C[3]);
>    double rp = sp * (te + h * to);
>    double rm = sm * (te - h * to);
>    double r = rp - rm;

ok

> @@ -121,8 +98,8 @@ __ieee754_sinhf (float x)
>        const double iln2l = 0x1.5c17f0bbbe88p-26;
>        h = (iln2h * z - ia) + iln2l * z;
>        h2 = h * h;
> -      te = ch[0] + h2 * ch[2] + (h2 * h2) * (ch[4] + h2 * ch[6]);
> -      to = ch[1] + h2 * (ch[3] + h2 * ch[5]);
> +      te = CH[0] + h2 * CH[2] + (h2 * h2) * (CH[4] + h2 * CH[6]);
> +      to = CH[1] + h2 * (CH[3] + h2 * CH[5]);
>        r = sp * (te + h * to) - sm * (te - h * to);
>        ub = r;
>      }
> 

ok

-- 
Andreas K. Hüttel
dilfridge@gentoo.org
Gentoo Linux developer
(council, toolchain, base-system, perl, libreoffice)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 981 bytes
Desc: This is a digitally signed message part.
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20250211/a314457d/attachment-0001.sig>


More information about the Libc-alpha mailing list