diff options
| author | Mattias Andrée <m@maandree.se> | 2025-12-06 16:13:53 +0100 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2025-12-06 16:13:53 +0100 |
| commit | bc03a666203de25faf6246847fecbc8502267212 (patch) | |
| tree | 080080b24980640c6d4935a4684bf78b4d2d3077 /libj2_j2u_sub_ju.c | |
| parent | Add division with underflow detection (diff) | |
| download | libj2-bc03a666203de25faf6246847fecbc8502267212.tar.gz libj2-bc03a666203de25faf6246847fecbc8502267212.tar.bz2 libj2-bc03a666203de25faf6246847fecbc8502267212.tar.xz | |
Add saturated arithmetic operations
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
| -rw-r--r-- | libj2_j2u_sub_ju.c | 47 |
1 files changed, 46 insertions, 1 deletions
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 @@ -42,6 +42,10 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) 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); EXPECT(a.low == a_low); @@ -70,6 +74,17 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) 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); EXPECT(a.high == a_high); EXPECT(a.low == a_low); @@ -92,6 +107,15 @@ check(uintmax_t a_high, uintmax_t a_low, uintmax_t b) 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); EXPECT(a.low == a_low); @@ -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); |
