| 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/catch_test_macros.hpp> |
| 10 |
#include <catch2/internal/catch_random_integer_helpers.hpp> |
| 11 |
#include <random> |
| 12 |
|
| 13 |
namespace { |
| 14 |
template <typename Int> |
| 15 |
static void |
| 16 |
CommutativeMultCheck( Int a, Int b, Int upper_result, Int lower_result ) { |
| 17 |
using Catch::Detail::extendedMult; |
| 18 |
using Catch::Detail::ExtendedMultResult; |
| 19 |
CHECK( extendedMult( a, b ) == |
| 20 |
ExtendedMultResult<Int>{ upper_result, lower_result } ); |
| 21 |
CHECK( extendedMult( b, a ) == |
| 22 |
ExtendedMultResult<Int>{ upper_result, lower_result } ); |
| 23 |
} |
| 24 |
|
| 25 |
// Simple (and slow) implementation of extended multiplication for tests |
| 26 |
constexpr Catch::Detail::ExtendedMultResult<std::uint64_t> |
| 27 |
extendedMultNaive( std::uint64_t lhs, std::uint64_t rhs ) { |
| 28 |
// This is a simple long multiplication, where we split lhs and rhs |
| 29 |
// into two 32-bit "digits", so that we can do ops with carry in 64-bits. |
| 30 |
// |
| 31 |
// 32b 32b 32b 32b |
| 32 |
// lhs L1 L2 |
| 33 |
// * rhs R1 R2 |
| 34 |
// ------------------------ |
| 35 |
// | R2 * L2 | |
| 36 |
// | R2 * L1 | |
| 37 |
// | R1 * L2 | |
| 38 |
// | R1 * L1 | |
| 39 |
// ------------------------- |
| 40 |
// | a | b | c | d | |
| 41 |
|
| 42 |
#define CarryBits( x ) ( x >> 32 ) |
| 43 |
#define Digits( x ) ( x & 0xFF'FF'FF'FF ) |
| 44 |
|
| 45 |
auto r2l2 = Digits( rhs ) * Digits( lhs ); |
| 46 |
auto r2l1 = Digits( rhs ) * CarryBits( lhs ); |
| 47 |
auto r1l2 = CarryBits( rhs ) * Digits( lhs ); |
| 48 |
auto r1l1 = CarryBits( rhs ) * CarryBits( lhs ); |
| 49 |
|
| 50 |
// Sum to columns first |
| 51 |
auto d = Digits( r2l2 ); |
| 52 |
auto c = CarryBits( r2l2 ) + Digits( r2l1 ) + Digits( r1l2 ); |
| 53 |
auto b = CarryBits( r2l1 ) + CarryBits( r1l2 ) + Digits( r1l1 ); |
| 54 |
auto a = CarryBits( r1l1 ); |
| 55 |
|
| 56 |
// Propagate carries between columns |
| 57 |
c += CarryBits( d ); |
| 58 |
b += CarryBits( c ); |
| 59 |
a += CarryBits( b ); |
| 60 |
|
| 61 |
// Remove the used carries |
| 62 |
c = Digits( c ); |
| 63 |
b = Digits( b ); |
| 64 |
a = Digits( a ); |
| 65 |
|
| 66 |
#undef CarryBits |
| 67 |
#undef Digits |
| 68 |
|
| 69 |
return { |
| 70 |
a << 32 | b, // upper 64 bits |
| 71 |
c << 32 | d // lower 64 bits |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
} // namespace |
| 77 |
|
| 78 |
TEST_CASE( "extendedMult 64x64", "[Integer][approvals]" ) { |
| 79 |
// a x 0 == 0 |
| 80 |
CommutativeMultCheck<uint64_t>( 0x1234'5678'9ABC'DEFF, 0, 0, 0 ); |
| 81 |
|
| 82 |
// bit carried from low half to upper half |
| 83 |
CommutativeMultCheck<uint64_t>( uint64_t( 1 ) << 63, 2, 1, 0 ); |
| 84 |
|
| 85 |
// bits in upper half on one side, bits in lower half on other side |
| 86 |
CommutativeMultCheck<uint64_t>( 0xcdcd'dcdc'0000'0000, |
| 87 |
0x0000'0000'aeae'aeae, |
| 88 |
0x0000'0000'8c6e'5a77, |
| 89 |
0x7391'a588'0000'0000 ); |
| 90 |
|
| 91 |
// Some input numbers without interesting patterns |
| 92 |
CommutativeMultCheck<uint64_t>( 0xaaaa'aaaa'aaaa'aaaa, |
| 93 |
0xbbbb'bbbb'bbbb'bbbb, |
| 94 |
0x7d27'd27d'27d2'7d26, |
| 95 |
0xd82d'82d8'2d82'd82e ); |
| 96 |
|
| 97 |
CommutativeMultCheck<uint64_t>( 0x7d27'd27d'27d2'7d26, |
| 98 |
0xd82d'82d8'2d82'd82e, |
| 99 |
0x69af'd991'8256'b953, |
| 100 |
0x8724'8909'fcb6'8cd4 ); |
| 101 |
|
| 102 |
CommutativeMultCheck<uint64_t>( 0xdead'beef'dead'beef, |
| 103 |
0xfeed'feed'feed'feef, |
| 104 |
0xddbf'680b'2b0c'b558, |
| 105 |
0x7a36'b06f'2ce9'6321 ); |
| 106 |
|
| 107 |
CommutativeMultCheck<uint64_t>( 0xddbf'680b'2b0c'b558, |
| 108 |
0x7a36'b06f'2ce9'6321, |
| 109 |
0x69dc'96c9'294b'fc7f, |
| 110 |
0xd038'39fa'a3dc'6858 ); |
| 111 |
|
| 112 |
CommutativeMultCheck<uint64_t>( 0x61c8'8646'80b5'83eb, |
| 113 |
0x61c8'8646'80b5'83eb, |
| 114 |
0x2559'92d3'8220'8bbe, |
| 115 |
0xdf44'2d22'ce48'59b9 ); |
| 116 |
} |
| 117 |
|
| 118 |
TEST_CASE("extendedMult 64x64 - all implementations", "[integer][approvals]") { |
| 119 |
using Catch::Detail::extendedMult; |
| 120 |
using Catch::Detail::extendedMultPortable; |
| 121 |
using Catch::Detail::fillBitsFrom; |
| 122 |
|
| 123 |
std::random_device rng; |
| 124 |
for (size_t i = 0; i < 100; ++i) { |
| 125 |
auto a = fillBitsFrom<std::uint64_t>( rng ); |
| 126 |
auto b = fillBitsFrom<std::uint64_t>( rng ); |
| 127 |
CAPTURE( a, b ); |
| 128 |
|
| 129 |
auto naive_ab = extendedMultNaive( a, b ); |
| 130 |
|
| 131 |
REQUIRE( naive_ab == extendedMultNaive( b, a ) ); |
| 132 |
REQUIRE( naive_ab == extendedMultPortable( a, b ) ); |
| 133 |
REQUIRE( naive_ab == extendedMultPortable( b, a ) ); |
| 134 |
REQUIRE( naive_ab == extendedMult( a, b ) ); |
| 135 |
REQUIRE( naive_ab == extendedMult( b, a ) ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
TEST_CASE( "SizedUnsignedType helpers", "[integer][approvals]" ) { |
| 140 |
using Catch::Detail::SizedUnsignedType_t; |
| 141 |
using Catch::Detail::DoubleWidthUnsignedType_t; |
| 142 |
|
| 143 |
STATIC_REQUIRE( sizeof( SizedUnsignedType_t<1> ) == 1 ); |
| 144 |
STATIC_REQUIRE( sizeof( SizedUnsignedType_t<2> ) == 2 ); |
| 145 |
STATIC_REQUIRE( sizeof( SizedUnsignedType_t<4> ) == 4 ); |
| 146 |
STATIC_REQUIRE( sizeof( SizedUnsignedType_t<8> ) == 8 ); |
| 147 |
|
| 148 |
STATIC_REQUIRE( sizeof( DoubleWidthUnsignedType_t<std::uint8_t> ) == 2 ); |
| 149 |
STATIC_REQUIRE( std::is_unsigned<DoubleWidthUnsignedType_t<std::uint8_t>>::value ); |
| 150 |
STATIC_REQUIRE( sizeof( DoubleWidthUnsignedType_t<std::uint16_t> ) == 4 ); |
| 151 |
STATIC_REQUIRE( std::is_unsigned<DoubleWidthUnsignedType_t<std::uint16_t>>::value ); |
| 152 |
STATIC_REQUIRE( sizeof( DoubleWidthUnsignedType_t<std::uint32_t> ) == 8 ); |
| 153 |
STATIC_REQUIRE( std::is_unsigned<DoubleWidthUnsignedType_t<std::uint32_t>>::value ); |
| 154 |
} |
| 155 |
|
| 156 |
TEST_CASE( "extendedMult 32x32", "[integer][approvals]" ) { |
| 157 |
// a x 0 == 0 |
| 158 |
CommutativeMultCheck<uint32_t>( 0x1234'5678, 0, 0, 0 ); |
| 159 |
|
| 160 |
// bit carried from low half to upper half |
| 161 |
CommutativeMultCheck<uint32_t>( uint32_t(1) << 31, 2, 1, 0 ); |
| 162 |
|
| 163 |
// bits in upper half on one side, bits in lower half on other side |
| 164 |
CommutativeMultCheck<uint32_t>( 0xdcdc'0000, 0x0000'aabb, 0x0000'934b, 0x6cb4'0000 ); |
| 165 |
|
| 166 |
// Some input numbers without interesting patterns |
| 167 |
CommutativeMultCheck<uint32_t>( |
| 168 |
0xaaaa'aaaa, 0xbbbb'bbbb, 0x7d27'd27c, 0x2d82'd82e ); |
| 169 |
|
| 170 |
CommutativeMultCheck<uint32_t>( |
| 171 |
0x7d27'd27c, 0x2d82'd82e, 0x163f'f7e8, 0xc5b8'7248 ); |
| 172 |
|
| 173 |
CommutativeMultCheck<uint32_t>( |
| 174 |
0xdead'beef, 0xfeed'feed, 0xddbf'6809, 0x6f8d'e543 ); |
| 175 |
|
| 176 |
CommutativeMultCheck<uint32_t>( |
| 177 |
0xddbf'6809, 0x6f8d'e543, 0x60a0'e71e, 0x751d'475b ); |
| 178 |
} |
| 179 |
|
| 180 |
TEST_CASE( "extendedMult 8x8", "[integer][approvals]" ) { |
| 181 |
// a x 0 == 0 |
| 182 |
CommutativeMultCheck<uint8_t>( 0xcd, 0, 0, 0 ); |
| 183 |
|
| 184 |
// bit carried from low half to upper half |
| 185 |
CommutativeMultCheck<uint8_t>( uint8_t( 1 ) << 7, 2, 1, 0 ); |
| 186 |
|
| 187 |
// bits in upper half on one side, bits in lower half on other side |
| 188 |
CommutativeMultCheck<uint8_t>( 0x80, 0x03, 0x01, 0x80 ); |
| 189 |
|
| 190 |
// Some input numbers without interesting patterns |
| 191 |
CommutativeMultCheck<uint8_t>( 0xaa, 0xbb, 0x7c, 0x2e ); |
| 192 |
CommutativeMultCheck<uint8_t>( 0x7c, 0x2e, 0x16, 0x48 ); |
| 193 |
CommutativeMultCheck<uint8_t>( 0xdc, 0xcd, 0xb0, 0x2c ); |
| 194 |
CommutativeMultCheck<uint8_t>( 0xb0, 0x2c, 0x1e, 0x40 ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
TEST_CASE( "negative and positive signed integers keep their order after transposeToNaturalOrder", |
| 199 |
"[integer][approvals]") { |
| 200 |
using Catch::Detail::transposeToNaturalOrder; |
| 201 |
int32_t negative( -1 ); |
| 202 |
int32_t positive( 1 ); |
| 203 |
uint32_t adjusted_negative = |
| 204 |
transposeToNaturalOrder<int32_t>( static_cast<uint32_t>( negative ) ); |
| 205 |
uint32_t adjusted_positive = |
| 206 |
transposeToNaturalOrder<int32_t>( static_cast<uint32_t>( positive ) ); |
| 207 |
REQUIRE( adjusted_negative < adjusted_positive ); |
| 208 |
REQUIRE( adjusted_positive - adjusted_negative == 2 ); |
| 209 |
|
| 210 |
// Conversion has to be reversible |
| 211 |
REQUIRE( negative == static_cast<int32_t>( transposeToNaturalOrder<int32_t>( |
| 212 |
adjusted_negative ) ) ); |
| 213 |
REQUIRE( positive == static_cast<int32_t>( transposeToNaturalOrder<int32_t>( |
| 214 |
adjusted_positive ) ) ); |
| 215 |
} |
| 216 |
|
| 217 |
TEST_CASE( "unsigned integers are unchanged by transposeToNaturalOrder", |
| 218 |
"[integer][approvals]") { |
| 219 |
using Catch::Detail::transposeToNaturalOrder; |
| 220 |
uint32_t max = std::numeric_limits<uint32_t>::max(); |
| 221 |
uint32_t zero = 0; |
| 222 |
REQUIRE( max == transposeToNaturalOrder<uint32_t>( max ) ); |
| 223 |
REQUIRE( zero == transposeToNaturalOrder<uint32_t>( zero ) ); |
| 224 |
} |