From c28859c3e10c84fd9f81ebf88336c9b693448eeb Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 6 Dec 2025 10:21:36 +0100 Subject: Add overflow and underflow prediction functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libj2_j2u_add_ju.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'libj2_j2u_add_ju.c') diff --git a/libj2_j2u_add_ju.c b/libj2_j2u_add_ju.c index 8592662..f368857 100644 --- a/libj2_j2u_add_ju.c +++ b/libj2_j2u_add_ju.c @@ -35,6 +35,11 @@ check_zero_low(uintmax_t high, uintmax_t low) EXPECT(a.high == high); EXPECT(a.low == low); + a = (struct libj2_j2u){.high = high, .low = 0}; + EXPECT(libj2_j2u_add_ju_overflow_p((const struct libj2_j2u *)&a, low) == 0); + EXPECT(a.high == high); + EXPECT(a.low == 0); + r = (struct libj2_j2u){111, 222}; a = (struct libj2_j2u){.high = high, .low = 0}; libj2_j2u_add_ju_to_j2u(&a, low, &r); @@ -86,6 +91,11 @@ check_zero_low(uintmax_t high, uintmax_t low) EXPECT(libj2_ju_add_j2u_to_j2u_overflow(low, &a, &a) == 0); EXPECT(a.high == high); EXPECT(a.low == low); + + a = (struct libj2_j2u){.high = high, .low = 0}; + EXPECT(libj2_ju_add_j2u_overflow_p(low, (const struct libj2_j2u *)&a) == 0); + EXPECT(a.high == high); + EXPECT(a.low == 0); } @@ -107,6 +117,11 @@ check_max_low(uintmax_t high, uintmax_t low) EXPECT(a.high == expected_high); EXPECT(a.low == expected_low); + a = (struct libj2_j2u){.high = high, .low = UINTMAX_MAX}; + EXPECT(libj2_j2u_add_ju_overflow_p((const struct libj2_j2u *)&a, low) == expected_overflow); + EXPECT(a.high == high); + EXPECT(a.low == UINTMAX_MAX); + r = (struct libj2_j2u){111, 222}; a = (struct libj2_j2u){.high = high, .low = UINTMAX_MAX}; libj2_j2u_add_ju_to_j2u(&a, low, &r); @@ -158,6 +173,11 @@ check_max_low(uintmax_t high, uintmax_t low) EXPECT(libj2_ju_add_j2u_to_j2u_overflow(low, &a, &a) == expected_overflow); EXPECT(a.high == expected_high); EXPECT(a.low == expected_low); + + a = (struct libj2_j2u){.high = high, .low = UINTMAX_MAX}; + EXPECT(libj2_ju_add_j2u_overflow_p(low, (const struct libj2_j2u *)&a) == expected_overflow); + EXPECT(a.high == high); + EXPECT(a.low == UINTMAX_MAX); } @@ -206,6 +226,11 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) EXPECT(a.high == expected_high); EXPECT(a.low == expected_low); + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + EXPECT(libj2_j2u_add_ju_overflow_p((const struct libj2_j2u *)&a, b) == expected_overflow); + EXPECT(a.high == a_high); + EXPECT(a.low == a_low); + r = (struct libj2_j2u){111, 222}; a = (struct libj2_j2u){.high = a_high, .low = a_low}; libj2_j2u_add_ju_to_j2u(&a, b, &r); @@ -257,6 +282,11 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) EXPECT(libj2_ju_add_j2u_to_j2u_overflow(b, &a, &a) == expected_overflow); EXPECT(a.high == expected_high); EXPECT(a.low == expected_low); + + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + EXPECT(libj2_ju_add_j2u_overflow_p(b, (const struct libj2_j2u *)&a) == expected_overflow); + EXPECT(a.high == a_high); + EXPECT(a.low == a_low); } -- cgit v1.2.3-70-g09d2