aboutsummaryrefslogtreecommitdiffstats
path: root/libj2_j2u_add_ju.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-12-06 10:21:36 +0100
committerMattias Andrée <m@maandree.se>2025-12-06 10:21:36 +0100
commitc28859c3e10c84fd9f81ebf88336c9b693448eeb (patch)
tree22f9804f4ba96474c23c93c021d293ba5959ccfa /libj2_j2u_add_ju.c
parentSecond commit (diff)
downloadlibj2-c28859c3e10c84fd9f81ebf88336c9b693448eeb.tar.gz
libj2-c28859c3e10c84fd9f81ebf88336c9b693448eeb.tar.bz2
libj2-c28859c3e10c84fd9f81ebf88336c9b693448eeb.tar.xz
Add overflow and underflow prediction functions
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'libj2_j2u_add_ju.c')
-rw-r--r--libj2_j2u_add_ju.c30
1 files changed, 30 insertions, 0 deletions
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);
}