Re: Constant divisions not converted to multiplications
Andrew Pinski via Gcc <[email protected]>
| Newsgroups | gmane.comp.gcc.devel |
|---|---|
| Message-ID | <CALvbMcByh3KBtsmvcVQjEkCcHM3SW1s3aZkXfAgJWHPPeFTgJA@mail.gmail.com> |
On Thu, May 21, 2026 at 6:04 AM Undisclosed via Gcc <[email protected]> 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