<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54927>54927</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Change in floating-point behavior in clang 14
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
glandium
</td>
</tr>
</table>
<pre>
I'm not sure this qualifies as a problem in itself, but I'll lay it down, hopefully someone who knows better will say whether something needs to be changed or not.
The following is reduced from fdlibm (https://github.com/freebsd/freebsd/tree/master/lib/msun/src), as used in Firefox for trigonometric functions with reduced fingerprint in JS, along with how the function is called within the context of a test that fails.
```
#include <stdio.h>
static const double
half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
double
__kernel_sin(double x, double y, int iy)
{
double z,r,v,w;
z = x*x;
w = z*z;
r = S2+z*(S3+z*S4) + z*w*(S5+z*S6);
v = z*x;
if(iy==0) return x+v*(S1+z*r);
else return x-((z*(half*y-v*r)-y)-v*S1);
}
int main() {
printf("%.20f\n", __kernel_sin(0.41892385060478199, 4.0204527408880763E-18, 1));
return 0;
}
```
On x86 and x86_64, this prints `0.40677759702517241047`.
On aarch64 with clang 13, this prints the same thing.
On aarch64 with clang 14, this prints `0.40677759702517235496`.
Interestingly, things change when clang is allowed to optimize the function call, in which case it uses a constant with a value that doesn't match what the actual function call returns. That is, on x86_64, it prints `0.40677759702517235496` at -O3 (which uses a constant), but prints `0.40677759702517241047` when `__attribute__((noinline))` is added to the function definition.
The behavior changed with f04e387055e495e3e14570087d68e93593cf2918 (and thus can be changed with `-ffp-contract=fast`).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVstyozoQ_Rq8UZnSA4FYeOFn1dzNLDJ7lwzC6I6McpGI43z9bQns2E5qagjBAnWfPt0tqftg68viR0KLE-qsR27oFfKtdui_QRrdaOWQhBu99vZg1AnpDmnvlGkSukaHwaOgawwy8gITqLbnLsy09lU1gzEX5OxJ2U6hc2vR786eHToo71WPzhrUHKidW-Vb-BAkwXR3RJ1StUPegiiqWtkdVY1sHwimCd4keDk-f7UKNdYYew5KwLlX9VCBbNPbE2pqow8nlFDRev_qErZM6A7uo_btcEgre4KXplfq4OqHkYcR_JykA5owAJjw6gbwbOf6KqFlcBHCMjgwBhHZ6V419h3I9Mj3-mi74EqvK9QMXeW17Ry469tPgkBY9a-97nzQ_-clAhoLbkS51p4hC-qmHpyrpDGgGuZBJcxWtvPq3SPbQIK8ch6-So8aqY27BirH0z2-Uqa7ygy1QglbO19rm7YJ295H1XnpgTmAu5DPAdI-TrTSNKC2QYin-JtLzTEJfsQwLxF-Z7ttnAgf8ftVDOZjJiLmC0ERck7S_OFiNGOZ-AK52q15uEbIOORZ-QhJR0gkUna7KM1EmeWEZgDJnlgKEq4REgZ4J5b5IyS7sSxFRmheCloKXpRZyQgLkNkTS7rEZDk5Tso1ycmGP0JmE0uaFpwXAFPg8IfzIi9ECZD5I8vtuiCbLZscL1Zkty02j5B8YklTjjkuckw5y3AucpaRkgOkeGS55cst3-YjpFjSVbnerh4h84klSbko8xKXJaeEcI4JppSoOcEJW92x3Gz5pgw7LbJcrncbXqwfIMfn_bra73-rvlNm7zTsMDFOofeAMY0vYRz3yiVsvhGnWI0DNF2T7AfIwq5dv8H_GcjdW72Kfky_0TUwtHy_CV5FzvcigLn8-CLS34u80ISuPqKj4oVN4xdYFmFxriLCeZrl19k8OPOM-vZs-Cs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