crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Faster floating-point finiteness checks

Open HertzDevil opened this issue 1 year ago • 0 comments

When checking whether a Float32 or Float64 is finite, both in Crystal code itself via #finite? and in certain primitive conversions like Float64::MAX.to_f32, the check is done with x != x * 2. It seems this could generate suboptimal code compared to direct comparisons against infinity: https://godbolt.org/z/zzT8va1oP

There is exactly one binary representation of each signed infinity for Float32 and Float64, and it should be possible to use them directly. Additionally, the llvm.is.fpclass.* intrinsic was introduced in LLVM 15, presumably to handle cases where a floating-point type has more binary representations. We should switch to this intrinsic in both the standard library and the primitive codegen if the LLVM version is new enough.

HertzDevil avatar Sep 18 '24 17:09 HertzDevil