<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61613>61613</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Optimizing functions that compute absolute values of x
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
k-arrows
</td>
</tr>
</table>
<pre>
Test code:
```c
#include <stdlib.h>
#include <math.h>
float f1(float x)
{
return fabsf(x);
}
float f2(float x)
{
return sqrtf(x * x);
}
float f3(float x)
{
return (x >= 0.0f) ? x : -x;
}
float f4(float x)
{
if(x >= 0.0f) return x;
else return -x;
}
```
gcc:
```asm
f1:
fabs s0, s0
ret
f2:
fmul s0, s0, s0
fsqrt s0, s0
ret
f3:
fabs s0, s0
ret
f4:
fabs s0, s0
ret
```
clang
```asm
f1: // @f1
fabs s0, s0
ret
f2: // @f2
fabs s0, s0
ret
f3: // @f3
fabs s0, s0
ret
f4: // @f4
fneg s1, s0
fcmp s0, #0.0
fcsel s0, s1, s0, lt
ret
```
https://godbolt.org/z/EercfYfnd
Interestingly, clang generates different code for f4 and so does gcc for f2.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVc1yozgQfhpx6TIlWhibAwcnDlV72ste9iighbUrJI8k8vf0U2An9iTUxBPKBbK6vx_6c8kyBN1booqt79h6n8gxHpyv_l9J791TSBrXvVT_UIjQuo6Y2DG-Z3zHCn76tOfvKLRtzdgRMHEfYmd0kx6YeFgqDzIerovzXRknI6iM4fa0fGZYnhs2d6cFAICnOHoLSjZBMdzOXeLurXG_QIk3UoYfPs6UwHAHNxCLG4lPnOKBiT3wlCuGJTBRw7S5g9XzFzL5VzJaLSmcxS_sQCbQ2_ai6luo1yb6tv2cugzD2WL2XoTzNSUDgTO8n-6_lDzFMww_w4bRTM935AJeTRF9yS2WLV1zn6oXSP7HkMVZtUba_rejAoY1wxpYzlV2k-Li6K5p8Hs04gON-MbI4JbrSiP_oGGpnzWyxbDb4XhxwFDw9FNHIHPxmF1-OCbelNchxmOYop8t9q5rnImp8z3D-pVh_UC-Vf8q212D_rKRPIWobW9eJrE5dejJkpeRAnRaKfJkT0cmKOdB5SBtB8FB5yhA37anbUyTrhJdKUqZUJUVmzLf5GteJIeqVHIjiPhW8oJwk60b1ZRrydWGupI2RaIr5Ci4QOTbPMuLdF1QQZLzrOFSNBxZzmmQ2qTGPA7TWyU6hJGqIisykRjZkAnzsY9o6QnmIkOc_gV8NWFWzdgHlnOjQwwXlqijoervY9SDftW2BzXaNmpnA8SDnN56OI6RQDbBmWnxKM1IAZyC52T0pvowdh0PY5O2bmBYTyLnx-ro3X_URob1bC0wrGfrPwMAAP__AG_BRQ">