FPEnvironment_WIN32.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // FPEnvironment_WIN32.h
  3. //
  4. // Library: Foundation
  5. // Package: Core
  6. // Module: FPEnvironment
  7. //
  8. // Definitions of class FPEnvironmentImpl for WIN32.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_FPEnvironment_WIN32_INCLUDED
  16. #define Foundation_FPEnvironment_WIN32_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include <float.h>
  19. #include <math.h>
  20. #ifndef _SW_INEXACT
  21. # define _SW_INEXACT 0x00000001 // inexact (precision)
  22. #endif
  23. #ifndef _SW_UNDERFLOW
  24. # define _SW_UNDERFLOW 0x00000002 // underflow
  25. #endif
  26. #ifndef _SW_OVERFLOW
  27. # define _SW_OVERFLOW 0x00000004 // overflow
  28. #endif
  29. #ifndef _SW_ZERODIVIDE
  30. # define _SW_ZERODIVIDE 0x00000008 // zero divide
  31. #endif
  32. #ifndef _SW_INVALID
  33. # define _SW_INVALID 0x00000010 // invalid
  34. #endif
  35. #ifndef _SW_DENORMAL
  36. # define _SW_DENORMAL 0x00080000 // denormal status bit
  37. #endif
  38. namespace Poco {
  39. class Foundation_API FPEnvironmentImpl
  40. {
  41. protected:
  42. enum RoundingModeImpl
  43. {
  44. FP_ROUND_DOWNWARD_IMPL = _RC_DOWN,
  45. FP_ROUND_UPWARD_IMPL = _RC_UP,
  46. FP_ROUND_TONEAREST_IMPL = _RC_NEAR,
  47. FP_ROUND_TOWARDZERO_IMPL = _RC_CHOP
  48. };
  49. enum FlagImpl
  50. {
  51. FP_DIVIDE_BY_ZERO_IMPL = _SW_ZERODIVIDE,
  52. FP_INEXACT_IMPL = _SW_INEXACT,
  53. FP_OVERFLOW_IMPL = _SW_OVERFLOW,
  54. FP_UNDERFLOW_IMPL = _SW_UNDERFLOW,
  55. FP_INVALID_IMPL = _SW_INVALID
  56. };
  57. FPEnvironmentImpl();
  58. FPEnvironmentImpl(const FPEnvironmentImpl& env);
  59. ~FPEnvironmentImpl();
  60. FPEnvironmentImpl& operator = (const FPEnvironmentImpl& env);
  61. void keepCurrentImpl();
  62. static void clearFlagsImpl();
  63. static bool isFlagImpl(FlagImpl flag);
  64. static void setRoundingModeImpl(RoundingModeImpl mode);
  65. static RoundingModeImpl getRoundingModeImpl();
  66. static bool isInfiniteImpl(float value);
  67. static bool isInfiniteImpl(double value);
  68. static bool isInfiniteImpl(long double value);
  69. static bool isNaNImpl(float value);
  70. static bool isNaNImpl(double value);
  71. static bool isNaNImpl(long double value);
  72. static float copySignImpl(float target, float source);
  73. static double copySignImpl(double target, double source);
  74. static long double copySignImpl(long double target, long double source);
  75. private:
  76. unsigned _env;
  77. };
  78. //
  79. // inlines
  80. //
  81. inline bool FPEnvironmentImpl::isInfiniteImpl(float value)
  82. {
  83. return _finite(value) == 0;
  84. }
  85. inline bool FPEnvironmentImpl::isInfiniteImpl(double value)
  86. {
  87. return _finite(value) == 0;
  88. }
  89. inline bool FPEnvironmentImpl::isInfiniteImpl(long double value)
  90. {
  91. return _finite(value) == 0;
  92. }
  93. inline bool FPEnvironmentImpl::isNaNImpl(float value)
  94. {
  95. return _isnan(value) != 0;
  96. }
  97. inline bool FPEnvironmentImpl::isNaNImpl(double value)
  98. {
  99. return _isnan(value) != 0;
  100. }
  101. inline bool FPEnvironmentImpl::isNaNImpl(long double value)
  102. {
  103. return _isnan(value) != 0;
  104. }
  105. inline float FPEnvironmentImpl::copySignImpl(float target, float source)
  106. {
  107. return float(_copysign(target, source));
  108. }
  109. inline double FPEnvironmentImpl::copySignImpl(double target, double source)
  110. {
  111. return _copysign(target, source);
  112. }
  113. inline long double FPEnvironmentImpl::copySignImpl(long double target, long double source)
  114. {
  115. return (source > 0 && target > 0) || (source < 0 && target < 0) ? target : -target;
  116. }
  117. } // namespace Poco
  118. #endif // Foundation_FPEnvironment_WIN32_INCLUDED