]> Savannah Git Hosting - gnulib.git/commitdiff
setpayloadsig: New module.
authorBruno Haible <bruno@clisp.org>
Tue, 16 Apr 2024 19:01:17 +0000 (21:01 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 16 Apr 2024 22:39:11 +0000 (00:39 +0200)
* lib/math.in.h (setpayloadsig): New declaration.
* lib/setpayloadsig.c: New file, based on lib/setpayload.c.
* m4/setpayloadsig.m4: New file, based on m4/setpayload.m4.
* m4/math_h.m4 (gl_MATH_H): Test whether setpayloadsig is declared.
(gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_SETPAYLOADSIG.
(gl_MATH_H_DEFAULTS): Initialize HAVE_SETPAYLOADSIG.
* modules/math (Makefile.am): Substitute GNULIB_SETPAYLOADSIG,
HAVE_SETPAYLOADSIG.
* modules/setpayloadsig: New file.
* doc/posix-functions/setpayloadsig.texi: Mention the new module.

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

index 7521411127f8a122d8fa7b1e11d4172a637c46fa..c4a23adb450e461a1b8cbb114fd5deef6ef4599a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2024-04-16  Bruno Haible  <bruno@clisp.org>
+
+       setpayloadsig: New module.
+       * lib/math.in.h (setpayloadsig): New declaration.
+       * lib/setpayloadsig.c: New file, based on lib/setpayload.c.
+       * m4/setpayloadsig.m4: New file, based on m4/setpayload.m4.
+       * m4/math_h.m4 (gl_MATH_H): Test whether setpayloadsig is declared.
+       (gl_MATH_H_REQUIRE_DEFAULTS): Initialize GNULIB_SETPAYLOADSIG.
+       (gl_MATH_H_DEFAULTS): Initialize HAVE_SETPAYLOADSIG.
+       * modules/math (Makefile.am): Substitute GNULIB_SETPAYLOADSIG,
+       HAVE_SETPAYLOADSIG.
+       * modules/setpayloadsig: New file.
+       * doc/posix-functions/setpayloadsig.texi: Mention the new module.
+
 2024-04-16  Bruno Haible  <bruno@clisp.org>
 
        setpayloadl: Fix platform-specific bugs.
index 09ef9b86fce952ceec5de702ffb0fc37db3adb6b..6c40c6039e0bf485ae3f7a83471dabd67faf0070 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/FP-Bit-Twiddling.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: setpayloadsig
 
 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.
 @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
index 98da404780a3ac84eddebddad7b4edabe3933a8a..c61e65c423a0d0cc22c8378a0a3d20ff4982f016 100644 (file)
@@ -2817,6 +2817,21 @@ _GL_WARN_ON_USE (setpayloadl, "setpayloadl is unportable - "
 #endif
 
 
+#if @GNULIB_SETPAYLOADSIG@
+# if !@HAVE_SETPAYLOADSIG@
+_GL_FUNCDECL_SYS (setpayloadsig, int, (double *, double));
+# endif
+_GL_CXXALIAS_SYS (setpayloadsig, int, (double *, double));
+_GL_CXXALIASWARN (setpayloadsig);
+#elif defined GNULIB_POSIXCHECK
+# undef setpayloadsig
+# if HAVE_RAW_DECL_SETPAYLOADSIG
+_GL_WARN_ON_USE (setpayloadsig, "setpayloadsig is unportable - "
+                 "use gnulib module setpayloadsig for portability");
+# endif
+#endif
+
+
 #if @GNULIB_TOTALORDERF@
 # if @REPLACE_TOTALORDERF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/lib/setpayloadsig.c b/lib/setpayloadsig.c
new file mode 100644 (file)
index 0000000..c336f08
--- /dev/null
@@ -0,0 +1,74 @@
+/* Construct a signalling NaN 'double' with a given payload.
+   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>
+
+#include "signed-snan.h"
+
+int
+setpayloadsig (double *result, double payload)
+{
+#if DBL_MANT_DIG == 53
+  if (
+# if defined __hppa || defined __mips__ || defined __sh__
+      payload >= 0.0
+# else
+      /* A zero payload is not allowed, because that would denote Infinity.
+         Cf. snan.h.  */
+      payload > 0.0
+# endif
+      && payload < 2251799813685248.0 /* (double) (1ULL << (DBL_MANT_DIG - 2)) */
+      && payload == (double) (int64_t) payload)
+    {
+      memory_double x = memory_positive_SNaNd ();
+      uint64_t pl = (int64_t) payload;
+      uint32_t pl_hi = (uint32_t) (pl >> 32);
+      uint32_t pl_lo = (uint32_t) pl;
+      x.word[DBL_EXPBIT0_WORD] =
+        (x.word[DBL_EXPBIT0_WORD] & ~((1U << (DBL_MANT_DIG - 2 - 32)) - 1))
+        | pl_hi;
+      x.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD == 0 ? 1 : -1)] = pl_lo;
+# if 0
+      *result = x.value;
+# else
+      /* 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
+         <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
+         for details.  Use memcpy to avoid this.  */
+      memcpy (result, &x.value, sizeof (double));
+# endif
+      return 0;
+    }
+  else
+    {
+      *result = 0.0;
+      return -1;
+    }
+#else
+# error "Please port gnulib setpayloadsig.c to your platform!"
+#endif
+}
index 3dc779160c72a22497e8c8a1a4b13049c58c4950..0c5ce4f89ce2bc7f229e8497313758f3f29a3dc8 100644 (file)
@@ -1,5 +1,5 @@
 # math_h.m4
