GCC Newspaper
JUNE 15, 2026
gcc Proposed

Constant divisions are not converted to multiplications with -O3

GCC with -O3 doesn't always turn divisions by constants into multiplications; -freciprocal-math enables this optimization at the cost of precision.

A user found that GCC 14.2 with optimization level -O3 doesn’t convert divisions by constants into multiplications, leading to slower code. This behavior was confirmed on GCC 16.1.1. The -freciprocal-math option enables this optimization, though it’s off by default due to potential precision loss. Enabling unsafe math optimizations also turns on this optimization.

In the Thread 4 participants
  1. Undisclosed <1007140@aruba.it> proposer

    Reports that GCC 14.2 doesn't convert divisions by constants into multiplications, even with -O3, and expresses surprise.

    “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.”
  2. Matteo Nicoli <matteo.nicoli001@gmail.com> other

    Confirms the behavior on GCC 16.1.1.

    “On GCC 16.1.1 I observe the same behavior (except from the fact that it uses the AVX instruction vdivss).”
  3. LIU Hao <lh_mouse@126.com> contributor

    States that the `-freciprocal-math` option enables the optimization, but it's off by default due to potential precision loss. Mentions `-funsafe-math-optimizations` or `-ffast-math` also enables it.

    “The option which enables such optimization is `-freciprocal-math`: 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`.”
  4. Andrew Pinski <andrew.pinski@oss.qualcomm.com> other

    Quotes the original poster's surprise at the lack of constant division optimization by default.

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

Technical Tradeoffs

  • Performance gain from using multiplication instead of division.
  • Potential loss of precision in floating-point calculations when using reciprocal math.
  • Default behavior prioritizes precision over performance.

In Details

GCC's middle-end optimization pass doesn't always transform division by constants into multiplication, even with -O3. This is controlled by -freciprocal-math, which is disabled by default for floating-point precision reasons. Compiler writers outside the FP optimization team might not immediately be aware of the exact circumstances where this optimization is skipped.

For Context

Compilers optimize code to improve performance. One common optimization is replacing division by a constant number with multiplication by its reciprocal, which is faster. However, this transformation can change the result slightly due to how computers represent numbers. GCC, a widely-used compiler, has an option to enable this optimization (-freciprocal-math), but it's turned off by default to ensure the most accurate results. This means that even with optimizations enabled, the compiler might still use division instructions in certain cases. The choice depends on a tradeoff between speed and precision.

Filed Under: gccoptimizationfloating-pointperformanceprecision