Where does the C standard describe overflow of signed integers?
Georg Bauhaus
bauhaus@futureapps.de
Fri Jul 15 13:20:00 GMT 2005
Paul Schlie wrote:
>>From: Robert Dewar <dewar@adacore.com>
>>
>>>Paul Schlie wrote:
>>
>>>I don't contest that it may, I simply don't believe it should.
>>
>>you can't seriously mean that with respect to uninitialized
>>variables. this would mean you could not put local variables in
>>registers. the effect on code quality woul be awful!
>
>
> Why would anyone care about the performance of an access to an
> un-initialized variable? [..] although do
> see substantial value in producing compiled code which is strictly
> consistent with the specified program and native target behavior,
> regardless of its portability.
You can have both, correctness and uninitialised local
variables. For an impression of the difference in performance,
and for a way to ensure correctness, I tried this
(switch register/volatile in the declaration lines in comp
and r to see the effects).
#include <stddef.h>
#include <assert.h>
#define BUFFER_SIZE 1000 // must be > 0
#define ITERATIONS 100000 // must be > 0
static inline int comp(const short, const short, const short);
/* pre: a has elements, that is hi > 0. Frequently called */
int r(short a[], size_t hi)
{
//register int x, y, z;
volatile int x=1, y=2, z=3;
assert(hi > 0);
for (size_t c=0; c < hi + 2; ++c) {
if (a[c]) {
jfssoae:
x = c + 3, y = z = a[c];
if (comp(x, y, z)) z = x - y;
}
}
return x + y + z;
}
static inline int comp(const short x, const short y, const short z)
{
//register int result = (x + y) == (x + z);
volatile int result = (x + y) == (x + z);
return result;
}
int main()
{
short buffer[BUFFER_SIZE];
int result;
assert(ITERATIONS > 0);
for (int runs = 0; runs < ITERATIONS; ++runs) {
result = r(buffer, BUFFER_SIZE);
}
return result;
}
More information about the Gcc
mailing list