fpclassify.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright (C) 2017, bel2125
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the
  15. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. */
  18. /* Note: Within "check", this file uses the LGPL license.
  19. * If you need only this file, you may find a MIT licensed version in
  20. * the "floatmagic" repository of the initial author.
  21. */
  22. #include "libcompat.h"
  23. double DOUBLE_ZERO = 0.0;
  24. #if defined(NEED_fpclassify)
  25. #if defined(HAVE_STDINT_H)
  26. #include <stdint.h>
  27. typedef uint64_t bitfield64;
  28. #elif defined(_MSC_VER)
  29. typedef unsigned __int64 bitfield64;
  30. #else
  31. typedef unsigned long long bitfield64;
  32. #endif
  33. static bitfield64 ms = 0x8000000000000000;
  34. static bitfield64 me = 0x7FF0000000000000;
  35. static bitfield64 mf = 0x000FFFFFFFFFFFFF;
  36. int fpclassify(double d)
  37. {
  38. bitfield64 *p = (bitfield64 *)&d;
  39. if ((*p & me) != me) {
  40. /* finite */
  41. if (*p & mf) {
  42. /* finite and not null */
  43. if (*p & me) {
  44. return FP_NORMAL;
  45. }
  46. return FP_SUBNORMAL;
  47. }
  48. return FP_ZERO;
  49. }
  50. if (*p & mf) {
  51. return FP_NAN;
  52. }
  53. return FP_INFINITE;
  54. }
  55. #endif