Skip to content

Instantly share code, notes, and snippets.

@toivoh
toivoh / bigfunction_parts.jl
Created November 2, 2016 07:38
Julia JIT compiler scaling benchmark
function code_big_function(n::Int)
code = [:( $(Symbol("x$k")) = $(Symbol("x$(k-1)")) + 1 ) for k=1:n]
quote
let
function f(x0)
$(code...)
return $(Symbol("x$n"))
end
end
end
@toivoh
toivoh / TestSLP.jl
Last active August 29, 2015 14:08
Trying out the SLP vectorizer with Julia
module TestSLP
function rmw!{T}(dest::Ptr{T}, src::Ptr{T})
s1 = unsafe_load(src, 1)
s2 = unsafe_load(src, 2)
d1 = unsafe_load(dest, 1)
d2 = unsafe_load(dest, 2)
d1 $= s1
d2 $= s2
unsafe_store!(dest, d1, 1)
@toivoh
toivoh / TestSIMDrmw.jl
Created November 5, 2014 20:41
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))
@toivoh
toivoh / TestSIMD2.jl
Created November 2, 2014 09:08
Trying to get Julia to emit an efficient sequence of SIMD instructions with the aid of llvmcall
module TestSIMD2
# requires Julia 0.4 for llvmcall
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
@toivoh
toivoh / TestSIMD.jl
Created October 31, 2014 21:02
Trying to get Julia to produce an unrolled loop with SIMD instructions in it
module TestSIMD
immutable TypeConst{T} end
function innerloop!{T, M}(dest::Vector{T}, dest_ofs, src::Vector{T}, src_ofs, ::TypeConst{M})
@simd for i=1:M
@inbounds dest[i + dest_ofs] $= src[i + src_ofs]
end
end
@toivoh
toivoh / FermatFactors.jl
Last active August 29, 2015 14:08
Testing the maximum period property of linear feedback (xorshift) rngs
# Copyright (c) 2014: Toivo Henningsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WIT
@toivoh
toivoh / gist:4386264
Created December 27, 2012 07:18
Type inference crash with PatternDispatch.jl
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" to list help topics
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.0.0+105549818.rb65f
_/ |\__'_|_|_|\__'_| | Commit b65f0478b4 (2012-12-26 20:02:44)
|__/ |
julia> include("test_show_dispatch.jl")
type error: subtype: expected Type{T<:Top}, got (CompositeKind,{:i0, Int64, #undef, #undef, #undef … :vals, Array{Any,1}, :#s176, Int64})
@toivoh
toivoh / get.jl
Created December 14, 2012 09:07
@get! macro
is_expr(ex, head::Symbol) = (isa(ex, Expr) && (ex.head == head))
is_expr(ex, head::Symbol, n::Int) = is_expr(ex, head) && length(ex.args) == n
# simple syntax version
macro get!(d, k, default)
quote
d, k = $(esc(d)), $(esc(k))
has(d, k) ? d[k] : (d[k] = $(esc(default)))
end
end
@toivoh
toivoh / sexpr.jl
Created November 20, 2012 21:06
lispy AST printer and reader
# ---- @sexpr: S-expression to AST conversion ----
is_expr(ex, head::Symbol) = (isa(ex, Expr) && (ex.head == head))
is_expr(ex, head::Symbol, n::Int) = is_expr(ex, head) && length(ex.args) == n
macro sexpr(ex)
esc(sexpr_to_expr(ex))
end
sexpr_to_expr(ex) = expr(:quote, ex)
@toivoh
toivoh / show_sexpr.jl
Created November 20, 2012 09:46
simple lispy AST printer for julia
show_sexpr(ex) = show_sexpr(OUTPUT_STREAM, ex)
show_sexpr(io::IO, ex) = show_sexpr(io, ex, 0)
show_sexpr(io::IO, ex, indent::Int) = show(io, ex)
const sexpr_indent_width = 2
function show_sexpr(io::IO, ex::Expr, indent::Int)
inner = indent + sexpr_indent_width
if (ex.head === :block) inter, post = ("\n"*" "^inner, "\n"*" "^indent)
else inter, post = (" ", "")