fix: substraction

This commit is contained in:
gbrochar 2024-02-18 15:37:37 +01:00
parent c8e5e6bf67
commit 2f7d2922c7
2 changed files with 23 additions and 11 deletions

View File

@ -18,9 +18,15 @@ fast:
profile:
gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME)
profile-clang:
clang -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME)
profile-fast:
gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME)
profile-fast-clang:
clang -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME)
fclean:
rm -rf $(NAME)

View File

@ -68,29 +68,35 @@ int64_t bigint_cmp(bigint_t a, bigint_t b) {
// TODO refactor/clean assume same length ?
int bigint_dif(bigint_t a, bigint_t b) {
int cursor = a.len - 1;
while (cursor >= 0) {
int cursor = a.len;
while (--cursor >= 0) {
if (a.data[cursor] ^ b.data[cursor]) {
return 1;
}
cursor -= 1;
//cursor -= 1;
}
return 0;
}
void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) {
while (bigint_dif(*y, zero)) {
for (size_t i = 0; i < a.len; i++) {
borrow.data[i] = ~a.data[i] & y->data[i];
a.data[i] = a.data[i] ^ y->data[i];
}
bigint_destroy(*y);
*y = assignable_bigint_bitwise_left_shift(borrow);
}
}
// TODO check opti
void bigint_substraction(bigint_t a, bigint_t b) {
bigint_t borrow = bigint_clone(b);
bigint_t y = bigint_clone(b);
bigint_t zero = bigint_zero(a.len);
while (bigint_dif(borrow, zero)) {
for (size_t i = 0; i < a.len; i++) {
borrow.data[i] = ~a.data[i] & y.data[i];
a.data[i] = a.data[i] ^ y.data[i];
}
bigint_destroy(y);
y = assignable_bigint_bitwise_left_shift(borrow);
}
tool(borrow, &y, zero, a);
bigint_destroy(y);
bigint_destroy(borrow);
bigint_destroy(zero);