Merge branch 'fixes'
[u/mdw/catacomb] / mp-arith.c
index 4bd0976..9cb5178 100644 (file)
@@ -229,9 +229,12 @@ int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); }
 
 int mp_cmp(const mp *a, const mp *b)
 {
-  if (!((a->f ^ b->f) & MP_NEG))
-    return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
-  else if (a->f & MP_NEG)
+  if (!((a->f ^ b->f) & MP_NEG)) {
+    if (a->f & MP_NEG)
+      return (-mpx_ucmp(a->v, a->vl, b->v, b->vl));
+    else
+      return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
+  } else if (a->f & MP_NEG)
     return (-1);
   else
     return (+1);
@@ -357,7 +360,7 @@ mp *mp_bit##n##2c(mp *d, mp *a, mp *b)                                      \
     p_bn                                                               \
   } else {                             /* Both negative */             \
     mp *t = mp_not2c(MP_NEW, a);                                       \
-    mp *d = mp_not2c(d, b);                                            \
+    d = mp_not2c(d, b);                                                        \
     d = mp_bit##abn(d, t, d);                                          \
     MP_DROP(t);                                                                \
     p_abn                                                              \
@@ -649,16 +652,16 @@ mp *mp_odd(mp *d, mp *m, size_t *s)
     ss = 0;
   else {
     mpw x = *v;
-    mpw mask = MPW_MAX;
-    unsigned z = MPW_BITS / 2;
+    unsigned z = MPW_P2;
+    mpw mask = ((mpw)1 << z) - 1;
 
     while (z) {
-      mask >>= z;
       if (!(x & mask)) {
        x >>= z;
        ss += z;
       }
       z >>= 1;
+      mask >>= z;
     }
   }
 
@@ -723,6 +726,7 @@ RIG(lsr2c, mp_lsr2c)
 RIG(add, mp_add)
 RIG(sub, mp_sub)
 RIG(mul, mp_mul)
+RIG(exp, mp_exp)
 
 #undef RIG
 
@@ -908,6 +912,7 @@ static test_chunk tests[] = {
   { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } },
   { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
   { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
+  { "exp", texp, { &type_mp, &type_mp, &type_mp, 0 } },
   { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } },
   { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
   { "neg", tneg, { &type_mp, &type_mp, 0 } },