]> Savannah Git Hosting - gnulib.git/commitdiff
getpayloadf: New module.
authorBruno Haible <bruno@clisp.org>
Wed, 17 Apr 2024 09:02:52 +0000 (11:02 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 17 Apr 2024 13:38:34 +0000 (15:38 +0200)
* lib/math.in.h (getpayloadf): New declaration.
* lib/getpayloadf.c: New file.
* m4/math_h.m4 (gl_MATH_H): Test whether getpayloadf is declared.
(gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_GETPAYLOADF.
(gl_MATH_H_DEFAULTS): Initialize HAVE_GETPAYLOADF, REPLACE_GETPAYLOADF.
* modules/math (Makefile.am): Substitute GNULIB_GETPAYLOADF,
HAVE_GETPAYLOADF, REPLACE_GETPAYLOADF.
* modules/getpayloadf: New file.
* doc/posix-functions/getpayloadf.texi: Mention the new module and the
glibc bug.

ChangeLog
doc/posix-functions/getpayloadf.texi
lib/getpayloadf.c [new file with mode: 0644]
lib/math.in.h
m4/math_h.m4
modules/getpayloadf [new file with mode: 0644]
modules/math

index 495ea4d9968aec3864ba2b142fb926b440f14dd3..6f97325216594f4372627344e00ac89d0c5285b6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2024-04-17  Bruno Haible  <bruno@clisp.org>
+
+       getpayloadf: New module.
+       * lib/math.in.h (getpayloadf): New declaration.
+       * lib/getpayloadf.c: New file.
+       * m4/math_h.m4 (gl_MATH_H): Test whether getpayloadf is declared.
+       (gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_GETPAYLOADF.
+       (gl_MATH_H_DEFAULTS): Initialize HAVE_GETPAYLOADF, REPLACE_GETPAYLOADF.
+       * modules/math (Makefile.am): Substitute GNULIB_GETPAYLOADF,
+       HAVE_GETPAYLOADF, REPLACE_GETPAYLOADF.
+       * modules/getpayloadf: New file.
+       * doc/posix-functions/getpayloadf.texi: Mention the new module and the
+       glibc bug.
+
 2024-04-17  Bruno Haible  <bruno@clisp.org>
 
        getpayload: Add tests.
index 39bd40ead9c846c7b0b3cf1306d57ae6f9f8dabd..98eecf0109b2912f21f21394c8d4726a21cd2de0 100644 (file)
@@ -10,15 +10,19 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/FP-Bit-Twiddling.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: getpayloadf
 
 Portability problems 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.
+@item
+This function returns a wrong result for non-NaN arguments on some platforms:
+@c https://sourceware.org/bugzilla/show_bug.cgi?id=26073
+glibc 2.31.
 @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.
 @end itemize
diff --git a/lib/getpayloadf.c b/lib/getpayloadf.c
new file mode 100644 (file)
index 0000000..6e24c55
--- /dev/null
@@ -0,0 +1,54 @@
+/* Extract the payload of a NaN 'float'.
+   Copyright 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 <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <math.h>
+
+#include <float.h>
+#include <stdint.h>
+#include <string.h>
+
+float
+getpayloadf (const float *value)
+{
+  if (isnanf (*value))
+    {
+#if FLT_MANT_DIG == 24
+      union { uint32_t i; float f; } x;
+# if 0
+      x.f = *value;
+# else
+      /* On 32-bit x86 processors, as well as on x86_64 processors with
+         CC="gcc -mfpmath=387", the evaluation of *value above is done
+         through an 'flds' instruction, which converts a signalling NaN to
+         a quiet NaN. See
+         <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
+         for details.  Use memcpy to avoid this.  */
+      memcpy (&x.f, value, sizeof (float));
+# endif
+      int32_t payload = x.i & (((uint32_t) 1 << (FLT_MANT_DIG - 2)) - 1);
+      return payload;
+#else
+# error "Please port gnulib getpayloadf.c to your platform!"
+#endif
+    }
+  else
+    return -1.0f;
+}
index 2e7dde2e07842883d51a3db22dc33780152fe874..7a59c8cf3581ef4d55dbdbaea45524df2390a0c8 100644 (file)
@@ -2774,6 +2774,29 @@ _GL_WARN_REAL_FLOATING_DECL (signbit);
 #endif
 
 
+#if @GNULIB_GETPAYLOADF@
+# if @REPLACE_GETPAYLOADF@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef getpayloadf
+#   define getpayloadf rpl_getpayloadf
+#  endif
+_GL_FUNCDECL_RPL (getpayloadf, float, (const float *));
+_GL_CXXALIAS_RPL (getpayloadf, float, (const float *));
+# else
+#  if !@HAVE_GETPAYLOADF@
+_GL_FUNCDECL_SYS (getpayloadf, float, (const float *));
+#  endif
+_GL_CXXALIAS_SYS (getpayloadf, float, (const float *));
+# endif
+_GL_CXXALIASWARN (getpayloadf);
+#elif defined GNULIB_POSIXCHECK
+# undef getpayloadf
+# if HAVE_RAW_DECL_GETPAYLOADF
+_GL_WARN_ON_USE (getpayloadf, "getpayloadf is unportable - "
+                 "use gnulib module getpayloadf for portability");
+# endif
+#endif
+
 #if @GNULIB_GETPAYLOAD@
 # if @REPLACE_GETPAYLOAD@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
index 370ea1405149ad5497a1c1f268171cd6559c694e..12aafb7fe94ad3f4a8cdadb263c3f87b47625a57 100644 (file)
@@ -1,5 +1,5 @@
 # math_h.m4
-# serial 133
+# serial 134
 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,
@@ -44,7 +44,7 @@ AC_DEFUN_ONCE([gl_MATH_H],
      expf expl exp2 exp2f exp2l expm1 expm1f expm1l
      fabsf fabsl floorf floorl fma fmaf fmal
      fmod fmodf fmodl frexpf frexpl
-     getpayload
+     getpayload getpayloadf
      hypotf hypotl
      ilogb ilogbf ilogbl
      ldexpf ldexpl
@@ -120,6 +120,7 @@ AC_DEFUN([gl_MATH_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_FREXP])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_FREXPL])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETPAYLOAD])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETPAYLOADF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_HYPOT])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_HYPOTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_HYPOTL])
@@ -227,6 +228,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   HAVE_FMODL=1;                     AC_SUBST([HAVE_FMODL])
   HAVE_FREXPF=1;                    AC_SUBST([HAVE_FREXPF])
   HAVE_GETPAYLOAD=1;                AC_SUBST([HAVE_GETPAYLOAD])
