add fast path to mp_add_d and mp_sub_d

This commit is contained in:
Daniel Mendler 2019-10-23 20:06:08 +02:00
parent 68c1594890
commit f21ea6ce18
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
5 changed files with 20 additions and 66 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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 <--- */