2019-10-19 17:25:55 +00:00
|
|
|
#include "tommath_private.h"
|
|
|
|
#ifdef 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)
|
|
|
|
{
|
2019-11-09 19:23:03 +00:00
|
|
|
mp_word result = 1u;
|
2019-10-19 17:25:55 +00:00
|
|
|
while (exponent != 0u) {
|
|
|
|
if ((exponent & 1u) == 1u) {
|
|
|
|
result *= base;
|
|
|
|
}
|
|
|
|
exponent >>= 1;
|
|
|
|
base *= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:52:29 +00:00
|
|
|
int s_mp_log_d(mp_digit base, mp_digit n)
|
2019-10-19 17:25:55 +00:00
|
|
|
{
|
2019-10-29 19:52:29 +00:00
|
|
|
mp_word bracket_low = 1uLL, bracket_high = base, N = n;
|
|
|
|
int ret, high = 1, low = 0;
|
2019-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
if (n < base) {
|
2019-10-29 19:52:29 +00:00
|
|
|
return 0;
|
2019-10-19 17:25:55 +00:00
|
|
|
}
|
|
|
|
if (n == base) {
|
2019-10-29 19:52:29 +00:00
|
|
|
return 1;
|
2019-10-19 17:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (bracket_high < N) {
|
|
|
|
low = high;
|
|
|
|
bracket_low = bracket_high;
|
|
|
|
high <<= 1;
|
|
|
|
bracket_high *= bracket_high;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (((mp_digit)(high - low)) > 1uL) {
|
2019-10-29 19:52:29 +00:00
|
|
|
int mid = (low + high) >> 1;
|
|
|
|
mp_word bracket_mid = bracket_low * s_pow(base, (mp_word)(mid - low));
|
2019-10-19 17:25:55 +00:00
|
|
|
|
|
|
|
if (N < bracket_mid) {
|
|
|
|
high = mid ;
|
|
|
|
bracket_high = bracket_mid ;
|
|
|
|
}
|
|
|
|
if (N > bracket_mid) {
|
|
|
|
low = mid ;
|
|
|
|
bracket_low = bracket_mid ;
|
|
|
|
}
|
|
|
|
if (N == bracket_mid) {
|
2019-10-29 19:52:29 +00:00
|
|
|
return mid;
|
2019-10-19 17:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bracket_high == N) {
|
|
|
|
ret = high;
|
|
|
|
} else {
|
|
|
|
ret = low;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|