Created
November 5, 2014 20:41
-
-
Save toivoh/19702e5ccf568304a807 to your computer and use it in GitHub Desktop.
Getting SIMD load/store instructions through llvmcall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TestSIMD5 | |
typealias Uint64x2 NTuple{2, Uint64} | |
function ($)(x::Uint64x2, y::Uint64x2) | |
Base.llvmcall("""%3 = xor <2 x i64> %1, %0 | |
ret <2 x i64> %3""", | |
Uint64x2, (Uint64x2, Uint64x2), x, y) | |
end | |
unsafe_aligned_load(p::Ptr{Uint64}, i::Int) = unsafe_aligned_load(ptradd(p, (i-1)*sizeof(Uint64)*2)) | |
function unsafe_aligned_load_2x(p::Ptr{Uint64}) | |
Base.llvmcall("""%2 = bitcast i64* %0 to <2 x i64>* | |
%3 = load <2 x i64>* %2, align 16 | |
ret <2 x i64> %3""", | |
Uint64x2, (Ptr{Uint64},), p) | |
end | |
unsafe_aligned_store!(p::Ptr{Uint64}, x::Uint64x2, i::Int) = unsafe_aligned_store!(ptradd(p, (i-1)*sizeof(Uint64)*2), x) | |
function unsafe_aligned_store_2x!(p::Ptr{Uint64}, x::Uint64x2) | |
Base.llvmcall("""%3 = bitcast i64* %0 to <2 x i64>* | |
store <2 x i64> %1, <2 x i64>* %3, align 16 | |
ret void""", | |
Void, (Ptr{Uint64}, Uint64x2), p, x) | |
end | |
function rmw!{T}(dest::Ptr{T}, src::Ptr{T}) | |
s = unsafe_aligned_load_2x(src) | |
d = unsafe_aligned_load_2x(dest) | |
d $= s | |
unsafe_aligned_store_2x!(dest, d) | |
end | |
code_native(rmw!, (Ptr{Uint64}, Ptr{Uint64})) | |
A, B = Uint64[1,2], Uint64[0x30,0x40] | |
rmw!(pointer(A), pointer(B)) | |
@show A | |
end # module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get the output
so it seems to be working!