1

In some other languages, you can allow the compiler to perform some floating point optimizations, one of which is rewriting your code into algebraically equivalent expressions. Here are some examples of what I mean:

a = a * a * a * a * a * a * a * a * a * a * a * a * a * a * a * a;

could be optimized as

a *= a;
a *= a;
a *= a;
a *= a;

...

b /= 0.123456789f;
b /= 17;

could be optimized as

// b *= 1f / (0.123456789f * 17)
b *= 0.47647059257117651004476506493677f;

Obviously, compilers do not do this by default because the optimized expressions will produce different results, I thought there would be an attribute you can assign to a method or a class to say you are allowing these optimizations to take place, but I couldn't find anything.

Petrusion
  • 940
  • 4
  • 11
  • 2
    C# doesn't have anything like that, which will algebrize for you, very few languages can fully algebrize an equation. But a lot of optimizations do happen anyway, such as constant folding (your second one), reordering, loop unrolling. The CPU microcode also has a lot of optimizations, such as concurrent execution (eg your first one isn't actually that optimal because of result dependencies, the original will prob be faster because the CPU can execute concurrently). – Charlieface May 13 '21 at 23:10
  • You can take a look at this [FastMath](https://github.com/wiu-wiu/FastMath) or [FasterMath](https://github.com/Lokad/FasterMath) or [FastMath](https://github.com/zix99/fastmath) for example in case that helps if you are looking for speed optimizations. –  May 14 '21 at 03:47

0 Answers0