math_win32.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // -*- C++ -*-
  2. //===---------------------- support/win32/math_win32.h --------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H
  11. #define _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H
  12. #if !defined(_LIBCPP_MSVCRT)
  13. #error "This header complements Microsoft's C Runtime library, and should not be included otherwise."
  14. #else
  15. #include <math.h>
  16. #include <float.h> // _FPCLASS_PN etc.
  17. #include <crtversion.h>
  18. #if ((_VC_CRT_MAJOR_VERSION-0) < 12)
  19. // Necessary?
  20. typedef float float_t;
  21. typedef double double_t;
  22. _LIBCPP_ALWAYS_INLINE bool isfinite( double num )
  23. {
  24. return _finite(num) != 0;
  25. }
  26. _LIBCPP_ALWAYS_INLINE bool isinf( double num )
  27. {
  28. return !isfinite(num) && !_isnan(num);
  29. }
  30. _LIBCPP_ALWAYS_INLINE bool isnan( double num )
  31. {
  32. return _isnan(num) != 0;
  33. }
  34. _LIBCPP_ALWAYS_INLINE bool isnormal( double num )
  35. {
  36. int class_ = _fpclass(num);
  37. return class_ == _FPCLASS_NN || class_ == _FPCLASS_PN;
  38. }
  39. _LIBCPP_ALWAYS_INLINE bool isgreater( double x, double y )
  40. {
  41. if(_fpclass(x) == _FPCLASS_SNAN || _fpclass(y) == _FPCLASS_SNAN) return false;
  42. else return x > y;
  43. }
  44. _LIBCPP_ALWAYS_INLINE bool isgreaterequal( double x, double y )
  45. {
  46. if(_fpclass(x) == _FPCLASS_SNAN || _fpclass(y) == _FPCLASS_SNAN) return false;
  47. else return x >= y;
  48. }
  49. _LIBCPP_ALWAYS_INLINE bool isless( double x, double y )
  50. {
  51. if(_fpclass(x) == _FPCLASS_SNAN || _fpclass(y) == _FPCLASS_SNAN) return false;
  52. else return x < y;
  53. }
  54. _LIBCPP_ALWAYS_INLINE bool islessequal( double x, double y )
  55. {
  56. if(::_fpclass(x) == _FPCLASS_SNAN || ::_fpclass(y) == _FPCLASS_SNAN) return false;
  57. else return x <= y;
  58. }
  59. _LIBCPP_ALWAYS_INLINE bool islessgreater( double x, double y )
  60. {
  61. if(::_fpclass(x) == _FPCLASS_SNAN || ::_fpclass(y) == _FPCLASS_SNAN) return false;
  62. else return x < y || x > y;
  63. }
  64. _LIBCPP_ALWAYS_INLINE bool isunordered( double x, double y )
  65. {
  66. return isnan(x) || isnan(y);
  67. }
  68. _LIBCPP_ALWAYS_INLINE bool signbit( double num )
  69. {
  70. switch(_fpclass(num))
  71. {
  72. case _FPCLASS_SNAN:
  73. case _FPCLASS_QNAN:
  74. case _FPCLASS_NINF:
  75. case _FPCLASS_NN:
  76. case _FPCLASS_ND:
  77. case _FPCLASS_NZ:
  78. return true;
  79. case _FPCLASS_PZ:
  80. case _FPCLASS_PD:
  81. case _FPCLASS_PN:
  82. case _FPCLASS_PINF:
  83. return false;
  84. }
  85. return false;
  86. }
  87. _LIBCPP_ALWAYS_INLINE float copysignf( float x, float y )
  88. {
  89. return (signbit (x) != signbit (y) ? - x : x);
  90. }
  91. _LIBCPP_ALWAYS_INLINE double copysign( double x, double y )
  92. {
  93. return ::_copysign(x,y);
  94. }
  95. _LIBCPP_ALWAYS_INLINE double copysignl( long double x, long double y )
  96. {
  97. return ::_copysignl(x,y);
  98. }
  99. _LIBCPP_ALWAYS_INLINE int fpclassify( double num )
  100. {
  101. return _fpclass(num);
  102. }
  103. #endif
  104. #endif // _LIBCPP_MSVCRT
  105. #endif // _LIBCPP_SUPPORT_WIN32_MATH_WIN32_H