[Algorithms] Strict Aliasing ( was Re: How to get 3dvector largest coordinate index? )
Brought to you by:
vexxed72
From: Justin S. <jus...@ho...> - 2009-03-05 07:01:53
|
FYI: I believe Mike Acton covered the issue pretty thoroughly here: http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html No explanation of the specialness of char* however. Cheers, Justin. On 05/03/2009, at 5:37 PM, Jeff Russell wrote: > yea i was gonna say, use of unions to reinterpret types is fairly > widespread from what i can tell. if its wrong, a lot of software > could have a lot of trouble with a "correct" compiler. or is this > one of those cases where no compiler does it "right" anyway (at > least by default) because so much existing code would break? > > the performance benefit of strict aliasing seems straightforward and > hard to argue with. but it also seems like there should be > *something* though. reinterpret_cast doesn't do the job either? > seems odd that somehow the char* would be magical... > > jeff > > On Thu, Mar 5, 2009 at 12:23 AM, Matt J <mjo...@gm...> > wrote: > > Well, I guess it depends on what standard you use (so many to choose > from). But like I mentioned before in the latest C99 TC3 standard it > adds a clause that says it is okay to do "type punning" through a > union. Here is an FFMPEG post that discusses it in detail: > > http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2008-December/056962.html > > Finally, even CERT thinks its okay: > > https://www.securecoding.cert.org/confluence/download/attachments/26017980/SD+West+Dangerous+Optimizations.pdf?version=1 > > See page 75. Which was written in 2009. I understand that is an > argument based on an appeal to authority, but the assumption here is > these four guys that wrote this understand good coding practices > better than most. GCC holds C++ code to C99 standards, from what I > read. So.... code that tried to optimize unions would be non- > conforming not to mention break tons of legacy code in C and C++ > > > > if cast-through-union violates the standard, then why are unions in > > the language? isn't storing bytes and interpreting them as different > > types what unions are for? > > No, that's not what unions are for. Unions are there to allow > you to store different type data in the same memory location > at DIFFERENT (nonoverlapping) TIMES. > > Standard-violating aliasing is when you have different type > data in the same memory location at the SAME TIME. > > The standard disallows same-time different-type aliasing, > because disallowing it allows for a lot of optimization > opportunities. This is a GOOD THING -- except programmers > need to learn to write standard-conforming code (which is > also a GOOD THING). > > > > ------------------------------------------------------------------------------ > Open Source Business Conference (OSBC), March 24-25, 2009, San > Francisco, CA > -OSBC tackles the biggest issue in open source: Open Sourcing the > Enterprise > -Strategies to boost innovation and cut costs with open source > participation > -Receive a $600 discount off the registration fee with the source > code: SFAD > http://p.sf.net/sfu/XcvMzF8H > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list > > ------------------------------------------------------------------------------ > Open Source Business Conference (OSBC), March 24-25, 2009, San > Francisco, CA > -OSBC tackles the biggest issue in open source: Open Sourcing the > Enterprise > -Strategies to boost innovation and cut costs with open source > participation > -Receive a $600 discount off the registration fee with the source > code: SFAD > http://p.sf.net/sfu/XcvMzF8H_______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list |