signbitf.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* signbit() macro: Determine the sign bit of a floating-point number.
  2. Copyright (C) 2007, 2009-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <math.h>
  16. #include <string.h>
  17. #include "isnanf-nolibm.h"
  18. #include "float+.h"
  19. #ifdef gl_signbitf_OPTIMIZED_MACRO
  20. # undef gl_signbitf
  21. #endif
  22. int
  23. gl_signbitf (float arg)
  24. {
  25. #if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT
  26. /* The use of a union to extract the bits of the representation of a
  27. 'long double' is safe in practice, despite of the "aliasing rules" of
  28. C99, because the GCC docs say
  29. "Even with '-fstrict-aliasing', type-punning is allowed, provided the
  30. memory is accessed through the union type."
  31. and similarly for other compilers. */
  32. # define NWORDS \
  33. ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  34. union { float value; unsigned int word[NWORDS]; } m;
  35. m.value = arg;
  36. return (m.word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1;
  37. #elif HAVE_COPYSIGNF_IN_LIBC
  38. return copysignf (1.0f, arg) < 0;
  39. #else
  40. /* This does not do the right thing for NaN, but this is irrelevant for
  41. most use cases. */
  42. if (isnanf (arg))
  43. return 0;
  44. if (arg < 0.0f)
  45. return 1;
  46. else if (arg == 0.0f)
  47. {
  48. /* Distinguish 0.0f and -0.0f. */
  49. static float plus_zero = 0.0f;
  50. float arg_mem = arg;
  51. return (memcmp (&plus_zero, &arg_mem, SIZEOF_FLT) != 0);
  52. }
  53. else
  54. return 0;
  55. #endif
  56. }