# Why doesn’t Julia fma automatically?

**URL:** https://discourse.julialang.org/t/why-doesn-t-julia-fma-automatically/104982
**Category:** General Usage
**Created:** [October 13, 2023, 3:31am UTC](https://discourse.julialang.org/t/why-doesn-t-julia-fma-automatically/104982 "2023-10-13T03:31:53Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [October 14, 2023, 10:30pm UTC](https://discourse.julialang.org/t/why-doesn-t-julia-fma-automatically/104982/5 "2023-10-14T22:30:24Z")

</div>

I guess this was already discussed lots of times. One relevant thread:

> [@X \* y + z does not automatically use FMA instruction](https://discourse.julialang.org/t/x-y-z-does-not-automatically-use-fma-instruction/100640):
>
> I am new to Julia and was wondering why the code x \* y + z does not use fmadd where all values are floats, but instead fmul followed by fadd. On the other hand, if I use the @fastmath decorator, the @code\_native changes to use the fmadd instruction. My question is, if FMA is faster and more precise, why won’t it use fmadd to begin with on supported machines? Machine: M1 apple silicon on 13.4 Ventura Julia: 1.9.1 Optimization: -O3 and -O2

The TLDR is **because of correctness** , it is assumed that the user uses the exact FP operations that are necessary, and changing the operations in such a way that the result is changed would be incorrect. This is consistent with other optimizations that compilers (not just Julia) do, in general an optimization is allowed if it doesn’t change any observable result.

The thread also gives specific examples of FP code that can break in such cases. Search for “error-free transforms” or “multi-word arithmetics” or “floating-point expansions” for more. An example Julia package that relies on the current behavior is MultiFloats. Many implementations of transcendental math functions would also be broken.

---

_[View the full topic](https://discourse.julialang.org/t/why-doesn-t-julia-fma-automatically/104982)._
