blob: 181d984244d70c5fde53118cf4e9028c80a3d982 (
plain)
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
|
#include "tommath_private.h"
#ifdef BN_S_MP_LOG_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
static mp_word s_pow(mp_word base, mp_word exponent)
{
mp_word result = 1u;
while (exponent != 0u) {
if ((exponent & 1u) == 1u) {
result *= base;
}
exponent >>= 1;
base *= base;
}
return result;
}
int s_mp_log_d(mp_digit base, mp_digit n)
{
mp_word bracket_low = 1uLL, bracket_high = base, N = n;
int ret, high = 1, low = 0;
if (n < base) {
return 0;
}
if (n == base) {
return 1;
}
while (bracket_high < N) {
low = high;
bracket_low = bracket_high;
high <<= 1;
bracket_high *= bracket_high;
}
while (((mp_digit)(high - low)) > 1uL) {
int mid = (low + high) >> 1;
mp_word bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low));
if (N < bracket_mid) {
high = mid ;
bracket_high = bracket_mid ;
}
if (N > bracket_mid) {
low = mid ;
bracket_low = bracket_mid ;
}
if (N == bracket_mid) {
return mid;
}
}
if (bracket_high == N) {
ret = high;
} else {
ret = low;
}
return ret;
}
#endif
|