Skip to content

Commit f4e028f

Browse files
authored
Merge pull request #17 from JuliaRandom/fix-16
Avoid type piracy related to `__m128i`. (Fix #16)
2 parents 195d1ee + 8b79c1b commit f4e028f

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/aesni.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ copy(src::AESNIKey) = copyto!(AESNIKey(), src)
2828
"""
2929
Assistant function for AES128. Compiled from the C++ source code:
3030
```cpp
31-
R123_STATIC_INLINE __m128i AES_128_ASSIST (__m128i temp1, __m128i temp2) {
32-
__m128i temp3;
33-
temp2 = _mm_shuffle_epi32 (temp2 ,0xff);
31+
R123_STATIC_INLINE __m128i AES_128_ASSIST (__m128i temp1, __m128i temp2) {
32+
__m128i temp3;
33+
temp2 = _mm_shuffle_epi32 (temp2 ,0xff);
3434
temp3 = _mm_slli_si128 (temp1, 0x4);
3535
temp1 = _mm_xor_si128 (temp1, temp3);
3636
temp3 = _mm_slli_si128 (temp3, 0x4);
3737
temp1 = _mm_xor_si128 (temp1, temp3);
3838
temp3 = _mm_slli_si128 (temp3, 0x4);
3939
temp1 = _mm_xor_si128 (temp1, temp3);
40-
temp1 = _mm_xor_si128 (temp1, temp2);
41-
return temp1;
40+
temp1 = _mm_xor_si128 (temp1, temp2);
41+
return temp1;
4242
}
4343
```
4444
"""
@@ -58,9 +58,9 @@ _aes_128_assist(a::__m128i, b::__m128i) = llvmcall(
5858
%15 = xor <2 x i64> %12, %5
5959
%16 = xor <2 x i64> %15, %14
6060
ret <2 x i64> %16""",
61-
__m128i, Tuple{__m128i, __m128i},
62-
a, b
63-
)
61+
__m128i_lvec, Tuple{__m128i_lvec, __m128i_lvec},
62+
a.data, b.data
63+
) |> __m128i
6464

6565
function _aesni_expand!(k::AESNIKey, rkey::__m128i)
6666
k.key1 = rkey
@@ -230,7 +230,7 @@ end
230230
"""
231231
aesni(key::NTuple{11,UInt128}, ctr::Tuple{UInt128})::Tuple{UInt128}
232232
233-
Functional variant of [`AESNI1x`](@ref) and [`AESNI4x`](@ref).
233+
Functional variant of [`AESNI1x`](@ref) and [`AESNI4x`](@ref).
234234
This function if free of mutability and side effects.
235235
"""
236236
@inline function aesni(key::NTuple{11,UInt128}, ctr::Tuple{UInt128})::Tuple{UInt128}

src/aesni_common.jl

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import Base.(+)
44
using ..Random123: R123Generator1x, R123Generator4x
55
import ..Random123: random123_r, set_counter!
66

7-
const __m128i = NTuple{2, VecElement{UInt64}}
7+
const __m128i_lvec = NTuple{2, VecElement{UInt64}}
8+
struct __m128i
9+
data::__m128i_lvec
10+
end
811
Base.convert(::Type{__m128i}, x::UInt128) = unsafe_load(Ptr{__m128i}(pointer_from_objref(Ref(x))))
912
Base.convert(::Type{UInt128}, x::__m128i) = unsafe_load(Ptr{UInt128}(pointer_from_objref(Ref(x))))
1013
UInt128(x::__m128i) = convert(UInt128, x)
@@ -13,42 +16,42 @@ Base.convert(::Type{__m128i}, x::Union{Signed, Unsigned}) = convert(__m128i, UIn
1316
Base.convert(::Type{T}, x::__m128i) where T <: Union{Signed, Unsigned} = convert(T, UInt128(x))
1417

1518
const LITTLE_ENDIAN = ENDIAN_BOM 0x04030201
16-
__m128i(hi::UInt64, lo::UInt64) = LITTLE_ENDIAN ? (VecElement(lo), VecElement(hi)) : (VecElement(hi), VecElement(lo))
19+
__m128i(hi::UInt64, lo::UInt64) = LITTLE_ENDIAN ? __m128i((VecElement(lo), VecElement(hi))) : __m128i((VecElement(hi), VecElement(lo)))
1720

1821
Base.zero(::Type{__m128i}) = __m128i(zero(UInt64), zero(UInt64))
1922
Base.one(::Type{__m128i}) = __m128i(zero(UInt64), one(UInt64))
2023
Base.xor(a::__m128i, b::__m128i) = llvmcall(
2124
"""%3 = xor <2 x i64> %1, %0
2225
ret <2 x i64> %3""",
23-
__m128i, Tuple{__m128i, __m128i},
24-
a, b
25-
)
26+
__m128i_lvec, Tuple{__m128i_lvec, __m128i_lvec},
27+
a.data, b.data
28+
) |> __m128i
2629
(+)(a::__m128i, b::__m128i) = llvmcall(
2730
"""%3 = add <2 x i64> %1, %0
2831
ret <2 x i64> %3""",
29-
__m128i, Tuple{__m128i, __m128i},
30-
a, b
31-
)
32+
__m128i_lvec, Tuple{__m128i_lvec, __m128i_lvec},
33+
a.data, b.data
34+
) |> __m128i
3235
(+)(a::__m128i, b::Integer) = a + __m128i(UInt128(b))
3336

3437
_aes_enc(a::__m128i, round_key::__m128i) = ccall(
3538
"llvm.x86.aesni.aesenc",
3639
llvmcall,
37-
__m128i, (__m128i, __m128i),
38-
a, round_key
39-
)
40+
__m128i_lvec, (__m128i_lvec, __m128i_lvec),
41+
a.data, round_key.data
42+
) |> __m128i
4043
_aes_enc_last(a::__m128i, round_key::__m128i) = ccall(
4144
"llvm.x86.aesni.aesenclast",
4245
llvmcall,
43-
__m128i, (__m128i, __m128i),
44-
a, round_key
45-
)
46+
__m128i_lvec, (__m128i_lvec, __m128i_lvec),
47+
a.data, round_key.data
48+
) |> __m128i
4649
_aes_key_gen_assist(a::__m128i, ::Val{R}) where R = ccall(
4750
"llvm.x86.aesni.aeskeygenassist",
4851
llvmcall,
49-
__m128i, (__m128i, UInt8),
50-
a, R
51-
)
52+
__m128i_lvec, (__m128i_lvec, UInt8),
53+
a.data, R
54+
) |> __m128i
5255

5356
"Abstract RNG that generates one number at a time and is based on AESNI."
5457
abstract type AbstractAESNI1x <: R123Generator1x{UInt128} end

0 commit comments

Comments
 (0)