Created
December 15, 2015 09:24
-
-
Save ityonemo/9b75b07558cb08262ffc to your computer and use it in GitHub Desktop.
sign is faster without branches.
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
| code_native(sign,(Float64,)) | |
| ## note two jbe opcodes. There are two jumps! | |
| arry = rand(10000000) - 0.5 | |
| @time map(sign, arry) | |
| # 0.38s | |
| function sgn2(x::Float64) | |
| xp::UInt64 = reinterpret(UInt64, x) | |
| yp::UInt64 = ((xp & 0x7FFF_FFFF_0000_0000) >> 32) | (xp & 0x0000_0000_FFFF_FFFF) | |
| yp = (yp & 0xFFFF_0000 >> 16) | (yp & 0x0000_FFFF) | |
| yp = (yp & 0xFF00 >> 8) | (yp & 0x00FF) | |
| yp = (yp & 0xF0 >> 4) | (yp & 0x0F) | |
| yp = (yp & 0x0C >> 2) | (yp & 0x03) | |
| yp = (yp & 0x02 >> 1) | (yp & 0x01) | |
| reinterpret(Float64, (xp & 0x8000_0000_0000_0000) | (0x3ff0_0000_0000_0000 * yp)) | |
| end | |
| @time map(sgn2, arry) | |
| #0.34s - a 10% improvement. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment