summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2007-10-12 18:12:28 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2007-10-12 18:12:28 (GMT)
commit32367802a06117aa8c30762ea4e24b449891b3d0 (patch)
treeeea0ac0c93cb2e4cf64bbf54e189ef1497e6b70e
parented6d3879f55e8ebf6d0f58c15d62befe848d84e5 (diff)
downloadpbn-32367802a06117aa8c30762ea4e24b449891b3d0.zip
pbn-32367802a06117aa8c30762ea4e24b449891b3d0.tar.gz
pbn-32367802a06117aa8c30762ea4e24b449891b3d0.tar.bz2
pbn-32367802a06117aa8c30762ea4e24b449891b3d0.tar.xz
Be more careful about the zero case
Handle the zero case better
-rw-r--r--pbn_add.c8
-rw-r--r--pbn_init.c5
-rw-r--r--pbn_mul.c8
3 files changed, 20 insertions, 1 deletions
diff --git a/pbn_add.c b/pbn_add.c
index 25372f0..f264b4d 100644
--- a/pbn_add.c
+++ b/pbn_add.c
@@ -25,6 +25,14 @@ struct pbn *pbn_addsub(struct pbn *s1, struct pbn *s2, int issub)
int s2minus = s2->minus ^ issub;
int dminus;
+ if (s1->bits == 0) {
+ pbn_free(s1);
+ return s2;
+ } else if (s2->bits == 0) {
+ pbn_free(s2);
+ return s1;
+ }
+
bits = s2->bits > s1->bits ? s2->bits+1 : s1->bits+1;
len = (bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
diff --git a/pbn_init.c b/pbn_init.c
index 53edb87..a6ce544 100644
--- a/pbn_init.c
+++ b/pbn_init.c
@@ -83,7 +83,10 @@ struct pbn *pbn_int(pbn_satom_t v)
if (!pbn)
return NULL;
- if (v < 0) {
+ if (v == 0) {
+ /* pbn->num[0] = pbn->bits = pbn->minus = 0; */
+ return pbn;
+ } else if (v < 0) {
v = -v;
pbn->minus = 1;
} else {
diff --git a/pbn_mul.c b/pbn_mul.c
index c7b13c9..a85a94f 100644
--- a/pbn_mul.c
+++ b/pbn_mul.c
@@ -27,6 +27,14 @@ struct pbn *pbn_mul(struct pbn *s1, struct pbn *s2)
pbn_atom_t x1;
pbn_2atom_t c;
+ if (s1->bits == 0) {
+ pbn_free(s2);
+ return s1; /* s1 == 0 */
+ } else if (s2->bits == 0) {
+ pbn_free(s1);
+ return s2; /* s2 == 0 */
+ }
+
bits = s1->bits + s2->bits;
len = (bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;