|
| 1 | +// Copyright 2026 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | +// |
| 5 | +// See: https://github.com/cppalliance/int128/issues/377 |
| 6 | + |
| 7 | +#define BOOST_INT128_ALLOW_SIGN_CONVERSION |
| 8 | +#include <boost/int128.hpp> |
| 9 | +#include <boost/core/lightweight_test.hpp> |
| 10 | +#include <limits> |
| 11 | +#include <cstdint> |
| 12 | + |
| 13 | +using namespace boost::int128; |
| 14 | + |
| 15 | +template <typename T> |
| 16 | +void test_div_by_one() |
| 17 | +{ |
| 18 | + constexpr auto min_val {std::numeric_limits<int128_t>::min()}; |
| 19 | + BOOST_TEST_EQ(min_val, min_val / T{1}); |
| 20 | +} |
| 21 | + |
| 22 | +template <typename T> |
| 23 | +void test_other_vals() |
| 24 | +{ |
| 25 | + constexpr auto min_val {std::numeric_limits<int128_t>::min()}; |
| 26 | + const auto min_div_2 {BOOST_INT128_INT128_C(-85070591730234615865843651857942052864)}; |
| 27 | + const auto min_div_4 {BOOST_INT128_INT128_C(-42535295865117307932921825928971026432)}; |
| 28 | + const auto min_div_16 {BOOST_INT128_INT128_C(-10633823966279326983230456482242756608)}; |
| 29 | + |
| 30 | + BOOST_TEST_EQ(min_div_2, min_val / T{2}); |
| 31 | + BOOST_TEST_EQ(min_div_4, min_val / T{4}); |
| 32 | + BOOST_TEST_EQ(min_div_16, min_val / T{16}); |
| 33 | +} |
| 34 | + |
| 35 | +// Bug 1: operator>>(int128_t, int128_t) was calling << instead of >> |
| 36 | +void test_right_shift_int128_amount() |
| 37 | +{ |
| 38 | + const auto val {int128_t(0, 0xFF00)}; |
| 39 | + const auto shift_4 {int128_t(0, 4)}; |
| 40 | + |
| 41 | + // Right-shift with int128_t shift amount must match integer shift |
| 42 | + BOOST_TEST_EQ(val >> shift_4, val >> 4); |
| 43 | + |
| 44 | + const auto expected_ff0 {int128_t(0, 0xFF0)}; |
| 45 | + BOOST_TEST_EQ(val >> shift_4, expected_ff0); |
| 46 | + |
| 47 | + // Test >>= with int128_t rhs |
| 48 | + auto val2 {val}; |
| 49 | + val2 >>= shift_4; |
| 50 | + BOOST_TEST_EQ(val2, expected_ff0); |
| 51 | + |
| 52 | + // Cross-word shift |
| 53 | + const auto big_val {int128_t(0x1234, 0)}; |
| 54 | + const auto shift_64 {int128_t(0, 64)}; |
| 55 | + const auto expected_1234 {int128_t(0, 0x1234)}; |
| 56 | + BOOST_TEST_EQ(big_val >> shift_64, expected_1234); |
| 57 | + |
| 58 | + // Arithmetic right shift preserves sign for negative values |
| 59 | + constexpr auto min_val {std::numeric_limits<int128_t>::min()}; |
| 60 | + const auto shift_1 {int128_t(0, 1)}; |
| 61 | + BOOST_TEST_EQ(min_val >> shift_1, min_val >> 1); |
| 62 | + BOOST_TEST((min_val >> shift_1) < 0); |
| 63 | +} |
| 64 | + |
| 65 | +// Bug 2: UnsignedInteger / int128_t returned {rhs.high, res} instead of proper sign handling |
| 66 | +void test_unsigned_div_negative_int128() |
| 67 | +{ |
| 68 | + const std::uint64_t lhs {10}; |
| 69 | + const auto neg3 {-int128_t(0, 3)}; |
| 70 | + const auto pos3 {int128_t(0, 3)}; |
| 71 | + const auto expected_neg3 {-int128_t(0, 3)}; |
| 72 | + const auto expected_pos3 {int128_t(0, 3)}; |
| 73 | + |
| 74 | + // 10 / -3 = -3 |
| 75 | + BOOST_TEST_EQ(lhs / neg3, expected_neg3); |
| 76 | + |
| 77 | + // 10 / 3 = 3 |
| 78 | + BOOST_TEST_EQ(lhs / pos3, expected_pos3); |
| 79 | + |
| 80 | + // 7 / -1 = -7 |
| 81 | + const std::uint64_t seven {7}; |
| 82 | + const auto neg1 {-int128_t(0, 1)}; |
| 83 | + const auto expected_neg7 {-int128_t(0, 7)}; |
| 84 | + BOOST_TEST_EQ(seven / neg1, expected_neg7); |
| 85 | +} |
| 86 | + |
| 87 | +// Bug 3: UnsignedInteger % int128_t used rhs.low instead of abs_rhs.low |
| 88 | +// and applied wrong sign to remainder |
| 89 | +void test_unsigned_mod_negative_int128() |
| 90 | +{ |
| 91 | + const std::uint64_t lhs {10}; |
| 92 | + const auto neg3 {-int128_t(0, 3)}; |
| 93 | + const auto pos3 {int128_t(0, 3)}; |
| 94 | + const auto expected_1 {int128_t(0, 1)}; |
| 95 | + |
| 96 | + // 10 % -3 = 1 (remainder has sign of dividend, which is unsigned/positive) |
| 97 | + BOOST_TEST_EQ(lhs % neg3, expected_1); |
| 98 | + |
| 99 | + // 10 % 3 = 1 |
| 100 | + BOOST_TEST_EQ(lhs % pos3, expected_1); |
| 101 | + |
| 102 | + // 12 % -5 = 2 |
| 103 | + const std::uint64_t twelve {12}; |
| 104 | + const auto neg5 {-int128_t(0, 5)}; |
| 105 | + const auto expected_2 {int128_t(0, 2)}; |
| 106 | + BOOST_TEST_EQ(twelve % neg5, expected_2); |
| 107 | +} |
| 108 | + |
| 109 | +// Bug 4: operator%(int128_t, int128_t) early return was wrong when lhs = INT128_MIN |
| 110 | +// because abs(INT128_MIN) overflows back to INT128_MIN |
| 111 | +void test_min_val_modulo() |
| 112 | +{ |
| 113 | + constexpr auto min_val {std::numeric_limits<int128_t>::min()}; |
| 114 | + const auto zero {int128_t(0, 0)}; |
| 115 | + |
| 116 | + // INT128_MIN % 1 = 0 |
| 117 | + const auto one {int128_t(0, 1)}; |
| 118 | + BOOST_TEST_EQ(min_val % one, zero); |
| 119 | + |
| 120 | + // INT128_MIN % 2 = 0 (2^127 is even) |
| 121 | + const auto two {int128_t(0, 2)}; |
| 122 | + BOOST_TEST_EQ(min_val % two, zero); |
| 123 | + |
| 124 | + // INT128_MIN % 3 = -2 |
| 125 | + // -170141183460469231731687303715884105728 = -56713727820156410577229101238628035242 * 3 + (-2) |
| 126 | + const auto three {int128_t(0, 3)}; |
| 127 | + const auto expected_neg2 {BOOST_INT128_INT128_C(-2)}; |
| 128 | + BOOST_TEST_EQ(min_val % three, expected_neg2); |
| 129 | + |
| 130 | + // INT128_MIN % INT128_MIN = 0 |
| 131 | + BOOST_TEST_EQ(min_val % min_val, zero); |
| 132 | +} |
| 133 | + |
| 134 | +int main() |
| 135 | +{ |
| 136 | + test_div_by_one<std::int32_t>(); |
| 137 | + test_div_by_one<std::int64_t>(); |
| 138 | + test_div_by_one<int128_t>(); |
| 139 | + |
| 140 | + test_other_vals<std::int32_t>(); |
| 141 | + test_other_vals<std::int64_t>(); |
| 142 | + test_other_vals<int128_t>(); |
| 143 | + |
| 144 | + test_right_shift_int128_amount(); |
| 145 | + test_unsigned_div_negative_int128(); |
| 146 | + test_unsigned_mod_negative_int128(); |
| 147 | + test_min_val_modulo(); |
| 148 | + |
| 149 | + return boost::report_errors(); |
| 150 | +} |
0 commit comments