tag:blogger.com,1999:blog-1016709139053396535.post8532598157732442121..comments2024-03-18T17:34:35.079+00:00Comments on A Smackerel of Opinion: C ternary operator hackColin Ian Kinghttp://www.blogger.com/profile/06458723239721015750noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-1016709139053396535.post-82413464870507089732012-10-17T11:02:54.029+01:002012-10-17T11:02:54.029+01:00I wonder how relevant that is to newer Intel CPUs?...I wonder how relevant that is to newer Intel CPUs? I need to investigate that.Colin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-30540172338368778882012-10-17T10:18:26.509+01:002012-10-17T10:18:26.509+01:00Linus Torvalds posted an interesting discussion on...Linus Torvalds posted an interesting discussion on the CMOVE instruction here. He appears to be deprecating its use:<br /><br />http://yarchive.net/comp/linux/cmov.htmlAdmin Eyehttps://www.blogger.com/profile/09855557226191452817noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-42787534117719892132012-01-29T14:16:42.728+00:002012-01-29T14:16:42.728+00:00This works fine in other architectures and compile...This works fine in other architectures and compiler?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-77784290315627468322012-01-25T09:43:10.006+00:002012-01-25T09:43:10.006+00:00I agree, I don't want to promote bad code styl...I agree, I don't want to promote bad code style to save a cycle or two. I thought it was an interesting abuse of C really.Colin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-34020390393132951112012-01-25T00:53:52.941+00:002012-01-25T00:53:52.941+00:00I'm sure there are many other things to worry ...I'm sure there are many other things to worry about before you care about optimizing this use-case.<br /><br />Personally I'm all for making the code easily readable. But then I prefer C++ to C.Tim Penheyhttps://launchpad.net/~thumpernoreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-51332397637943903252012-01-21T11:12:08.151+00:002012-01-21T11:12:08.151+00:00In which case, perhaps you should also consider: h...In which case, perhaps you should also consider: http://smackerelofopinion.blogspot.com/2009/07/abusing-c-for-fun.htmlColin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-28434295212908717692012-01-21T11:10:49.032+00:002012-01-21T11:10:49.032+00:00c ? x = v : y = v; produces "error: lvalue re...c ? x = v : y = v; produces "error: lvalue required as left operand of assignment", so I suggest using instead the following:<br /><br />c ? (x = v) : (y = v);<br /><br />or even the more uglier:<br /><br />c ? 1, x = y : 1, y = v;<br /><br />These compile down to a jump and no cmove.Colin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-2278511550660351062012-01-21T07:48:08.420+00:002012-01-21T07:48:08.420+00:00How about just:
c ? x = v : y = v;How about just:<br />c ? x = v : y = v;Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-26958598695049128212012-01-21T03:54:06.899+00:002012-01-21T03:54:06.899+00:00Nice. I might have to add this to my list of evil ...Nice. I might have to add this to my list of evil C interview questions. ;-)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-19786346591616552012012-01-20T21:44:12.219+00:002012-01-20T21:44:12.219+00:00Good observation Kamal. Not sure. I suspect I nee...Good observation Kamal. Not sure. I suspect I need to really see how it works in real world examples. We can learn from this is never make rash statements that one saves cycles until one has used the code in real examples rather than simple tests.Colin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-18501775978897233202012-01-20T19:40:06.505+00:002012-01-20T19:40:06.505+00:00But doesn't the hack version force the compile...But doesn't the hack version force the compiler to use actual memory for x and y, where the normal version would allow it to use registers for those?<br /><br />Is the hack version *really* faster in a real-world-ish test case (where x and y aren't declared as volatile or otherwise forced to land in memory locations?)Kamal Mostafahttp://www.whence.com/kamalnoreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-17248712841532326132012-01-20T18:51:30.624+00:002012-01-20T18:51:30.624+00:00And the resulting assembler is just like the if st...And the resulting assembler is just like the if statement using g++ -O2Colin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-9737128071879753632012-01-20T18:30:07.003+00:002012-01-20T18:30:07.003+00:00In C++ one can do this directly:
( c ? x : y) = v...In C++ one can do this directly:<br /><br />( c ? x : y) = v;Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-31191898956620417342012-01-20T18:24:54.033+00:002012-01-20T18:24:54.033+00:00I'm not going to advocate the use of it. It ju...I'm not going to advocate the use of it. It just was a small C mind doodle really.Colin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-33419476062222681202012-01-20T18:04:56.564+00:002012-01-20T18:04:56.564+00:00This is nice, but I'm often hesitant to design...This is nice, but I'm often hesitant to design around GCC's heuristics.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-83163368046798148672012-01-20T18:02:07.417+00:002012-01-20T18:02:07.417+00:00I did say it was a hack ;-)I did say it was a hack ;-)Colin Ian Kinghttps://www.blogger.com/profile/06458723239721015750noreply@blogger.comtag:blogger.com,1999:blog-1016709139053396535.post-9319085279509108182012-01-20T17:02:05.337+00:002012-01-20T17:02:05.337+00:00This is just awful. Make the compiler smarter and ...This is just awful. Make the compiler smarter and leave this code alone.Miek Giebenhttp://miek.nlnoreply@blogger.com