catch_floating_point_helpers.cpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 9 #include <catch2/internal/catch_floating_point_helpers.hpp> 10 11 #include <cstring> 12 13 namespace Catch { 14 namespace Detail { 15 16 uint32_t convertToBits(float f) { 17 static_assert(sizeof(float) == sizeof(uint32_t), "Important ULP matcher assumption violated"); 18 uint32_t i; 19 std::memcpy(&i, &f, sizeof(f)); 20 return i; 21 } 22 23 uint64_t convertToBits(double d) { 24 static_assert(sizeof(double) == sizeof(uint64_t), "Important ULP matcher assumption violated"); 25 uint64_t i; 26 std::memcpy(&i, &d, sizeof(d)); 27 return i; 28 } 29 30 #if defined( __GNUC__ ) || defined( __clang__ ) 31 # pragma GCC diagnostic push 32 # pragma GCC diagnostic ignored "-Wfloat-equal" 33 #endif 34 bool directCompare( float lhs, float rhs ) { return lhs == rhs; } 35 bool directCompare( double lhs, double rhs ) { return lhs == rhs; } 36 #if defined( __GNUC__ ) || defined( __clang__ ) 37 # pragma GCC diagnostic pop 38 #endif 39 40 41 } // end namespace Detail 42 } // end namespace Catch 43