/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "mozilla/Casting.h" #include "mozilla/ThreadSafety.h" #include #include #include #include #include #include #include using mozilla::AssertedCast; using mozilla::BitwiseCast; using mozilla::SaturatingCast; using mozilla::detail::IsInBounds; static const uint8_t floatMantissaBitsPlusOne = 24; static const uint8_t doubleMantissaBitsPlusOne = 53; template struct UintUlongBitwiseCast; template struct UintUlongBitwiseCast { static void test() { MOZ_RELEASE_ASSERT(BitwiseCast(Uint(8675309)) == Ulong(8675309)); } }; template struct UintUlongBitwiseCast { static void test() {} }; static void TestBitwiseCast() { MOZ_RELEASE_ASSERT(BitwiseCast(int(8675309)) == int(8675309)); UintUlongBitwiseCast::test(); } static void TestSameSize() { MOZ_RELEASE_ASSERT((IsInBounds(int16_t(0)))); MOZ_RELEASE_ASSERT((IsInBounds(int16_t(INT16_MIN)))); MOZ_RELEASE_ASSERT((IsInBounds(int16_t(INT16_MAX)))); MOZ_RELEASE_ASSERT((IsInBounds(uint16_t(UINT16_MAX)))); MOZ_RELEASE_ASSERT((IsInBounds(uint16_t(0)))); MOZ_RELEASE_ASSERT((!IsInBounds(uint16_t(-1)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(-1)))); MOZ_RELEASE_ASSERT((IsInBounds(int16_t(INT16_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(INT16_MIN)))); MOZ_RELEASE_ASSERT((IsInBounds(int32_t(INT32_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int32_t(INT32_MIN)))); } static void TestToBiggerSize() { MOZ_RELEASE_ASSERT((IsInBounds(int16_t(0)))); MOZ_RELEASE_ASSERT((IsInBounds(int16_t(INT16_MIN)))); MOZ_RELEASE_ASSERT((IsInBounds(int16_t(INT16_MAX)))); MOZ_RELEASE_ASSERT((IsInBounds(uint16_t(UINT16_MAX)))); MOZ_RELEASE_ASSERT((IsInBounds(uint16_t(0)))); MOZ_RELEASE_ASSERT((IsInBounds(uint16_t(-1)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(-1)))); MOZ_RELEASE_ASSERT((IsInBounds(int16_t(INT16_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(INT16_MIN)))); MOZ_RELEASE_ASSERT((IsInBounds(int32_t(INT32_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int32_t(INT32_MIN)))); } static void TestToSmallerSize() { MOZ_RELEASE_ASSERT((IsInBounds(int16_t(0)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(INT16_MIN)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(INT16_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(uint16_t(UINT16_MAX)))); MOZ_RELEASE_ASSERT((IsInBounds(uint16_t(0)))); MOZ_RELEASE_ASSERT((!IsInBounds(uint16_t(-1)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(-1)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(INT16_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int16_t(INT16_MIN)))); MOZ_RELEASE_ASSERT((!IsInBounds(int32_t(INT32_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int32_t(INT32_MIN)))); // Boundary cases MOZ_RELEASE_ASSERT((!IsInBounds(int64_t(INT32_MIN) - 1))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(INT32_MIN)))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(INT32_MIN) + 1))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(INT32_MAX) - 1))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(INT32_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int64_t(INT32_MAX) + 1))); MOZ_RELEASE_ASSERT((!IsInBounds(int64_t(-1)))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(0)))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(1)))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(UINT32_MAX) - 1))); MOZ_RELEASE_ASSERT((IsInBounds(int64_t(UINT32_MAX)))); MOZ_RELEASE_ASSERT((!IsInBounds(int64_t(UINT32_MAX) + 1))); } template void checkBoundariesFloating(In aEpsilon = {}, Out aIntegerOffset = {}) { // Check the max value of the input float can't be represented as an integer. // This is true for all floating point and integer width. MOZ_RELEASE_ASSERT((!IsInBounds(std::numeric_limits::max()))); // Check that the max value of the integer, as a float, minus an offset that // depends on the magnitude, can be represented as an integer. MOZ_RELEASE_ASSERT((IsInBounds( static_cast(std::numeric_limits::max() - aIntegerOffset)))); // Check that the max value of the integer, plus a number that depends on the // magnitude of the number, can't be represented as this integer (because it // becomes too big). MOZ_RELEASE_ASSERT((!IsInBounds( aEpsilon + static_cast(std::numeric_limits::max())))); if constexpr (std::is_signed_v) { // Same for negative numbers. MOZ_RELEASE_ASSERT( (!IsInBounds(std::numeric_limits::lowest()))); MOZ_RELEASE_ASSERT((IsInBounds( static_cast(std::numeric_limits::lowest())))); MOZ_RELEASE_ASSERT((!IsInBounds( static_cast(std::numeric_limits::lowest()) - aEpsilon))); } else { // Check for negative floats and unsigned integer types. MOZ_RELEASE_ASSERT((!IsInBounds(static_cast(-1)))); } } void TestFloatConversion() { MOZ_RELEASE_ASSERT((!IsInBounds(UINT64_MAX))); MOZ_RELEASE_ASSERT((!IsInBounds(UINT32_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(UINT16_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(UINT8_MAX))); MOZ_RELEASE_ASSERT((!IsInBounds(INT64_MAX))); MOZ_RELEASE_ASSERT((!IsInBounds(INT64_MIN))); MOZ_RELEASE_ASSERT((!IsInBounds(INT32_MAX))); MOZ_RELEASE_ASSERT((!IsInBounds(INT32_MIN))); MOZ_RELEASE_ASSERT((IsInBounds(INT16_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(INT16_MIN))); MOZ_RELEASE_ASSERT((IsInBounds(INT8_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(INT8_MIN))); MOZ_RELEASE_ASSERT((!IsInBounds(UINT64_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(UINT32_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(UINT16_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(UINT8_MAX))); MOZ_RELEASE_ASSERT((!IsInBounds(INT64_MAX))); MOZ_RELEASE_ASSERT((!IsInBounds(INT64_MIN))); MOZ_RELEASE_ASSERT((IsInBounds(INT32_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(INT32_MIN))); MOZ_RELEASE_ASSERT((IsInBounds(INT16_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(INT16_MIN))); MOZ_RELEASE_ASSERT((IsInBounds(INT8_MAX))); MOZ_RELEASE_ASSERT((IsInBounds(INT8_MIN))); // Floor check MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(4.3))); MOZ_RELEASE_ASSERT((AssertedCast(4.3f) == 4u)); MOZ_RELEASE_ASSERT((IsInBounds(-4.3))); MOZ_RELEASE_ASSERT((AssertedCast(-4.3f) == -4)); MOZ_RELEASE_ASSERT((IsInBounds(-4.3))); MOZ_RELEASE_ASSERT((AssertedCast(-4.3f) == -4)); MOZ_RELEASE_ASSERT((IsInBounds(-4.3))); MOZ_RELEASE_ASSERT((AssertedCast(-4.3f) == -4)); MOZ_RELEASE_ASSERT((IsInBounds(-4.3))); MOZ_RELEASE_ASSERT((AssertedCast(-4.3f) == -4)); // Bound check for float to unsigned integer conversion. The parameters are // espilons and offsets allowing to check boundaries, that depend on the // magnitude of the numbers. checkBoundariesFloating(2049.); checkBoundariesFloating(1.); checkBoundariesFloating(1.); checkBoundariesFloating(1.); // Large number because of the lack of precision of floats at this magnitude checkBoundariesFloating(1.1e12f); checkBoundariesFloating(1.f, 128u); checkBoundariesFloating(1.f); checkBoundariesFloating(1.f); checkBoundariesFloating(1025.); checkBoundariesFloating(1.); checkBoundariesFloating(1.); checkBoundariesFloating(1.); // Large number because of the lack of precision of floats at this magnitude checkBoundariesFloating(1.1e12f); checkBoundariesFloating(256.f, 64u); checkBoundariesFloating(1.f); checkBoundariesFloating(1.f); // Integer to floating point, boundary cases MOZ_RELEASE_ASSERT(!(IsInBounds( int64_t(std::pow(2, floatMantissaBitsPlusOne)) + 1))); MOZ_RELEASE_ASSERT((IsInBounds( int64_t(std::pow(2, floatMantissaBitsPlusOne))))); MOZ_RELEASE_ASSERT((IsInBounds( int64_t(std::pow(2, floatMantissaBitsPlusOne)) - 1))); MOZ_RELEASE_ASSERT(!(IsInBounds( int64_t(-std::pow(2, floatMantissaBitsPlusOne)) - 1))); MOZ_RELEASE_ASSERT((IsInBounds( int64_t(-std::pow(2, floatMantissaBitsPlusOne))))); MOZ_RELEASE_ASSERT((IsInBounds( int64_t(-std::pow(2, floatMantissaBitsPlusOne)) + 1))); MOZ_RELEASE_ASSERT(!(IsInBounds( uint64_t(std::pow(2, doubleMantissaBitsPlusOne)) + 1))); MOZ_RELEASE_ASSERT((IsInBounds( uint64_t(std::pow(2, doubleMantissaBitsPlusOne))))); MOZ_RELEASE_ASSERT((IsInBounds( uint64_t(std::pow(2, doubleMantissaBitsPlusOne)) - 1))); MOZ_RELEASE_ASSERT(!(IsInBounds( int64_t(-std::pow(2, doubleMantissaBitsPlusOne)) - 1))); MOZ_RELEASE_ASSERT((IsInBounds( int64_t(-std::pow(2, doubleMantissaBitsPlusOne))))); MOZ_RELEASE_ASSERT((IsInBounds( int64_t(-std::pow(2, doubleMantissaBitsPlusOne)) + 1))); MOZ_RELEASE_ASSERT(!(IsInBounds(UINT64_MAX))); MOZ_RELEASE_ASSERT(!(IsInBounds(INT64_MAX))); MOZ_RELEASE_ASSERT(!(IsInBounds(INT64_MIN))); MOZ_RELEASE_ASSERT( !(IsInBounds(std::numeric_limits::max()))); MOZ_RELEASE_ASSERT( !(IsInBounds(-std::numeric_limits::max()))); } #define ASSERT_EQ(a, b) \ if ((a) != (b)) { \ std::cerr << __FILE__ << ":" << __LINE__ << " Actual: " << +(a) << ", " \ << "expected: " << +(b) << std::endl; \ MOZ_CRASH(); \ } #ifdef ENABLE_DEBUG_PRINT # define DEBUG_PRINT(in, out) \ std::cout << "\tIn: " << +in << ", " << "out: " << +out << std::endl; #else # define DEBUG_PRINT(in, out) #endif template void TestTypePairImpl() { std::cout << __PRETTY_FUNCTION__ << std::endl; std::cout << std::fixed; // Test casting infinities to integer works if constexpr (std::is_floating_point_v && !std::is_floating_point_v) { Out v = SaturatingCast(std::numeric_limits::infinity()); ASSERT_EQ(v, std::numeric_limits::max()); v = SaturatingCast(-std::numeric_limits::infinity()); ASSERT_EQ(v, std::numeric_limits::lowest()); } // Saturation of a floating point value that is infinity is infinity if constexpr (std::is_floating_point_v && std::is_floating_point_v) { In in = std::numeric_limits::infinity(); Out v = SaturatingCast(in); DEBUG_PRINT(in, v); ASSERT_EQ(v, std::numeric_limits::infinity()); in = -std::numeric_limits::infinity(); v = SaturatingCast(in); DEBUG_PRINT(in, v); ASSERT_EQ(v, -std::numeric_limits::infinity()); return; } else { if constexpr (sizeof(In) > sizeof(Out) && std::is_integral_v) { // Test with a value just outside the range of the output type In in = static_cast(std::numeric_limits::max()) + 1ull; Out v = SaturatingCast(in); DEBUG_PRINT(in, v); ASSERT_EQ(v, std::numeric_limits::max()); if (std::is_signed_v) { // Test with a value just below the range of the output type Out lowest = std::numeric_limits::lowest(); in = static_cast(lowest) - 1; v = SaturatingCast(in); DEBUG_PRINT(in, v); if constexpr (std::is_signed_v && !std::is_signed_v) { ASSERT_EQ(v, 0); } else { ASSERT_EQ(v, std::numeric_limits::lowest()); } } } else if constexpr (std::is_integral_v && std::is_integral_v && sizeof(In) == sizeof(Out) && !std::is_signed_v && std::is_signed_v) { // Test that max uintXX_t saturates to max intXX_t In in = static_cast(std::numeric_limits::max()) + 1; Out v = SaturatingCast(in); DEBUG_PRINT(in, v); ASSERT_EQ(v, std::numeric_limits::max()); } // SaturatingCast of zero is zero In in = static_cast(0); Out v = SaturatingCast(in); DEBUG_PRINT(in, v); ASSERT_EQ(v, 0); if constexpr (sizeof(In) >= sizeof(Out) && std::is_signed_v && std::is_signed_v) { // Test with a value within the range of the output type In in = static_cast(std::numeric_limits::max() / 2); Out v = SaturatingCast(in); DEBUG_PRINT(in, v); ASSERT_EQ(v, in); // Test with a negative value within the range of the output type in = static_cast(std::numeric_limits::lowest() / 2); v = SaturatingCast(in); DEBUG_PRINT(in, v); ASSERT_EQ(v, in); } } } template void TestTypePair() { constexpr bool fromFloat = std::is_floating_point_v; constexpr bool toFloat = std::is_floating_point_v; // Don't test casting to the same type if constexpr (!std::is_same_v) { if constexpr ((fromFloat && !toFloat) || (!fromFloat && !toFloat)) { TestTypePairImpl(); } } } template void for_each_type_pair(std::tuple) { (TestTypePair(), ...); (TestTypePair(), ...); if constexpr (sizeof...(Ts) > 1) { for_each_type_pair(std::tuple{}); } } template void TestSaturatingCastImpl() { for_each_type_pair(std::tuple{}); } template void TestFirstToOthers() { (TestTypePair(), ...); } void TestSaturatingCast() { // Each integer type against every other TestSaturatingCastImpl(); // Floating point types to every integer type TestFirstToOthers(); TestFirstToOthers(); } int main() { TestBitwiseCast(); TestSameSize(); TestToBiggerSize(); TestToSmallerSize(); TestFloatConversion(); TestSaturatingCast(); return 0; }