<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/101946>101946</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang][armv8] Lack of optimisation in floating point constant
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
VirtualTim
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/KG9eETjWG
Example code:
```cpp
float getNum() {
return ((0.299f * 222.0f) + (0.587f * 96.0f) + (0.114f * 117.0f));
}
double getNum2() {
return ((0.299 * 222) + (0.587 * 96) + (0.114 * 117));
}
```
Compiling with the following: `-O3 -std=c++11` generates this:
**armv7-a (trunk)**
```asm
getNum():
ldr r0, .LCPI0_0
bx lr
.LCPI0_0:
.long 1124602216
getNum2():
ldr r0, .LCPI1_0
ldr r1, .LCPI1_1
bx lr
.LCPI1_0:
.long 240518168
.LCPI1_1:
.long 1080099373
```
**armv8-a (trunk)**
```asm
getNum():
mov w8, #4456
movk w8, #17160, lsl #16
fmov s0, w8
ret
.LCPI1_0:
.xword 0x4061022d0e560418
getNum2():
adrp x8, .LCPI1_0
ldr d0, [x8, :lo12:.LCPI1_0]
ret
```
Notice how in armv7 the float is stored as a constant precomputed `.long`, and the double is stored as 2x precomputed `.long`s.
In armv8 the double is now stored as a precomputed `.xword`, but the float is instead calculated at runtime.
For reference, this is how gcc (ARM GCC trunk) compiles this:
```asm
getNum():
vldr.32 s0, .L3
bx lr
.L3:
.word 1124602216
getNum2():
vldr.64 d0, .L6
bx lr
.L6:
.word 240518168
.word 1080099373
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyslV2P4yYXxz8NuTkaCw5-vchFMtmMVs8826patZcVtrHDDjYW4EnaT18ZJ5OXSVa9qIRmYv6cw4_zAsI51fZSLkmyJslmIUa_M3b5u7J-FPq76halqf9a7rwfHOErgluC29bUpdE-M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