add fast path to mp_add_d and mp_sub_d
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
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 <--- */