Re: Constant divisions not converted to multiplications

Matteo Nicoli via Gcc <[email protected]>
Newsgroups gmane.comp.gcc.devel
Message-ID <[email protected]>
On GCC 16.1.1 I observe the same behavior (except from the fact that it uses the 
AVX instruction vdivss).

Here's my GCC configuration:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/16/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap 
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,cobol,algol68,lto 
--prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info 
--with-bugurl=https://bugzilla.redhat.com/ --enable-shared 
--enable-threads=posix --enable-checking=release --enable-multilib 
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions 
--enable-gnu-unique-object --enable-linker-build-id 
--with-gcc-major-version-only --enable-libstdcxx-backtrace 
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu 
--enable-plugin --enable-initfini-array 
--with-isl=/builddir/build/BUILD/gcc-16.1.1-build/gcc-16.1.1-20260515/obj-x86_64-redhat-linux/isl-install 
--enable-offload-targets=nvptx-none,amdgcn-amdhsa --enable-offload-defaulted 
--without-cuda-driver --enable-gnu-indirect-function --enable-cet 
--with-tune=generic --with-tls=gnu2 --with-arch_32=i686 
--build=x86_64-redhat-linux --with-build-config=bootstrap-lto 
--enable-link-serialization=1 --disable-libssp
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.1.1 20260515 (Red Hat 16.1.1-2) (GCC)


Here's the output of objdump -D:

00000000004004a0 <Func>:
   4004a0:       c5 fa 5e 05 cc 0c 00    vdivss 0xccc(%rip),%xmm0,%xmm0        # 
401174 <_IO_stdin_used+0x4>
   4004a7:       00
   4004a8:       c3                      ret


Matteo



On 21/05/2026 15:03, Undisclosed via Gcc wrote:
> Hi, I am using gcc 14.2 on windows (mingw64).
> I always gave for granted that any compiler would automatically turn constant divisions into multiplications. Thus, I never cared of writing i.e
> float x = a * 0.1f
> but I always wrote
> float x = a / 10.f
> trusting the compiler.
> Now I decided to inspect the asm, to be 100% sure, cos I was detecting abnormal cpu consumption in some time-critical code, and I had a very bad surprise !!!
>
> Here is how:
>
> float Func(float x)
> {
>    return x / 10.f;
> }
>
> gets resolved:
>
> divss .LC12(%rip), %xmm0 !!!
>
> This with:
>
> -m64 -march=x86-64-v3 -O3
>
> Does it make any sense ? Why doesn't it convert the division by 10 to a multiplication by 0.1 ?? Has one to enable some specific option for that ?
>
> Thx
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.