72 #ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_REAL_H_
73 #define ELEMENTSKERNEL_ELEMENTSKERNEL_REAL_H_
76 #include <type_traits>
99 template<std::
size_t size>
124 using UInt =
unsigned long long;
127 template <
typename RawType>
172 template<
typename RawType>
188 static const std::size_t s_exponent_bitcount = s_bitcount - 1 - s_fraction_bitcount;
191 static const Bits s_sign_bitmask =
static_cast<Bits>(1) << (s_bitcount - 1);
194 static const Bits s_fraction_bitmask = ~static_cast<
Bits>(0) >> (s_exponent_bitcount + 1);
197 static const Bits s_exponent_bitmask = ~(s_sign_bitmask | s_fraction_bitmask);
236 return ReinterpretBits(s_exponent_bitmask);
248 return s_exponent_bitmask & m_u.m_bits;
253 return s_fraction_bitmask & m_u.m_bits;
258 return s_sign_bitmask & m_u.m_bits;
265 return (exponentBits() == s_exponent_bitmask) && (fractionBits() != 0);
280 return distanceBetweenSignAndMagnitudeNumbers(m_u.m_bits, rhs.
m_u.
m_bits) <= m_max_ulps;
299 if (s_sign_bitmask & sam) {
304 return s_sign_bitmask | sam;
311 const Bits biased1 = signAndMagnitudeToBiased(sam1);
312 const Bits biased2 = signAndMagnitudeToBiased(sam2);
313 return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
329 template <
typename FloatType>
337 template <
typename RawType>
341 Bits x_bits = *
reinterpret_cast<const Bits *
>(&x);
350 template <
typename RawType, std::
size_t max_ulps = defaultMaxUlps<RawType>()>
351 bool isEqual(
const RawType& left,
const RawType& right) {
353 bool is_equal{
false};
355 if (not(isNan<RawType>(left) or isNan<RawType>(right))) {
357 Bits l_bits = *
reinterpret_cast<const Bits *
>(&left);
358 Bits r_bits = *
reinterpret_cast<const Bits *
>(&right);
365 template <std::
size_t max_ulps>
366 inline bool isEqual(
const float& left,
const float& right) {
367 return (isEqual<float, max_ulps>(left, right));
370 template <std::
size_t max_ulps>
371 inline bool isEqual(
const double& left,
const double& right) {
372 return (isEqual<double, max_ulps>(left, right));
375 template <
typename RawType, std::
size_t max_ulps = defaultMaxUlps<RawType>()>
376 inline bool isNotEqual(
const RawType& left,
const RawType& right) {
377 return (not isEqual<RawType, max_ulps>(left, right) );
380 template <std::
size_t max_ulps>
381 inline bool isNotEqual(
const float& left,
const float& right) {
382 return (isNotEqual<float, max_ulps>(left, right));
385 template <std::
size_t max_ulps>
386 inline bool isNotEqual(
const double& left,
const double& right) {
387 return (isNotEqual<double, max_ulps>(left, right));
391 template <
typename RawType, std::
size_t max_ulps = defaultMaxUlps<RawType>()>
392 bool isLess(
const RawType& left,
const RawType& right) {
395 if ( left < right && (not isEqual<RawType, max_ulps>(left, right)) ) {
402 template<std::
size_t max_ulps>
403 inline bool isLess(
const float& left,
const float& right) {
404 return (isLess<float, max_ulps>(left, right));
407 template<std::
size_t max_ulps>
408 inline bool isLess(
const double& left,
const double& right) {
409 return (isLess<double, max_ulps>(left, right));
412 template <
typename RawType, std::
size_t max_ulps = defaultMaxUlps<RawType>()>
413 bool isGreater(
const RawType& left,
const RawType& right) {
414 bool is_greater{
false};
416 if ( left > right && (not isEqual<RawType, max_ulps>(left, right)) ) {
423 template<std::
size_t max_ulps>
424 inline bool isGreater(
const float& left,
const float& right) {
425 return (isGreater<float, max_ulps>(left, right));
428 template<std::
size_t max_ulps>
429 inline bool isGreater(
const double& left,
const double& right) {
430 return (isGreater<double, max_ulps>(left, right));
433 template <
typename RawType, std::
size_t max_ulps = defaultMaxUlps<RawType>()>
437 if (not isGreater<RawType, max_ulps>(left, right)) {
444 template<std::
size_t max_ulps>
446 return (isLessOrEqual<float, max_ulps>(left, right));
449 template<std::
size_t max_ulps>
451 return (isLessOrEqual<double, max_ulps>(left, right));
454 template <
typename RawType, std::
size_t max_ulps = defaultMaxUlps<RawType>()>
458 if (not isLess<RawType, max_ulps>(left, right)) {
465 template<std::
size_t max_ulps>
467 return (isGreaterOrEqual<float, max_ulps>(left, right));
470 template<std::
size_t max_ulps>
472 return (isGreaterOrEqual<double, max_ulps>(left, right));
525 template<
typename RawType>
527 #pragma GCC diagnostic push
528 #pragma GCC diagnostic ignored "-Wfloat-equal"
529 return (left == right);
530 #pragma GCC diagnostic pop
536 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_REAL_H_
Bits exponentBits() const
bool isGreaterOrEqual(const RawType &left, const RawType &right)
static Bits signAndMagnitudeToBiased(const Bits &sam)
Bits fractionBits() const
Macro to silence unused variables warnings from the compiler.
static RawType ReinterpretBits(const Bits &bits)
ELEMENTS_API const double DBL_DEFAULT_TEST_TOLERANCE
Double precision float default test tolerance.
constexpr std::size_t FLT_DEFAULT_MAX_ULPS
Single precision float default maximum unit in the last place.
bool almostEqual2sComplement(ELEMENTS_UNUSED const FloatType &a, ELEMENTS_UNUSED const FloatType &b, ELEMENTS_UNUSED const std::size_t &max_ulps=0)
typename TypeWithSize< sizeof(RawType)>::UInt Bits
bool isGreater(const RawType &left, const RawType &right)
defines the macros to be used for explicit export of the symbols
bool isLessOrEqual(const RawType &left, const RawType &right)
bool AlmostEquals(const FloatingPoint &rhs) const
constexpr std::size_t defaultMaxUlps< double >()
ELEMENTS_API bool realBitWiseEqual(const RawType &left, const RawType &right)
This function compares 2 floating point numbers bitwise. These are the strict equivalent of the "=="...
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
const Bits & bits() const
static Bits distanceBetweenSignAndMagnitudeNumbers(const Bits &sam1, const Bits &sam2)
constexpr std::size_t defaultMaxUlps< float >()
static RawType Infinity()
bool isEqual(const RawType &left, const RawType &right)
bool isNan(const RawType &x)
bool isNotEqual(const RawType &left, const RawType &right)
constexpr std::size_t defaultMaxUlps()
bool isLess(const RawType &left, const RawType &right)
constexpr std::size_t DBL_DEFAULT_MAX_ULPS
Double precision float default maximum unit in the last place.
ELEMENTS_API const double FLT_DEFAULT_TEST_TOLERANCE
Single precision float default test tolerance.
FloatingPoint(const RawType &x)