+  HAVE_GETPAYLOADF=1;               AC_SUBST([HAVE_GETPAYLOADF])
   HAVE_HYPOTF=1;                    AC_SUBST([HAVE_HYPOTF])
   HAVE_HYPOTL=1;                    AC_SUBST([HAVE_HYPOTL])
   HAVE_ILOGB=1;                     AC_SUBST([HAVE_ILOGB])
@@ -337,6 +339,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   REPLACE_FREXP=0;                  AC_SUBST([REPLACE_FREXP])
   REPLACE_FREXPL=0;                 AC_SUBST([REPLACE_FREXPL])
   REPLACE_GETPAYLOAD=0;             AC_SUBST([REPLACE_GETPAYLOAD])
+  REPLACE_GETPAYLOADF=0;            AC_SUBST([REPLACE_GETPAYLOADF])
   REPLACE_HUGE_VAL=0;               AC_SUBST([REPLACE_HUGE_VAL])
   REPLACE_HYPOT=0;                  AC_SUBST([REPLACE_HYPOT])
   REPLACE_HYPOTF=0;                 AC_SUBST([REPLACE_HYPOTF])
diff --git a/modules/getpayloadf b/modules/getpayloadf
new file mode 100644 (file)
index 0000000..a330b4f
--- /dev/null
@@ -0,0 +1,37 @@
+Description:
+getpayloadf function: extract the payload of a NaN
+
+Files:
+lib/getpayloadf.c
+m4/mathfunc.m4
+m4/getpayload.m4
+
+Depends-on:
+math
+extensions
+float           [test $HAVE_GETPAYLOADF = 0 || test $REPLACE_GETPAYLOADF = 1]
+stdint          [test $HAVE_GETPAYLOADF = 0 || test $REPLACE_GETPAYLOADF = 1]
+isnanf          [test $HAVE_GETPAYLOADF = 0 || test $REPLACE_GETPAYLOADF = 1]
+
+configure.ac:
+gl_FUNC_GETPAYLOADF
+gl_CONDITIONAL([GL_COND_OBJ_GETPAYLOADF],
+               [test $HAVE_GETPAYLOADF = 0 || test $REPLACE_GETPAYLOADF = 1])
+gl_MATH_MODULE_INDICATOR([getpayloadf])
+
+Makefile.am:
+if GL_COND_OBJ_GETPAYLOADF
+lib_SOURCES += getpayloadf.c
+endif
+
+Include:
+<math.h>
+
+Link:
+$(GETPAYLOADF_LIBM)
+
+License:
+LGPL
+
+Maintainer:
+all
index b6a15f6d68ea4b649964664e55587a29afd4d457..a8c9646124c038c5a83cd5768e64ae3385cb9f0a 100644 (file)
@@ -76,6 +76,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's/@''GNULIB_FREXP''@/$(GNULIB_FREXP)/g' \
              -e 's/@''GNULIB_FREXPL''@/$(GNULIB_FREXPL)/g' \
              -e 's/@''GNULIB_GETPAYLOAD''@/$(GNULIB_GETPAYLOAD)/g' \
