From 9b5f6e60d2b6ec1d995245ff628923e5a4e5d493 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 19 Apr 2024 02:21:21 +0200 Subject: [PATCH] totalordermag: New module. * lib/math.in.h (totalordermag): New declaration. * lib/totalordermag.c: New file, based on lib/totalorder.c. * m4/totalordermag.m4: New file, based on m4/totalorder.m4. * m4/math_h.m4 (gl_MATH_H): Test whether totalordermag is declared. (gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOTALORDERMAG. (gl_MATH_H_DEFAULTS): Initialize HAVE_TOTALORDERMAG, REPLACE_TOTALORDERMAG. * modules/math (Makefile.am): Substitute GNULIB_TOTALORDERMAG, HAVE_TOTALORDERMAG, REPLACE_TOTALORDERMAG. * modules/totalordermag: New file, based on modules/totalorder. * doc/posix-functions/totalordermag.texi: Mention the new module. --- ChangeLog | 15 +++ doc/posix-functions/totalordermag.texi | 10 +- lib/math.in.h | 26 +++++ lib/totalordermag.c | 95 +++++++++++++++++ m4/math_h.m4 | 8 +- m4/totalordermag.m4 | 137 +++++++++++++++++++++++++ modules/math | 3 + modules/totalordermag | 39 +++++++ 8 files changed, 326 insertions(+), 7 deletions(-) create mode 100644 lib/totalordermag.c create mode 100644 m4/totalordermag.m4 create mode 100644 modules/totalordermag diff --git a/ChangeLog b/ChangeLog index 8c90a81e0a..c2e2e40d97 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2024-04-18 Bruno Haible + + totalordermag: New module. + * lib/math.in.h (totalordermag): New declaration. + * lib/totalordermag.c: New file, based on lib/totalorder.c. + * m4/totalordermag.m4: New file, based on m4/totalorder.m4. + * m4/math_h.m4 (gl_MATH_H): Test whether totalordermag is declared. + (gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOTALORDERMAG. + (gl_MATH_H_DEFAULTS): Initialize HAVE_TOTALORDERMAG, + REPLACE_TOTALORDERMAG. + * modules/math (Makefile.am): Substitute GNULIB_TOTALORDERMAG, + HAVE_TOTALORDERMAG, REPLACE_TOTALORDERMAG. + * modules/totalordermag: New file, based on modules/totalorder. + * doc/posix-functions/totalordermag.texi: Mention the new module. + 2024-04-18 Bruno Haible setpayloadsig*: Support newer MIPS CPUs. diff --git a/doc/posix-functions/totalordermag.texi b/doc/posix-functions/totalordermag.texi index 49685f9e66..1e1d9154e3 100644 --- a/doc/posix-functions/totalordermag.texi +++ b/doc/posix-functions/totalordermag.texi @@ -10,14 +10,10 @@ Documentation:@* @url{https://www.gnu.org/software/libc/manual/html_node/FP-Comparison-Functions.html}. @end ifnotinfo -Gnulib module: --- +Gnulib module: totalordermag 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 @@ glibc 2.24, macOS 11.1, FreeBSD 14.0, NetBSD 10.0, OpenBSD 6.7, Minix 3.1.8, AIX 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 84b743e7ab..c814d2c89e 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -3006,6 +3006,32 @@ _GL_WARN_ON_USE (totalorderl, "totalorderl is unportable - " #endif +#if @GNULIB_TOTALORDERMAG@ +# if @REPLACE_TOTALORDERMAG@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef totalordermag +# define totalordermag rpl_totalordermag +# endif +_GL_FUNCDECL_RPL (totalordermag, int, (double const *, double const *)); +_GL_CXXALIAS_RPL (totalordermag, int, (double const *, double const *)); +# else +# if !@HAVE_TOTALORDERMAG@ +_GL_FUNCDECL_SYS (totalordermag, int, (double const *, double const *)); +# endif +_GL_CXXALIAS_SYS (totalordermag, int, (double const *, double const *)); +# endif +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN1 (totalordermag, int, (double const *, double const *)); +# endif +#elif defined GNULIB_POSIXCHECK +# undef totalordermag +# if HAVE_RAW_DECL_TOTALORDERMAG +_GL_WARN_ON_USE (totalordermag, "totalordermag is unportable - " + "use gnulib module totalordermag for portability"); +# endif +#endif + + _GL_INLINE_HEADER_END #endif /* _@GUARD_PREFIX@_MATH_H */ diff --git a/lib/totalordermag.c b/lib/totalordermag.c new file mode 100644 index 0000000000..7a52f89342 --- /dev/null +++ b/lib/totalordermag.c @@ -0,0 +1,95 @@ +/* Total order of absolute value for 'double'. + 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 +totalordermag (double const *x, double 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 = isnand (*x); + int yn = isnand (*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 DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT + /* The use of a union to extract the bits of the representation of a + 'double' 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 (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)) + union { unsigned int word[NWORDS]; unsigned long long i; double 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 'fldl' instruction, which converts a signalling NaN to a quiet NaN. See + + for details. Use memcpy to avoid this. */ + memcpy (&xu.value, x, sizeof (double)); + memcpy (&yu.value, y, sizeof (double)); +# endif + + verify (NWORDS == 2); + + return ((xu.i & ~(1ULL << (DBL_SIGNBIT_BIT + 32))) /* *X without its sign bit */ +# if defined __hppa || (defined __mips__ && !MIPS_NAN2008_DOUBLE) || defined __sh__ + /* Invert the most significant bit of the mantissa field. + Cf. snan.h. */ + ^ (1ULL << 51) +# endif + ) <= + ((yu.i & ~(1ULL << (DBL_SIGNBIT_BIT + 32))) /* *Y without its sign bit */ +# if defined __hppa || (defined __mips__ && !MIPS_NAN2008_DOUBLE) || defined __sh__ + /* Invert the most significant bit of the mantissa field. + Cf. snan.h. */ + ^ (1ULL << 51) +# 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 217c4d225d..b28c827958 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -1,5 +1,5 @@ # math_h.m4 -# serial 135 +# serial 136 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, @@ -56,7 +56,8 @@ AC_DEFUN_ONCE([gl_MATH_H], setpayload setpayloadf setpayloadl setpayloadsig setpayloadsigf setpayloadsigl sinf sinl sinhf sqrtf sqrtl - tanf tanl tanhf totalorder totalorderf totalorderl trunc truncf truncl]) + tanf tanl tanhf totalorder totalorderf totalorderl totalordermag + trunc truncf truncl]) ]) # gl_MATH_MODULE_INDICATOR([modulename]) @@ -183,6 +184,7 @@ AC_DEFUN([gl_MATH_H_REQUIRE_DEFAULTS], gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOTALORDER]) 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_TRUNC]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TRUNCF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TRUNCL]) @@ -273,6 +275,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], HAVE_TOTALORDER=1; AC_SUBST([HAVE_TOTALORDER]) HAVE_TOTALORDERF=1; AC_SUBST([HAVE_TOTALORDERF]) HAVE_TOTALORDERL=1; AC_SUBST([HAVE_TOTALORDERL]) + HAVE_TOTALORDERMAG=1; AC_SUBST([HAVE_TOTALORDERMAG]) 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]) @@ -392,6 +395,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], REPLACE_TOTALORDER=0; AC_SUBST([REPLACE_TOTALORDER]) REPLACE_TOTALORDERF=0; AC_SUBST([REPLACE_TOTALORDERF]) REPLACE_TOTALORDERL=0; AC_SUBST([REPLACE_TOTALORDERL]) + REPLACE_TOTALORDERMAG=0; AC_SUBST([REPLACE_TOTALORDERMAG]) REPLACE_TRUNC=0; AC_SUBST([REPLACE_TRUNC]) REPLACE_TRUNCF=0; AC_SUBST([REPLACE_TRUNCF]) REPLACE_TRUNCL=0; AC_SUBST([REPLACE_TRUNCL]) diff --git a/m4/totalordermag.m4 b/m4/totalordermag.m4 new file mode 100644 index 0000000000..971ea6bf4d --- /dev/null +++ b/m4/totalordermag.m4 @@ -0,0 +1,137 @@ +# totalordermag.m4 +# serial 1 +dnl Copyright 2023-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, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_TOTALORDERMAGF], +[ + AC_REQUIRE([gl_MATH_H_DEFAULTS]) + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + + dnl glibc versions < 2.31 had an incompatible declaration of this function, + dnl see + AC_CACHE_CHECK([whether totalordermagf has a non-standard declaration], + [gl_cv_func_totalordermagf_incompatible], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[#include + ]], + [[extern + #ifdef __cplusplus + "C" + #endif + int totalordermagf (float const *, float const *); + ]]) + ], + [gl_cv_func_totalordermagf_incompatible=no], + [gl_cv_func_totalordermagf_incompatible=yes]) + ]) + if test $gl_cv_func_totalordermagf_incompatible = yes; then + REPLACE_TOTALORDERMAGF=1 + else + gl_MATHFUNC([totalordermagf], [int], [(float const *, float const *)]) + if test $gl_cv_func_totalordermagf_no_libm != yes \ + && test $gl_cv_func_totalordermagf_in_libm != yes; then + HAVE_TOTALORDERMAGF=0 + fi + fi + if test $HAVE_TOTALORDERMAGF = 0 || test $REPLACE_TOTALORDERMAGF = 1; then + TOTALORDERMAGF_LIBM='$(ISNANF_LIBM)' + dnl Prerequisite of lib/totalordermagf.c. + gl_FLOAT_SIGN_LOCATION + gl_NAN_MIPS + fi + AC_SUBST([TOTALORDERMAGF_LIBM]) +]) + +AC_DEFUN([gl_FUNC_TOTALORDERMAG], +[ + AC_REQUIRE([gl_MATH_H_DEFAULTS]) + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + + dnl glibc versions < 2.31 had an incompatible declaration of this function, + dnl see + AC_CACHE_CHECK([whether totalordermag has a non-standard declaration], + [gl_cv_func_totalordermag_incompatible], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[#include + ]], + [[extern + #ifdef __cplusplus + "C" + #endif + int totalordermag (double const *, double const *); + ]]) + ], + [gl_cv_func_totalordermag_incompatible=no], + [gl_cv_func_totalordermag_incompatible=yes]) + ]) + if test $gl_cv_func_totalordermag_incompatible = yes; then + REPLACE_TOTALORDERMAG=1 + else + gl_MATHFUNC([totalordermag], [int], [(double const *, double const *)]) + if test $gl_cv_func_totalordermag_no_libm != yes \ + && test $gl_cv_func_totalordermag_in_libm != yes; then + HAVE_TOTALORDERMAG=0 + fi + fi + if test $HAVE_TOTALORDERMAG = 0 || test $REPLACE_TOTALORDERMAG = 1; then + TOTALORDERMAG_LIBM='$(ISNAND_LIBM)' + dnl Prerequisite of lib/totalordermag.c. + gl_DOUBLE_SIGN_LOCATION + gl_NAN_MIPS + fi + AC_SUBST([TOTALORDERMAG_LIBM]) +]) + +AC_DEFUN([gl_FUNC_TOTALORDERMAGL], +[ + AC_REQUIRE([gl_MATH_H_DEFAULTS]) + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE]) + + dnl glibc versions < 2.31 had an incompatible declaration of this function, + dnl see + AC_CACHE_CHECK([whether totalordermagl has a non-standard declaration], + [gl_cv_func_totalordermagl_incompatible], + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[#include + ]], + [[extern + #ifdef __cplusplus + "C" + #endif + int totalordermagl (long double const *, long double const *); + ]]) + ], + [gl_cv_func_totalordermagl_incompatible=no], + [gl_cv_func_totalordermagl_incompatible=yes]) + ]) + if test $gl_cv_func_totalordermagl_incompatible = yes; then + REPLACE_TOTALORDERMAGL=1 + else + gl_MATHFUNC([totalordermagl], [int], + [(long double const *, long double const *)]) + if test $gl_cv_func_totalordermagl_no_libm != yes \ + && test $gl_cv_func_totalordermagl_in_libm != yes; then + HAVE_TOTALORDERMAGL=0 + fi + fi + if test $HAVE_TOTALORDERMAGL = 0 || test $REPLACE_TOTALORDERMAGL = 1; then + dnl Find libraries needed to link lib/totalorderl.c. + if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then + AC_REQUIRE([gl_FUNC_TOTALORDERMAG]) + TOTALORDERMAGL_LIBM="$TOTALORDERMAG_LIBM" + else + TOTALORDERMAGL_LIBM='$(ISNANL_LIBM)' + fi + dnl Prerequisite of lib/totalordermagl.c. + gl_LONG_DOUBLE_SIGN_LOCATION + gl_NAN_MIPS + fi + AC_SUBST([TOTALORDERMAGL_LIBM]) +]) diff --git a/modules/math b/modules/math index 8c853f0979..a82a77a9d0 100644 --- a/modules/math +++ b/modules/math @@ -144,6 +144,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_TOTALORDER''@/$(GNULIB_TOTALORDER)/g' \ -e 's/@''GNULIB_TOTALORDERF''@/$(GNULIB_TOTALORDERF)/g' \ -e 's/@''GNULIB_TOTALORDERL''@/$(GNULIB_TOTALORDERL)/g' \ + -e 's/@''GNULIB_TOTALORDERMAG''@/$(GNULIB_TOTALORDERMAG)/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' \ @@ -224,6 +225,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''HAVE_TOTALORDER''@|$(HAVE_TOTALORDER)|g' \ -e 's|@''HAVE_TOTALORDERF''@|$(HAVE_TOTALORDERF)|g' \ -e 's|@''HAVE_TOTALORDERL''@|$(HAVE_TOTALORDERL)|g' \ + -e 's|@''HAVE_TOTALORDERMAG''@|$(HAVE_TOTALORDERMAG)|g' \ < $@-t2 > $@-t3 $(AM_V_at)sed \ -e 's|@''HAVE_DECL_ACOSL''@|$(HAVE_DECL_ACOSL)|g' \ @@ -350,6 +352,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''REPLACE_TOTALORDER''@|$(REPLACE_TOTALORDER)|g' \ -e 's|@''REPLACE_TOTALORDERF''@|$(REPLACE_TOTALORDERF)|g' \ -e 's|@''REPLACE_TOTALORDERL''@|$(REPLACE_TOTALORDERL)|g' \ + -e 's|@''REPLACE_TOTALORDERMAG''@|$(REPLACE_TOTALORDERMAG)|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/totalordermag b/modules/totalordermag new file mode 100644 index 0000000000..d5fbe620b5 --- /dev/null +++ b/modules/totalordermag @@ -0,0 +1,39 @@ +Description: +totalordermag function: total order of absolute value on double + +Files: +lib/totalordermag.c +m4/mathfunc.m4 +m4/totalordermag.m4 +m4/nan-mips.m4 +m4/signbit.m4 + +Depends-on: +math +extensions +verify [test $HAVE_TOTALORDERMAG = 0 || test $REPLACE_TOTALORDERMAG = 1] +isnand [test $HAVE_TOTALORDERMAG = 0 || test $REPLACE_TOTALORDERMAG = 1] +signbit [test $HAVE_TOTALORDERMAG = 0 || test $REPLACE_TOTALORDERMAG = 1] + +configure.ac: +gl_FUNC_TOTALORDERMAG +gl_CONDITIONAL([GL_COND_OBJ_TOTALORDERMAG], + [test $HAVE_TOTALORDERMAG = 0 || test $REPLACE_TOTALORDERMAG = 1]) +gl_MATH_MODULE_INDICATOR([totalordermag]) + +Makefile.am: +if GL_COND_OBJ_TOTALORDERMAG +lib_SOURCES += totalordermag.c +endif + +Include: + + +Link: +$(TOTALORDERMAG_LIBM) + +License: +LGPL + +Maintainer: +all -- 2.39.5