From bc03a666203de25faf6246847fecbc8502267212 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 6 Dec 2025 16:13:53 +0100 Subject: Add saturated arithmetic operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libj2_j2u_sub_ju.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'libj2_j2u_sub_ju.c') diff --git a/libj2_j2u_sub_ju.c b/libj2_j2u_sub_ju.c index 0cd0343..651cd1c 100644 --- a/libj2_j2u_sub_ju.c +++ b/libj2_j2u_sub_ju.c @@ -41,6 +41,10 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) EXPECT(libj2_j2u_sub_ju_overflow(&a, b) == expected_overflow); EXPECT(libj2_j2u_eq_j2u(&a, &expected)); + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + libj2_j2u_sat_sub_ju(&a, b); + EXPECT(expected_overflow ? libj2_j2u_is_zero(&a) : libj2_j2u_eq_j2u(&a, &expected)); + a = (struct libj2_j2u){.high = a_high, .low = a_low}; EXPECT(libj2_j2u_sub_ju_overflow_p((const struct libj2_j2u *)&a, b) == expected_overflow); EXPECT(a.high == a_high); @@ -68,6 +72,17 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) EXPECT(libj2_j2u_sub_ju_to_j2u_overflow(&a, b, &a) == expected_overflow); EXPECT(libj2_j2u_eq_j2u(&a, &expected)); + r = (struct libj2_j2u){111, 222}; + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + libj2_j2u_sat_sub_ju_to_j2u(&a, b, &r); + EXPECT(a.high == a_high); + EXPECT(a.low == a_low); + EXPECT(expected_overflow ? libj2_j2u_is_zero(&r) : libj2_j2u_eq_j2u(&r, &expected)); + + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + libj2_j2u_sat_sub_ju_to_j2u(&a, b, &a); + EXPECT(expected_overflow ? libj2_j2u_is_zero(&a) : libj2_j2u_eq_j2u(&a, &expected)); + r = (struct libj2_j2u){111, 222}; a = (struct libj2_j2u){.high = a_high, .low = a_low}; libj2_ju_sub_j2u_to_j2u(b, &a, &r); @@ -91,6 +106,15 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) libj2_minus_j2u(&a); EXPECT(libj2_j2u_eq_j2u(&a, &expected)); + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + libj2_j2u_sat_rsub_ju(&a, b); + if (expected_roverflow) { + EXPECT(libj2_j2u_is_zero(&a)); + } else { + libj2_minus_j2u(&a); + EXPECT(libj2_j2u_eq_j2u(&a, &expected)); + } + a = (struct libj2_j2u){.high = a_high, .low = a_low}; EXPECT(libj2_j2u_rsub_ju_overflow_p((const struct libj2_j2u *)&a, b) == expected_roverflow); EXPECT(a.high == a_high); @@ -107,7 +131,28 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) a = (struct libj2_j2u){.high = a_high, .low = a_low}; EXPECT(libj2_ju_sub_j2u_to_j2u_overflow(b, &a, &a) == expected_roverflow); libj2_minus_j2u(&a); - EXPECT(libj2_j2u_eq_j2u(&a, &expected));; + EXPECT(libj2_j2u_eq_j2u(&a, &expected)); + + r = (struct libj2_j2u){111, 222}; + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + libj2_ju_sat_sub_j2u_to_j2u(b, &a, &r); + EXPECT(a.high == a_high); + EXPECT(a.low == a_low); + if (expected_roverflow) { + EXPECT(libj2_j2u_is_zero(&r)); + } else { + libj2_minus_j2u(&r); + EXPECT(libj2_j2u_eq_j2u(&r, &expected)); + } + + a = (struct libj2_j2u){.high = a_high, .low = a_low}; + libj2_ju_sat_sub_j2u_to_j2u(b, &a, &a); + if (expected_roverflow) { + EXPECT(libj2_j2u_is_zero(&a)); + } else { + libj2_minus_j2u(&a); + EXPECT(libj2_j2u_eq_j2u(&a, &expected)); + } a = (struct libj2_j2u){.high = a_high, .low = a_low}; EXPECT(libj2_ju_sub_j2u_overflow_p(b, (const struct libj2_j2u *)&a) == expected_roverflow); -- cgit v1.2.3-70-g09d2