LLVM version of modf


#1

Is LLVM version of modf guaranteed to be safer or faster for some input or platform, compared to the fallback method

modf(x) = rem(x,one(x)), trunc(x)

? On my machine (64-bit GNU/Linux), the fallback method is much faster than LLVM with Float64 numbers:

julia> test_llvm_modf() = for x in -1000:0.001:1000; modf(x); end
test_llvm_modf (generic function with 1 method)

julia> @benchmark test_llvm_modf()
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     16.505 ms (0.00% GC)
  median time:      16.613 ms (0.00% GC)
  mean time:        16.700 ms (0.00% GC)
  maximum time:     17.188 ms (0.00% GC)
  --------------
  samples:          300
  evals/sample:     1

julia> test_fallback_modf() = for x in -1000:0.001:1000; rem(x,one(x)), trunc(x); end
test_fallback_modf (generic function with 1 method)

julia> @benchmark test_fallback_modf()
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     589.929 μs (0.00% GC)
  median time:      589.941 μs (0.00% GC)
  mean time:        596.599 μs (0.00% GC)
  maximum time:     856.916 μs (0.00% GC)
  --------------
  samples:          8373
  evals/sample:     1

#2
  1. There’s no LLVM version of modf

    You can see either from the source code (using @which/@edit) or code_lowered, code_warntype that the modf is using the libm version.

  2. The main difference here is that the fallback version IS using LLVM intrinsics and the libm version is not.

    So in your fallback loop, the loop is not actually doing anything at all.

In normal code it is usually expected that the result is used so this is not a major concern.