aboutsummaryrefslogtreecommitdiff
path: root/BigInt.c
diff options
context:
space:
mode:
Diffstat (limited to 'BigInt.c')
-rw-r--r--BigInt.c70
1 files changed, 50 insertions, 20 deletions
diff --git a/BigInt.c b/BigInt.c
index 1e1a0db..fe0eedf 100644
--- a/BigInt.c
+++ b/BigInt.c
@@ -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;