This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Optimisations and undefined behaviour
- From: Vincent Lefevre <vincent+gcc at vinc17 dot org>
- To: Markus Trippelsdorf <markus at trippelsdorf dot de>
- Cc: David Brown <david dot brown at hesbynett dot no>, "[gcc-help]" <gcc-help at gcc dot gnu dot org>
- Date: Sat, 7 Nov 2015 02:09:04 +0100
- Subject: Re: Optimisations and undefined behaviour
- Authentication-results: sourceware.org; auth=none
- References: <563BC190 dot 7080406 at hesbynett dot no> <20151105212301 dot GD325 at x4>
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)