tags:

views:

124

answers:

2

I'm trying to implement George Marsaglia's Complementary Multiply-With-Carry algorithm in C. It seems to work great under Win7 64 bit and Linux 32 bit, but seems to behave strangely under Win 7 32 bit. The random number it returns is 32 bit, but there's a temporary value used internally that's supposed to be 64 bits, and it's declared:

unsigned long long t;

I suspect this might be the cause of the misbehaviour, so my question is: is the type "long long" 64 bits? Is it supported in 32 bit Windows? Thanks in advance for an answer.

+5  A: 

If your compiler has stdint.h I would suggest using uint64_t instead.

Trent
+4  A: 

The type long long is guaranteed to be at least 64 bits (although the guarantee is formally in the form of the range of values it must be able to represent).

The following is in §5.2.4.2.1 of the C99 standard (link to draft):

— maximum value for an object of type unsigned long long int

ULLONG_MAX 18446744073709551615 // 2**64 − 1

caf
It's not much use that the size is guaranteed in C99, a standard *which MSVC does not implement*. Here's the MSDN documentation which guarantees that long long is exactly 64 bits on Windows: http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx. For C++, anyway. I'm not sure where MS keeps its C documentation, but it's all the "same compiler".
Steve Jessop
So for MSVC's case, it's a Quality Of Implementation issue. The OP can always check the value of `ULLONG_MAX` to be sure.
caf
It's not exactly a quality of implementation issue. MSVC does not implement C99. AFAIK it has no plans to do so, unlike GCC, so it isn't even a case of "there are a few documented failures to meet the standard". It's a C89 compiler, not a C99 compiler, but it does happen to have long long as an extension that's consistent with C99. The reasons for lack of an update are different, but you could as well say that it's a quality of implementation issue that my wireless router still doesn't do 802.11n, even though the standard was ratified back in July :-)
Steve Jessop
Btw, I'm not sure what versions of MSVC have `ULLONG_MAX`. `sizeof(long long)` is the backup option, given that I think we're confident that no matter what compiler version you're using, Win32 does have an 8 bit byte.
Steve Jessop
I mean it's a QOI issue that if you're going to implement a feature from a later version of the same standard, you should do it in a way that's compatible with that later standard.
caf