Zig Version
0.9.0 (Godbolt)
Steps to Reproduce
This behaviour can be verified e.g. by implementing the generic Minkowski distance and compiling the instances with order 1 (equivalent to Manhattan distance) and order 2 (equivalent to Euclidean distance). Make sure to enable optimizations with fast math.
It can be done in Godbolt:
In this case, compare the deassembled outputs.
Expected Behavior
std.math.powf should be inlined and optimized out where applicable (i.e. where the compiler can guarantee safe behaviour, and thus simply use the equivalent of libm's powf).
The first- and second-order instances of the generic Minkowski function should be automatically optimized into specific algorithms by the compiler. This can be most easily verified by checking that the assembly versions of both cases are short and don't contain call, and that only the 2nd-order case contains vsqrtss (since in the first-order case, all powf calls are passed an exponent of 1 and can be safely optimized out).
Actual Behavior
In Zig 0.9.0, the Minkowski function is neither inlined, much less optimized, in the specific cases of orders 1 and 2, as a result of the compiler failing to inline and optimize out std.math.pow.
Zig Version
0.9.0 (Godbolt)
Steps to Reproduce
This behaviour can be verified e.g. by implementing the generic Minkowski distance and compiling the instances with order 1 (equivalent to Manhattan distance) and order 2 (equivalent to Euclidean distance). Make sure to enable optimizations with fast math.
It can be done in Godbolt:
-O ReleaseFast)-O3 -ffast-math)--releasearg)In this case, compare the deassembled outputs.
Expected Behavior
std.math.powfshould be inlined and optimized out where applicable (i.e. where the compiler can guarantee safe behaviour, and thus simply use the equivalent oflibm'spowf).The first- and second-order instances of the generic Minkowski function should be automatically optimized into specific algorithms by the compiler. This can be most easily verified by checking that the assembly versions of both cases are short and don't contain
call, and that only the 2nd-order case containsvsqrtss(since in the first-order case, allpowfcalls are passed an exponent of 1 and can be safely optimized out).Actual Behavior
In Zig 0.9.0, the Minkowski function is neither inlined, much less optimized, in the specific cases of orders 1 and 2, as a result of the compiler failing to inline and optimize out
std.math.pow.