Skip to content

Commit b51ffec

Browse files
committed
add s_mp_floor_ilog2()
instead of all those copies Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent d6cb87a commit b51ffec

File tree

5 files changed

+19
-32
lines changed

5 files changed

+19
-32
lines changed

mp_prime_is_prime.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

6-
/* portable integer log of two with small footprint */
7-
static unsigned int s_floor_ilog2(int value)
8-
{
9-
unsigned int r = 0;
10-
while ((value >>= 1) != 0) {
11-
r++;
12-
}
13-
return r;
14-
}
15-
166
mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
177
{
188
mp_int b;
@@ -186,7 +176,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
186176
* Hence the ugly type-fiddling in the following code.
187177
*/
188178
size_a = mp_count_bits(a);
189-
mask = (1u << s_floor_ilog2(size_a)) - 1u;
179+
mask = (1u << s_mp_floor_ilog2(size_a)) - 1u;
190180
/*
191181
Assuming the General Rieman hypothesis (never thought to write that in a
192182
comment) the upper bound can be lowered to 2*(log a)^2.

s_mp_faster_read_radix.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ static const uint8_t s_read_radix_cutoff[65] = { 0, 0,
1818
18, 17, 17, 17, 17, 17, 17 /* 58 .. 64 */
1919
};
2020

21-
/* This is in mp_prime_is_prime.c and can be reused */
22-
static int s_floor_ilog2(int value)
23-
{
24-
int r = 0;
25-
while ((value >>= 1) != 0) {
26-
r++;
27-
}
28-
return r;
29-
}
30-
3121
mp_err s_mp_faster_read_radix(mp_int *a, const char *str, size_t start, size_t end, int radix)
3222
{
3323
size_t len, mid;
@@ -54,7 +44,7 @@ mp_err s_mp_faster_read_radix(mp_int *a, const char *str, size_t start, size_t e
5444
if ((err = s_mp_slower_read_radix(&B, str, start + mid +1, end, radix)) != MP_OKAY) goto LTM_ERR;
5545

5646
if (MP_IS_2EXPT((unsigned int)radix)) {
57-
if ((err = mp_mul_2d(&A, (int)(((len - mid) - 1u) * (size_t)s_floor_ilog2(radix)), &A)) != MP_OKAY)goto LTM_ERR;
47+
if ((err = mp_mul_2d(&A, (int)(((len - mid) - 1u) * s_mp_floor_ilog2(radix)), &A)) != MP_OKAY)goto LTM_ERR;
5848
} else {
5949
if ((err = mp_expt_n(&m, (int)((len - mid) - 1u), &m)) != MP_OKAY) goto LTM_ERR;
6050
if ((err = mp_mul(&A, &m, &A)) != MP_OKAY) goto LTM_ERR;

s_mp_floor_ilog2.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "tommath_private.h"
2+
#ifdef S_MP_FLOOR_ILOG2_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
size_t s_mp_floor_ilog2(mp_word value)
7+
{
8+
size_t r = 0u;
9+
while ((value >>= 1) != 0u) {
10+
r++;
11+
}
12+
return r;
13+
}
14+
15+
#endif

s_mp_fp_log_d.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

6-
static mp_word s_mp_flog2_mp_word_d(mp_word value)
7-
{
8-
mp_word r = 0u;
9-
while ((value >>= 1) != 0u) {
10-
r++;
11-
}
12-
return r;
13-
}
14-
156
/* Fixed point bitwise logarithm base two of "x" with precision "p" */
167
static mp_err s_mp_fp_log_fraction_d(mp_word x, int p, mp_word *c)
178
{
189
mp_word b, L_out, L, a_bar, twoep;
1910
int i;
2011

21-
L = s_mp_flog2_mp_word_d(x);
12+
L = s_mp_floor_ilog2(x);
2213

2314
if ((L + (mp_word)p) > MP_UPPER_LIMIT_FIXED_LOG) {
2415
return MP_VAL;

tommath_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits);
223223
MP_PRIVATE void s_mp_zero_buf(void *mem, size_t size);
224224
MP_PRIVATE void s_mp_zero_digs(mp_digit *d, int digits);
225225
MP_PRIVATE mp_err s_mp_radix_size_overestimate(const mp_int *a, const int radix, size_t *size);
226+
MP_PRIVATE size_t s_mp_floor_ilog2(mp_word value);
226227

227228
#define MP_PRECISION_FIXED_LOG ( (int) (((sizeof(mp_word) * CHAR_BIT) / 2) - 1))
228229
#define MP_UPPER_LIMIT_FIXED_LOG ( (int) ( (sizeof(mp_word) * CHAR_BIT) - 1))

0 commit comments

Comments
 (0)