Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in zz_addi, zz_subi functions #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 61 additions & 50 deletions zz.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,72 +211,83 @@ void zz_zero(zz_ptr a)
void zz_addi(zz_ptr r, zz_srcptr a, sword_t c)
{
if (c < 0)
zz_subi(r, a, -c);
zz_subu(r, a, (word_t)-c);
else
{
len_t usize = BSDNT_ABS(a->size);
zz_addu(r, a, (word_t)c);
}

zz_fit(r, usize + 1);

if (a->size >= 0)
{
r->n[usize] = nn_add1(r->n, a->n, usize, c);
r->size = usize + (r->n[usize] != 0);
} else if (usize == 1)
{
word_t d = a->n[0];
void zz_subi(zz_ptr r, zz_srcptr a, sword_t c)
{
if (c < 0)
zz_addu(r, a, (word_t)-c);
else
zz_subu(r, a, (word_t)c);
}

if (d == (word_t) c)
r->size = 0;
else
{
r->n[0] = d > (word_t) c ? d - c : c - d;
r->size = d > (word_t) c ? -1 : 1;
}
} else
void zz_addu(zz_ptr r, zz_srcptr a, word_t c)
{
len_t usize = BSDNT_ABS(a->size);

zz_fit(r, usize + 1);

if (a->size >= 0)
{
r->n[usize] = nn_add1(r->n, a->n, usize, c);
r->size = usize + (r->n[usize] != 0);
} else if (usize == 1)
{
word_t d = a->n[0];

if (d == c)
r->size = 0;
else
{
nn_sub1(r->n, a->n, usize, c);
r->size = -usize + (r->n[usize - 1] == 0);
}
r->n[0] = d > c ? d - c : c - d;
r->size = d > c ? -1 : 1;
}
} else
{
nn_sub1(r->n, a->n, usize, c);
r->size = -usize + (r->n[usize - 1] == 0);
}
}

void zz_subi(zz_ptr r, zz_srcptr a, sword_t c)
void zz_subu(zz_ptr r, zz_srcptr a, word_t c)
{
if (c < 0)
zz_addi(r, a, -c);
else
if (a->size == 0)
{
if (c != 0)
{
zz_fit(r, 1);
r->n[0] = c;
r->size = -1;
} else
r->size = 0;
} else
{
long usize = BSDNT_ABS(a->size);

zz_fit(r, usize + 1);
if (a->size == 0)

if (a->size < 0)
{
r->size = -1;
r->n[0] = c;
} else
r->n[usize] = nn_add1(r->n, a->n, usize, c);
r->size = -usize - (r->n[usize] != 0);
} else if (usize == 1)
{
if (a->size < 0)
{
r->n[usize] = nn_add1(r->n, a->n, usize, c);
r->size = -usize - (r->n[usize] != 0);
} else if (usize == 1)
{
word_t d = a->n[0];

if (d == (word_t) c)
r->size = 0;
else
{
r->n[0] = d > (word_t) c ? d - c : c - d;
r->size = d > (word_t) c ? 1 : -1;
}
} else
word_t d = a->n[0];

if (d == c)
r->size = 0;
else
{
nn_sub1(r->n, a->n, usize, c);
r->size = usize - (r->n[usize - 1] == 0);
r->n[0] = d > c ? d - c : c - d;
r->size = d > c ? 1 : -1;
}
} else
{
nn_sub1(r->n, a->n, usize, c);
r->size = usize - (r->n[usize - 1] == 0);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions zz.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ void zz_addi(zz_ptr r, zz_srcptr a, sword_t c);
*/
void zz_subi(zz_ptr r, zz_srcptr a, sword_t c);

/*
Set r = a + c, where c is an unsigned word.
*/
void zz_addu(zz_ptr r, zz_srcptr a, word_t c);

/*
Set r = a - c, where c is an unsigned word.
*/
void zz_subu(zz_ptr r, zz_srcptr a, word_t c);

/*
Set r = a + b.
*/
Expand Down