Unsigned to signed cast is implementation defined (though in practice it has the expected 2's complement semantics on essentially every relevant platform). However, the only reason we used one in the first place was to take advantage of arithmetic right shift broadcasting the sign bit to the full length of a 32-bit integer. This requires right shift of negative numbers to be defined as arithmetic – in C it is instead UB. The remaining unsigned-to-unsigned casts would have been all defined, but it is easier to keep the codebase UB-free by just banning all direct casts. Since we do want to convert between 32-bit and 64-bit integers, introduce meri_casts.h as a way to make the intent clear and warn on any extra unintended implicit casts at the callsite.
128 lines
3.7 KiB
C
128 lines
3.7 KiB
C
#include <assert.h>
|
|
#include <inttypes.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "meri_object.h"
|
|
#include "meri_bigint.h"
|
|
#include "meri_casts.h"
|
|
|
|
#include "tests.h"
|
|
|
|
// bigint: owned
|
|
static int64_t i64_from_bigint(struct meri_bigint *bigint) {
|
|
// One limb is 32 bits, so 3 or more limbs cannot fit in an i64
|
|
assert(bigint->nlimbs <= 2);
|
|
|
|
uint32_t low = 0; // A zero-limb bigint has the value 0
|
|
if (bigint->nlimbs > 0)
|
|
low = bigint->limbs[0];
|
|
|
|
uint32_t high = meri_u32_extend_sign_bit(low); // Default to sign extending
|
|
if (bigint->nlimbs > 1)
|
|
high = bigint->limbs[1];
|
|
|
|
RELEASE(bigint);
|
|
|
|
// Unsigned to signed conversion is implementation defined, but we'll risk it
|
|
// in the tests
|
|
return (int64_t)(meri_u32_u64(high) << 32 | meri_u32_u64(low));
|
|
}
|
|
|
|
// Ensures that addition of two i32 values gives the same result, regardless of
|
|
// whether we use bigints or native 64-bit arithmetic
|
|
static void test_addition(int32_t a, int32_t b) {
|
|
struct meri_bigint *a_bigint = meri_new_bigint(1, (uint32_t[]){(uint32_t)a});
|
|
struct meri_bigint *b_bigint = meri_new_bigint(1, (uint32_t[]){(uint32_t)b});
|
|
struct meri_bigint *result = meri_bigint_add(a_bigint, b_bigint);
|
|
assert(i64_from_bigint(/* move */ result) == (int64_t)a + (int64_t)b);
|
|
}
|
|
|
|
// Like test_addition, but testing subtraction instead
|
|
static void test_subtraction(int32_t a, int32_t b) {
|
|
struct meri_bigint *a_bigint = meri_new_bigint(1, (uint32_t[]){(uint32_t)a});
|
|
struct meri_bigint *b_bigint = meri_new_bigint(1, (uint32_t[]){(uint32_t)b});
|
|
struct meri_bigint *result = meri_bigint_sub(a_bigint, b_bigint);
|
|
assert(i64_from_bigint(/* move */ result) == (int64_t)a - (int64_t)b);
|
|
}
|
|
|
|
#define TEST(...) {\
|
|
printf("%u", ++test);\
|
|
fflush(stdout);\
|
|
{ __VA_ARGS__; }\
|
|
printf(".");\
|
|
}
|
|
|
|
static int32_t interesting_ints[] = {
|
|
1,
|
|
2,
|
|
(int32_t)((1ul<<31) - 1), // The most positive i32
|
|
-1,
|
|
-2,
|
|
(int32_t)(1ul<<31), // The most negative i32
|
|
0
|
|
};
|
|
|
|
#define NELEMS(x) ((sizeof (x)) / (sizeof (x)[0]))
|
|
|
|
void test_bigint(void) {
|
|
unsigned int test = 0;
|
|
for (size_t i = 0; i < NELEMS(interesting_ints); i++) {
|
|
for (size_t j = 0; j < NELEMS(interesting_ints); j++) {
|
|
TEST(test_addition(interesting_ints[i], interesting_ints[j]));
|
|
TEST(test_subtraction(interesting_ints[i], interesting_ints[j]));
|
|
}
|
|
}
|
|
|
|
printf("."); // Mark a new section of tests
|
|
|
|
struct meri_bigint *a, *b, *result;
|
|
|
|
// If first addend is moved, it is reused for the result
|
|
TEST(
|
|
a = meri_new_bigint(1, (uint32_t[]){2});
|
|
b = meri_new_bigint(1, (uint32_t[]){3});
|
|
result = meri_bigint_add(/* move */ a, COPY(b));
|
|
assert(result == a);
|
|
assert(result->header.refcount == 1);
|
|
assert(result != b);
|
|
assert(b->header.refcount == 1);
|
|
RELEASE(result);
|
|
)
|
|
|
|
// If second addend is moved but first is not, it is reused for the result
|
|
TEST(
|
|
a = meri_new_bigint(1, (uint32_t[]){2});
|
|
// b is still live from previous test
|
|
result = meri_bigint_add(COPY(a), /* move */ b);
|
|
assert(result != a);
|
|
assert(a->header.refcount == 1);
|
|
assert(result == b);
|
|
assert(result->header.refcount == 1);
|
|
RELEASE(a);
|
|
RELEASE(result);
|
|
)
|
|
|
|
// Redundant prefix of a result is removed
|
|
TEST(
|
|
a = meri_new_bigint(2, (uint32_t[]){0xffffffff, 0xfffffffe});
|
|
b = meri_new_bigint(2, (uint32_t[]){0, 1});
|
|
result = meri_bigint_add(/* move */ a, /* move */ b);
|
|
assert(result->nlimbs == 1);
|
|
assert(result->limbs[0] == 0xffffffff);
|
|
RELEASE(result);
|
|
)
|
|
|
|
// Not of subtraction's second argument does not affect other copies
|
|
TEST(
|
|
a = meri_new_bigint(1, (uint32_t[]){4});
|
|
b = meri_new_bigint(1, (uint32_t[]){5});
|
|
result = meri_bigint_sub(/* move */ a, COPY(b));
|
|
assert(b->header.refcount == 1);
|
|
assert(i64_from_bigint(/* move */ b) == 5);
|
|
assert(i64_from_bigint(/* move */ result) == -1);
|
|
)
|
|
|
|
// TODO: Test that all allocations are dead
|
|
}
|