Blame lib/signbitf.c

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