-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factored out duplicate code blocks from zadd/zsub
- Loading branch information
1 parent
1bba62e
commit cecedd0
Showing
7 changed files
with
128 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* hebimath - arbitrary precision arithmetic library | ||
* See LICENSE file for copyright and license details | ||
*/ | ||
|
||
#include "../../internal.h" | ||
|
||
static inline HEBI_ALWAYSINLINE | ||
void | ||
hebi_zaddsub__(hebi_zptr r, hebi_zsrcptr a, hebi_zsrcptr b, int as, int bs) | ||
{ | ||
hebi_packet *rp; | ||
uint64_t carry; | ||
size_t au; | ||
size_t bu; | ||
int c; | ||
|
||
au = a->hz_used; | ||
bu = b->hz_used; | ||
|
||
if ((au < bu) || ((au == bu) && (r == b))) { | ||
SWAP(hebi_zsrcptr, a, b); | ||
SWAP(size_t, au, bu); | ||
SWAP(int, as, bs); | ||
} | ||
|
||
if (SIGNXOR(as, bs) >= 0) { | ||
if (r == a) { | ||
rp = hebi_zgrowcopy__(r, au + 1); | ||
carry = hebi_padda(rp, b->hz_packs, au, bu); | ||
} else { | ||
rp = hebi_zgrowcopyif__(r, au + 1, r == b); | ||
carry = hebi_padd(rp, a->hz_packs, b->hz_packs, au, bu); | ||
} | ||
if (carry != 0) { | ||
hebi_psetu(rp + au, carry); | ||
au++; | ||
} | ||
} else { | ||
if (au == bu) { | ||
c = hebi_pcmp(a->hz_packs, b->hz_packs, au); | ||
if (UNLIKELY(!c)) { | ||
hebi_zsetzero(r); | ||
return; | ||
} | ||
if (c < 0) { | ||
SWAP(hebi_zsrcptr, a, b); | ||
as = -as; | ||
} | ||
} | ||
if (r == a) { | ||
rp = r->hz_packs; | ||
(void)hebi_psuba(rp, b->hz_packs, au, bu); | ||
} else { | ||
rp = hebi_zgrowcopyif__(r, au, r == b); | ||
(void)hebi_psub(rp, a->hz_packs, b->hz_packs, au, bu); | ||
} | ||
au = hebi_pnorm(rp, au); | ||
} | ||
|
||
r->hz_used = au; | ||
r->hz_sign = as; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* hebimath - arbitrary precision arithmetic library | ||
* See LICENSE file for copyright and license details | ||
*/ | ||
|
||
#include "../../internal.h" | ||
|
||
static inline HEBI_ALWAYSINLINE | ||
void | ||
hebi_zaddsubu__(hebi_zptr r, hebi_zsrcptr a, uint64_t b, int as, int bs) | ||
{ | ||
const hebi_packet *ap; | ||
hebi_packet *rp; | ||
uint64_t c; | ||
size_t u; | ||
|
||
u = a->hz_used; | ||
|
||
if (((as > 0) && (bs > 0)) || ((as < 0) && (bs < 0))) { | ||
rp = hebi_zgrowcopyif__(r, u + 1, r == a); | ||
ap = a->hz_packs; | ||
if ((c = hebi_paddu(rp, ap, b, u)) != 0) { | ||
hebi_psetu(rp + u, c); | ||
u++; | ||
} | ||
r->hz_used = u; | ||
r->hz_sign = as; | ||
return; | ||
} | ||
|
||
ap = a->hz_packs; | ||
|
||
if ((u > 1) || (ap->hp_limbs64[0] > b) || (ap->hp_limbs64[1] != 0)) { | ||
rp = r == a ? r->hz_packs : hebi_zgrow__(r, u); | ||
(void)hebi_psubu(rp, ap, b, u); | ||
u = hebi_pnorm(rp, u); | ||
r->hz_used = u; | ||
r->hz_sign = as; | ||
} else { | ||
c = b - ap->hp_limbs64[0]; | ||
if (LIKELY(c)) { | ||
rp = hebi_zgrow__(r, 1); | ||
hebi_psetu(rp, c); | ||
r->hz_used = 1; | ||
r->hz_sign = bs; | ||
} else { | ||
r->hz_sign = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters