Re: [math-fun] min(x, y) and sorting without if()
From: Joerg Arndt <arndt@jjj.de>
These (branch-avoiding) techniques are "well known", see (fxtbook) section 1.11 "Avoiding branches", pp.15ff where a warning of the form "Your compiler may be smarter than you thought" is given.
Shouldn't the warning be more Nuddsian? In C, such expressions are either unnecessary or UB, pure and simple. Your compiler doesn't have to show smart behaviour at all - it may kill your cat if it wants to. Phil (with comp.{lang,std}.c hat on)
* Phil Carmody <thefatphil@yahoo.co.uk> [Jan 07. 2012 11:41]:
From: Joerg Arndt <arndt@jjj.de>
These (branch-avoiding) techniques are "well known", see (fxtbook) section 1.11 "Avoiding branches", pp.15ff where a warning of the form "Your compiler may be smarter than you thought" is given.
Shouldn't the warning be more Nuddsian?
I do not parse "Nuddsian" here.
In C, such expressions are either unnecessary or UB, pure and simple.
Reading UB as "useless b*ll****": There are a couple of tricks that prove useful in practice. E.g., replacing if ( (a<b) || (c<d) ) { ... } by if ( (a<b) | (c<d) ) { ... } can be an improvement. One that I use regularly: Replace if ( (a<0) || (a>=n) ) { ... } by if ( (unsigned)a>=n ) { ... } Yes, there are assumptions made, but I am not aware of any _existing_ system where these do no hold.
Your compiler doesn't have to show smart behaviour at all - it may kill your cat if it wants to.
Haven't spotted the part about cat slaughter in the standard yet :-) And quite a few people work extremely hard to make compilers smart.
Phil (with comp.{lang,std}.c hat on)
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
participants (2)
-
Joerg Arndt -
Phil Carmody