Blame lib/signbitl.c

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