opti: is_not_zero

This commit is contained in:
gbrochar 2024-02-18 15:55:05 +01:00
parent 2f7d2922c7
commit d368c925fb
1 changed files with 12 additions and 6 deletions

View File

@ -79,8 +79,17 @@ int bigint_dif(bigint_t a, bigint_t b) {
return 0;
}
void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) {
while (bigint_dif(*y, zero)) {
int is_not_zero(bigint_t n) {
for (size_t i = 0; i < n.len; i++) {
if (n.data[i]) {
return 1;
}
}
return 0;
}
void tool(bigint_t borrow, bigint_t *y, bigint_t a) {
while (is_not_zero(*y)) {
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];
@ -88,18 +97,15 @@ void tool(bigint_t borrow, bigint_t *y, bigint_t zero, bigint_t a) {
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);
tool(borrow, &y, zero, a);
tool(borrow, &y, a);
bigint_destroy(y);
bigint_destroy(borrow);
bigint_destroy(zero);
}
// TODO check opti