summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2012-03-13 05:01:59 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2012-03-13 05:01:59 (GMT)
commitfc3eb93a04d2b9d0ab7821b6bbf95e852d13c847 (patch)
tree7d0d73c134d9822418aa31731cd26b3a2dd42dea
parentd58300585648dc798ac0a6d1d55590ea9ce26cd3 (diff)
downloadpbn-fc3eb93a04d2b9d0ab7821b6bbf95e852d13c847.zip
pbn-fc3eb93a04d2b9d0ab7821b6bbf95e852d13c847.tar.gz
pbn-fc3eb93a04d2b9d0ab7821b6bbf95e852d13c847.tar.bz2
pbn-fc3eb93a04d2b9d0ab7821b6bbf95e852d13c847.tar.xz
Switch terminology from "atom" to the more standard "limb"
The "limb" terminology ("like a digit but larger") seems to have become standard enough that we might as well follow suit.
-rw-r--r--pbn.h26
-rw-r--r--pbn_add.c12
-rw-r--r--pbn_and.c2
-rw-r--r--pbn_bit.c16
-rw-r--r--pbn_cmp.c4
-rw-r--r--pbn_div.c56
-rw-r--r--pbn_divs.c10
-rw-r--r--pbn_dump.c2
-rw-r--r--pbn_init.c42
-rw-r--r--pbn_mul.c14
-rw-r--r--pbn_shift.c26
-rw-r--r--pbnint.h12
-rw-r--r--test.c4
13 files changed, 113 insertions, 113 deletions
diff --git a/pbn.h b/pbn.h
index f480c47..fc4fbfd 100644
--- a/pbn.h
+++ b/pbn.h
@@ -26,22 +26,22 @@
/* Configurables */
-#define PBN_ATOM_BITS 32
-#define PBN_ATOM_SHIFT 5
-#define PBN_ATOM_xFMT "08"PRIx32
-#define PBN_ATOM_dFMT "08"PRId32
-#define PBN_ATOM_uFMT "08"PRIu32
-typedef uint32_t pbn_atom_t;
-typedef uint64_t pbn_2atom_t;
-typedef int32_t pbn_satom_t;
-typedef int64_t pbn_2satom_t;
+#define PBN_LIMB_BITS 32
+#define PBN_LIMB_SHIFT 5
+#define PBN_LIMB_xFMT "08"PRIx32
+#define PBN_LIMB_dFMT "08"PRId32
+#define PBN_LIMB_uFMT "08"PRIu32
+typedef uint32_t pbn_limb_t;
+typedef uint64_t pbn_2limb_t;
+typedef int32_t pbn_slimb_t;
+typedef int64_t pbn_2slimb_t;
struct pbn {
int ref;
int len;
int bits;
int minus;
- pbn_atom_t num[1];
+ pbn_limb_t num[1];
};
@@ -67,7 +67,7 @@ int pbn_cmp(const struct pbn *, const struct pbn *);
int pbn_abscmp(const struct pbn *, const struct pbn *);
struct pbn *pbn_mul(struct pbn *, struct pbn *);
int pbn_div(struct pbn **, struct pbn **, struct pbn *, struct pbn *);
-int pbn_divs(struct pbn **, pbn_satom_t *, struct pbn *, pbn_satom_t);
+int pbn_divs(struct pbn **, pbn_slimb_t *, struct pbn *, pbn_slimb_t);
struct pbn *pbn_abs(struct pbn *);
struct pbn *pbn_shr(struct pbn *, int);
@@ -81,8 +81,8 @@ struct pbn *pbn_clr_bit(struct pbn *, int);
/* pbn_bit() does NOT consume a reference! */
int pbn_bit(const struct pbn *, int);
-struct pbn *pbn_int(pbn_satom_t);
-struct pbn *pbn_uint(pbn_atom_t);
+struct pbn *pbn_int(pbn_slimb_t);
+struct pbn *pbn_uint(pbn_limb_t);
/* Primarily for debugging; does NOT consume a reference */
void pbn_dump(FILE *, const struct pbn *);
diff --git a/pbn_add.c b/pbn_add.c
index 1f8c1f8..01e647f 100644
--- a/pbn_add.c
+++ b/pbn_add.c
@@ -34,7 +34,7 @@ struct pbn *pbn_addsub(struct pbn *s1, struct pbn *s2, int issub)
}
bits = s2->bits > s1->bits ? s2->bits+1 : s1->bits+1;
- len = (bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+ len = (bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
if (s1 == s2) {
s1 = pbn_dupx(s1, len);
@@ -42,7 +42,7 @@ struct pbn *pbn_addsub(struct pbn *s1, struct pbn *s2, int issub)
}
if (s1minus == s2minus) {
- pbn_2atom_t c;
+ pbn_2limb_t c;
/* Sign is the same, addition */
if (s1->ref > s2->ref) {
@@ -61,13 +61,13 @@ struct pbn *pbn_addsub(struct pbn *s1, struct pbn *s2, int issub)
c += s2->num[i];
d->num[i] = c;
- c >>= PBN_ATOM_BITS;
+ c >>= PBN_LIMB_BITS;
}
pbn_free(s2);
/* d->minus already copied from s1->minus */
} else {
- pbn_2satom_t c; /* Relies on 2's complement with
+ pbn_2slimb_t c; /* Relies on 2's complement with
proper right arithmetic shift... */
/* We know the sign is different */
dminus = 0;
@@ -99,7 +99,7 @@ struct pbn *pbn_addsub(struct pbn *s1, struct pbn *s2, int issub)
c += s1->num[i];
d->num[i] = c;
- c >>= PBN_ATOM_BITS;
+ c >>= PBN_LIMB_BITS;
}
pbn_free(s1);
} else {
@@ -112,7 +112,7 @@ struct pbn *pbn_addsub(struct pbn *s1, struct pbn *s2, int issub)
c -= s2->num[i];
d->num[i] = c;
- c >>= PBN_ATOM_BITS;
+ c >>= PBN_LIMB_BITS;
}
pbn_free(s2);
}
diff --git a/pbn_and.c b/pbn_and.c
index 795c05c..ec81ec5 100644
--- a/pbn_and.c
+++ b/pbn_and.c
@@ -40,7 +40,7 @@ struct pbn *pbn_and(struct pbn *s1, struct pbn *s2)
len = d->len;
if (s2->len < len) {
len = s2->len;
- memset(&d->num[len], 0, (d->len-len)*sizeof(pbn_atom_t));
+ memset(&d->num[len], 0, (d->len-len)*sizeof(pbn_limb_t));
}
for (i = 0; i < d->len; i++)
diff --git a/pbn_bit.c b/pbn_bit.c
index 51b0677..0ede6ea 100644
--- a/pbn_bit.c
+++ b/pbn_bit.c
@@ -15,15 +15,15 @@
struct pbn *pbn_set_bit(struct pbn *pbn, int bit)
{
int len;
- pbn_atom_t mask;
+ pbn_limb_t mask;
- len = (bit+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+ len = (bit+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
if (len < pbn->len)
len = pbn->len;
pbn = pbn_cow(pbn, len);
- mask = (pbn_atom_t)1 << (bit % PBN_ATOM_BITS);
- pbn->num[bit/PBN_ATOM_BITS] |= mask;
+ mask = (pbn_limb_t)1 << (bit % PBN_LIMB_BITS);
+ pbn->num[bit/PBN_LIMB_BITS] |= mask;
bit++;
pbn->bits = (bit > pbn->bits) ? bit : pbn->bits;
@@ -33,14 +33,14 @@ struct pbn *pbn_set_bit(struct pbn *pbn, int bit)
struct pbn *pbn_clr_bit(struct pbn *pbn, int bit)
{
- pbn_atom_t mask;
+ pbn_limb_t mask;
if (bit >= pbn->bits)
return pbn;
pbn = pbn_cow(pbn, pbn->len);
- mask = (pbn_atom_t)1 << (bit % PBN_ATOM_BITS);
- pbn->num[bit / PBN_ATOM_BITS] &= ~mask;
+ mask = (pbn_limb_t)1 << (bit % PBN_LIMB_BITS);
+ pbn->num[bit / PBN_LIMB_BITS] &= ~mask;
bit++;
if (bit == pbn->bits)
@@ -54,5 +54,5 @@ int pbn_bit(const struct pbn *pbn, int bit)
if (bit >= pbn->bits)
return 0;
- return (pbn->num[bit/PBN_ATOM_BITS] >> (bit % PBN_ATOM_BITS)) & 1;
+ return (pbn->num[bit/PBN_LIMB_BITS] >> (bit % PBN_LIMB_BITS)) & 1;
}
diff --git a/pbn_cmp.c b/pbn_cmp.c
index 45039b9..72dc5b8 100644
--- a/pbn_cmp.c
+++ b/pbn_cmp.c
@@ -30,7 +30,7 @@ int pbn_cmp(const struct pbn *s1, const struct pbn *s2)
else if (s1->bits > s2->bits)
return gt;
- i = (s1->bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+ i = (s1->bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
while (i >= 0) {
if (s1->num[i] != s2->num[i])
return (s1->num[i] < s2->num[i]) ? lt : gt;
@@ -50,7 +50,7 @@ int pbn_abscmp(const struct pbn *s1, const struct pbn *s2)
else if (s1->bits > s2->bits)
return 1;
- i = (s1->bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+ i = (s1->bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
while (i >= 0) {
if (s1->num[i] != s2->num[i])
return (s1->num[i] < s2->num[i]) ? -1 : 1;
diff --git a/pbn_div.c b/pbn_div.c
index 87665fc..88cb060 100644
--- a/pbn_div.c
+++ b/pbn_div.c
@@ -30,12 +30,12 @@
int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
{
struct pbn *q; /* Quotient accumulator */
- pbn_atom_t *un; /* Numerator/remainder array */
- const pbn_atom_t *vn; /* Denominator array */
- pbn_atom_t qhat; /* Estimated quotient digit */
- pbn_2atom_t rhat; /* Estimated remainder */
- pbn_2satom_t p, t, k; /* Multiply-subtract loop intermediates */
- int nl, dl; /* Length of n, d in atoms */
+ pbn_limb_t *un; /* Numerator/remainder array */
+ const pbn_limb_t *vn; /* Denominator array */
+ pbn_limb_t qhat; /* Estimated quotient digit */
+ pbn_2limb_t rhat; /* Estimated remainder */
+ pbn_2slimb_t p, t, k; /* Multiply-subtract loop intermediates */
+ int nl, dl; /* Length of n, d in limbs */
int s; /* Normalization shift */
int i, j;
int rv;
@@ -60,19 +60,19 @@ int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
}
/* Numerator and denominator length in limbs */
- nl = (n->bits + PBN_ATOM_BITS - 1) >> PBN_ATOM_SHIFT;
- dl = (d->bits + PBN_ATOM_BITS - 1) >> PBN_ATOM_SHIFT;
+ nl = (n->bits + PBN_LIMB_BITS - 1) >> PBN_LIMB_SHIFT;
+ dl = (d->bits + PBN_LIMB_BITS - 1) >> PBN_LIMB_SHIFT;
- q = pbn_new((((n->bits | (PBN_ATOM_BITS - 1)) -
- (d->bits | (PBN_ATOM_BITS - 1)))
- >> PBN_ATOM_SHIFT) + 1);
+ q = pbn_new((((n->bits | (PBN_LIMB_BITS - 1)) -
+ (d->bits | (PBN_LIMB_BITS - 1)))
+ >> PBN_LIMB_SHIFT) + 1);
q->minus = n->minus ^= d->minus; /* Set the sign */
if (dl == 1) {
/* Single limb denominator */
- pbn_atom_t c;
- const pbn_atom_t dn = d->num[0];
+ pbn_limb_t c;
+ const pbn_limb_t dn = d->num[0];
c = 0;
for (i = nl - 1; i >= 0; i--)
@@ -86,7 +86,7 @@ int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
}
} else {
/* Normalize denominator so the MSB of the MSW is on */
- s = -d->bits & (PBN_ATOM_BITS-1);
+ s = -d->bits & (PBN_LIMB_BITS-1);
d = pbn_shl(d, s);
n = pbn_cow(pbn_shl(n, s), nl+1);
@@ -94,10 +94,10 @@ int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
vn = d->num;
for (j = nl - dl; j >= 0; j--) {
- pbn_atom_t nx = un[j+dl];
- pbn_atom_t rhatx;
+ pbn_limb_t nx = un[j+dl];
+ pbn_limb_t rhatx;
- /* Compute initial estimate of the quotient atom */
+ /* Compute initial estimate of the quotient limb */
rhat = 0;
if (nx >= vn[dl-1]) {
@@ -112,9 +112,9 @@ int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
* Note: this loop can only be executed at most twice;
* consider unrolling
*/
- while (rhat < ((pbn_2atom_t)1 << PBN_ATOM_BITS) &&
- (pbn_2atom_t)qhat * vn[dl-2] >
- (rhat << PBN_ATOM_BITS) + un[j+dl-2]) {
+ while (rhat < ((pbn_2limb_t)1 << PBN_LIMB_BITS) &&
+ (pbn_2limb_t)qhat * vn[dl-2] >
+ (rhat << PBN_LIMB_BITS) + un[j+dl-2]) {
qhat--;
rhat += vn[dl-1];
}
@@ -122,13 +122,13 @@ int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
/* Multiply-subtract loop */
k = 0;
for (i = 0; i < dl; i++) {
- p = (pbn_2atom_t)qhat * vn[i];
- t = un[i+j] - k - (pbn_atom_t)p;
- un[i+j] = (pbn_atom_t)t;
- k = (p >> PBN_ATOM_BITS) - (t >> PBN_ATOM_BITS);
+ p = (pbn_2limb_t)qhat * vn[i];
+ t = un[i+j] - k - (pbn_limb_t)p;
+ un[i+j] = (pbn_limb_t)t;
+ k = (p >> PBN_LIMB_BITS) - (t >> PBN_LIMB_BITS);
}
t = un[j+dl] - k;
- un[j+dl] = (pbn_atom_t)t;
+ un[j+dl] = (pbn_limb_t)t;
/* Correct in case of oversubtraction */
if (unlikely(t < 0)) {
@@ -136,10 +136,10 @@ int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
k = 0;
for (i = 0; i < dl; i++) {
t = k + un[i+j] + vn[i];
- un[i+j] = (pbn_atom_t)t;
- k = t >> PBN_ATOM_BITS;
+ un[i+j] = (pbn_limb_t)t;
+ k = t >> PBN_LIMB_BITS;
}
- un[j+dl] += (pbn_atom_t)k;
+ un[j+dl] += (pbn_limb_t)k;
}
q->num[j] = qhat;
diff --git a/pbn_divs.c b/pbn_divs.c
index aadad1d..26cc730 100644
--- a/pbn_divs.c
+++ b/pbn_divs.c
@@ -15,17 +15,17 @@
*
* "Small division"
*
- * Divide a large number with a small (single-atom) number
+ * Divide a large number with a small (single-limb) number
*/
#include "pbnint.h"
-int pbn_divs(struct pbn **qp, pbn_satom_t *rp, struct pbn *n, pbn_satom_t d)
+int pbn_divs(struct pbn **qp, pbn_slimb_t *rp, struct pbn *n, pbn_slimb_t d)
{
int i;
int len;
struct pbn *q;
- pbn_atom_t c;
+ pbn_limb_t c;
int minus;
if (d == 0) {
@@ -39,7 +39,7 @@ int pbn_divs(struct pbn **qp, pbn_satom_t *rp, struct pbn *n, pbn_satom_t d)
return -1; /* Division by zero */
}
- len = (n->bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+ len = (n->bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
q = pbn_new(len);
minus = n->minus ^ (d < 0);
@@ -55,7 +55,7 @@ int pbn_divs(struct pbn **qp, pbn_satom_t *rp, struct pbn *n, pbn_satom_t d)
pbn_free(n);
if (rp)
- *rp = minus ? -(pbn_satom_t)c : (pbn_satom_t)c;
+ *rp = minus ? -(pbn_slimb_t)c : (pbn_slimb_t)c;
if (qp)
*qp = pbn_adjust_bits(q);
diff --git a/pbn_dump.c b/pbn_dump.c
index da2eb7d..c53adff 100644
--- a/pbn_dump.c
+++ b/pbn_dump.c
@@ -26,7 +26,7 @@ void pbn_dump(FILE *f, const struct pbn *pbn)
putc(pbn->minus ? '-' : '+', f);
for (i = pbn->len-1; i >= 0; i--) {
- fprintf(f, "%" PBN_ATOM_xFMT, pbn->num[i]);
+ fprintf(f, "%" PBN_LIMB_xFMT, pbn->num[i]);
if (i)
putc('_', f);
}
diff --git a/pbn_init.c b/pbn_init.c
index b34f218..07d490b 100644
--- a/pbn_init.c
+++ b/pbn_init.c
@@ -23,7 +23,7 @@ struct pbn *pbn_new(int len)
{
struct pbn *pbn;
- pbn = _pbn_zalloc(sizeof(struct pbn)+(len-1)*sizeof(pbn_atom_t));
+ pbn = _pbn_zalloc(sizeof(struct pbn)+(len-1)*sizeof(pbn_limb_t));
if (!pbn)
return NULL;
@@ -33,25 +33,25 @@ struct pbn *pbn_new(int len)
return pbn;
}
-#if PBN_ATOM_BITS != 32 && PBN_ATOM_BITS != 64
-# error "Fix pbn_ilog2p1 for new PBN_ATOM_BITS size"
+#if PBN_LIMB_BITS != 32 && PBN_LIMB_BITS != 64
+# error "Fix pbn_ilog2p1 for new PBN_LIMB_BITS size"
#endif
-static int pbn_ilog2p1(pbn_atom_t v)
+static int pbn_ilog2p1(pbn_limb_t v)
{
int p = 1;
#if defined(__GNUC__) && __GNUC__ >= 4
- if (sizeof(pbn_atom_t) == sizeof(int))
- return (__builtin_clz(v) ^ (PBN_ATOM_BITS-1)) + 1;
- if (sizeof(pbn_atom_t) == sizeof(long))
- return (__builtin_clzl(v) ^ (PBN_ATOM_BITS-1)) + 1;
- if (sizeof(pbn_atom_t) == sizeof(long long))
- return (__builtin_clzll(v) ^ (PBN_ATOM_BITS-1)) + 1;
+ if (sizeof(pbn_limb_t) == sizeof(int))
+ return (__builtin_clz(v) ^ (PBN_LIMB_BITS-1)) + 1;
+ if (sizeof(pbn_limb_t) == sizeof(long))
+ return (__builtin_clzl(v) ^ (PBN_LIMB_BITS-1)) + 1;
+ if (sizeof(pbn_limb_t) == sizeof(long long))
+ return (__builtin_clzll(v) ^ (PBN_LIMB_BITS-1)) + 1;
/* else fall through, wtf... */
#endif
-#if PBN_ATOM_BITS >= 64
+#if PBN_LIMB_BITS >= 64
if (v & 0xffffffff00000000) {
p += 32;
v >>= 32;
@@ -82,7 +82,7 @@ static int pbn_ilog2p1(pbn_atom_t v)
return p;
}
-struct pbn *pbn_int(pbn_satom_t v)
+struct pbn *pbn_int(pbn_slimb_t v)
{
struct pbn *pbn = pbn_new(1);
if (!pbn)
@@ -103,7 +103,7 @@ struct pbn *pbn_int(pbn_satom_t v)
return pbn;
}
-struct pbn *pbn_uint(pbn_atom_t v)
+struct pbn *pbn_uint(pbn_limb_t v)
{
struct pbn *pbn = pbn_new(1);
if (!pbn)
@@ -117,7 +117,7 @@ struct pbn *pbn_uint(pbn_atom_t v)
/* Produce a duplicate */
struct pbn *pbn_dup(const struct pbn *src)
{
- int bytes = sizeof(struct pbn)+(src->len-1)*sizeof(pbn_atom_t);
+ int bytes = sizeof(struct pbn)+(src->len-1)*sizeof(pbn_limb_t);
struct pbn *dst = _pbn_malloc(bytes);
memcpy(dst, src, bytes);
@@ -130,15 +130,15 @@ struct pbn *pbn_dup(const struct pbn *src)
struct pbn *pbn_dupx(const struct pbn *src, int len)
{
int slack = src->len >= len ? 0 : len-src->len;
- int bytes = sizeof(struct pbn)+(src->len-1)*sizeof(pbn_atom_t);
- struct pbn *dst = _pbn_malloc(bytes+slack*sizeof(pbn_atom_t));
+ int bytes = sizeof(struct pbn)+(src->len-1)*sizeof(pbn_limb_t);
+ struct pbn *dst = _pbn_malloc(bytes+slack*sizeof(pbn_limb_t));
if (!dst)
return NULL;
memcpy(dst, src, bytes);
dst->ref = 1;
- memset(&dst->num[slack], 0, slack*sizeof(pbn_atom_t));
+ memset(&dst->num[slack], 0, slack*sizeof(pbn_limb_t));
return dst;
}
@@ -152,10 +152,10 @@ struct pbn *pbn_cow(struct pbn *src, int len)
src->ref = 1;
if (len > olen) {
src = _pbn_realloc(src, sizeof(struct pbn)+
- (len-1)*sizeof(pbn_atom_t));
+ (len-1)*sizeof(pbn_limb_t));
src->len = len;
}
- memset(&src->num[olen], 0, (len-olen)*sizeof(pbn_atom_t));
+ memset(&src->num[olen], 0, (len-olen)*sizeof(pbn_limb_t));
return src;
}
@@ -169,12 +169,12 @@ struct pbn *pbn_cow(struct pbn *src, int len)
struct pbn *pbn_adjust_bits(struct pbn *pbn)
{
int i;
- pbn_atom_t v;
+ pbn_limb_t v;
for (i = pbn->len-1; i >= 0; i--) {
v = pbn->num[i];
if (v) {
- pbn->bits = i*PBN_ATOM_BITS + pbn_ilog2p1(v);
+ pbn->bits = i*PBN_LIMB_BITS + pbn_ilog2p1(v);
return pbn;
}
}
diff --git a/pbn_mul.c b/pbn_mul.c
index 2ec50d3..6b16bd2 100644
--- a/pbn_mul.c
+++ b/pbn_mul.c
@@ -24,8 +24,8 @@ struct pbn *pbn_mul(struct pbn *s1, struct pbn *s2)
size_t bits, len;
struct pbn *d;
int i, j;
- pbn_atom_t x1;
- pbn_2atom_t c;
+ pbn_limb_t x1;
+ pbn_2limb_t c;
if (s1->bits == 0) {
pbn_free(s2);
@@ -36,7 +36,7 @@ struct pbn *pbn_mul(struct pbn *s1, struct pbn *s2)
}
bits = s1->bits + s2->bits;
- len = (bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+ len = (bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
d = pbn_new(len);
d->bits = bits;
@@ -45,12 +45,12 @@ struct pbn *pbn_mul(struct pbn *s1, struct pbn *s2)
x1 = s1->num[i];
c = 0;
for (j = 0; j < s2->len; j++) {
- c += (pbn_2atom_t)x1 * s2->num[j];
- d->num[i+j] += (pbn_atom_t)c;
- c >>= PBN_ATOM_BITS;
+ c += (pbn_2limb_t)x1 * s2->num[j];
+ d->num[i+j] += (pbn_limb_t)c;
+ c >>= PBN_LIMB_BITS;
}
if (c)
- d->num[i+j] += (pbn_atom_t)c;
+ d->num[i+j] += (pbn_limb_t)c;
}
d->minus = s1->minus ^ s2->minus;
diff --git a/pbn_shift.c b/pbn_shift.c
index 3a631bd..7712f84 100644
--- a/pbn_shift.c
+++ b/pbn_shift.c
@@ -20,7 +20,7 @@
struct pbn *pbn_shl(struct pbn *src, int shift)
{
int bits, len;
- pbn_atom_t b, c;
+ pbn_limb_t b, c;
int i;
int sw, sb;
struct pbn *dst;
@@ -32,30 +32,30 @@ struct pbn *pbn_shl(struct pbn *src, int shift)
return pbn_shr(src, -shift);
bits = src->bits + shift;
- len = (bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+ len = (bits+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
dst = pbn_cow(src, len);
dst->bits = bits; /* This is guaranteed accurate */
- sw = shift / PBN_ATOM_BITS;
- sb = shift % PBN_ATOM_BITS;
+ sw = shift / PBN_LIMB_BITS;
+ sb = shift % PBN_LIMB_BITS;
if (sb == 0) {
/* Particularly simple case... */
- memmove(&dst->num[sw], &dst->num[0], (len-sw)*sizeof(pbn_atom_t));
+ memmove(&dst->num[sw], &dst->num[0], (len-sw)*sizeof(pbn_limb_t));
} else {
b = dst->num[len-sw-1];
for (i = len-sw-1; i > 0; i--) {
/* b == dst->num[i] */
c = dst->num[i-1];
- dst->num[i+sw] = (b << sb) | (c >> (PBN_ATOM_BITS-sb));
+ dst->num[i+sw] = (b << sb) | (c >> (PBN_LIMB_BITS-sb));
b = c;
}
dst->num[sw] = b << sb;
}
if (sw)
- memset(&dst->num[0], 0, sw*sizeof(pbn_atom_t));
+ memset(&dst->num[0], 0, sw*sizeof(pbn_limb_t));
return dst;
}
@@ -63,7 +63,7 @@ struct pbn *pbn_shl(struct pbn *src, int shift)
struct pbn *pbn_shr(struct pbn *src, int shift)
{
int len;
- pbn_atom_t b, c;
+ pbn_limb_t b, c;
int i;
int sw, sb;
struct pbn *dst;
@@ -83,18 +83,18 @@ struct pbn *pbn_shr(struct pbn *src, int shift)
} else {
dst->bits -= shift;
- sw = shift / PBN_ATOM_BITS;
- sb = shift % PBN_ATOM_BITS;
+ sw = shift / PBN_LIMB_BITS;
+ sb = shift % PBN_LIMB_BITS;
if (sb == 0) {
/* Particularly simple case... */
- memmove(&dst->num[0], &dst->num[sw], (len-sw)*sizeof(pbn_atom_t));
+ memmove(&dst->num[0], &dst->num[sw], (len-sw)*sizeof(pbn_limb_t));
} else {
c = dst->num[sw];
for (i = sw; i < len-1; i++) {
/* c == dst->num[i] */
b = dst->num[i+1];
- dst->num[i-sw] = (c >> sb) | (b << (PBN_ATOM_BITS-sb));
+ dst->num[i-sw] = (c >> sb) | (b << (PBN_LIMB_BITS-sb));
c = b;
}
dst->num[len-sw-1] = c >> sb;
@@ -102,7 +102,7 @@ struct pbn *pbn_shr(struct pbn *src, int shift)
}
if (sw)
- memset(&dst->num[len-sw], 0, sw*sizeof(pbn_atom_t));
+ memset(&dst->num[len-sw], 0, sw*sizeof(pbn_limb_t));
return dst;
}
diff --git a/pbnint.h b/pbnint.h
index 2cdbee6..b80c96c 100644
--- a/pbnint.h
+++ b/pbnint.h
@@ -20,7 +20,7 @@
#include "pbn.h"
/* Macro to do a 2A/A -> A division/remainder operation */
-#if defined(__GNUC__) && PBN_ATOM_BITS == 32 && \
+#if defined(__GNUC__) && PBN_LIMB_BITS == 32 && \
(defined(__i386__) || defined(__x86_64__))
#define DIVIDE(hn,ln,d,q,r) \
asm("divl %4" \
@@ -33,11 +33,11 @@
*/
#define DIVIDE(hn,ln,d,q,r) \
do { \
- pbn_2atom_t _n; \
- pbn_atom_t _d = (d); \
- _n= ((pbn_2atom_t)(hn) << PBN_ATOM_BITS) + (pbn_atom_t)(ln); \
- (q) = (pbn_atom_t)(_n/_d); \
- (r) = (pbn_atom_t)(_n%_d); \
+ pbn_2limb_t _n; \
+ pbn_limb_t _d = (d); \
+ _n= ((pbn_2limb_t)(hn) << PBN_LIMB_BITS) + (pbn_limb_t)(ln); \
+ (q) = (pbn_limb_t)(_n/_d); \
+ (r) = (pbn_limb_t)(_n%_d); \
} while(0)
#endif
diff --git a/test.c b/test.c
index 538122d..2a2934a 100644
--- a/test.c
+++ b/test.c
@@ -96,7 +96,7 @@ static void divide_test(void)
static void small_divide_test(void)
{
struct pbn *n, *q;
- pbn_satom_t r;
+ pbn_slimb_t r;
int rv;
printf("\n*** Small divide test ***\n\n");
@@ -110,7 +110,7 @@ static void small_divide_test(void)
rv = pbn_divs(&q, &r, n, 3);
pbn_dump(stdout, q);
- printf(" : %"PBN_ATOM_xFMT"\n", r);
+ printf(" : %"PBN_LIMB_xFMT"\n", r);
}
int main(int argc, char *argv[])