Browse thread
Multiplication of matrix in C and OCaml
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | ls-ocaml-developer-2006@m... |
| Subject: | Re: [Caml-list] Multiplication of matrix in C and OCaml |
Jon Harrop <jon@ffconsultancy.com> writes: > On Saturday 10 February 2007 14:41, ls-ocaml-developer-2006@m-e-leypold.de > wrote: >> Just to be sure: Would the compiler be wrong to optimize >> >> c * q > c * k >> >> to just >> >> q > k >> >> (all floats). And why? > > Consider c=0, q=3 and k=2: > > 0 * 3 > 0 * 2 --> false > 3 > 2 --> true > > It is just mathematically incorrect. Damn. I should have taken '+'. I've not been paying attention. My question should rather have been: Is the compiler allowed to make optimizations according to known mathematical laws? >> If not, why, in the case above? > > Floats are not reals, they are just an approximation that happens to be very > useful. Floats do not obey the same laws (e.g. associativity). However, > programmers may be relying upon this fact, e.g. when doing exact float > arithmetic. Apart from my obvious error above, I wonder, how much the compiler is required to keep to that expectations and when. Some examples to illustrate: Recently someone complained to the Gcc team that the compiler optimizes a + 100 < 100 to false in case a is an unsigned int. Actually the lnaguage standard allows it: If the addition flows over, anything is allowed (undefined behaviour) and if not, a+100 < 100 is just like a < 0. The expectation that the overflow must be visible in the language was not justfied in this case. I've been wondering about similar optimizations for floats, but didn't get my examples right. The transformtion done by the compiler would of course be forbidden to increase the error. >> I don't want >> letter and verse, but a general hand waving in the right direction >> would be nice, since I have the impression, that is exactly what Gcc >> 4.1. is currently doing (though for the integer case). > Ints are completely different because they are exact (as modulo integers). So > they do obey the same laws and they do not have special constants (like > infinity, neg_infinity, nan, -0. and so on as floats do). Yes, thanks. I hope I have justfied sufficiently my motiviation to ask my question, about which I should have been thinking just for some more minutes (I admit :-). Still: With a certain Gcc version and flags combination the OP saw a threefold improvement in performance. That in intself is suspicious (I don't think that this much optimization potential was left in Gcc ...) and I still would check for optimization errors in this case. Gcc is not bug free either, so one should test the correctness of the compiled program first and wether it really does the work it is supposed to do. Regards -- Markus