Skip to content

Instantly share code, notes, and snippets.

@simonster
Last active August 29, 2015 14:11
Show Gist options
  • Save simonster/b1b4cc2ad0daa8e20a99 to your computer and use it in GitHub Desktop.
Save simonster/b1b4cc2ad0daa8e20a99 to your computer and use it in GitHub Desktop.

Revisions

  1. simonster revised this gist Dec 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.jl
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ immutable BitWrapper
    x::Vector{Uint64}
    end
    Base.getindex(x::BitWrapper, i) = CheapMulBool(!Base.unsafe_bitgetindex(x.x, i))
    Base.getindex(x::Negated, i) = CheapMulBool(!x.x[i])
    Base.getindex(x::Negated, i) = CheapMulBool(unsafe_load(convert(Ptr{Uint8}, pointer(x.x)), i) == 0)
    *(x::CheapMulBool, y::Number) = ifelse(x.x, y, zero(y))

    function f(x, y)
  2. simonster revised this gist Dec 21, 2014. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions gistfile1.jl
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,14 @@
    immutable BitWrapper{T}
    x::T
    end
    immutable CheapMulBool
    x::Bool
    end
    Base.getindex(x::BitWrapper{BitVector}, i) = CheapMulBool(!Base.unsafe_bitgetindex(x.x.chunks, i))
    Base.getindex(x::BitWrapper, i) = CheapMulBool(!x.x[i])
    immutable Negated{T}
    x::T
    end
    immutable BitWrapper
    x::Vector{Uint64}
    end
    Base.getindex(x::BitWrapper, i) = CheapMulBool(!Base.unsafe_bitgetindex(x.x, i))
    Base.getindex(x::Negated, i) = CheapMulBool(!x.x[i])
    *(x::CheapMulBool, y::Number) = ifelse(x.x, y, zero(y))

    function f(x, y)
    @@ -20,9 +23,9 @@ x = randn(10000000)
    ybool = rand(Bool, length(x))
    ybit = bitpack(ybool)
    yfloat = convert(Vector{Float64}, !ybool)
    f(x, BitWrapper(ybool))
    f(x, BitWrapper(ybit))
    f(x, Negated(ybool))
    f(x, BitWrapper(ybit.chunks))
    f(x, yfloat)
    @time for i = 1:50 f(x, BitWrapper(ybool)) end
    @time for i = 1:50 f(x, BitWrapper(ybit)) end
    @time for i = 1:50 f(x, Negated(ybool)) end
    @time for i = 1:50 f(x, BitWrapper(ybit.chunks)) end
    @time for i = 1:50 f(x, yfloat) end
  3. simonster created this gist Dec 21, 2014.
    28 changes: 28 additions & 0 deletions gistfile1.jl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    immutable BitWrapper{T}
    x::T
    end
    immutable CheapMulBool
    x::Bool
    end
    Base.getindex(x::BitWrapper{BitVector}, i) = CheapMulBool(!Base.unsafe_bitgetindex(x.x.chunks, i))
    Base.getindex(x::BitWrapper, i) = CheapMulBool(!x.x[i])
    *(x::CheapMulBool, y::Number) = ifelse(x.x, y, zero(y))

    function f(x, y)
    z = zero(eltype(x))
    @simd for i = 1:length(x)
    @inbounds z += y[i]*x[i]
    end
    z
    end

    x = randn(10000000)
    ybool = rand(Bool, length(x))
    ybit = bitpack(ybool)
    yfloat = convert(Vector{Float64}, !ybool)
    f(x, BitWrapper(ybool))
    f(x, BitWrapper(ybit))
    f(x, yfloat)
    @time for i = 1:50 f(x, BitWrapper(ybool)) end
    @time for i = 1:50 f(x, BitWrapper(ybit)) end
    @time for i = 1:50 f(x, yfloat) end