Skip to content

Commit 764d71c

Browse files
Isolate AES support for x86
1 parent ac03781 commit 764d71c

7 files changed

Lines changed: 41 additions & 34 deletions

File tree

src/Random123.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ include("philox.jl")
2929

3030
export R123_USE_AESNI
3131

32-
"True when AES-NI has been enabled."
33-
const R123_USE_AESNI = @static if Sys.isapple() && Sys.ARCH :aarch64
34-
false
35-
else
32+
"True when x86 AES-NI instructiona have been detected."
33+
const R123_USE_X86_AES_NI::Bool = @static if Sys.ARCH :x86_64 || Sys.ARCH :i686
3634
try
3735
cmd = Base.julia_cmd()
3836
push!(
@@ -47,16 +45,24 @@ else
4745
catch e
4846
false
4947
end
48+
else
49+
false
5050
end
5151

52+
"True when AES-acceleration instructions have been detected."
53+
const R123_USE_AESNI::Bool = R123_USE_X86_AES_NI
54+
5255
@static if R123_USE_AESNI
5356
export AESNI1x, AESNI4x, aesni
5457
export ARS1x, ARS4x, ars
55-
include("./aesni_common.jl")
56-
include("./aesni.jl")
57-
include("./ars.jl")
5858
else
59-
@warn "AES-NI instruction set is not enabled, so the related RNGs (AESNI and ARS) are not available."
59+
@warn "AES-acceleration instructions have not been detected, so the related RNGs (AESNI and ARS) are not available."
60+
end
61+
62+
@static if R123_USE_X86_AES_NI
63+
include("./x86/aesni_common.jl")
64+
include("./x86/aesni.jl")
65+
include("./x86/ars.jl")
6066
end
6167

6268
end
File renamed without changes.
File renamed without changes.

test/runtests.jl

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using Printf: @printf
1212
seed1 = 1
1313
seed2 = (1,2)
1414
seed4 = (1,2,3,4)
15-
for (rng, alg, options) in [
15+
alg_choices = [
1616
(Threefry2x(UInt32, seed2) , threefry, (Val(20),)) ,
1717
(Threefry2x(UInt64, seed2) , threefry, (Val(20),)) ,
1818
(Threefry4x(UInt32, seed4) , threefry, (Val(20),)) ,
@@ -21,11 +21,16 @@ using Printf: @printf
2121
(Philox2x(UInt64 , seed1) , philox , (Val(10),)) ,
2222
(Philox4x(UInt32 , seed2) , philox , (Val(10),)) ,
2323
(Philox4x(UInt64 , seed2) , philox , (Val(10),)) ,
24-
(AESNI1x(seed1) , aesni , () ) ,
25-
(AESNI4x(seed4) , aesni , () ) ,
26-
(ARS1x(seed1) , ars , (Val(7),) ) ,
27-
(ARS4x(seed4) , ars , (Val(7),) ) ,
2824
]
25+
if R123_USE_AESNI
26+
append!(alg_choices, [
27+
(AESNI1x(seed1) , aesni , () ) ,
28+
(AESNI4x(seed4) , aesni , () ) ,
29+
(ARS1x(seed1) , ars , (Val(7),) ) ,
30+
(ARS4x(seed4) , ars , (Val(7),) ) ,
31+
])
32+
end
33+
for (rng, alg, options) in alg_choices
2934
key = @inferred get_key(rng)
3035
ctr = @inferred get_ctr(rng)
3136
@test isbitstype(typeof(key))
@@ -89,17 +94,19 @@ end
8994
@test x9 === y9
9095
end
9196

92-
rng = ARS1x(1)
93-
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
94-
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
95-
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
96-
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
97-
98-
rng = AESNI1x(1)
99-
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
100-
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
101-
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
102-
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
97+
if R123_USE_AESNI
98+
rng = ARS1x(1)
99+
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
100+
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
101+
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
102+
@test (rand(rng, UInt128),) === ars(get_key(rng), get_ctr(rng), Val(7))
103+
104+
rng = AESNI1x(1)
105+
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
106+
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
107+
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
108+
@test (rand(rng, UInt128),) === aesni(get_key(rng), get_ctr(rng))
109+
end
103110

104111
end
105112

@@ -164,5 +171,7 @@ redirect_stdout(stdout_)
164171
compare_dirs("expected", "actual")
165172
cd(pwd_)
166173

167-
include("aesni.jl")
168-
include("ars.jl")
174+
if Random123.R123_USE_X86_AES_NI
175+
include("./x86/aesni.jl")
176+
include("./x86/ars.jl")
177+
end

test/aesni.jl renamed to test/x86/aesni.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
if R123_USE_AESNI
2-
31
import Random: seed!
42
using Test: @test
53

@@ -26,5 +24,3 @@ r1 = AESNI4x(split_uint(key, UInt32))
2624
set_counter!(r, 0)
2725
set_counter!(r1, 1)
2826
@test rand(r, Tuple{UInt128})[1] rand(r1, UInt128)
29-
30-
end

test/ars.jl renamed to test/x86/ars.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
if R123_USE_AESNI
2-
31
import Random: seed!
42
using Test: @test
53

@@ -29,5 +27,3 @@ r1 = ARS4x(split_uint(key, UInt32))
2927
set_counter!(r, 0)
3028
set_counter!(r1, 1)
3129
@test rand(r, Tuple{UInt128})[1] rand(r1, UInt128)
32-
33-
end

0 commit comments

Comments
 (0)