Re: [Algorithms] How to get 3dvector largest coordinate index?
Brought to you by:
vexxed72
From: Philip T. <ph...@za...> - 2009-03-06 14:16:34
|
Gino van den Bergen wrote: > Yes, I know. Call me reckless but I just do not believe that > strict-aliasing on union fields will be exploited by a C++ compiler any > time soon. It will break too much existing code (not only mine). Exploiting strict aliasing for normal non-union variables breaks a lot of existing code too, but GCC does it by default anyway, presumably because the performance benefits are significant and because it can give adequate warnings for broken code. It still seems unlikely that a compiler would break code using union, because the cost/benefit tradeoff isn't as worthwhile as for general pointer strict-aliasing (it probably wouldn't help performance in many programs, and it would probably be hard to emit warnings without lots of false positives or negatives). But what reasons are there to intentionally *not* write code that unambiguously respects the aliasing rules? In the float-to-int case, instead of writing "i = *(int*)&f" you can simply write "memcpy(&i, &f, 4)", which (as far as I can tell) is perfectly valid in terms of aliasing, and usually just as efficient (sometimes a little more, sometimes a little less) as using *(int*)& or union, since the function call is entirely optimised away by common compilers. And then there's no need to continue arguing over the safety of union :-) (And if you really care about performance in the float-to-int code then you'll use whatever compiler-specific tricks give the right result, and be careful to test its correctness and performance whenever you change compiler or compiler options, and none of the standards-related discussion matters.) -- Philip Taylor ph...@za... |