This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimisations and undefined behaviour


On 2015-11-05 22:23:01 +0100, Markus Trippelsdorf wrote:
> On 2015.11.05 at 21:52 +0100, David Brown wrote:
> > There has been some discussions going on in the comp.lang.c newsgroup 
> > about how far compilers are allowed to go regarding optimisation using 
> > their knowledge of undefined behaviour (i.e., if a piece of code has 
> > undefined behaviour, the compiler can assume that the user does not care 
> > about the result in that case, and can therefore do whatever it wants in 
> > order to generate faster code).
> 
> The compiler just assumes that undefined behavior will not happen and
> optimizes accordingly.

That's undefined by the C standard and this is useful for some
optimizations, but it would be great if GCC could, as an extension,
provide builtins with some well-defined behavior in cases that would
be UB by the C standard.

Let me give an example. I have a code that must run as fast as possible.
There is a 64-bit integer division. A division by 0 is possible, but
occurs with a very low probability (something like 1/2^64, I assume).
When it occurs, anything that would yield an abnormal termination is
acceptable (no other behavior is acceptable). I could do a test, but
this would slow down the code, while the x86 division instruction
generates a trap in case of division by 0, so that such an instruction
could be used alone (with no associated test). So, such a builtin would
be very useful.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]