On Tue, Aug 27, 2024 at 12:04:12AM +0200, Kai Song wrote: > I would like to add an example so as to clarify what it is that I seek help > on: > > I have a custom implementation of a class called Number. Number has > overloaded operators that write down a program of all computations that > Number witnesses in an original program. > For example, when the original program reads (original code): > z=x-(-y); > > Then upon execution with type Number the following program will be produced > (generated code) as caused through stream buffers in Number: > tmp1 = -y; > z = x-tmp1; > > This generated code is inferior to as if the original code had been > "z=x+y;". The compiler does exactly this always, *without* -ffast-math as well, since it produces exactly the same results (at least with IEEE float). > > Oh dear. > > > > > In particular, I have a type Number that is used in a code > > > > -ffast-math means you are not dealing with numbers at all, but instead > > with quantities somewhat akin to numbers, and you don't give two hoots > > about what happens with them. > > Well if it was able to "deal" with anything akin to numbers, that would > imply I can tell it to treat a type as if it was a number. > Clearly however, that is not the case. So you may have meant something > else, which is impossible to identify then due to ambiguity. > This is unfortunate because I deeply care. So if you indeed mean that > fastmath can be applied to anything akin to numbers, > I am looking forward to how I can use the compilers abilities in that > regard to transform algebraic expressions with custom types. I mean that -ffast-math is only suitable for an audience that does not care much that the code they write does what they wrote. It turns on all kinds of stuff that it hopes result in faster code, albeit code that might compute something totally different. Like, -funsafe-math-optimizations. It is called that because it i *unsafe*, not because it is "fun"! And the worst of course is -ffinite-math-only. Even people who do not know about it, or think they care about it, use infinities and NaNs. Heck, *especially* those people use these things! Such people are their /raison d'être/, in large part! > Well apparently I cannot, as per my question, since the algebraic > expressions are involving my custom class Number. -ffast-math is not about algebraic simplifications. Algebraic simplifications are done always (at -O2, that is). The things that -ffast-math enables are transformations that assume your data is "well-behaved" in some way, and transformations that are like "oh we're sure the user does not *actually* care about the last ten bits of precision (although the code he wrote does literally ask for those). -ffast-math can be useful, for sure, but there is a big fat reason it is not the default -- it does NOT DO WHAT YOU WANT, in many cases -- so anyone telling you to always use it (inclusing using -Ofast at all) is giving you bad advice. > > Or, you can admit you think arithmetic is magic. It is fine to use > > then, too. > > I am a mathematician. And so am I. I focus on mostly algebra, btw (not much related to what you call "algebra" here, heh) :-) > I do not know nor wonder what you seek to say. > I do care that arithmetic expressions are concise and equivalent. > For instance, I like when "z=x-(-y);" is replaced by "z=x+y;" where x and y > may be of type Number. A program is not a set of mathematical equations. For that, you want a computer algebra system, like Maple or GAP or whatever. A program is a set of instructions: "do this, and then that". Optimisations are transformations that result in a different (hopefully faster performing) program that does exactly the same. -ffast-math is not an optimisation, since it *does* change the semantics of your program: to something unexpected often, with harmless differences in many cases, but with completely unacceptable differences in many other cases. Segher