+             -e 's/@''GNULIB_GETPAYLOADF''@/$(GNULIB_GETPAYLOADF)/g' \
              -e 's/@''GNULIB_HYPOT''@/$(GNULIB_HYPOT)/g' \
              -e 's/@''GNULIB_HYPOTF''@/$(GNULIB_HYPOTF)/g' \
              -e 's/@''GNULIB_HYPOTL''@/$(GNULIB_HYPOTL)/g' \
@@ -178,6 +179,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's|@''HAVE_FMODL''@|$(HAVE_FMODL)|g' \
              -e 's|@''HAVE_FREXPF''@|$(HAVE_FREXPF)|g' \
              -e 's|@''HAVE_GETPAYLOAD''@|$(HAVE_GETPAYLOAD)|g' \
+             -e 's|@''HAVE_GETPAYLOADF''@|$(HAVE_GETPAYLOADF)|g' \
              -e 's|@''HAVE_HYPOTF''@|$(HAVE_HYPOTF)|g' \
              -e 's|@''HAVE_HYPOTL''@|$(HAVE_HYPOTL)|g' \
              -e 's|@''HAVE_ILOGB''@|$(HAVE_ILOGB)|g' \
@@ -292,6 +294,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's|@''REPLACE_FREXP''@|$(REPLACE_FREXP)|g' \
              -e 's|@''REPLACE_FREXPL''@|$(REPLACE_FREXPL)|g' \
              -e 's|@''REPLACE_GETPAYLOAD''@|$(REPLACE_GETPAYLOAD)|g' \
+             -e 's|@''REPLACE_GETPAYLOADF''@|$(REPLACE_GETPAYLOADF)|g' \
              -e 's|@''REPLACE_HUGE_VAL''@|$(REPLACE_HUGE_VAL)|g' \
              -e 's|@''REPLACE_HYPOT''@|$(REPLACE_HYPOT)|g' \
              -e 's|@''REPLACE_HYPOTF''@|$(REPLACE_HYPOTF)|g' \