Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:05

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_FLOATING_POINT_HELPERS_HPP_INCLUDED
0009 #define CATCH_FLOATING_POINT_HELPERS_HPP_INCLUDED
0010 
0011 #include <catch2/internal/catch_polyfills.hpp>
0012 
0013 #include <cassert>
0014 #include <cmath>
0015 #include <cstdint>
0016 #include <utility>
0017 #include <limits>
0018 
0019 namespace Catch {
0020     namespace Detail {
0021 
0022         uint32_t convertToBits(float f);
0023         uint64_t convertToBits(double d);
0024 
0025     } // end namespace Detail
0026 
0027 
0028 
0029 #if defined( __GNUC__ ) || defined( __clang__ )
0030 #    pragma GCC diagnostic push
0031     // We do a bunch of direct compensations of floating point numbers,
0032     // because we know what we are doing and actually do want the direct
0033     // comparison behaviour.
0034 #    pragma GCC diagnostic ignored "-Wfloat-equal"
0035 #endif
0036 
0037     /**
0038      * Calculates the ULP distance between two floating point numbers
0039      *
0040      * The ULP distance of two floating point numbers is the count of
0041      * valid floating point numbers representable between them.
0042      *
0043      * There are some exceptions between how this function counts the
0044      * distance, and the interpretation of the standard as implemented.
0045      * by e.g. `nextafter`. For this function it always holds that:
0046      * * `(x == y) => ulpDistance(x, y) == 0` (so `ulpDistance(-0, 0) == 0`)
0047      * * `ulpDistance(maxFinite, INF) == 1`
0048      * * `ulpDistance(x, -x) == 2 * ulpDistance(x, 0)`
0049      *
0050      * \pre `!isnan( lhs )`
0051      * \pre `!isnan( rhs )`
0052      * \pre floating point numbers are represented in IEEE-754 format
0053      */
0054     template <typename FP>
0055     uint64_t ulpDistance( FP lhs, FP rhs ) {
0056         assert( std::numeric_limits<FP>::is_iec559 &&
0057             "ulpDistance assumes IEEE-754 format for floating point types" );
0058         assert( !Catch::isnan( lhs ) &&
0059                 "Distance between NaN and number is not meaningful" );
0060         assert( !Catch::isnan( rhs ) &&
0061                 "Distance between NaN and number is not meaningful" );
0062 
0063         // We want X == Y to imply 0 ULP distance even if X and Y aren't
0064         // bit-equal (-0 and 0), or X - Y != 0 (same sign infinities).
0065         if ( lhs == rhs ) { return 0; }
0066 
0067         // We need a properly typed positive zero for type inference.
0068         static constexpr FP positive_zero{};
0069 
0070         // We want to ensure that +/- 0 is always represented as positive zero
0071         if ( lhs == positive_zero ) { lhs = positive_zero; }
0072         if ( rhs == positive_zero ) { rhs = positive_zero; }
0073 
0074         // If arguments have different signs, we can handle them by summing
0075         // how far are they from 0 each.
0076         if ( std::signbit( lhs ) != std::signbit( rhs ) ) {
0077             return ulpDistance( std::abs( lhs ), positive_zero ) +
0078                    ulpDistance( std::abs( rhs ), positive_zero );
0079         }
0080 
0081         // When both lhs and rhs are of the same sign, we can just
0082         // read the numbers bitwise as integers, and then subtract them
0083         // (assuming IEEE).
0084         uint64_t lc = Detail::convertToBits( lhs );
0085         uint64_t rc = Detail::convertToBits( rhs );
0086 
0087         // The ulp distance between two numbers is symmetric, so to avoid
0088         // dealing with overflows we want the bigger converted number on the lhs
0089         if ( lc < rc ) {
0090             std::swap( lc, rc );
0091         }
0092 
0093         return lc - rc;
0094     }
0095 
0096 #if defined( __GNUC__ ) || defined( __clang__ )
0097 #    pragma GCC diagnostic pop
0098 #endif
0099 
0100 
0101 } // end namespace Catch
0102 
0103 #endif // CATCH_FLOATING_POINT_HELPERS_HPP_INCLUDED