Skip to content

Commit

Permalink
bitarray kernels can now be functions instead of macros with good per…
Browse files Browse the repository at this point in the history
…formance

define sort functions with @eval instead of macro
export @timed and remove a couple exports
add some disabled debug code for printing backtraces during bootstrap
  • Loading branch information
JeffBezanson committed Dec 17, 2012
1 parent 63dd471 commit c6a3c85
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 48 deletions.
46 changes: 20 additions & 26 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1173,17 +1173,14 @@ function rot180(A::BitMatrix)
return B
end

# implemented as a macro to improve performance
macro _jl_reverse_bits(dest, src)
quote
z = $(esc(src))
z = ((z >>> 1) & 0x5555555555555555) | ((z << 1) & 0xaaaaaaaaaaaaaaaa)
z = ((z >>> 2) & 0x3333333333333333) | ((z << 2) & 0xcccccccccccccccc)
z = ((z >>> 4) & 0x0f0f0f0f0f0f0f0f) | ((z << 4) & 0xf0f0f0f0f0f0f0f0)
z = ((z >>> 8) & 0x00ff00ff00ff00ff) | ((z << 8) & 0xff00ff00ff00ff00)
z = ((z >>> 16) & 0x0000ffff0000ffff) | ((z << 16) & 0xffff0000ffff0000)
$(esc(dest)) = ((z >>> 32) & 0x00000000ffffffff) | ((z << 32) & 0xffffffff00000000)
end
function _jl_reverse_bits(src::Uint64)
z = src
z = ((z >>> 1) & 0x5555555555555555) | ((z << 1) & 0xaaaaaaaaaaaaaaaa)
z = ((z >>> 2) & 0x3333333333333333) | ((z << 2) & 0xcccccccccccccccc)
z = ((z >>> 4) & 0x0f0f0f0f0f0f0f0f) | ((z << 4) & 0xf0f0f0f0f0f0f0f0)
z = ((z >>> 8) & 0x00ff00ff00ff00ff) | ((z << 8) & 0xff00ff00ff00ff00)
z = ((z >>> 16) & 0x0000ffff0000ffff) | ((z << 16) & 0xffff0000ffff0000)
return ((z >>> 32) & 0x00000000ffffffff) | ((z << 32) & 0xffffffff00000000)
end

function reverse!(B::BitVector)
Expand All @@ -1198,9 +1195,9 @@ function reverse!(B::BitVector)

for i = 1 : hnc
j = ((i - 1) << 6)
@_jl_reverse_bits aux_chunks[1] B.chunks[i]
aux_chunks[1] = _jl_reverse_bits(B.chunks[i])
_jl_copy_chunks(B.chunks, j+1, B.chunks, n-63-j, 64)
@_jl_reverse_bits B.chunks[i] B.chunks[i]
B.chunks[i] = _jl_reverse_bits(B.chunks[i])
_jl_copy_chunks(B.chunks, n-63-j, aux_chunks, 1, 64)
end

Expand All @@ -1213,7 +1210,7 @@ function reverse!(B::BitVector)
l = (@_mod64 (n+63)) + 1
msk = @_mskr l

@_jl_reverse_bits aux_chunks[1] (B.chunks[i] & msk)
aux_chunks[1] = _jl_reverse_bits(B.chunks[i] & msk)
aux_chunks[1] >>>= (64 - l)
_jl_copy_chunks(B.chunks, j+1, aux_chunks, 1, l)

Expand Down Expand Up @@ -1495,17 +1492,14 @@ transpose(B::BitVector) = reshape(copy(B), 1, length(B))

# fast 8x8 bit transpose from Henry S. Warrens's "Hacker's Delight"
# http://www.hackersdelight.org/HDcode/transpose8.c.txt
# implemented as a macro to improve performance
macro _jl_transpose8x8(x)
quote
y = $(esc(x))
t = (y $ (y >>> 7)) & 0x00aa00aa00aa00aa;
y = y $ t $ (t << 7)
t = (y $ (y >>> 14)) & 0x0000cccc0000cccc
y = y $ t $ (t << 14)
t = (y $ (y >>> 28)) & 0x00000000f0f0f0f0
$(esc(x)) = y $ t $ (t << 28)
end
function _jl_transpose8x8(x::Uint64)
y = x
t = (y $ (y >>> 7)) & 0x00aa00aa00aa00aa
y = y $ t $ (t << 7)
t = (y $ (y >>> 14)) & 0x0000cccc0000cccc
y = y $ t $ (t << 14)
t = (y $ (y >>> 28)) & 0x00000000f0f0f0f0
return y $ t $ (t << 28)
end

