3
3
# ##
4
4
# CRT
5
5
6
+ # Auxiliary functions allowing to avoid overflow
7
+ # on Windows for numbers between 32 and 64 bits
8
+ function my_set_ui! (a:: BigInt , b:: UInt64 )
9
+ @static if Culong == UInt64
10
+ Base. GMP. MPZ. set_ui! (a, b)
11
+ else
12
+ if b < 2 ^ 32
13
+ Base. GMP. MPZ. set_ui! (a, b)
14
+ else
15
+ Base. GMP. MPZ. set! (a, BigInt (b))
16
+ end
17
+ end
18
+ end
19
+
20
+ function my_mul_ui! (a:: BigInt , b:: BigInt , c:: UInt64 )
21
+ @static if Culong == UInt64
22
+ Base. GMP. MPZ. mul_ui! (a, b, c)
23
+ else
24
+ if c < 2 ^ 32
25
+ Base. GMP. MPZ. mul_ui! (a, b, c)
26
+ else
27
+ Base. GMP. MPZ. mul! (a, b, BigInt (c))
28
+ end
29
+ end
30
+ end
31
+
32
+ function my_mul_ui! (a:: BigInt , b:: UInt64 )
33
+ @static if Culong == UInt64
34
+ Base. GMP. MPZ. mul_ui! (a, b)
35
+ else
36
+ if b < 2 ^ 32
37
+ Base. GMP. MPZ. mul_ui! (a, b)
38
+ else
39
+ Base. GMP. MPZ. mul! (a, BigInt (b))
40
+ end
41
+ end
42
+ end
43
+
6
44
"""
7
45
crt!
8
46
@@ -23,9 +61,9 @@ Then, `x` is obtained as `x = ∑ ci[i] ai[i] mod M`.
23
61
function crt! (M:: BigInt , buf:: BigInt , n1:: BigInt , n2:: BigInt , ai:: Vector{UInt} , ci:: Vector{BigInt} )
24
62
@invariant length (ai) == length (ci)
25
63
26
- Base . GMP . MPZ . set_ui ! (n1, UInt (0 ))
64
+ my_set_ui ! (n1, UInt (0 ))
27
65
for i in 1 : length (ai)
28
- Base . GMP . MPZ . mul_ui ! (n2, ci[i], ai[i])
66
+ my_mul_ui ! (n2, ci[i], ai[i])
29
67
Base. GMP. MPZ. add! (n1, n2)
30
68
end
31
69
@@ -52,13 +90,13 @@ function crt_precompute!(
52
90
@invariant length (ci) == length (moduli)
53
91
54
92
n3, n4 = BigInt (), BigInt ()
55
- @inbounds Base . GMP . MPZ . set_ui ! (M, moduli[1 ])
93
+ @inbounds my_set_ui ! (M, moduli[1 ])
56
94
@inbounds for i in 2 : length (moduli)
57
- Base . GMP . MPZ . mul_ui ! (M, moduli[i])
95
+ my_mul_ui ! (M, moduli[i])
58
96
end
59
97
60
98
@inbounds for i in 1 : length (moduli)
61
- Base . GMP . MPZ . set_ui ! (n2, moduli[i])
99
+ my_set_ui ! (n2, moduli[i])
62
100
Base. GMP. MPZ. tdiv_q! (ci[i], M, n2)
63
101
Base. GMP. MPZ. gcdext! (n2, n3, n4, ci[i], n2)
64
102
Base. GMP. MPZ. mul! (ci[i], n3)
0 commit comments