Getting intermediate results in 64 bits

T

tnowles00

Hi,

Using GCC on my G4, if I have a calculation like this:

#include <stdint.h>

uint64_t a = 0xffff * 0xffff ;

the result will be clobbered to 32 bits because that's the length of an
integer. I want to keep all the intermediate steps at 64 bits without
peppering my operands with (uint64_t) casts.

It would help if my default sizeof(int) were 8, I guess.
 
W

Walter Roberson

Using GCC on my G4, if I have a calculation like this:
#include <stdint.h>
uint64_t a = 0xffff * 0xffff ;
the result will be clobbered to 32 bits because that's the length of an
integer. I want to keep all the intermediate steps at 64 bits without
peppering my operands with (uint64_t) casts.

uint64_t a = 0xffffULL * 0xffffULL;
It would help if my default sizeof(int) were 8, I guess.

Changing the size of a type for a specific compiler is off topic in
this newsgroup. There are gnu.* newsgroups that might be able to assist
in this matter. (Do not be surprised if it turns out that the closest
you can easily do for your platform is to change the size of a long but
not of an int.)
 
T

Tom St Denis

Hi,

Using GCC on my G4, if I have a calculation like this:

#include <stdint.h>

uint64_t a = 0xffff * 0xffff ;

For the record, that is a 32-bit value :)

The nice way is to cast the value to 64-bit first, e.g.

(uint64_t)0xFFFF * (uint64_t)0xFFFF

Or you can use a modified e.g. 0xFFFFULL but that'll get you into
trouble on MSVC platforms where their modifier is diff.

#ifdef _MSC_VER
#define CONST64(n) n ## ui64
typedef unsigned __int64 ulong64;
#else
#define CONST64(n) n ## ULL
typedef unsigned long long ulong64;
#endif

That works on all the platforms I've tested on [which is quite a bit].

Tom
 
C

Coos Haak

Op 2 Aug 2006 10:07:09 -0700 schreef (e-mail address removed):
Hi,

Using GCC on my G4, if I have a calculation like this:

#include <stdint.h>

uint64_t a = 0xffff * 0xffff ;

the result will be clobbered to 32 bits because that's the length of an
integer. I want to keep all the intermediate steps at 64 bits without
peppering my operands with (uint64_t) casts.

It would help if my default sizeof(int) were 8, I guess.

I don't get it, 0xfffe0001 fits nicely in 32 bits.
Written as 64 bits it is 0x00000000fffe0001
Nothing to worry I think.
 
E

Eric Sosman

Tom St Denis wrote On 08/02/06 13:48,:
For the record, that is a 32-bit value :)

The type of 0xffff is either int or unsigned int,
depending on the platform. If it's int, the multiplication
is carried out in int arithmetic and produces an overflow
unless int has at least 32 value bits plus a sign bit, or
at least 33 bits in all. If it's an unsigned int, the
multiplication is carried out in unsigned int arithmetic
and does not overflow, but produces the 16-bit product
0x0001.

A judicious sprinkling of u or ul or even ull
suffixes might improve matters ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top