function _jl_form_8x8_chunk(B::BitMatrix, i1::Int, i2::Int, m::Int, cgap::Int, cinc::Int, nc::Int, msk8::Uint64)
Expand Down Expand Up @@ -1571,7 +1565,7 @@ function transpose(B::BitMatrix)

for j = 1 : 8 : l2
x = _jl_form_8x8_chunk(B, i, j, l1, cgap1, cinc1, nc, msk8_1)
@_jl_transpose8x8 x
x = _jl_transpose8x8(x)

msk8_2 = uint64(0xff)
if (l2 < j + 7)
Expand Down
3 changes: 1 addition & 2 deletions base/export.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export
# Modules
Grisu,
Printf,
PCRE,
FFTW,
DSP,
Expand Down Expand Up @@ -74,7 +73,6 @@ export
Regex,
RegexMatch,
RegexMatchIterator,
Dimspec,
RemoteRef,
RepString,
RevString,
Expand Down Expand Up @@ -1307,6 +1305,7 @@ export
@cmd,
@time,
@elapsed,
@timed,
@windows_only,
@unix_only,
@osx_only,
Expand Down
32 changes: 13 additions & 19 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ _jl_fp_neg_le(x::Float32, y::Float32) = sle_int(unbox(Float32,y),unbox(Float32,x
_jl_fp_neg_le(x::Float64, y::Float64) = sle_int(unbox(Float64,y),unbox(Float64,x))

## internal sorting functionality ##

macro _jl_sort_functions(suffix, lt, args...)
insertionsort = esc(symbol("_jl_insertionsort$suffix"))
quicksort = esc(symbol("_jl_quicksort$suffix"))
mergesort = esc(symbol("_jl_mergesort$suffix"))
pivot_middle = esc(symbol("_jl_pivot_middle$suffix"))
lt = @eval (a,b)->$lt
quote
for (suffix, lt, args) in (("", (a,b)->:(isless($a,$b)), ()),
("_r", (a,b)->:(isless($b,$a)), ()),
("_lt", (a,b)->:(lt($a,$b)), (:(lt::Function),)),
("_by", (a,b)->:(isless(by($a),by($b))), (:(by::Function),)),
## special sorting for floating-point arrays ##
("_fp_pos", (a,b)->:(_jl_fp_pos_lt($a,$b)), ()),
("_fp_neg", (a,b)->:(_jl_fp_neg_lt($a,$b)), ()))
insertionsort = symbol("_jl_insertionsort$suffix")
quicksort = symbol("_jl_quicksort$suffix")
mergesort = symbol("_jl_mergesort$suffix")
pivot_middle = symbol("_jl_pivot_middle$suffix")
@eval begin

# sorting should be stable
# Thus, if a permutation is required, or records are being sorted
Expand Down Expand Up @@ -183,12 +187,7 @@ function ($mergesort)($(args...),
return a, p
end

end; end # quote / macro

@_jl_sort_functions "" :(isless($a,$b))
@_jl_sort_functions "_r" :(isless($b,$a))
@_jl_sort_functions "_lt" :(lt($a,$b)) lt::Function
@_jl_sort_functions "_by" :(isless(by($a),by($b))) by::Function
end; end # @eval / for

## external sorting functions ##

Expand All @@ -202,11 +201,6 @@ sort!{T}(lt::Function, a::AbstractVector{T}) =
sort_by!{T}(by::Function, a::AbstractVector{T}) =
_jl_mergesort_by(by, a, 1, length(a), Array(T,length(a)))

## special sorting for floating-point arrays ##

@_jl_sort_functions "_fp_pos" :(_jl_fp_pos_lt($a,$b))
@_jl_sort_functions "_fp_neg" :(_jl_fp_neg_lt($a,$b))

# push NaNs to the end of a, returning # of non-NaNs
function _jl_nans_to_end{T<:FloatingPoint}(a::AbstractVector{T})
n = length(a)
Expand Down
8 changes: 8 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@ static void record_backtrace(void)
while (unw_step(&cursor) && n < MAX_BT_SIZE) {
unw_get_reg(&cursor, UNW_REG_IP, &ip);
bt_data[n++] = ip;
/*
char *func_name;
int line_num;
const char *file_name;
getFunctionInfo(&func_name, &line_num, &file_name, ip);
if (func_name != NULL)
ios_printf(ios_stdout, "in %s at %s:%d\n", func_name, file_name, line_num);
*/
}
bt_size = n;
}
Expand Down
2 changes: 1 addition & 1 deletion src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jl_value_t *jl_eval_module_expr(jl_expr_t *ex)
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
// remove non-exported macros
if (b->name->name[0]=='@' && !b->exportp)
if (b->name->name[0]=='@' && !b->exportp && b->owner==newm)
b->value = NULL;
// error for unassigned exports
/*
Expand Down

0 comments on commit c6a3c85

Please sign in to comment.