From 5960f7654980710b11e30ad78b283cd3d9438a06 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 19 Apr 2024 02:21:56 +0200 Subject: [PATCH 3/6] totalordermagf: New module. * lib/math.in.h (totalordermagf): New declaration. * lib/totalordermagf.c: New file, based on lib/totalorderf.c. * m4/math_h.m4 (gl_MATH_H): Test whether totalordermagf is declared. (gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOTALORDERMAGF. (gl_MATH_H_DEFAULTS): Initialize HAVE_TOTALORDERMAGF, REPLACE_TOTALORDERMAGF. * modules/math (Makefile.am): Substitute GNULIB_TOTALORDERMAGF, HAVE_TOTALORDERMAGF, REPLACE_TOTALORDERMAGF. * modules/totalordermagf: New file, based on modules/totalorderf. * doc/posix-functions/totalordermagf.texi: Mention the new module. --- ChangeLog | 14 ++++ doc/posix-functions/totalordermagf.texi | 10 +-- lib/math.in.h | 25 +++++++ lib/totalordermagf.c | 95 +++++++++++++++++++++++++ m4/math_h.m4 | 7 +- modules/math | 3 + modules/totalordermagf | 39 ++++++++++ 7 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 lib/totalordermagf.c create mode 100644 modules/totalordermagf diff --git a/ChangeLog b/ChangeLog index 8ddc7cf6ba..42d2c6cbc1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2024-04-18 Bruno Haible + + totalordermagf: New module. + * lib/math.in.h (totalordermagf): New declaration. + * lib/totalordermagf.c: New file, based on lib/totalorderf.c. + * m4/math_h.m4 (gl_MATH_H): Test whether totalordermagf is declared. + (gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOTALORDERMAGF. + (gl_MATH_H_DEFAULTS): Initialize HAVE_TOTALORDERMAGF, + REPLACE_TOTALORDERMAGF. + * modules/math (Makefile.am): Substitute GNULIB_TOTALORDERMAGF, + HAVE_TOTALORDERMAGF, REPLACE_TOTALORDERMAGF. + * modules/totalordermagf: New file, based on modules/totalorderf. + * doc/posix-functions/totalordermagf.texi: Mention the new module. + 2024-04-18 Bruno Haible totalordermag: Add tests. diff --git a/doc/posix-functions/totalordermagf.texi b/doc/posix-functions/totalordermagf.texi index 7bec6ff62e..1bd0d56ade 100644 --- a/doc/posix-functions/totalordermagf.texi +++ b/doc/posix-functions/totalordermagf.texi @@ -10,14 +10,10 @@ @url{https://www.gnu.org/software/libc/manual/html_node/FP-Comparison-Functions.html}. @end ifnotinfo -Gnulib module: --- +Gnulib module: totalordermagf Portability problems fixed by Gnulib: @itemize -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item This function is missing on all non-glibc platforms: glibc 2.24, macOS 11.1, FreeBSD 14.0, NetBSD 10.0, OpenBSD 6.7, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, Solaris 11.4, Cygwin 2.9, mingw, MSVC 14, Android 9.0. @@ -25,3 +21,7 @@ This function has a different signature on some platforms: glibc 2.30. @end itemize + +Portability problems not fixed by Gnulib: +@itemize +@end itemize diff --git a/lib/math.in.h b/lib/math.in.h index c814d2c89e..43a9674fff 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -3006,6 +3006,31 @@ _GL_WARN_ON_USE (totalorderl, "totalorderl is unportable - " #endif +#if @GNULIB_TOTALORDERMAGF@ +# if @REPLACE_TOTALORDERMAGF@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef totalordermagf +# define totalordermagf rpl_totalordermagf +# endif +_GL_FUNCDECL_RPL (totalordermagf, int, (float const *, float const *)); +_GL_CXXALIAS_RPL (totalordermagf, int, (float const *, float const *)); +# else +# if !@HAVE_TOTALORDERMAGF@ +_GL_FUNCDECL_SYS (totalordermagf, int, (float const *, float const *)); +# endif +_GL_CXXALIAS_SYS (totalordermagf, int, (float const *, float const *)); +# endif +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN1 (totalordermagf, int, (float const *, float const *)); +# endif +#elif defined GNULIB_POSIXCHECK +# undef totalordermagf +# if HAVE_RAW_DECL_TOTALORDERMAGF +_GL_WARN_ON_USE (totalordermagf, "totalordermagf is unportable - " + "use gnulib module totalordermagf for portability"); +# endif +#endif + #if @GNULIB_TOTALORDERMAG@ # if @REPLACE_TOTALORDERMAG@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) diff --git a/lib/totalordermagf.c b/lib/totalordermagf.c new file mode 100644 index 0000000000..07a35c9689 --- /dev/null +++ b/lib/totalordermagf.c @@ -0,0 +1,95 @@ +/* Total order of absolute value for 'float'. + Copyright 2023-2024 Free Software Foundation, Inc. + + This file 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 3 of the + License, or (at your option) any later version. + + This file 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 this program. If not, see . */ + +/* Written by Paul Eggert and Bruno Haible. */ + +#include + +/* Specification. */ +#include + +#include + +#include "verify.h" + +int +totalordermagf (float const *x, float const *y) +{ + /* If one of *X, *Y is a NaN and the other isn't, the answer is easy: + the NaN is "greater" than the other argument. */ + int xn = isnanf (*x); + int yn = isnanf (*y); + if (!xn != !yn) + return yn; + /* If none of *X, *Y is a NaN, the '<=' operator on the absolute values + does the job, including for -Infinity and +Infinity. */ + if (!xn) + return (signbit (*x) ? - *x : *x) <= (signbit (*y) ? - *y : *y); + + /* At this point, *X and *Y are NaNs. */ + +#if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT + /* The use of a union to extract the bits of the representation of a + 'float' is safe in practice, despite of the "aliasing rules" of + C99, because the GCC docs say + "Even with '-fstrict-aliasing', type-punning is allowed, provided the + memory is accessed through the union type." + and similarly for other compilers. */ +# define NWORDS \ + ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)) + union { unsigned int word[NWORDS]; float value; } + xu = {0}, yu = {0}; + +# if 0 + xu.value = *x; + yu.value = *y; +# else +# if defined __GNUC__ || defined __clang__ + /* Prevent gcc and clang from reusing the values of *x and *y (fetched above) + in optimized inlined memcpy expansions. + Seen with gcc + and with clang 16.0.6 on OpenBSD 7.5. */ + __asm__ __volatile__ ("" : : : "memory"); +# endif + /* On 32-bit x86 processors, as well as on x86_64 processors with + CC="gcc -mfpmath=387", the evaluation of *x and *y above is done through + an 'flds' instruction, which converts a signalling NaN to a quiet NaN. See + + for details. Use memcpy to avoid this. */ + memcpy (&xu.value, x, sizeof (float)); + memcpy (&yu.value, y, sizeof (float)); +# endif + + verify (NWORDS == 1); + + return ((xu.word[0] & ~(1U << FLT_SIGNBIT_BIT)) /* *X without its sign bit */ +# if defined __hppa || (defined __mips__ && !MIPS_NAN2008_FLOAT) || defined __sh__ + /* Invert the most significant bit of the mantissa field. + Cf. snan.h. */ + ^ (1U << 22) +# endif + ) <= + ((yu.word[0] & ~(1U << FLT_SIGNBIT_BIT)) /* *Y without its sign bit */ +# if defined __hppa || (defined __mips__ && !MIPS_NAN2008_FLOAT) || defined __sh__ + /* Invert the most significant bit of the mantissa field. + Cf. snan.h. */ + ^ (1U << 22) +# endif + ); +#else +# error "Please port gnulib totalordermagf.c to your platform!" +#endif +} diff --git a/m4/math_h.m4 b/m4/math_h.m4 index b28c827958..d07813a016 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -1,5 +1,5 @@ # math_h.m4 -# serial 136 +# serial 137 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -57,7 +57,7 @@ AC_DEFUN_ONCE([gl_MATH_H] setpayloadsig setpayloadsigf setpayloadsigl sinf sinl sinhf sqrtf sqrtl tanf tanl tanhf totalorder totalorderf totalorderl totalordermag - trunc truncf truncl]) + totalordermagf trunc truncf truncl]) ]) # gl_MATH_MODULE_INDICATOR([modulename]) @@ -185,6 +185,7 @@ AC_DEFUN([gl_MATH_H_REQUIRE_DEFAULTS] gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOTALORDERF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOTALORDERL]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOTALORDERMAG]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOTALORDERMAGF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TRUNC]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TRUNCF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TRUNCL]) @@ -276,6 +277,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS] HAVE_TOTALORDERF=1; AC_SUBST([HAVE_TOTALORDERF]) HAVE_TOTALORDERL=1; AC_SUBST([HAVE_TOTALORDERL]) HAVE_TOTALORDERMAG=1; AC_SUBST([HAVE_TOTALORDERMAG]) + HAVE_TOTALORDERMAGF=1; AC_SUBST([HAVE_TOTALORDERMAGF]) HAVE_DECL_ACOSL=1; AC_SUBST([HAVE_DECL_ACOSL]) HAVE_DECL_ASINL=1; AC_SUBST([HAVE_DECL_ASINL]) HAVE_DECL_ATANL=1; AC_SUBST([HAVE_DECL_ATANL]) @@ -396,6 +398,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS] REPLACE_TOTALORDERF=0; AC_SUBST([REPLACE_TOTALORDERF]) REPLACE_TOTALORDERL=0; AC_SUBST([REPLACE_TOTALORDERL]) REPLACE_TOTALORDERMAG=0; AC_SUBST([REPLACE_TOTALORDERMAG]) + REPLACE_TOTALORDERMAGF=0; AC_SUBST([REPLACE_TOTALORDERMAGF]) REPLACE_TRUNC=0; AC_SUBST([REPLACE_TRUNC]) REPLACE_TRUNCF=0; AC_SUBST([REPLACE_TRUNCF]) REPLACE_TRUNCL=0; AC_SUBST([REPLACE_TRUNCL]) diff --git a/modules/math b/modules/math index a82a77a9d0..04fa287209 100644 --- a/modules/math +++ b/modules/math @@ -145,6 +145,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_TOTALORDERF''@/$(GNULIB_TOTALORDERF)/g' \ -e 's/@''GNULIB_TOTALORDERL''@/$(GNULIB_TOTALORDERL)/g' \ -e 's/@''GNULIB_TOTALORDERMAG''@/$(GNULIB_TOTALORDERMAG)/g' \ + -e 's/@''GNULIB_TOTALORDERMAGF''@/$(GNULIB_TOTALORDERMAGF)/g' \ -e 's/@''GNULIB_MDA_J0''@/$(GNULIB_MDA_J0)/g' \ -e 's/@''GNULIB_MDA_J1''@/$(GNULIB_MDA_J1)/g' \ -e 's/@''GNULIB_MDA_JN''@/$(GNULIB_MDA_JN)/g' \ @@ -226,6 +227,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''HAVE_TOTALORDERF''@|$(HAVE_TOTALORDERF)|g' \ -e 's|@''HAVE_TOTALORDERL''@|$(HAVE_TOTALORDERL)|g' \ -e 's|@''HAVE_TOTALORDERMAG''@|$(HAVE_TOTALORDERMAG)|g' \ + -e 's|@''HAVE_TOTALORDERMAGF''@|$(HAVE_TOTALORDERMAGF)|g' \ < $@-t2 > $@-t3 $(AM_V_at)sed \ -e 's|@''HAVE_DECL_ACOSL''@|$(HAVE_DECL_ACOSL)|g' \ @@ -353,6 +355,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''REPLACE_TOTALORDERF''@|$(REPLACE_TOTALORDERF)|g' \ -e 's|@''REPLACE_TOTALORDERL''@|$(REPLACE_TOTALORDERL)|g' \ -e 's|@''REPLACE_TOTALORDERMAG''@|$(REPLACE_TOTALORDERMAG)|g' \ + -e 's|@''REPLACE_TOTALORDERMAGF''@|$(REPLACE_TOTALORDERMAGF)|g' \ -e 's|@''REPLACE_TRUNC''@|$(REPLACE_TRUNC)|g' \ -e 's|@''REPLACE_TRUNCF''@|$(REPLACE_TRUNCF)|g' \ -e 's|@''REPLACE_TRUNCL''@|$(REPLACE_TRUNCL)|g' \ diff --git a/modules/totalordermagf b/modules/totalordermagf new file mode 100644 index 0000000000..0686bb4e01 --- /dev/null +++ b/modules/totalordermagf @@ -0,0 +1,39 @@ +Description: +totalordermagf function: total order of absolute value on float + +Files: +lib/totalordermagf.c +m4/mathfunc.m4 +m4/totalordermag.m4 +m4/nan-mips.m4 +m4/signbit.m4 + +Depends-on: +math +extensions +verify [test $HAVE_TOTALORDERMAGF = 0 || test $REPLACE_TOTALORDERMAGF = 1] +isnanf [test $HAVE_TOTALORDERMAGF = 0 || test $REPLACE_TOTALORDERMAGF = 1] +signbit [test $HAVE_TOTALORDERMAGF = 0 || test $REPLACE_TOTALORDERMAGF = 1] + +configure.ac: +gl_FUNC_TOTALORDERMAGF +gl_CONDITIONAL([GL_COND_OBJ_TOTALORDERMAGF], + [test $HAVE_TOTALORDERMAGF = 0 || test $REPLACE_TOTALORDERMAGF = 1]) +gl_MATH_MODULE_INDICATOR([totalordermagf]) + +Makefile.am: +if GL_COND_OBJ_TOTALORDERMAGF +lib_SOURCES += totalordermagf.c +endif + +Include: + + +Link: +$(TOTALORDERMAGF_LIBM) + +License: +LGPL + +Maintainer: +all -- 2.34.1