public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Constant divisions not converted to multiplications
@ 2026-05-21 13:03 Undisclosed
  2026-05-21 13:49 ` Matteo Nicoli
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Undisclosed @ 2026-05-21 13:03 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 751 bytes --]

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Constant divisions not converted to multiplications
  2026-05-21 13:03 Constant divisions not converted to multiplications Undisclosed
@ 2026-05-21 13:49 ` Matteo Nicoli
  2026-05-21 14:06   ` LIU Hao
  2026-05-22  1:19 ` Andrew Pinski
  2026-05-22 19:48 ` Sebastian Galindo
  2 siblings, 1 reply; 6+ messages in thread
From: Matteo Nicoli @ 2026-05-21 13:49 UTC (permalink / raw)
  To: gcc, 1007140

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Constant divisions not converted to multiplications
  2026-05-21 13:49 ` Matteo Nicoli
@ 2026-05-21 14:06   ` LIU Hao
  0 siblings, 0 replies; 6+ messages in thread
From: LIU Hao @ 2026-05-21 14:06 UTC (permalink / raw)
  To: Matteo Nicoli, gcc, 1007140


[-- Attachment #1.1: Type: text/plain, Size: 448 bytes --]

在 2026-5-21 21:49, Matteo Nicoli via Gcc 写道:
> On GCC 16.1.1 I observe the same behavior (except from the fact that it uses the AVX instruction vdivss).

The option which enables such optimization is `-freciprocal-math`: https://gcc.godbolt.org/z/YovYMMdbj

It's off by default as it may result in precision loss. It can also be enabled indirectly with 
`-funsafe-math-optimizations` or `-ffast-math`.


-- 
Best regards,
LIU Hao

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Constant divisions not converted to multiplications
  2026-05-21 13:03 Constant divisions not converted to multiplications Undisclosed
  2026-05-21 13:49 ` Matteo Nicoli
@ 2026-05-22  1:19 ` Andrew Pinski
  2026-05-22  6:33   ` Jonathan Wakely
  2026-05-22 19:48 ` Sebastian Galindo
  2 siblings, 1 reply; 6+ messages in thread
From: Andrew Pinski @ 2026-05-22  1:19 UTC (permalink / raw)
  To: Undisclosed; +Cc: gcc

On Thu, May 21, 2026 at 6:04 AM Undisclosed via Gcc <gcc@gcc.gnu.org> 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 ?

0.1 is not exactly representable in floating point types. This is why
it is not converted by default.
If you had used -ffast-math it will be converted but I am not sure you
want that option in general.
Note 0.5 is exactly representable in floating point types so GCC
already converts `/2.0` into `*0.5`. This is true of many power of 2s.

basically if `1/value` is exactly representable in floating point GCC
will convert it into a multiply. IIRC this is only true for powers of
2.
Because of the way floating point is represented. as 1.xxxE-N .

Thanks,
Andrea

>
> Thx

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Constant divisions not converted to multiplications
  2026-05-22  1:19 ` Andrew Pinski
@ 2026-05-22  6:33   ` Jonathan Wakely
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2026-05-22  6:33 UTC (permalink / raw)
  To: Undisclosed; +Cc: gcc, Andrew Pinski

[-- Attachment #1: Type: text/plain, Size: 1222 bytes --]

On Fri, 22 May 2026, 02:20 Andrew Pinski via Gcc, <gcc@gcc.gnu.org> wrote:

> On Thu, May 21, 2026 at 6:04 AM Undisclosed via Gcc <gcc@gcc.gnu.org>
> 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 ?
>
> 0.1 is not exactly representable in floating point types. This is why
> it is not converted by default.
>

Consider:

(0.1f / 10.0f) == (0.1f * 0.1f)

This is false, they are not equal.



>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Constant divisions not converted to multiplications
  2026-05-21 13:03 Constant divisions not converted to multiplications Undisclosed
  2026-05-21 13:49 ` Matteo Nicoli
  2026-05-22  1:19 ` Andrew Pinski
@ 2026-05-22 19:48 ` Sebastian Galindo
  2 siblings, 0 replies; 6+ messages in thread
From: Sebastian Galindo @ 2026-05-22 19:48 UTC (permalink / raw)
  To: Undisclosed, gcc

Hi,

Besides the fact that you can enable such optimization...

> 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.

This article look at this specific case: 
https://doi.org/10.1145/103162.103163 (Page 29, section 3.2.3), it's old 
but describe some interesting things about float numbers.
Therefore, in this example is better to use x = a / 10.f :-)

Best,
Sebastian


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-05-22 19:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-21 13:03 Constant divisions not converted to multiplications Undisclosed
2026-05-21 13:49 ` Matteo Nicoli
2026-05-21 14:06   ` LIU Hao
2026-05-22  1:19 ` Andrew Pinski
2026-05-22  6:33   ` Jonathan Wakely
2026-05-22 19:48 ` Sebastian Galindo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).