-# serial 129
+# serial 130
 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,
@@ -52,6 +52,7 @@ AC_DEFUN_ONCE([gl_MATH_H],
      remainder remainderf remainderl
      rint rintf rintl round roundf roundl
      setpayload setpayloadf setpayloadl
+     setpayloadsig
      sinf sinl sinhf sqrtf sqrtl
      tanf tanl tanhf totalorder totalorderf totalorderl trunc truncf truncl])
 ])
@@ -162,6 +163,7 @@ AC_DEFUN([gl_MATH_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SETPAYLOAD])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SETPAYLOADF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SETPAYLOADL])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SETPAYLOADSIG])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SIGNBIT])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SINF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SINL])
@@ -247,6 +249,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS],
   HAVE_SETPAYLOAD=1;                AC_SUBST([HAVE_SETPAYLOAD])
   HAVE_SETPAYLOADF=1;               AC_SUBST([HAVE_SETPAYLOADF])
   HAVE_SETPAYLOADL=1;               AC_SUBST([HAVE_SETPAYLOADL])
+  HAVE_SETPAYLOADSIG=1;             AC_SUBST([HAVE_SETPAYLOADSIG])
   HAVE_SINF=1;                      AC_SUBST([HAVE_SINF])
   HAVE_SINL=1;                      AC_SUBST([HAVE_SINL])
   HAVE_SINHF=1;                     AC_SUBST([HAVE_SINHF])
