3

I just wrote some inlined maths utility functions and, while testing, noticed that

let quat = (3, 0, 0, 3)
(quat |||~||| quat)
|> ijk
|> printAn

is completely folded into printAn (18, 0, 0) while starting with (3.0f, 0.0f, 0.0f, 3.0f) only inserts the original 0.0f and 3.0f constant values without evaluating any of the float operators and with some Tuples being instantiated (but not nearly as many as in debug mode).

(I use Visual Studio 2013 Update 4 targeting the 4.5 framework, with the default Release configuration. Undefining the TRACE constant doesn't seem to make a difference and neither does open Unchecked.)


I'm not too concerned with the performance implications since it should be easy to fold the expression through IL weaving if it becomes an issue, but I'm nonetheless interested in what exactly the compiler can constant-fold.

Tamschi
  • 1,089
  • 7
  • 23
  • It would be good to add the definitions of the various functions here. – John Palmer Dec 01 '14 at 04:07
  • @JohnPalmer It's a huge chunk of code with little relevance as far as I can tell, I just linked it in case someone is curious. As everything else is the same the difference must be in how the compiler (doesn't) fold basic `Float32` operators. – Tamschi Dec 01 '14 at 06:05
  • @JohnPalmer ...though it may be a good idea to create a short example to illustrate it a bit more clearly. Maybe I'll do that later today. – Tamschi Dec 01 '14 at 06:07
  • 1
    I don't think the language spec guarantees _any_ folding (it's just an optimization after all), and the compiler itself is an open-source, moving target, so the answer could change at any time. I.e., I think this question is unanswerable in general, as even if there's an answer for the current compiler bits, that answer could change at any point in the future. – ildjarn Dec 01 '14 at 07:14
  • @ildjarn I had worried that may be the case... I'll check if I can find information about it in the source. – Tamschi Dec 01 '14 at 07:54
  • 1
    I do believe though that in general floating point folding is in a worse state than for integers - I remember a relatively recent pull request for eliminating `x * 1.0` and `x/1.0` - which turned out to be surprisingly complicated. – John Palmer Dec 01 '14 at 21:18

0 Answers0