Created
October 30, 2015 18:01
-
-
Save ArchRobison/0151b1b7374f5fe7ea3b to your computer and use it in GitHub Desktop.
Julia example susing Tuples or Immutable that vectorize if patch http://reviews.llvm.org/D14185 is applied.
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
import Base.rand | |
immutable Quad | |
a :: Float32 | |
b :: Float32 | |
c :: Float32 | |
d :: Float32 | |
end | |
@inline function add(x::Quad, y::Quad) | |
Quad(x.a+y.a, x.b+y.b, x.c+y.c, x.d+y.d) | |
end | |
@inline function mul(x::Quad, y::Quad) | |
Quad(x.a*y.a, x.b*y.b, x.c*y.c, x.d*y.d) | |
end | |
function muladd(x::Vector{Quad}, y::Vector{Quad}, z::Vector{Quad}) | |
@inbounds for i=1:length(x) | |
x[i] = add(x[i],mul(y[i],z[i])) | |
end | |
end | |
function rand(::Type{Quad},n::Integer) | |
Quad[Quad(rand(Float32),rand(Float32),rand(Float32),rand(Float32)) for i=1:n] | |
end | |
a = rand(Quad,1000) | |
b = rand(Quad,1000) | |
c = rand(Quad,1000) | |
function flog(a,b,c) | |
for i=1:1000 | |
muladd(a,b,c) | |
end | |
end | |
@time flog(a,b,c) | |
@time flog(a,b,c) | |
@time flog(a,b,c) |
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
import Base.rand | |
Quad = Tuple{Float32,Float32,Float32,Float32} | |
@inline function add(x::Quad, y::Quad) | |
(x[1]+y[1], x[2]+y[2], x[3]+y[3], x[4]+y[4]) | |
end | |
@inline function mul(x::Quad, y::Quad) | |
(x[1]*y[1], x[2]*y[2], x[3]*y[3], x[4]*y[4]) | |
end | |
function muladd(x::Vector{Quad}, y::Vector{Quad}, z::Vector{Quad}) | |
@inbounds for i=1:length(x) | |
x[i] = add(x[i],mul(y[i],z[i])) | |
end | |
end | |
function rand(::Type{Quad},n::Integer) | |
Quad[(rand(Float32),rand(Float32),rand(Float32),rand(Float32)) for i=1:n] | |
end | |
a = rand(Quad,1000) | |
b = rand(Quad,1000) | |
c = rand(Quad,1000) | |
function flog(a,b,c) | |
for i=1:1000 | |
muladd(a,b,c) | |
end | |
end | |
@time flog(a,b,c) | |
@time flog(a,b,c) | |
@time flog(a,b,c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment