diff options
| -rw-r--r-- | BigInt.c | 70 | ||||
| -rw-r--r-- | BigInt.h | 18 |
2 files changed, 59 insertions, 29 deletions
@@ -18,8 +18,8 @@ struct BigIntMeta { }; static inline struct BigIntMeta* -B_metadata(BigInt s) { -return (struct BigIntMeta*)(s - sizeof(struct BigIntMeta)); +B_metadata(const BigInt s) { + return (struct BigIntMeta*)(s - sizeof(struct BigIntMeta)); } /** Helpers **/ @@ -127,7 +127,7 @@ B_newbytes(MemoryManager *mm, usize bytes) { } BigInt -B_dup(MemoryManager *mm, BigInt bigint) { +B_dup(MemoryManager *mm, const BigInt bigint) { if (NULL == bigint) return NULL; @@ -182,10 +182,13 @@ B_free(BigInt *bigint) { /* Modify */ Result -B_sum(BigInt *inta, BigInt intb) { - if (NULL == inta || NULL == intb || NULL == *inta) +B_sum(BigInt *inta, const BigInt intb) { + if (NULL == inta || NULL == intb) return R_NullArgument; + if (NULL == *inta) + return R_InvalidArgument; + struct BigIntMeta *am = B_metadata(*inta), *bm = B_metadata(intb); if (am->resizeable && am->words < bm->words) { @@ -226,10 +229,13 @@ B_sum(BigInt *inta, BigInt intb) { } Result -B_sub(BigInt *inta, BigInt intb) { - if (NULL == inta || NULL == intb || NULL == *inta) +B_sub(BigInt *inta, const BigInt intb) { + if (NULL == inta || NULL == intb) return R_NullArgument; + if (NULL == *inta) + return R_InvalidArgument; + struct BigIntMeta *am = B_metadata(*inta), *bm = B_metadata(intb); if (am->resizeable && am->words < bm->words) { @@ -269,6 +275,8 @@ B_sub(BigInt *inta, BigInt intb) { return R_Ok; } + + Result B_compl(BigInt *intx) { if (NULL == intx) @@ -284,11 +292,14 @@ B_compl(BigInt *intx) { } Result -B_bitand(BigInt *intx, BigInt *inty) { +B_bitand(BigInt *intx, const BigInt inty) { if (NULL == intx || NULL == inty) return R_NullArgument; - struct BigIntMeta *xm = B_metadata(intx), *ym = B_metadata(inty); + if (NULL == *intx) + return R_InvalidArgument; + + struct BigIntMeta *xm = B_metadata(*intx), *ym = B_metadata(inty); if (xm->resizeable && xm->words < ym->words) { RETURN_NOTOK(B_resize(intx, ym->words)); @@ -306,11 +317,14 @@ B_bitand(BigInt *intx, BigInt *inty) { } Result -B_bitor(BigInt *intx, BigInt *inty) { +B_bitor(BigInt *intx, const BigInt inty) { if (NULL == intx || NULL == inty) return R_NullArgument; - struct BigIntMeta *xm = B_metadata(intx), *ym = B_metadata(inty); + if (NULL == *intx) + return R_InvalidArgument; + + struct BigIntMeta *xm = B_metadata(*intx), *ym = B_metadata(inty); if (xm->resizeable && xm->words < ym->words) { RETURN_NOTOK(B_resize(intx, ym->words)); @@ -328,11 +342,14 @@ B_bitor(BigInt *intx, BigInt *inty) { } Result -B_bitxor(BigInt *intx, BigInt *inty) { +B_bitxor(BigInt *intx, const BigInt inty) { if (NULL == intx || NULL == inty) return R_NullArgument; - struct BigIntMeta *xm = B_metadata(intx), *ym = B_metadata(inty); + if (NULL == *intx) + return R_InvalidArgument; + + struct BigIntMeta *xm = B_metadata(*intx), *ym = B_metadata(inty); if (xm->resizeable && xm->words < ym->words) { RETURN_NOTOK(B_resize(intx, ym->words)); @@ -351,13 +368,20 @@ B_bitxor(BigInt *intx, BigInt *inty) { Result B_rshift(BigInt *intx, usize shift) { - if (NULL == intx || NULL == *intx) + if (NULL == intx) return R_NullArgument; - if (shift == 0) + if (NULL == *intx) + return R_InvalidArgument; + + if (0 == shift) return R_Ok; struct BigIntMeta *xm = B_metadata(*intx); + + if (0 == xm->words) + return R_Uninitialized; + UWord *x = *intx; usize word_moves = shift / UWORD_BIT; @@ -392,14 +416,20 @@ B_rshift(BigInt *intx, usize shift) { Result B_lshift(BigInt *intx, usize shift) { - if (NULL == intx || NULL == *intx) + if (NULL == intx) return R_NullArgument; - if (shift == 0) + if (NULL == *intx) + return R_InvalidArgument; + + if (0 == shift) return R_Ok; struct BigIntMeta *xm = B_metadata(*intx); + if (0 == xm->words) + return R_Uninitialized; + if (xm->resizeable) { UWord width = B_bitwidth(*intx); if (width + shift > xm->words * UWORD_BIT) { @@ -445,14 +475,14 @@ B_lshift(BigInt *intx, usize shift) { /* Get */ usize -B_bytes(BigInt x) { +B_bytes(const BigInt x) { if (NULL == x) return 0; return B_metadata(x)->words * sizeof(UWord); } usize -B_bitwidth(BigInt intx) { +B_bitwidth(const BigInt intx) { if (NULL == intx) return 0; @@ -474,7 +504,7 @@ B_bitwidth(BigInt intx) { } char* -B_tostr(MemoryManager *mm, BigInt intx) { +B_tostr(MemoryManager *mm, const BigInt intx) { if (NULL == intx) return NULL; @@ -10,7 +10,7 @@ typedef void* BigInt; BigInt B_new(MemoryManager *mm, int value); BigInt B_newstr(MemoryManager *mm, const char* str); BigInt B_newbytes(MemoryManager *mm, usize bytes); -BigInt B_dup(MemoryManager *mm, BigInt bigint); +BigInt B_dup(MemoryManager *mm, const BigInt bigint); BigInt B_newsum(MemoryManager *mm, BigInt a, BigInt b); BigInt B_newsub(MemoryManager *mm, BigInt a, BigInt b); @@ -21,21 +21,21 @@ Result B_free(BigInt *bigint); /* Modify */ -Result B_sum(BigInt *a, BigInt b); -Result B_sub(BigInt *a, BigInt b); +Result B_sum(BigInt *a, const BigInt b); +Result B_sub(BigInt *a, const BigInt b); Result B_compl(BigInt *x); -Result B_bitand(BigInt *x, BigInt *y); -Result B_bitor(BigInt *x, BigInt *y); -Result B_bitxor(BigInt *x, BigInt *y); +Result B_bitand(BigInt *x, const BigInt y); +Result B_bitor(BigInt *x, const BigInt y); +Result B_bitxor(BigInt *x, const BigInt y); Result B_rshift(BigInt *x, usize shift); Result B_lshift(BigInt *x, usize shift); /* Get */ -usize B_bytes(BigInt x); -usize B_bitwidth(BigInt x); -char* B_tostr(MemoryManager *mm, BigInt x); +usize B_bytes(const BigInt x); +usize B_bitwidth(const BigInt x); +char* B_tostr(MemoryManager *mm, const BigInt x); int B_eq(const BigInt x, const BigInt y); int B_not_eq(const BigInt x, const BigInt y); |
