Re: GCC bug? (4.2.4 on Ubuntu 8.04 / AMD64)



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
.



Relevant Pages

  • Ubuntu-modem
    ... They will know your Country's modem code, which may be essential for dialup service. ... Versions adequately match for the compiler installed: ... under Ubuntu linux-libc-dev ... run the following command to display the needed package list: ...
    (Ubuntu)
  • Re: Restore Dapper after Edgy
    ... if it is perhaps you mean sudo aptitude install ... The following packages will be automatically REMOVED: ... if you install the ubuntu desktop package ... (compiler versions, library version etc), so they create software packages ...
    (Ubuntu)
  • Re: Help me.........
    ... going to be a very rough and probably wrong translation. ... Mirza is saying that he installed Ubuntu 6.06 for the first time and is ... He uses C++ and would like to know which is the better compiler and how ... >> Salut mi-am instalat si eu pentru prima data ubuntu 6.06 si de ...
    (Ubuntu)
  • Re: Linux and Open Source... What is still missing?
    ... If I list what ubuntu is missing from developer's viewpoint then ... first would be latest GCC compiler out of box (forget the IDE part. ... build-essential in the default install was proposed by Matt Zimmerman ...
    (Ubuntu)
  • Re: Sory
    ... I want to know what is the best compiler form c++ in ubuntu because I want ... to do same programs and haw to install that?(I install g++ but I don't now ... And the language is Romanian for the romanian people but I sow ...
    (Ubuntu)