Skip to content

Instantly share code, notes, and snippets.

@carlobaldassi
Last active January 20, 2017 22:50
Show Gist options
  • Save carlobaldassi/ecce15f3c6e92051146e3c6d4eb9554b to your computer and use it in GitHub Desktop.
Save carlobaldassi/ecce15f3c6e92051146e3c6d4eb9554b to your computer and use it in GitHub Desktop.
Code to reproduce julia bug 20065
module BitBug
function copy_to_bitarray_chunks_simplified!(Bc::Vector{UInt64}, C::Array{Bool}, numbits::Int)
ld1 = numbits - 1
u = Base._msk64
msk_d1 = (u << (ld1+1))
ind = 1
@show C
@show C[1]
@show ind,C[1]
c = UInt64(0)
for j = 0:ld1
c |= (UInt64(C[ind]) << j) # bug happens here
# c |= ((C[ind] ? UInt64(1) : UInt64(0)) << j) # this fixes the issue
@show j, C[ind], c
ind += 1
end
@show c
Bc[1] = (Bc[1] & msk_d1) | (c & ~msk_d1)
end
function f()
data = [0.0:0.1:1.0;]
l = length(data)
res = Vector{Bool}(l)
bes = BitArray(l)
# Decommenting this line fixes the random behavior
# fill!(res, true)
for i = 1:l
if i > 1
res[i] = data[i] >= 0.5
end
end
@show res
copy_to_bitarray_chunks_simplified!(bes.chunks, res, l)
@show res
@show bes
@assert bes == res
end
# run f() a number of times until we hit a bug
for n = 1:100
info("test $n")
f()
end
end # module
module Bug
# note: the bug does not happen with a generic f(T, A, i) = T(A[i])
fint8(A, i) = Int8(A[i])
fint16(A, i) = Int16(A[i])
fint32(A, i) = Int32(A[i])
fint64(A, i) = Int64(A[i])
fint128(A, i) = Int128(A[i])
fuint8(A, i) = UInt8(A[i])
fuint16(A, i) = UInt16(A[i])
fuint32(A, i) = UInt32(A[i])
fuint64(A, i) = UInt64(A[i])
fuint128(A, i) = UInt128(A[i])
ffloat16(A, i) = Float16(A[i])
ffloat32(A, i) = Float32(A[i])
ffloat64(A, i) = Float64(A[i])
fchar(A, i) = Char(A[i])
# for n = 1:10000 # bug does not happen with the for loop
A = Vector{Bool}(5); i = 1
x = Bug.fint8(A, i); y = Int8(A[i]); x == y || @show x, y, Int8
x = Bug.fint16(A, i); y = Int16(A[i]); x == y || @show x, y, Int16
x = Bug.fint32(A, i); y = Int32(A[i]); x == y || @show x, y, Int32
x = Bug.fint64(A, i); y = Int64(A[i]); x == y || @show x, y, Int64
x = Bug.fint128(A, i); y = Int128(A[i]); x == y || @show x, y, Int128
x = Bug.fuint8(A, i); y = UInt8(A[i]); x == y || @show x, y, UInt8
x = Bug.fuint16(A, i); y = UInt16(A[i]); x == y || @show x, y, UInt16
x = Bug.fuint32(A, i); y = UInt32(A[i]); x == y || @show x, y, UInt32
x = Bug.fuint64(A, i); y = UInt64(A[i]); x == y || @show x, y, UInt64
x = Bug.fuint128(A, i); y = UInt128(A[i]); x == y || @show x, y, UInt128
x = Bug.ffloat16(A, i); y = Float16(A[i]); x == y || @show x, y, Float16
x = Bug.ffloat32(A, i); y = Float32(A[i]); x == y || @show x, y, Float32
x = Bug.ffloat64(A, i); y = Float64(A[i]); x == y || @show x, y, Float64
x = Bug.fchar(A, i); y = Char(A[i]); x == y || @show x, y, Char
# end
end # module
julia> include("Bug.jl");
(x,y,Int8) = (-48,0,Int8)
(x,y,Int16) = (208,0,Int16)
(x,y,Int32) = (208,0,Int32)
(x,y,Int64) = (208,0,Int64)
(x,y,Int128) = (208,0,Int128)
(x,y,UInt8) = (0xd0,0x00,UInt8)
(x,y,UInt16) = (0x00d0,0x0000,UInt16)
(x,y,UInt32) = (0x000000d0,0x00000000,UInt32)
(x,y,UInt64) = (0x00000000000000d0,0x0000000000000000,UInt64)
(x,y,UInt128) = (0x000000000000000000000000000000d0,0x00000000000000000000000000000000,UInt128)
(x,y,Float32) = (208.0f0,0.0f0,Float32)
(x,y,Float64) = (208.0,0.0,Float64)
(x,y,Char) = ('Ð','\0',Char)
INFO: test 32
res = Bool[false,false,false,false,false,true,true,true,true,true,true]
C = Bool[false,false,false,false,false,true,true,true,true,true,true]
C[1] = false
(ind,C[1]) = (1,false)
(j,C[ind],c) = (0,false,0x0000000000000000)
(j,C[ind],c) = (1,false,0x0000000000000000)
(j,C[ind],c) = (2,false,0x0000000000000000)
(j,C[ind],c) = (3,false,0x0000000000000000)
(j,C[ind],c) = (4,false,0x0000000000000000)
(j,C[ind],c) = (5,true,0x0000000000000020)
(j,C[ind],c) = (6,true,0x0000000000000060)
(j,C[ind],c) = (7,true,0x00000000000000e0)
(j,C[ind],c) = (8,true,0x00000000000001e0)
(j,C[ind],c) = (9,true,0x00000000000003e0)
(j,C[ind],c) = (10,true,0x00000000000007e0)
c = 0x00000000000007e0
res = Bool[false,false,false,false,false,true,true,true,true,true,true]
bes = Bool[false,false,false,false,false,true,true,true,true,true,true]
INFO: test 33
res = Bool[true,false,false,false,false,true,true,true,true,true,true]
C = Bool[true,false,false,false,false,true,true,true,true,true,true]
C[1] = true
(ind,C[1]) = (1,true)
(j,C[ind],c) = (0,true,0x0000000000000031)
(j,C[ind],c) = (1,false,0x0000000000000031)
(j,C[ind],c) = (2,false,0x0000000000000031)
(j,C[ind],c) = (3,false,0x0000000000000031)
(j,C[ind],c) = (4,false,0x0000000000000031)
(j,C[ind],c) = (5,true,0x0000000000000031)
(j,C[ind],c) = (6,true,0x0000000000000071)
(j,C[ind],c) = (7,true,0x00000000000000f1)
(j,C[ind],c) = (8,true,0x00000000000001f1)
(j,C[ind],c) = (9,true,0x00000000000003f1)
(j,C[ind],c) = (10,true,0x00000000000007f1)
c = 0x00000000000007f1
res = Bool[true,false,false,false,false,true,true,true,true,true,true]
bes = Bool[true,false,false,false,true,true,true,true,true,true,true]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment