Skip to content

Instantly share code, notes, and snippets.

@musm
Last active October 20, 2016 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musm/8150451b04120ef27974d8270dedbf6f to your computer and use it in GitHub Desktop.
Save musm/8150451b04120ef27974d8270dedbf6f to your computer and use it in GitHub Desktop.
using BenchmarkTools
using Base: sign_mask, exponent_mask
typealias IEEEFloat Union{Float16,Float32,Float64}
_isinf{T<:IEEEFloat}(x::T) = (reinterpret(Unsigned, x) & ~sign_mask(T)) == exponent_mask(T)
_isfinite{T<:IEEEFloat}(x::T) = (reinterpret(Unsigned, x) & exponent_mask(T)) != exponent_mask(T)
_isnan{T<:IEEEFloat}(x::T) = (reinterpret(Unsigned, x) & ~sign_mask(T)) > exponent_mask(T)
julia> @benchmark isinf(2.0)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 1.578 ns (0.00% GC)
median time: 1.579 ns (0.00% GC)
mean time: 1.723 ns (0.00% GC)
maximum time: 12.632 ns (0.00% GC)
julia> @benchmark _isinf(2.0)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 1.578 ns (0.00% GC)
median time: 1.973 ns (0.00% GC)
mean time: 1.832 ns (0.00% GC)
maximum time: 115.269 ns (0.00% GC)
julia> @benchmark _isnan(2.0)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 1.578 ns (0.00% GC)
median time: 1.579 ns (0.00% GC)
mean time: 1.790 ns (0.00% GC)
maximum time: 88.031 ns (0.00% GC)
julia> @benchmark isnan(2.0)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 1.578 ns (0.00% GC)
median time: 1.973 ns (0.00% GC)
mean time: 1.841 ns (0.00% GC)
maximum time: 97.900 ns (0.00% GC)
julia> @benchmark isfinite(2.0)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 1.578 ns (0.00% GC)
median time: 1.579 ns (0.00% GC)
mean time: 1.756 ns (0.00% GC)
maximum time: 54.082 ns (0.00% GC)
julia> @benchmark _isfinite(2.0)
BenchmarkTools.Trial:
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 1.578 ns (0.00% GC)
median time: 1.579 ns (0.00% GC)
mean time: 1.752 ns (0.00% GC)
maximum time: 53.687 ns (0.00% GC)
julia> versioninfo()
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
System: NT (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.7.1 (ORCJIT, haswell)
julia> @code_llvm isnan(2.0)
; Function Attrs: uwtable
define i8 @jlsys_isnan_66420(double) #0 {
top:
%1 = fcmp uno double %0, 0.000000e+00
%2 = zext i1 %1 to i8
ret i8 %2
}
julia> @code_llvm _isnan(2.0)
; Function Attrs: uwtable
define i8 @julia__isnan_72034(double) #0 {
top:
%1 = bitcast double %0 to i64
%2 = and i64 %1, 9223372036854775807
%3 = icmp ugt i64 %2, 9218868437227405312
%4 = zext i1 %3 to i8
ret i8 %4
}
julia> @code_llvm isinf(2.0)
; Function Attrs: uwtable
define i8 @jlsys_isinf_67353(double) #0 {
top:
%1 = fcmp ord double %0, 0.000000e+00
%2 = fsub double %0, %0
%3 = fcmp une double %2, 0.000000e+00
%4 = and i1 %1, %3
%5 = zext i1 %4 to i8
ret i8 %5
}
julia> @code_llvm _isinf(2.0)
; Function Attrs: uwtable
define i8 @julia__isinf_72361(double) #0 {
top:
%1 = bitcast double %0 to i64
%2 = and i64 %1, 9223372036854775807
%3 = icmp eq i64 %2, 9218868437227405312
%4 = zext i1 %3 to i8
ret i8 %4
}
julia> @code_llvm isfinite(2.0)
; Function Attrs: uwtable
define i8 @jlsys_isfinite_66257(double) #0 {
top:
%1 = fsub double %0, %0
%2 = fcmp oeq double %1, 0.000000e+00
%3 = zext i1 %2 to i8
ret i8 %3
}
julia> @code_llvm _isfinite(2.0)
; Function Attrs: uwtable
define i8 @julia__isfinite_72365(double) #0 {
top:
%1 = bitcast double %0 to i64
%2 = and i64 %1, 9218868437227405312
%3 = icmp ne i64 %2, 9218868437227405312
%4 = zext i1 %3 to i8
ret i8 %4
}
# ON MY COMPUTER
julia> @code_native isinf(2.0)
.text
Filename: float.jl
pushq %rbp
movq %rsp, %rbp
Source line: 362
vucomisd %xmm0, %xmm0
setnp %cl
vsubsd %xmm0, %xmm0, %xmm0
vxorps %xmm1, %xmm1, %xmm1
vucomisd %xmm1, %xmm0
setp %dl
setne %al
orb %dl, %al
andb %cl, %al
popq %rbp
retq
nopw %cs:(%rax,%rax)
julia> @code_native _isinf(2.0)
.text
Filename: REPL[4]
pushq %rbp
movq %rsp, %rbp
Source line: 1
vmovq %xmm0, %rax
movb $63, %cl
bzhiq %rcx, %rax, %rax
movabsq $9218868437227405312, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
sete %al
popq %rbp
retq
nopw %cs:(%rax,%rax)
julia> @code_native isnan(2.0)
.text
Filename: float.jl
pushq %rbp
movq %rsp, %rbp
Source line: 355
vucomisd %xmm0, %xmm0
setp %al
popq %rbp
retq
nopl (%rax)
julia> @code_native _isnan(2.0)
.text
Filename: REPL[6]
pushq %rbp
movq %rsp, %rbp
Source line: 1
vmovq %xmm0, %rax
movb $63, %cl
bzhiq %rcx, %rax, %rax
movabsq $9218868437227405312, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
seta %al
popq %rbp
retq
nopw %cs:(%rax,%rax)
julia> @code_native isfinite(2.0)
.text
Filename: float.jl
pushq %rbp
movq %rsp, %rbp
Source line: 358
vsubsd %xmm0, %xmm0, %xmm0
vxorps %xmm1, %xmm1, %xmm1
vcmpeqsd %xmm1, %xmm0, %xmm0
vmovq %xmm0, %rax
andl $1, %eax
popq %rbp
retq
nopl (%rax,%rax)
julia> @code_native _isfinite(2.0)
.text
Filename: REPL[5]
pushq %rbp
movq %rsp, %rbp
Source line: 1
vmovq %xmm0, %rax
movabsq $9218868437227405312, %rcx # imm = 0x7FF0000000000000
andq %rcx, %rax
cmpq %rcx, %rax
setne %al
popq %rbp
retq
nop
@musm
Copy link
Author

musm commented Oct 19, 2016

On another machine:

julia> @benchmark isinf(2.0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.539 ns (0.00% GC)
  median time:      2.795 ns (0.00% GC)
  mean time:        2.852 ns (0.00% GC)
  maximum time:     15.950 ns (0.00% GC)

julia> @benchmark _isinf(2.0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.542 ns (0.00% GC)
  median time:      2.667 ns (0.00% GC)
  mean time:        2.744 ns (0.00% GC)
  maximum time:     16.177 ns (0.00% GC)

julia> @benchmark isnan(2.0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.540 ns (0.00% GC)
  median time:      2.695 ns (0.00% GC)
  mean time:        2.778 ns (0.00% GC)
  maximum time:     16.014 ns (0.00% GC)

julia> @benchmark _isnan(2.0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.542 ns (0.00% GC)
  median time:      2.665 ns (0.00% GC)
  mean time:        2.743 ns (0.00% GC)
  maximum time:     16.246 ns (0.00% GC)

julia> @benchmark isfinite(2.0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.541 ns (0.00% GC)
  median time:      2.756 ns (0.00% GC)
  mean time:        2.837 ns (0.00% GC)
  maximum time:     46.960 ns (0.00% GC)

julia> @benchmark _isfinite(2.0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.534 ns (0.00% GC)
  median time:      2.744 ns (0.00% GC)
  mean time:        2.830 ns (0.00% GC)
  maximum time:     16.107 ns (0.00% GC)

julia>versioninfo()
Julia Version 0.6.0-dev.1043
Commit 3b33217* (2016-10-19 12:51 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, broadwell)

```julia

@musm
Copy link
Author

musm commented Oct 20, 2016

julia> @benchmark isfinite(2.0f0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.536 ns (0.00% GC)
  median time:      2.704 ns (0.00% GC)
  mean time:        2.790 ns (0.00% GC)
  maximum time:     16.406 ns (0.00% GC)

julia> @benchmark _isfinite(2.0f0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.544 ns (0.00% GC)
  median time:      2.708 ns (0.00% GC)
  mean time:        2.788 ns (0.00% GC)
  maximum time:     18.906 ns (0.00% GC)

julia> @benchmark _isinf(2.0f0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.111 ns (0.00% GC)
  median time:      2.120 ns (0.00% GC)
  mean time:        2.171 ns (0.00% GC)
  maximum time:     15.133 ns (0.00% GC)

julia> @benchmark isinf(2.0f0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.110 ns (0.00% GC)
  median time:      2.119 ns (0.00% GC)
  mean time:        2.168 ns (0.00% GC)
  maximum time:     16.316 ns (0.00% GC)

julia> @benchmark isnan(2.0f0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.543 ns (0.00% GC)
  median time:      2.783 ns (0.00% GC)
  mean time:        2.841 ns (0.00% GC)
  maximum time:     15.666 ns (0.00% GC)

julia> @benchmark _isnan(2.0f0)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     2.552 ns (0.00% GC)
  median time:      2.841 ns (0.00% GC)
  mean time:        2.917 ns (0.00% GC)
  maximum time:     62.518 ns (0.00% GC)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment