https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90106
Bug ID: 90106
Summary: builtin sqrt() ignoring libm's sqrt call result
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: fredericopissarra at gmail dot com
Target Milestone: ---
GCC 7.3 and 8.2 (from Ubuntu 18.04 and MinGW-w64) seems to generate wrong code
for sqrtf() -- and sqrt() -- for multiple platforms (tested under x86-64 and
ARM Arch64, gcc 7.3, and Arch32 with gcc 6.3). Try to compile this simple
function:
/* test.c */
#include <math.h>
float f( float x ) { return sqrtf( x ); }
And I've got, for x86-64 using SSE:
# Compiled with gcc -O2 -S test.c
f:
pxor %xmm2,%xmm2
sqrtss %xmm0,%xmm1
ucomiss %xmm0,%xmm2
ja .L8
movaps %xmm1,%xmm0
ret
.L8:
subq $24,%rsp
movss %xmm1, 12(%rsp) # save xmm1 from sqrtss
call sqrtf@PLT
movss 12(%rsp),%xmm1 # restore xmm1.
addq $24,%rsp
movaps %xmm1,%xmm0 # use xmm1 anyway?!
ret
Notice, when 0 > x sqrt@PLT is called, but the result is ignored.
A similar code is created by GCC for ARM.
As expected, -ffast-math, creates:
f:
sqrtss %xmm0,%xmm0
ret
Which is correct.
My environment:
$ gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
$ gcc-8 --version
gcc-8 (Ubuntu 8.2.0-1ubuntu2~18.04) 8.2.0
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (15:6.3.1+svn253039-1build1) 6.3.1 20170620
$ aarch64-linux-gnu-gcc --version
aarch64-linux-gnu-gcc (Ubuntu/Linaro 7.3.0-27ubuntu1~18.04) 7.3.0
$ uname -srvp
Linux 4.15.0-47-generic #50-Ubuntu SMP Wed Mar 13 10:44:52 UTC 2019 x86_64
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.2 LTS
Release: 18.04
Codename: bionic