Last active
June 5, 2016 01:18
-
-
Save ityonemo/06be804e2a178db2169c4a61de73d60c to your computer and use it in GitHub Desktop.
converting to unsigned 16 bit integers in julia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # converting from Int64 to UInt16 in Julia can result in a minefield of inexacterrors, | |
| # which could be performance-debilitating (if you care about that). | |
| code_native(Int16, (Int64,)) | |
| # ==>(edited) | |
| # pushq %rbp | |
| # movq %rsp, %rbp | |
| # movswq %si, %rax | |
| # cmpq %rsi, %rax | |
| # jne L22 | |
| # movw %si, %ax | |
| # popq %rbp | |
| # ret | |
| # L22: | |
| # movabsq $jl_inexact_exception, %rax | |
| # movq (%rax), %rdi | |
| # movabsq $jl_throw, %rax | |
| # callq *%rax | |
| # unlike array bounds-checking, with the @inbounds macro, integer conversions do not have | |
| # a simple macro which takes care of this problem. In order to eliminate the checking, | |
| # you have to add extraneous information which will create static-analysis guarantees | |
| # that the desired result is in-range (even if your code is set up so that can't happen). | |
| to16(n::Int64) = UInt16(reinterpret(UInt64, n) & 0x0000_0000_0000_FFFF) | |
| code_native(to16, (Int64,)) | |
| # ==>(edited) | |
| # pushq %rbp | |
| # movq %rsp, %rbp | |
| # movw %di, %ax | |
| # popq %rbp | |
| # ret | |
| # this is optimized to one line of code! (movw %di, %ax), and may even be inlined. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment