Commit f21ea6ce184beea12d5b518ecdde1bded1ecf24a

Daniel Mendler 2019-10-23T20:06:08

add fast path to mp_add_d and mp_sub_d

diff --git a/mp_add_d.c b/mp_add_d.c
index 07242c6..3be2d23 100644
--- a/mp_add_d.c
+++ b/mp_add_d.c
@@ -10,6 +10,15 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
    int ix, oldused;
    mp_digit *tmpa, *tmpc;
 
+   /* fast path for a == c */
+   if (a == c &&
+       !MP_IS_ZERO(c) &&
+       c->sign == MP_ZPOS &&
+       c->dp[0] + b < MP_DIGIT_MAX) {
+      c->dp[0] += b;
+      return MP_OKAY;
+   }
+
    /* grow c as required */
    if (c->alloc < (a->used + 1)) {
       if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
diff --git a/mp_decr.c b/mp_decr.c
deleted file mode 100644
index 55516cf..0000000
--- a/mp_decr.c
+++ /dev/null
@@ -1,34 +0,0 @@
-#include "tommath_private.h"
-#ifdef MP_DECR_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* Decrement "a" by one like "a--". Changes input! */
-mp_err mp_decr(mp_int *a)
-{
-   if (MP_IS_ZERO(a)) {
-      mp_set(a,1uL);
-      a->sign = MP_NEG;
-      return MP_OKAY;
-   } else if (a->sign == MP_NEG) {
-      mp_err err;
-      a->sign = MP_ZPOS;
-      if ((err = mp_incr(a)) != MP_OKAY) {
-         return err;
-      }
-      /* There is no -0 in LTM */
-      if (!MP_IS_ZERO(a)) {
-         a->sign = MP_NEG;
-      }
-      return MP_OKAY;
-   } else if (a->dp[0] > 1uL) {
-      a->dp[0]--;
-      if (a->dp[0] == 0u) {
-         mp_zero(a);
-      }
-      return MP_OKAY;
-   } else {
-      return mp_sub_d(a, 1uL,a);
-   }
-}
-#endif
diff --git a/mp_incr.c b/mp_incr.c
deleted file mode 100644
index 12dc20b..0000000
--- a/mp_incr.c
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "tommath_private.h"
-#ifdef MP_INCR_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* Increment "a" by one like "a++". Changes input! */
-mp_err mp_incr(mp_int *a)
-{
-   if (MP_IS_ZERO(a)) {
-      mp_set(a,1uL);
-      return MP_OKAY;
-   } else if (a->sign == MP_NEG) {
-      mp_err err;
-      a->sign = MP_ZPOS;
-      if ((err = mp_decr(a)) != MP_OKAY) {
-         return err;
-      }
-      /* There is no -0 in LTM */
-      if (!MP_IS_ZERO(a)) {
-         a->sign = MP_NEG;
-      }
-      return MP_OKAY;
-   } else if (a->dp[0] < MP_DIGIT_MAX) {
-      a->dp[0]++;
-      return MP_OKAY;
-   } else {
-      return mp_add_d(a, 1uL,a);
-   }
-}
-#endif
diff --git a/mp_sub_d.c b/mp_sub_d.c
index 16c6165..a8a0d38 100644
--- a/mp_sub_d.c
+++ b/mp_sub_d.c
@@ -10,6 +10,15 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
    mp_err    err;
    int       ix, oldused;
 
+   /* fast path for a == c */
+   if (a == c &&
+       !MP_IS_ZERO(c) &&
+       c->sign == MP_ZPOS &&
+       c->dp[0] > b) {
+      c->dp[0] -= b;
+      return MP_OKAY;
+   }
+
    /* grow c as required */
    if (c->alloc < (a->used + 1)) {
       if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
diff --git a/tommath.h b/tommath.h
index b741674..f7711e1 100644
--- a/tommath.h
+++ b/tommath.h
@@ -377,10 +377,10 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
 mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* Increment "a" by one like "a++". Changes input! */
-mp_err mp_incr(mp_int *a) MP_WUR;
+#define mp_incr(a) mp_add_d((a), 1, (a))
 
 /* Decrement "a" by one like "a--". Changes input! */
-mp_err mp_decr(mp_int *a) MP_WUR;
+#define mp_decr(a) mp_sub_d((a), 1, (a))
 
 /* ---> single digit functions <--- */