Dan Byström’s Bwain

Blog without an interesting name

Optimizing away II.3

Posted by Dan Byström on January 1, 2009

Oh, the pain, the pain and the embarrassment…

I just came to realize that although a “long” in C# is 64 bits, in C++ it is still 32 bits. In order to get a 64 bit value in MSVC++ you must type either “long long” or “__int64”. I didn’t know that. 😦

This means that although the assembler function I just presented correctly calculates a 64 bit value, it will be truncated to 32 bits because the surrounding C++ function is declared as a long.

This in turn means that for bitmaps larger than 138 x 138 pixels – the correct result cannot be guaranteed. (With 64 bit values, the bitmap can instead be 9724315 x 9724315 pixels in size before an overflow can occur.)

Unfortunately, although I had unit test to verify the correctness of the function, I only tested with small bitmaps.

I have uploaded a new version. Ekeforshus


3 Responses to “Optimizing away II.3”

  1. […] Optimizing away II.3 […]

  2. scaryreasoner said

    “just came to realize that although a “long” in C# is 64 bits, in C++ it is still 32 bits.”

    This is not a C++ (or C) thing. C (and therefore C++) doesn’t define exactly how big a long (or an int) is. For example, on Windows, a long is 32 bits whether running on x86, or on x86_64 — it’s the same on both. But, on linux, a long is 32 bits on x86, but 64 bits on x86_64.

    If you want a particular size, include inttypes.h, or stdint.h (see the man page — oh yeah, windows doesn’t even have man pages — google it to see which suits you best) then use, e.g:



    I think __int64 isn’t standard.

    • danbystrom said

      Yes of course. It is not even an OS thing – it’s a compiler manufacturer thing. When I typed C++ I though that maybe I should be explicit and write MSVC++, but then I thought I was pretty obvious that that’s what I’m using. Thanks for making this completely clear – I have updated the post accordingly. In MSVC++ 1.52 an “int” was 16 bits and a “long” was 32 bits, but in the next version, 4.0, an “int” was changed to 32 bits. Somehow I just thought that a “long” would have been “modernized” during all those years… Here’s the whole truth for MSVC++: http://msdn.microsoft.com/en-us/library/s3f49ktz(VS.80).aspx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: