bigint.c: cosmetic changes (add spaces, etc.)
MODIFY
mrbgems/mruby-bigint/core/bigint.c
mrbgems/mruby-bigint/core/bigint.c
#define HIGH(x) ((x) >> DIG_SIZE)
#define LOW(x) ((x) & DIG_MASK)
-#define iabs(x) ((x>0) ? (x) : (-x))
-#define imax(x,y) ((x>y)?x:y)
-#define imin(x,y) ((x<y)?x:y)
+#define iabs(x) (((x)>0)?(x):(-x))
+#define imax(x,y) (((x)>(y))?(x):(y))
+#define imin(x,y) (((x)<(y))?(x):(y))
#define dg(x,i) (((size_t)i < (x)->sz)?(x)->p[i]:0)
static void
@@ -38,7 +38,7 @@ mpz_realloc(mrb_state *mrb, mpz_t *x, size_t size)
if (x->sz < size) {
x->p=(mp_limb*)mrb_realloc(mrb, x->p, size*sizeof(mp_limb));
for (size_t i=x->sz; i<size; i++)
- (x->p)[i] = 0;
+ x->p[i] = 0;
x->sz = size;
}
}
@@ -48,12 +48,12 @@ mpz_set(mrb_state *mrb, mpz_t *y, mpz_t *x)
{
size_t i, k = x->sz;
- mpz_realloc(mrb, y, (size_t)k);
+ mpz_realloc(mrb, y, k);
for (i=0;i < k; i++)
- (y->p)[i] = (x->p)[i];
+ y->p[i] = x->p[i];
for (;i<y->sz;i++)
- (y->p)[i] = 0;
+ y->p[i] = 0;
y->sn = x->sn;
}
@@ -117,7 +117,7 @@ digits(mpz_t *x)
size_t i;
if (x->sz == 0) return 0;
- for (i = (x->sz) - 1; (x->p)[i] == 0 ; i--)
+ for (i = x->sz - 1; x->p[i] == 0 ; i--)
if (i == 0) break;
return i+1;
}
@@ -140,7 +140,7 @@ uadd(mrb_state *mrb, mpz_t *z, mpz_t *x, mpz_t *y)
}
/* now y->sz >= x->sz */
- mpz_realloc(mrb, z, (size_t)((y->sz)+1));
+ mpz_realloc(mrb, z, y->sz+1);
mp_limb2 c = 0;
size_t i;
@@ -200,7 +200,7 @@ static int
uzero(mpz_t *x)
{
for (size_t i=0; i < x->sz; i++)
- if ((x->p)[i] != 0)
+ if (x->p[i] != 0)
return 0;
return 1;
}
@@ -224,6 +224,7 @@ mpz_add(mrb_state *mrb, mpz_t *zz, mpz_t *x, mpz_t *y)
{
int mg;
mpz_t z;
+
if (x->sn == 0) {
mpz_set(mrb, zz, y);
return;
@@ -266,6 +267,7 @@ static void
mpz_sub(mrb_state *mrb, mpz_t *z, mpz_t *x, mpz_t *y)
{
mpz_t u;
+
mpz_init(mrb, &u);
mpz_set(mrb, &u, y);
u.sn = -(u.sn);
@@ -278,6 +280,7 @@ static void
mpz_sub_int(mrb_state *mrb, mpz_t *x, mpz_t *y, mrb_int n)
{
mpz_t z;
+
mpz_init_set_int(mrb, &z, n);
mpz_sub(mrb, x, y, &z);
mpz_clear(mrb, &z);
@@ -295,7 +298,7 @@ mpz_mul(mrb_state *mrb, mpz_t *ww, mpz_t *u, mpz_t *v)
return;
}
mpz_init(mrb, &w);
- mpz_realloc(mrb, &w, (size_t)(u->sz + v->sz));
+ mpz_realloc(mrb, &w, u->sz + v->sz);
for (j=0; j < u->sz; j++) {
mp_limb2 cc = (mp_limb)0;
mp_limb u0 = u->p[j];
@@ -311,7 +314,7 @@ mpz_mul(mrb_state *mrb, mpz_t *ww, mpz_t *u, mpz_t *v)
w.p[i+j] = cc;
}
}
- w.sn = (u->sn) * (v->sn);
+ w.sn = u->sn * v->sn;
trim(&w);
mpz_set(mrb, ww, &w);
mpz_clear(mrb, &w);
@@ -343,9 +346,9 @@ lzb(mp_limb x)
return __builtin_clz(x);
#endif
- mp_limb i; int j=0;
+ int j=0;
- for (i = ((mp_limb)1 << (DIG_SIZE-1)); i && !(x&i) ; j++,i>>=1)
+ for (mp_limb i = ((mp_limb)1 << (DIG_SIZE-1)); i && !(x&i) ; j++,i>>=1)
;
return j;
}
@@ -356,13 +359,16 @@ static void
urshift(mrb_state *mrb, mpz_t *c1, mpz_t *a, size_t n)
{
mrb_assert(n < DIG_SIZE);
+
if (n == 0)
mpz_set(mrb, c1, a);
else {
mpz_t c;
mp_limb cc = 0;
mp_limb2 rm = (((mp_limb2)1<<n) - 1);
- mpz_init(mrb,&c); mpz_realloc(mrb,&c,(size_t)(a->sz));
+
+ mpz_init(mrb, &c);
+ mpz_realloc(mrb, &c, a->sz);
for (size_t i=a->sz-1;; i--) {
c.p[i] = ((a->p[i] >> n) | cc) & DIG_MASK;
cc = (a->p[i] & rm) << (DIG_SIZE - n);
@@ -386,7 +392,10 @@ ulshift(mrb_state *mrb, mpz_t *c1, mpz_t *a, size_t n)
else {
mpz_t c;
mp_limb rm = (((mp_limb2)1<<n) - 1) << (DIG_SIZE-n);
- mpz_init(mrb,&c); mpz_realloc(mrb,&c,(size_t)(a->sz + 1));
+
+ mpz_init(mrb, &c);
+ mpz_realloc(mrb, &c, a->sz+1);
+
size_t i;
for (i=0; i<a->sz; i++) {
c.p[i] = ((a->p[i] << n) | cc) & DIG_MASK;
@@ -400,6 +409,7 @@ ulshift(mrb_state *mrb, mpz_t *c1, mpz_t *a, size_t n)
}
/* internal routine to compute x/y and x%y ignoring signs */
+/* qq = xx/yy; rr = xx%yy */
static void
udiv(mrb_state *mrb, mpz_t *qq, mpz_t *rr, mpz_t *xx, mpz_t *yy)
{
@@ -419,8 +429,10 @@ udiv(mrb_state *mrb, mpz_t *qq, mpz_t *rr, mpz_t *xx, mpz_t *yy)
mpz_t q, x, y;
size_t i;
- if (uzero(yy)) return; /* divided by zero */
- mpz_init(mrb,&q); mpz_init(mrb,&x); mpz_init(mrb,&y);
+ mrb_assert(!uzero(yy)); /* divided by zero */
+ mpz_init(mrb, &q);
+ mpz_init(mrb, &x);
+ mpz_init(mrb, &y);
mpz_realloc(mrb, &x, xx->sz+1);
size_t yd = digits(yy);
size_t ns = lzb(yy->p[yd-1]);
@@ -456,7 +468,8 @@ udiv(mrb_state *mrb, mpz_t *qq, mpz_t *rr, mpz_t *xx, mpz_t *yy)
urshift(mrb, rr, &x, ns);
trim(&q);
mpz_set(mrb, qq, &q);
- mpz_clear(mrb,&x); mpz_clear(mrb,&y);
+ mpz_clear(mrb, &x);
+ mpz_clear(mrb, &y);
mpz_clear(mrb, &q);
}
@@ -465,6 +478,7 @@ mpz_mdiv(mrb_state *mrb, mpz_t *q, mpz_t *x, mpz_t *y)
{
mpz_t r;
short sn1 = x->sn, sn2 = y->sn, qsign;
+
if (uzero(x)) {
mpz_init_set_int(mrb, q, 0);
return;
@@ -485,6 +499,7 @@ mpz_mmod(mrb_state *mrb, mpz_t *r, mpz_t *x, mpz_t *y)
{
mpz_t q;
short sn1 = x->sn, sn2 = y->sn;
+
mpz_init(mrb, &q);
if (sn1 == 0) {
zero(r);
@@ -512,6 +527,7 @@ static void
mpz_mdivmod(mrb_state *mrb, mpz_t *q, mpz_t *r, mpz_t *x, mpz_t *y)
{
short sn1 = x->sn, sn2 = y->sn, qsign;
+
if (sn1 == 0) {
zero(q);
zero(r);
@@ -766,7 +782,7 @@ mpz_div_2exp(mrb_state *mrb, mpz_t *z, mpz_t *x, mrb_int e)
mpz_init(mrb, &y);
mpz_realloc(mrb, &y, x->sz-digs);
for (size_t i=0; i < x->sz-digs; i++)
- (y.p)[i] = (x->p)[i+digs];
+ y.p[i] = x->p[i+digs];
if (bs) {
urshift(mrb, z, &y, bs);
}
@@ -797,7 +813,7 @@ mpz_and(mrb_state *mrb, mpz_t *z, mpz_t *x, mpz_t *y) /* not the most efficient
mpz_realloc(mrb, z, sz);
for (size_t i=0; i < sz; i++)
- (z->p)[i] = x->p[i] & y->p[i];
+ z->p[i] = x->p[i] & y->p[i];
if (x->sn < 0 && y->sn < 0)
z->sn = (-1);
else
@@ -814,7 +830,7 @@ mpz_or(mrb_state *mrb, mpz_t *z, mpz_t *x, mpz_t *y) /* not the most efficient
mpz_realloc(mrb, z, sz);
for (i=0; i < sz; i++)
- (z->p)[i] = dg(x,i) | dg(y,i);
+ z->p[i] = dg(x,i) | dg(y,i);
if (x->sn < 0 || y->sn < 0)
z->sn = (-1);
else
@@ -831,7 +847,7 @@ mpz_xor(mrb_state *mrb, mpz_t *z, mpz_t *x, mpz_t *y) /* not the most efficient
size_t sz = imax(x->sz, y->sz);
mpz_realloc(mrb, z, sz);
for (i=0; i < sz; i++)
- (z->p)[i] = dg(x,i) ^ dg(y,i);
+ z->p[i] = dg(x,i) ^ dg(y,i);
if ((x->sn <= 0 && y->sn > 0) || (x->sn > 0 && y->sn <=0))
z->sn = (-1);
else

bigint.c: cosmetic changes (add spaces, etc.)