+

Core内部直接引用的Base的实现, Base的实现在 src/int_math.ml 中:

+
1
2
3
4
5
6
7
8
let int_pow base exponent =
if exponent < 0 then negative_exponent ();
if abs base > 1
&& (exponent > 63
|| abs base > Pow_overflow_bounds.int_positive_overflow_bounds.(exponent))
then overflow ();
int_math_int_pow base exponent
;;
+ +

其中 int_math_int_pow() 由 C 实现:

+
1
external int_math_int_pow : int -> int -> int = "Base_int_math_int_pow_stub" [@@noalloc]
+ +

其实现在 src/int_math_stubs.c 中:

+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static int64_t int_pow(int64_t base, int64_t exponent) {
int64_t ret = 1;
int64_t mul[4];
mul[0] = 1;
mul[1] = base;
mul[3] = 1;

while (exponent != 0) {
mul[1] *= mul[3];
mul[2] = mul[1] * mul[1];
mul[3] = mul[2] * mul[1];
ret *= mul[exponent & 3];
exponent >>= 2;
}

return ret;
}
+ + +