Re: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- From: Måns Rullgård <mans@xxxxxxxxx>
- Date: Tue, 25 Nov 2008 19:02:23 +0000
Carlos Moreno <cm_news@xxxxxxxxxxxxxx> writes:
Hello group,
As the subject line indicates, the environment is: gcc version
4.2.4 on Ubuntu 8.04LTS (with all the latest updates), running
on an AMD64 machine (the version installed is the 64-bit
version of Ubuntu, which implies the 64-bit version of the
compiler)
This is the alleged bug: the following program produces
incorrect output when compiled with -O2, and correct output
when compiled with -O1 or below (i.e., when compiled with
either -O1, -O, or no compiler switches at all):
---- BEGIN COPY-AND-PASTED CODE ----
#include <iostream>
using namespace std;
#include <inttypes.h>
typedef unsigned int uint128_t __attribute__((mode(TI)));
int main()
{
const int N = 1;
uint128_t v1=0xFFFFFFFFFFFFFFFF, v2=0xFFFFFFFFFFFFFFFF;
const uint128_t p = static_cast<uint128_t>(v1) * v2;
cout << hex << *(reinterpret_cast<const uint64_t *>(&p)) << *
(reinterpret_cast<const uint64_t *>(&p) + 1) << endl;
return 0;
}
---- END COPY-AND-PASTED CODE ----
The correct output is: 1fffffffffffffffe
And that's what it outputs with -O1 or below. When compiled with
-O2, the output is: 4009400
I seem to recall that any use of reinterpret_cast by definition
invokes undefined behaviour (I might be wrong on that --- when you
think about it, such thing would be a genuine atrocity)... But
still, at the very least, it would be a QOI issue, no?
The C++ encryption might change things, but in C it is invalid to
access something through the "wrong" pointer type. In your case, you
are accessing a uint128_t using a uint64_t pointer, which violates
strict aliasing rules. If you compile this with -Wall, do you get a
warning about that? Does compiling with -fno-strict-aliasing produce
the expected result?
BTW, you shouldn't be defining symbols (typedefs or normal symbols)
ending with _t. That part of the namespace is reserved if any POSIX
header (e.g. inttypes.h) is included.
--
Måns Rullgård
mans@xxxxxxxxx
.
- Follow-Ups:
- Re: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- From: malc
- Re: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- From: Gil Hamilton
- Re: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- References:
- GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- From: Carlos Moreno
- GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- Prev by Date: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- Next by Date: Re: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- Previous by thread: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- Next by thread: Re: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)
- Index(es):
Relevant Pages
|