progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / math / f-niceprime.c
index 342cb86..f31752d 100644 (file)
@@ -52,20 +52,21 @@ static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
 
 static mp *fneg(field *ff, mp *d, mp *x) {
   fctx_niceprime *f = (fctx_niceprime *)ff;
-  return (mp_sub(d, f->r.p, x));
+  if (MP_ZEROP(x)) { if (d != x) mp_drop(d); return (MP_COPY(x)); }
+  else return (mp_sub(d, f->r.p, x));
 }
 
 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
   fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_add(d, x, y);
   if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
-  else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
+  else if (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
   return (d);
 }
 
 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
   fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_sub(d, x, y);
   if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
-  else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
+  else if (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
   return (d);
 }