Skip to content

Instantly share code, notes, and snippets.

Avatar
🦘

Simon Byrne simonbyrne

🦘
View GitHub Profile
@simonbyrne
simonbyrne / jbug.jl
Created Oct 15, 2012
A bugs-type sampler in julia
View jbug.jl
require("extras/distributions.jl")
require("slicesampler.jl")
import Distributions.*
macro buildslicesampler(model)
# pull apart the model into variables and distributions
variables = Dict{Symbol,Expr}()
unboundvars = Set{Symbol}()
for line in model.args
@simonbyrne
simonbyrne / IBMFloat64.jl
Created Apr 23, 2013
converts 64bit IBM floating point numbers to IEEE754 64bit floats
View IBMFloat64.jl
import Base.convert
bitstype 64 IBMFloat64
# ibm float format is hex based:
# * bit 1 = sign
# * bits 2-8 = exponent to the power of 16, with bias of 64
# * bits 9-64 = mantissa: no implied leading 1 (unlike ieee),
# typically normalised (hex of bits 9-12 is non-zero), though this need
# not be the case
View rantree.jl
abstract UTree{T}
immutable UTreeBranch{T} <: UTree{T}
left::UTree{T}
right::UTree{T}
end
immutable UTreeLeaf{T} <: UTree{T}
v::T
end
View sincos.jl
function sincos(x)
psin = Cdouble[0]; pcos = Cdouble[0]
ccall(:sincos, Void, (Cdouble, Ptr{Cdouble}, Ptr{Cdouble}), x, psin, pcos)
psin[1], pcos[1]
end
function foo(X)
ta = 0.0
tb = 0.0
for x in X
View mxcsr.jl
function getmxcsr()
Base.llvmcall("""%ptr = alloca i32
call void @llvm.x86.sse.stmxcsr(i32 * %ptr)
%curval = load i32 * %ptr
ret i32 %curval""", UInt32, ())
end
function setmxcsr(u::UInt32)
Base.llvmcall("""%ptr = alloca i32
store i32 %0, i32 * %ptr
View roundtest.jl
function rint_llvm(x::Float64)
Base.llvmcall("""%x = call double @llvm.rint.f64(double %0)
ret double %x""",Float64,(Float64,),x)
end
function trunc_llvm(x::Float64)
Base.llvmcall("""%x = call double @llvm.trunc.f64(double %0)
ret double %x""",Float64,(Float64,),x)
end
# warm up llvmcall: ignore errors
View DataFrameX.jl
type DataFrame{N,D}
data::D
end
immutable Field{s}
end
Field(s::Symbol) = Field{s}()
function DataFrame(;kwds...)
View DataFrameX.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@simonbyrne
simonbyrne / truncbits.jl
Last active Aug 29, 2015
Comparison of methods for zeroing out lower order bits of a floating point number
View truncbits.jl
# use floating point AND instructions (andps/andpd)
# https://github.com/JuliaLang/julia/issues/9868
function and_float(x::Float64,y::Float64)
Base.llvmcall("""%av = insertelement<2 x double> undef, double %0, i32 0
%bv = insertelement<2 x double> undef, double %1, i32 0
%ai = bitcast <2 x double> %av to <2 x i64>
%bi = bitcast <2 x double> %bv to <2 x i64>
%and.i = and <2 x i64> %ai, %bi
%cf = bitcast <2 x i64> %and.i to <2 x double>
%cfe = extractelement<2 x double> %cf, i32 0
View fmatest.c
#include <stdio.h>
#include <math.h>
int main() {
volatile double a = 0.1;
volatile double x = a + 0.2;
printf("%.20f\n", x);
volatile double y = fma(a, 1.0, 0.0);
printf("%.20f\n", y);
volatile double z = y + 0.2;