diff --git a/m4/setpayloadsig.m4 b/m4/setpayloadsig.m4
new file mode 100644 (file)
index 0000000..a6ff790
--- /dev/null
@@ -0,0 +1,63 @@
+# setpayloadsig.m4
+# serial 1
+dnl Copyright 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_SETPAYLOADSIGF],
+[
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+
+  gl_MATHFUNC([setpayloadsigf], [int], [(float *, float)])
+  if test $gl_cv_func_setpayloadsigf_no_libm != yes \
+     && test $gl_cv_func_setpayloadsigf_in_libm != yes; then
+    HAVE_SETPAYLOADSIGF=0
+  fi
+  if test $HAVE_SETPAYLOADSIGF = 0; then
+    SETPAYLOADSIGF_LIBM=
+  fi
+  AC_SUBST([SETPAYLOADSIGF_LIBM])
+])
+
+AC_DEFUN_ONCE([gl_FUNC_SETPAYLOADSIG],
+[
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+
+  gl_MATHFUNC([setpayloadsig], [int], [(double *, double)])
+  if test $gl_cv_func_setpayloadsig_no_libm != yes \
+     && test $gl_cv_func_setpayloadsig_in_libm != yes; then
+    HAVE_SETPAYLOADSIG=0
+  fi
+  if test $HAVE_SETPAYLOADSIG = 0; then
+    SETPAYLOADSIG_LIBM=
+  fi
+  AC_SUBST([SETPAYLOADSIG_LIBM])
+])
+
+AC_DEFUN([gl_FUNC_SETPAYLOADSIGL],
+[
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+  AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
+
+  gl_MATHFUNC([setpayloadsigl], [int], [(long double *, long double)])
+  if test $gl_cv_func_setpayloadsigl_no_libm != yes \
+     && test $gl_cv_func_setpayloadsigl_in_libm != yes; then
+    HAVE_SETPAYLOADSIGL=0
+  fi
+  if test $HAVE_SETPAYLOADSIGL = 0; then
+    dnl Find libraries needed to link lib/setpayloadsigl.c.
+    if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
+      AC_REQUIRE([gl_FUNC_SETPAYLOADSIG])
+      SETPAYLOADSIGL_LIBM="$SETPAYLOADSIG_LIBM"
+    else
+      SETPAYLOADSIGL_LIBM=
+    fi
+    dnl Prerequisite of lib/setpayloadsigl.c.
+    gl_LONG_DOUBLE_EXPONENT_LOCATION
+  fi
+  AC_SUBST([SETPAYLOADSIGL_LIBM])
+])
index 80de39a7f7fa1ceceab5bf230a51cedd78ddea91..722aad91b9e262a88ec13feac817cd6160b0ae38 100644 (file)
@@ -123,6 +123,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's/@''GNULIB_SETPAYLOAD''@/$(GNULIB_SETPAYLOAD)/g' \
              -e 's/@''GNULIB_SETPAYLOADF''@/$(GNULIB_SETPAYLOADF)/g' \
              -e 's/@''GNULIB_SETPAYLOADL''@/$(GNULIB_SETPAYLOADL)/g' \
+             -e 's/@''GNULIB_SETPAYLOADSIG''@/$(GNULIB_SETPAYLOADSIG)/g' \
              -e 's/@''GNULIB_SIGNBIT''@/$(GNULIB_SIGNBIT)/g' \
              -e 's/@''GNULIB_SINF''@/$(GNULIB_SINF)/g' \
              -e 's/@''GNULIB_SINL''@/$(GNULIB_SINL)/g' \
@@ -201,6 +202,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(
              -e 's|@''HAVE_SETPAYLOAD''@|$(HAVE_SETPAYLOAD)|g' \
              -e 's|@''HAVE_SETPAYLOADF''@|$(HAVE_SETPAYLOADF)|g' \
              -e 's|@''HAVE_SETPAYLOADL''@|$(HAVE_SETPAYLOADL)|g' \
+             -e 's|@''HAVE_SETPAYLOADSIG''@|$(HAVE_SETPAYLOADSIG)|g' \
              -e 's|@''HAVE_SINF''@|$(HAVE_SINF)|g' \
              -e 's|@''HAVE_SINL''@|$(HAVE_SINL)|g' \
              -e 's|@''HAVE_SINHF''@|$(HAVE_SINHF)|g' \
diff --git a/modules/setpayloadsig b/modules/setpayloadsig
new file mode 100644 (file)
index 0000000..27bb3a6
--- /dev/null
@@ -0,0 +1,36 @@
+Description:
+setpayloadsig function: construct a signalling NaN with a given payload
+
+Files:
+lib/setpayloadsig.c
+m4/mathfunc.m4
+m4/setpayloadsig.m4
+
+Depends-on:
+math
+extensions
+float           [test $HAVE_SETPAYLOADSIG = 0]
+stdint          [test $HAVE_SETPAYLOADSIG = 0]
+signed-snan     [test $HAVE_SETPAYLOADSIG = 0]
+
+configure.ac:
+gl_FUNC_SETPAYLOADSIG
+gl_CONDITIONAL([GL_COND_OBJ_SETPAYLOADSIG], [test $HAVE_SETPAYLOADSIG = 0])
+gl_MATH_MODULE_INDICATOR([setpayloadsig])
+
+Makefile.am:
+if GL_COND_OBJ_SETPAYLOADSIG
+lib_SOURCES += setpayloadsig.c
+endif
+
+Include:
+<math.h>
+
+Link:
+$(SETPAYLOADSIG_LIBM)
+
+License:
+LGPL
+
+Maintainer:
+all