https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107520
Bug ID: 107520 Summary: Optimize std::lerp(d, d, 0.5) Product: gcc Version: 13.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: enhancement Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Target Milestone: --- In some C++ code I have, it would be convenient if the compiler, possibly with the help of the standard library, could make the following function cheap, ideally just the identity. I'll probably end up wrapping lerp with a function that first checks with __builtin_constant_p if the 2 bounds are equal, but I'll post this in case people have ideas how to improve things. #include <cmath> double f(double d){ return std::lerp(d, d, .5); } Currently, with -O3, we generate movapd %xmm0, %xmm1 pxor %xmm0, %xmm0 comisd %xmm1, %xmm0 jnb .L7 comisd %xmm0, %xmm1 jb .L6 .L7: pxor %xmm0, %xmm0 ucomisd %xmm0, %xmm1 jp .L6 je .L11 .L6: movapd %xmm1, %xmm0 subsd %xmm1, %xmm0 mulsd .LC1(%rip), %xmm0 addsd %xmm1, %xmm0 maxsd %xmm1, %xmm0 ret .p2align 4,,10 .p2align 3 .L11: mulsd .LC1(%rip), %xmm1 movapd %xmm1, %xmm0 addsd %xmm1, %xmm0 ret (clang is better at avoiding the redundant comparison) With -fno-trapping-math to help a bit, I see at the beginning if (d_2(D) == 0.0) goto <bb 3>; [34.00%] else goto <bb 4>; [66.00%] <bb 3> [local count: 475287355]: _7 = d_2(D) * 5.0e-1; _10 = _7 * 2.0e+0; I think that even with the default -fsigned-zeros, simplifying to _10 = d_2(D) is valid. Adding -fno-signed-zeros <bb 2> [local count: 1073741824]: if (d_2(D) == 0.0) goto <bb 5>; [34.00%] else goto <bb 3>; [66.00%] <bb 3> [local count: 598454470]: _13 = d_2(D) - d_2(D); _14 = _13 * 5.0e-1; __x_15 = d_2(D) + _14; if (d_2(D) u>= __x_15) goto <bb 5>; [50.00%] else goto <bb 4>; [50.00%] <bb 4> [local count: 299227235]: <bb 5> [local count: 1073741825]: # _12 = PHI <d_2(D)(2), __x_15(4), d_2(D)(3)> return _12; _13 is 0 or NaN, which doesn't change for _14, and __x_15 is just d_2, so we always return d_2.