summaryrefslogtreecommitdiff
path: root/source/kit
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2022-12-11 14:45:33 +0100
committerMitya Selivanov <automainint@guattari.tech>2022-12-11 14:45:33 +0100
commit550214f7b4cd5ee39ce3d246b928f6f6f9a04773 (patch)
tree829d1454ce3e54e1346e089a9046014b9de5035d /source/kit
parent2a673a8cb44fe34e1a0feed7fb40d7fc9e0203cd (diff)
downloadkit-550214f7b4cd5ee39ce3d246b928f6f6f9a04773.zip
[bigint] tests
Diffstat (limited to 'source/kit')
-rw-r--r--source/kit/bigint.h28
-rw-r--r--source/kit/file.c4
-rw-r--r--source/kit/string_ref.h26
3 files changed, 47 insertions, 11 deletions
diff --git a/source/kit/bigint.h b/source/kit/bigint.h
index 26268a6..30233c6 100644
--- a/source/kit/bigint.h
+++ b/source/kit/bigint.h
@@ -99,8 +99,7 @@ static ptrdiff_t kit_bi_significant_bit_count(kit_bigint_t const x) {
: (byte & 0x08) != 0 ? 4
: (byte & 0x04) != 0 ? 3
: (byte & 0x02) != 0 ? 2
- : (byte & 0x01) != 0 ? 1
- : 0;
+ : 1;
return bytes * 8 + bits;
}
@@ -250,7 +249,7 @@ static kit_bigint_t kit_bi_mul(kit_bigint_t const x,
continue;
for (ptrdiff_t j = 0; i + j < KIT_BIGINT_SIZE / 4; j++) {
- if (y.v32[i] == 0)
+ if (y.v32[j] == 0)
continue;
uint64_t carry = ((uint64_t) x.v32[i]) * ((uint64_t) y.v32[j]);
@@ -294,7 +293,7 @@ static kit_bi_division_t kit_bi_div(kit_bigint_t const x,
y = kit_bi_shl_uword(y, (kit_uword_t) shift);
while (shift >= 0) {
- if (kit_bi_compare(result.remainder, y) > 0) {
+ if (kit_bi_compare(result.remainder, y) >= 0) {
result.remainder = kit_bi_sub(result.remainder, y);
result.quotient.v8[shift / 8] |= (1u << (shift % 8));
}
@@ -432,6 +431,21 @@ static kit_bigint_t kit_bi_base58(kit_str_t const base58) {
# pragma GCC diagnostic pop
#endif
+#define KIT_BIN(static_str_) \
+ kit_bi_bin(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_DEC(static_str_) \
+ kit_bi_dec(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_HEX(static_str_) \
+ kit_bi_hex(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_BASE32(static_str_) \
+ kit_bi_base32(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_BASE58(static_str_) \
+ kit_bi_base58(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
#ifndef KIT_DISABLE_SHORT_NAMES
# define bigint_t kit_bigint_t
# define bi_uword kit_bi_uword
@@ -442,9 +456,15 @@ static kit_bigint_t kit_bi_base58(kit_str_t const base58) {
# define bi_neg kit_bi_neg
# define bi_sub kit_bi_sub
# define bi_mul kit_bi_mul
+# define bi_div kit_bi_div
# define hex_digit kit_hex_digit
# define bi_hex kit_bi_hex
# define bi_base58 kit_bi_base58
+# define BIN KIT_BIN
+# define DEC KIT_DEC
+# define HEX KIT_HEX
+# define BASE32 KIT_BASE32
+# define BASE58 KIT_BASE58
#endif
#ifdef __cplusplus
diff --git a/source/kit/file.c b/source/kit/file.c
index ff3a18d..d69bf76 100644
--- a/source/kit/file.c
+++ b/source/kit/file.c
@@ -24,9 +24,9 @@ static int is_delim(char const c) {
kit_string_t kit_path_norm(kit_str_t const path,
kit_allocator_t const alloc) {
- SZ(parent, "..");
+ str_t parent = SZ("..");
- kit_string_t norm;
+ string_t norm;
DA_INIT(norm, path.size, alloc);
assert(norm.size == path.size);
diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h
index 2f285dc..4b2cabd 100644
--- a/source/kit/string_ref.h
+++ b/source/kit/string_ref.h
@@ -13,11 +13,27 @@ typedef KIT_AR(char) kit_string_ref_t;
typedef kit_string_mut_t kit_out_str_t;
typedef kit_string_ref_t kit_str_t;
-#define KIT_SZ(name_, static_str_) \
- kit_str_t name_ = { \
- .size = (sizeof(static_str_) / sizeof((static_str_)[0])) - 1, \
- .values = (static_str_) \
- }
+#ifdef __GNUC__
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wunused-function"
+# pragma GCC diagnostic ignored "-Wunknown-pragmas"
+# pragma GCC push_options
+# pragma GCC optimize("O3")
+#endif
+
+static kit_str_t kit_str(ptrdiff_t const size,
+ char const *const static_string) {
+ kit_str_t const s = { .size = size, .values = static_string };
+ return s;
+}
+
+#ifdef __GNUC__
+# pragma GCC pop_options
+# pragma GCC diagnostic pop
+#endif
+
+#define KIT_SZ(static_str_) \
+ kit_str(sizeof(static_str_) - 1, (static_str_))
#ifndef KIT_DISABLE_SHORT_NAMES
# define string_mut_t kit_string_mut_t