Is it ok to use 64bit integers in a 32bit application?
Asked Answered
C

2

18

I notice in C and C++, we can use int64_t, or simply a long long.

If I compile 32bit code using these types, will I suffer any performance issues on 64bit and/or 32bit machines?

Aside from saving some RAM, would I ever have a reason to just use int?
After all, 64bit ints are far more useful in storing large numbers.

Calia answered 11/3, 2015 at 2:32 Comment(4)
It's fine to use 64-bit integers in a 32-bit application. 32/64-bit code refers to the length of pointers, not the length of normal data types, so it makes no difference to the use of types like int64_t.Cheke
A 64 bit machine will still run 32 bit code in a special 32 bit mode, so the 64 bit instructions won't be available. 32 bits is enough for a range of -2147483648 to 2147483647, many times if you need more than that you'd find a double to be even more useful.Yee
64 bit multiplication is for sure a lot slower than 32bit multiplication equally on both 32 bit and 64 bit machines.Polypus
#5531406Intent
R
18

If I compile 32bit code using these types, will I suffer any performance issues on 64bit and/or 32bit machines?

Your compiler may need to generate several machine code instructions to perform operations on the 64 bit values, slowing down those operations by several times. If that might be a concern, you'd want to do some benchmarking to assess the impact on a particular program with realistic data. That issue exists where you're executing the 32 bit executable on a 32 or 64 bit machine.

would I ever have a reason to just use int?

Aside from performance and memory usage, there's occasionally reason to use ints because other APIs/streams etc. that you work with use int. There's also subtle documentary value in using int if it's clearly adequate, otherwise other programmers may waste time wondering why you'd gone out of your way to use a long long.

After all, 64bit ints are far more useful in storing large numbers.

Far more useful in storing very large numbers - sure - but that's relatively rarely needed. If you're storing something like a year or someone's age, there's just no particular point in having 64 bits.

Renege answered 11/3, 2015 at 2:40 Comment(0)
F
12

If I compile 32bit code using these types, will I suffer any performance issues on 64bit and/or 32bit machines?

64 bit integers on 32 bit architectures require two registers to store the value*. 32 bit values require only one register. This matters because:

  • You may run out of registers sooner, requiring registers to be spilled to memory. This takes more time.
  • Loading and storing 2 registers typically takes two instructions, not one.
  • Doing addition or subtraction on operands in two registers takes two or more instructions versus just one for operands in just one register.

Bottom line is that if 32 bit performance is important, don't use 64 bit integers unless you need them.s

* This is true for x86, ARM, and PowerPC processors, which covers most of the processors people program for these days. It is probably true of most other processors as well.

Freak answered 11/3, 2015 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.