# Compiling to branch table

**URL:** <https://discourse.julialang.org/t/compiling-to-branch-table/16599>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 21, 2018, 1:12pm UTC](https://discourse.julialang.org/t/compiling-to-branch-table/16599 "2018-10-21T13:12:54Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 23, 2018, 1:43am UTC](https://discourse.julialang.org/t/compiling-to-branch-table/16599/13 "2018-10-23T01:43:01Z")

</div>

For even better performance, we can use a packed version of the bit shifting solution to convert 8 codes at a time:

```julia
const ONE = 0x101010101010101
const SEVEN = 0x707070707070707

findcode_shift_packed(x) = ((x+ONE)>>1-x>>3&ONE)&SEVEN

```

With the codes stored in an `Int8` array, we can wrap it as an `Int64` array to operate on it efficiently (same memory):

```julia
src8 = rand(Int8[2,3,5,7,11], 10_000);
src64 = unsafe_wrap(Array, convert(Ptr{Int64}, pointer(src8)), length(src8)>>3);
dst64 = similar(src64);

broadcast!(findcode_shift_packed, dst64, src64);

dst8 = unsafe_wrap(Array, convert(Ptr{Int8}, pointer(dst64)), length(dst64)<<3);

```

Verification:

```julia
julia> for i=1:6; print("$(src8[i])->$(dst8[i]) "); end
3->2 5->3 7->4 2->1 3->2 11->5

```

Timings:

```julia
julia> @btime broadcast!($findcode_shift_packed, $dst64, $src64);
  207.870 ns (0 allocations: 0 bytes)
  
julia> @btime broadcast!(identity, $dst64, $src64);
  101.603 ns (0 allocations: 0 bytes)

```

`code_native` shows that this method uses 256 bit SSE2 instructions, operating on 32 codes in parallel. The timings above show that ~16.7 codes are handled per clock cycle. Correcting for the overhead of copying elements, ~32.0 codes are handled per clock cycle. **460 times faster** than the original implementation!

---

_[View the full topic](https://discourse.julialang.org/t/compiling-to-branch-table/16599)._
