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



Carlos Moreno <cm_news@xxxxxxxxxxxxxx> wrote in news:c5e5b039-f8cb-4f12-
a8bd-b60d3a9005c5@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:

But perhaps the better question is: how can I print a 128-bit
unsigned int
with cout? (actually, I need to print it with an ostream --- so that
it is
compatible with stringstreams, file streams, etc.) You may have
noticed
that the ugly pointer conversion and all was to get away with printing
a
128-bit value as two chunks of 64-bits (since it is unsigned, and
assuming
a given endianness, I was planning o get away with that).

You can print it in two pieces with simple static_casts to uint64_t.
And the code can be encapsulated in a << operator overload -- see below.

GH


#include <iostream>
#include <iomanip>
using namespace std;

#include <inttypes.h>
typedef unsigned int uint128_t __attribute__((mode(TI)));

ostream& operator<<(ostream& os, const uint128_t& value)
{
ostream::char_type prev_fill = os.fill('0');
ios_base::fmtflags prev_fmt = os.flags(ios::hex);
os << static_cast<uint64_t>(value >> 64);
os << right << setw(16) << static_cast<uint64_t>(value);
os.flags(prev_fmt);
os.fill(prev_fill);
return os;
}

int main()
{
uint128_t v1=0xFFFFFFFFFFFFFFFF, v2=0xFFFFFFFFFFFFFFFF;

cout << "v1 * v2 = " << v1 * v2 << endl;
cout << "v1 + v2 = " << v1 + v2 << endl;

return 0;
}

.