Skip to content

Instantly share code, notes, and snippets.

@toivoh
Created November 5, 2014 20:41
Show Gist options
  • Save toivoh/19702e5ccf568304a807 to your computer and use it in GitHub Desktop.
Save toivoh/19702e5ccf568304a807 to your computer and use it in GitHub Desktop.
Getting SIMD load/store instructions through llvmcall
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
@toivoh
Copy link
Author

toivoh commented Nov 5, 2014

I get the output

Source line: 30
    push    RBP
    mov RBP, RSP
    movaps  XMM0, XMMWORD PTR [RSI]
    xorps   XMM0, XMMWORD PTR [RDI]
    movaps  XMMWORD PTR [RDI], XMM0
Source line: 30
    pop RBP
    ret
A = Uint64[0x0000000000000031,0x0000000000000042]

so it seems to be working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment