Blame lib/signbitd.c

Packit 8f70b4
/* signbit() macro: Determine the sign bit of a floating-point number.
Packit 8f70b4
   Copyright (C) 2007-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <math.h>
Packit 8f70b4
Packit 8f70b4
#include <string.h>
Packit 8f70b4
#include "isnand-nolibm.h"
Packit 8f70b4
#include "float+.h"
Packit 8f70b4
Packit 8f70b4
#ifdef gl_signbitd_OPTIMIZED_MACRO
Packit 8f70b4
# undef gl_signbitd
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
gl_signbitd (double arg)
Packit 8f70b4
{
Packit 8f70b4
#if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT
Packit 8f70b4
  /* The use of a union to extract the bits of the representation of a
Packit 8f70b4
     'long double' is safe in practice, despite of the "aliasing rules" of
Packit 8f70b4
     C99, because the GCC docs say
Packit 8f70b4
       "Even with '-fstrict-aliasing', type-punning is allowed, provided the
Packit 8f70b4
        memory is accessed through the union type."
Packit 8f70b4
     and similarly for other compilers.  */
Packit 8f70b4
# define NWORDS \
Packit 8f70b4
    ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
Packit 8f70b4
  union { double value; unsigned int word[NWORDS]; } m;
Packit 8f70b4
  m.value = arg;
Packit 8f70b4
  return (m.word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1;
Packit 8f70b4
#elif HAVE_COPYSIGN_IN_LIBC
Packit 8f70b4
  return copysign (1.0, arg) < 0;
Packit 8f70b4
#else
Packit 8f70b4
  /* This does not do the right thing for NaN, but this is irrelevant for
Packit 8f70b4
     most use cases.  */
Packit 8f70b4
  if (isnand (arg))
Packit 8f70b4
    return 0;
Packit 8f70b4
  if (arg < 0.0)
Packit 8f70b4
    return 1;
Packit 8f70b4
  else if (arg == 0.0)
Packit 8f70b4
    {
Packit 8f70b4
      /* Distinguish 0.0 and -0.0.  */
Packit 8f70b4
      static double plus_zero = 0.0;
Packit 8f70b4
      double arg_mem = arg;
Packit 8f70b4
      return (memcmp (&plus_zero, &arg_mem, SIZEOF_DBL) != 0);
Packit 8f70b4
    }
Packit 8f70b4
  else
Packit 8f70b4
    return 0;
Packit 8f70b4
#endif
Packit 8f70b4
}