aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2026-05-28 15:44:24 +0300
committerSyndamia <kamen@syndamia.com>2026-07-12 09:14:06 +0300
commit5d755f45e31d81d771140cfe5f3dbd13d5442bcd (patch)
treec7a6b213eafd5f221db05052172ea6d3f9b5954c
parentbe67821d6fddfa97abb91188c6ef8799bff15c74 (diff)
downloadfoollib-5d755f45e31d81d771140cfe5f3dbd13d5442bcd.tar
foollib-5d755f45e31d81d771140cfe5f3dbd13d5442bcd.tar.gz
foollib-5d755f45e31d81d771140cfe5f3dbd13d5442bcd.zip
feat: Implement comparisons
-rw-r--r--BigInt.c85
-rw-r--r--BigInt.h7
2 files changed, 92 insertions, 0 deletions
diff --git a/BigInt.c b/BigInt.c
index 21c22d3..1e1a0db 100644
--- a/BigInt.c
+++ b/BigInt.c
@@ -565,3 +565,88 @@ B_tostr(MemoryManager *mm, BigInt intx) {
mm->free(*mm, (void**)&bcd);
return str;
}
+
+int
+B_eq(const BigInt intx, const BigInt inty) {
+ if (NULL == intx || NULL == inty)
+ return intx == inty;
+
+ struct BigIntMeta *xm = B_metadata(intx), *ym = B_metadata(inty);
+ UWord *x = intx, *y = inty;
+
+ usize words;
+ if (xm->words < ym->words) {
+ for (usize w = xm->words; w < ym->words; ++w)
+ if (y[w] != 0)
+ return 0;
+
+ words = xm->words;
+ }
+ else {
+ for (usize w = ym->words; w < xm->words; ++w)
+ if (x[w] != 0)
+ return 0;
+
+ words = ym->words;
+ }
+
+ for (usize w = words - 1; w < words; --w)
+ if (x[w] != y[w])
+ return 0;
+
+ return 1;
+}
+
+int
+B_not_eq(const BigInt x, const BigInt y) {
+ return !B_eq(x, y);
+}
+
+int
+B_lt(const BigInt intx, const BigInt inty) {
+ if (NULL == intx || NULL == inty)
+ return R_NullArgument;
+
+ struct BigIntMeta *xm = B_metadata(intx), *ym = B_metadata(inty);
+ UWord *x = (void*)intx, *y = (void*)inty;
+
+ usize words;
+ if (xm->words < ym->words) {
+ for (usize w = xm->words; w < ym->words; ++w)
+ if (y[w] != 0)
+ return 1;
+
+ words = xm->words;
+ }
+ else {
+ for (usize w = ym->words; w < xm->words; ++w)
+ if (x[w] != 0)
+ return 0;
+
+ words = ym->words;
+ }
+
+ for (usize w = words - 1; w < words; --w) {
+ if (x[w] < y[w])
+ return 1;
+ else if (x[w] > y[w])
+ return 0;
+ }
+
+ return 0;
+}
+
+int
+B_le(const BigInt x, const BigInt y) {
+ return B_lt(x, y) || B_eq(x, y);
+}
+
+int
+B_gt(const BigInt x, const BigInt y) {
+ return !B_lt(x, y) && !B_eq(x, y);
+}
+
+int
+B_ge(const BigInt x, const BigInt y) {
+ return !B_lt(x, y);
+}
diff --git a/BigInt.h b/BigInt.h
index d894b35..89cb3ab 100644
--- a/BigInt.h
+++ b/BigInt.h
@@ -37,4 +37,11 @@ usize B_bytes(BigInt x);
usize B_bitwidth(BigInt x);
char* B_tostr(MemoryManager *mm, BigInt x);
+int B_eq(const BigInt x, const BigInt y);
+int B_not_eq(const BigInt x, const BigInt y);
+int B_lt(const BigInt x, const BigInt y);
+int B_le(const BigInt x, const BigInt y);
+int B_gt(const BigInt x, const BigInt y);
+int B_ge(const BigInt x, const BigInt y);
+
#endif /* _BIGINT */