bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'signbit' patch to use 'copysign' if available


From: Bruno Haible
Subject: Re: 'signbit' patch to use 'copysign' if available
Date: Tue, 10 Apr 2007 23:37:50 +0200
User-agent: KMail/1.5.4

Paul Eggert wrote:
> Here's a proposed patch to 'signbit' to have it use 'copysign' if
> available.

I'm applying this patch instead, that takes into account the previous
discussions.

> In Solaris, copysign is in -lm.  If we can't assume -lm then we
> probably shouldn't bother trying to use copysign.

On Cygwin, copysign is in libc. It makes sense to use copysign when cross-
compiling to cygwin:
$ i386-pc-cygwin-nm signbitf.o 
         U _copysignf
00000000 T _gl_signbitf
$ i386-pc-cygwin-nm signbitd.o 
         U _copysign
00000000 T _gl_signbitd
$ i386-pc-cygwin-nm signbitl.o 
00000000 T _gl_signbitl
         U _rpl_isnanl
00000000 b plus_zero.0


2007-04-10  Paul Eggert  <address@hidden>
            Bruno Haible  <address@hidden>

        * m4/signbit.m4 (gl_SIGNBIT): When the sign bit position could not
        be determined, test for availability of the copysignf, copysign,
        copysignl functions.
        * lib/signbitf.c (gl_signbitf): Use copysignf if available in libc.
        * lib/signbitd.c (gl_signbitd): Use copysign if available in libc.
        * lib/signbitl.c (gl_signbitl): Use copysignl if available in libc.

*** m4/signbit.m4       6 Apr 2007 20:55:44 -0000       1.1
--- m4/signbit.m4       10 Apr 2007 21:31:40 -0000
***************
*** 1,4 ****
! # signbit.m4 serial 1
  dnl Copyright (C) 2007 Free Software Foundation, Inc.
  dnl This file is free software; the Free Software Foundation
  dnl gives unlimited permission to copy and/or distribute it,
--- 1,4 ----
! # signbit.m4 serial 2
  dnl Copyright (C) 2007 Free Software Foundation, Inc.
  dnl This file is free software; the Free Software Foundation
  dnl gives unlimited permission to copy and/or distribute it,
***************
*** 65,70 ****
--- 65,130 ----
      gl_FLOAT_SIGN_LOCATION
      gl_DOUBLE_SIGN_LOCATION
      gl_LONG_DOUBLE_SIGN_LOCATION
+     if test "$gl_cv_cc_float_signbit" = unknown; then
+       dnl Test whether copysignf() is declared.
+       AC_CHECK_DECLS([copysignf], , , [#include <math.h>])
+       if test "$ac_cv_have_decl_copysignf" = yes; then
+         dnl Test whether copysignf() can be used without libm.
+         AC_CACHE_CHECK([whether copysignf can be used without linking with 
libm],
+           [gl_cv_func_copysignf_no_libm],
+           [
+             AC_TRY_LINK([#include <math.h>
+                          float x, y;],
+                         [return copysignf (x, y) < 0;],
+               [gl_cv_func_copysignf_no_libm=yes],
+               [gl_cv_func_copysignf_no_libm=no])
+           ])
+         if test $gl_cv_func_copysignf_no_libm = yes; then
+           AC_DEFINE([HAVE_COPYSIGNF_IN_LIBC], 1,
+             [Define if the copysignf function is declared in <math.h> and 
available in libc.])
+         fi
+       fi
+     fi
+     if test "$gl_cv_cc_double_signbit" = unknown; then
+       dnl Test whether copysign() is declared.
+       AC_CHECK_DECLS([copysign], , , [#include <math.h>])
+       if test "$ac_cv_have_decl_copysign" = yes; then
+         dnl Test whether copysign() can be used without libm.
+         AC_CACHE_CHECK([whether copysign can be used without linking with 
libm],
+           [gl_cv_func_copysign_no_libm],
+           [
+             AC_TRY_LINK([#include <math.h>
+                          double x, y;],
+                         [return copysign (x, y) < 0;],
+               [gl_cv_func_copysign_no_libm=yes],
+               [gl_cv_func_copysign_no_libm=no])
+           ])
+         if test $gl_cv_func_copysign_no_libm = yes; then
+           AC_DEFINE([HAVE_COPYSIGN_IN_LIBC], 1,
+             [Define if the copysign function is declared in <math.h> and 
available in libc.])
+         fi
+       fi
+     fi
+     if test "$gl_cv_cc_long_double_signbit" = unknown; then
+       dnl Test whether copysignl() is declared.
+       AC_CHECK_DECLS([copysignl], , , [#include <math.h>])
+       if test "$ac_cv_have_decl_copysignl" = yes; then
+         dnl Test whether copysignl() can be used without libm.
+         AC_CACHE_CHECK([whether copysignl can be used without linking with 
libm],
+           [gl_cv_func_copysignl_no_libm],
+           [
+             AC_TRY_LINK([#include <math.h>
+                          long double x, y;],
+                         [return copysignl (x, y) < 0;],
+               [gl_cv_func_copysignl_no_libm=yes],
+               [gl_cv_func_copysignl_no_libm=no])
+           ])
+         if test $gl_cv_func_copysignl_no_libm = yes; then
+           AC_DEFINE([HAVE_COPYSIGNL_IN_LIBC], 1,
+             [Define if the copysignl function is declared in <math.h> and 
available in libc.])
+         fi
+       fi
+     fi
    fi
  ])
  
*** lib/signbitf.c      6 Apr 2007 20:55:44 -0000       1.1
--- lib/signbitf.c      10 Apr 2007 21:31:40 -0000
***************
*** 30,40 ****
--- 30,48 ----
  gl_signbitf (float arg)
  {
  #if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT
+   /* The use of a union to extract the bits of the representation of a
+      'long 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 (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
    union { float value; unsigned int word[NWORDS]; } m;
    m.value = arg;
    return (m.word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1;
+ #elif HAVE_COPYSIGNF_IN_LIBC
+   return copysignf (1.0f, arg) < 0;
  #else
    /* This does not do the right thing for NaN, but this is irrelevant for
       most use cases.  */
*** lib/signbitd.c      6 Apr 2007 20:55:44 -0000       1.1
--- lib/signbitd.c      10 Apr 2007 21:31:40 -0000
***************
*** 30,40 ****
--- 30,48 ----
  gl_signbitd (double arg)
  {
  #if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT
+   /* The use of a union to extract the bits of the representation of a
+      'long 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 { double value; unsigned int word[NWORDS]; } m;
    m.value = arg;
    return (m.word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1;
+ #elif HAVE_COPYSIGN_IN_LIBC
+   return copysign (1.0, arg) < 0;
  #else
    /* This does not do the right thing for NaN, but this is irrelevant for
       most use cases.  */
*** lib/signbitl.c      6 Apr 2007 20:55:44 -0000       1.1
--- lib/signbitl.c      10 Apr 2007 21:31:40 -0000
***************
*** 30,40 ****
--- 30,48 ----
  gl_signbitl (long double arg)
  {
  #if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT
+   /* The use of a union to extract the bits of the representation of a
+      'long 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 (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned 
int))
    union { long double value; unsigned int word[NWORDS]; } m;
    m.value = arg;
    return (m.word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1;
+ #elif HAVE_COPYSIGNL_IN_LIBC
+   return copysignl (1.0L, arg) < 0;
  #else
    /* This does not do the right thing for NaN, but this is irrelevant for
       most use cases.  */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]