[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 0f3563c 05/10: Simplify conditionals using ce
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 0f3563c 05/10: Simplify conditionals using certain object-like macros |
Date: |
Tue, 20 Dec 2016 01:31:04 +0000 (UTC) |
branch: master
commit 0f3563c1314f07e22f0dfbba51c8aaa3f76fbba0
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Simplify conditionals using certain object-like macros
LMI_RINT_AVAILABLE was used only where LMI_LACKING_RINT_OR_EQUIVALENT
was conditionally (un)defined. LMI_LACKING_RINT_OR_EQUIVALENT was used
only in the negative sense:
#ifndef LMI_LACKING_RINT_OR_EQUIVALENT
where it was not the case that it was not the case that std::rint() was
available...or where MinGW was used and therefore neither of these
macros was defined.
It is less confusing to use only LMI_HAVE_RINT, and to define it even
for MinGW (which has provided C99 rint() for some time, and provides
std::rint() as well now that lmi requires C++11). For now at least,
only the preprocessor conditionals have been modified, in order to
keep this commit simple.
Incidentally improved comments on preprocessor conditionals for
concinnity.
---
round_to.hpp | 55 ++++++++++++++++++++++++++-----------------------------
1 file changed, 26 insertions(+), 29 deletions(-)
diff --git a/round_to.hpp b/round_to.hpp
index 4e9b1b9..67595c4 100644
--- a/round_to.hpp
+++ b/round_to.hpp
@@ -53,6 +53,9 @@
// this typedef to double.
typedef long double max_prec_real;
+// Any modern C++ compiler provides std::rint().
+#define LMI_HAVE_RINT
+
namespace detail
{
// 26.5/6 requires float and long double versions of std::fabs() and
@@ -77,7 +80,7 @@ namespace detail
# define LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
#endif // defined __MINGW32__
-#ifdef LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
+#if defined LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
// Returns 'r' raised to the 'n'th power. The sgi stl provides a faster
// implementation as an extension (although it does not seem to work
@@ -113,7 +116,7 @@ RealType perform_pow(RealType r, int n)
}
}
-#else // not defined LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
+#else // !defined LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
// Unlike the kludges above, these are defined inline to avoid
// penalizing compliant compilers.
@@ -124,7 +127,7 @@ inline RealType perform_pow(RealType r, int n)
return std::pow(r, n);
}
-#endif // not defined LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
+#endif // !defined LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
} // namespace detail
inline rounding_style& default_rounding_style()
@@ -157,28 +160,23 @@ inline RealType perform_rint(RealType r)
return r;
}
-#else // not (__GNUC__ && LMI_X86)
+#else // !(defined __GNUC__ && defined LMI_X86)
// The round_X functions below work with any real_type-to-integer_type.
// Compilers that provide rint() may have optimized it (or you can
// provide a fast implementation yourself).
-#if defined __GNUC__ && !defined __MINGW32__
-# define LMI_RINT_AVAILABLE
-#endif // defined __GNUC__ && !defined __MINGW32__
-
template<typename RealType>
inline RealType perform_rint(RealType r)
{
-#ifdef LMI_RINT_AVAILABLE
+#if defined LMI_HAVE_RINT
return rint(r);
-#else // not defined LMI_RINT_AVAILABLE
-# define LMI_LACKING_RINT_OR_EQUIVALENT
+#else // !defined LMI_HAVE_RINT
throw std::logic_error("rint() not defined.");
-#endif // not defined LMI_RINT_AVAILABLE
+#endif // !defined LMI_HAVE_RINT
}
-#endif // not (__GNUC__ && LMI_X86)
+#endif // !(defined __GNUC__ && defined LMI_X86)
// Auxiliary rounding functions: one for each supported rounding style.
// These functions avoid switching the hardware rounding mode as long
@@ -196,7 +194,7 @@ RealType round_not(RealType r)
template<typename RealType>
RealType round_up(RealType r)
{
-#ifndef LMI_LACKING_RINT_OR_EQUIVALENT
+#if defined LMI_HAVE_RINT
RealType i_part = perform_rint(r);
if(i_part < r)
{
@@ -207,32 +205,32 @@ RealType round_up(RealType r)
i_part++;
}
return i_part;
-#else // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#else // !defined LMI_HAVE_RINT
return std::ceil(r);
-#endif // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#endif // !defined LMI_HAVE_RINT
}
// Round down.
template<typename RealType>
RealType round_down(RealType r)
{
-#ifndef LMI_LACKING_RINT_OR_EQUIVALENT
+#if defined LMI_HAVE_RINT
RealType i_part = perform_rint(r);
if(r < i_part)
{
i_part--;
}
return i_part;
-#else // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#else // !defined LMI_HAVE_RINT
return std::floor(r);
-#endif // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#endif // !defined LMI_HAVE_RINT
}
// Truncate.
template<typename RealType>
RealType round_trunc(RealType r)
{
-#ifndef LMI_LACKING_RINT_OR_EQUIVALENT
+#if defined LMI_HAVE_RINT
RealType i_part = perform_rint(r);
RealType f_part = r - i_part;
// Consider the integer part 'i_part' and the fractional part
@@ -252,19 +250,19 @@ RealType round_trunc(RealType r)
i_part++;
}
return i_part;
-#else // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#else // !defined LMI_HAVE_RINT
double x = std::floor(std::fabs(r));
return (0.0 <= r) ? x : -x;
-#endif // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#endif // !defined LMI_HAVE_RINT
}
// Round to nearest using bankers method.
template<typename RealType>
RealType round_near(RealType r)
{
-#ifndef LMI_LACKING_RINT_OR_EQUIVALENT
+#if defined LMI_HAVE_RINT
RealType i_part = perform_rint(r);
-#else // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#else // !defined LMI_HAVE_RINT
// To return immediately with this value:
// return (RealType(0) < r) ? std::floor(r + 0.5) : std::ceil(r -0.5);
// would be incorrect, because halfway cases must be rounded to even.
@@ -276,7 +274,7 @@ RealType round_near(RealType r)
// This 'i_part' needn't equal the value that std::rint() would
// return, as long as both produce the same correct result after
// adjustment below.
-#endif // defined LMI_LACKING_RINT_OR_EQUIVALENT
+#endif // !defined LMI_HAVE_RINT
RealType f_part = r - i_part;
RealType abs_f_part = std::fabs(f_part);
@@ -483,7 +481,7 @@ template<typename RealType>
typename round_to<RealType>::rounding_function_t
round_to<RealType>::select_rounding_function(rounding_style const a_style)
const
{
-#ifndef LMI_LACKING_RINT_OR_EQUIVALENT
+#if defined LMI_HAVE_RINT
if
( a_style == default_rounding_style()
&& a_style != r_indeterminate
@@ -491,7 +489,7 @@ round_to<RealType>::select_rounding_function(rounding_style
const a_style) const
{
return detail::perform_rint;
}
-#endif // not defined LMI_LACKING_RINT_OR_EQUIVALENT
+#endif // defined LMI_HAVE_RINT
switch(a_style)
{
@@ -527,8 +525,7 @@ round_to<RealType>::select_rounding_function(rounding_style
const a_style) const
}
#undef LMI_BROKEN_FLOAT_AND_LONG_DOUBLE_CMATH_FNS
-#undef LMI_RINT_AVAILABLE
-#undef LMI_LACKING_RINT_OR_EQUIVALENT
+#undef LMI_HAVE_RINT
#endif // round_to_hpp
- [lmi-commits] [lmi] master updated (ae22927 -> 83d8eb3), Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master 6cfeca5 02/10: Drop support for an ancient non-free compiler, Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master dc47d4a 04/10: Improve documentation, Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master fa18f82 01/10: Remove references to nonexistent html documentation, Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master bdd39b7 08/10: Fix defect introduced 20050526T1235Z: precision loss, Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master 83d8eb3 10/10: Experimentally rely on optimization of pow(long double, int), Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master eff3c7f 09/10: Rename a member datum for concinnity, Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master 0f3563c 05/10: Simplify conditionals using certain object-like macros,
Greg Chicares <=
- [lmi-commits] [lmi] master 46d02c5 03/10: Expunge inline replacements for std::fabs() and std::floor() [425], Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master 54d2503 06/10: Presume std::rint() is available [424], Greg Chicares, 2016/12/19
- [lmi-commits] [lmi] master 84ed793 07/10: Simplify preprocessor conditionals, Greg Chicares, 2016/12/19