Is it possible to use 64-bit integers in a 32-bit application?

I notice that in C and C ++ we can use int64_t or just long long .

If I compile 32-bit code using these types, will I experience performance problems on 64-bit and / or 32-bit machines?

Besides saving memory, would I ever have a reason to just use int ?
In the end, 64-bit ints are much more useful to store large numbers.

+6
source share
2 answers

If I compile 32-bit code using these types, will I experience performance problems on 64-bit and / or 32-bit machines?

For the compiler, you may need to generate several machine code instructions to perform operations on 64-bit values, which somewhat slows down these operations. If this could be a problem, you would like to conduct a comparative analysis to assess the impact of a particular program on realistic data. This problem exists where you are executing a 32-bit executable on a 32- or 64-bit machine.

Would I ever have a reason to just use int ?

In addition to performance and memory usage, sometimes it becomes necessary to use int , because other APIs / threads, etc. that you work with using int . There is also subtle documentary value when using int , if it is explicitly adequate, otherwise other programmers may lose time wondering why you have avoided using long long .

In the end, 64-bit ints are much more useful to store large numbers.

It is much more useful to store very large numbers - of course - but this is relatively rarely necessary. If you store something like a year or someone of age, it just doesn't make much sense to have 64 bits.

+8
source

If I compile 32-bit code using these types, will I experience performance problems on 64-bit and / or 32-bit machines?

For 64-bit integers on 32-bit architectures, two registers are required to store the value *. For 32-bit values, only one register is required. This is important because:

  • You may run out of registrar earlier, requiring that the registers be skipped in memory. It takes more time.
  • Loading and saving 2 registers usually takes two instructions, not one.
  • Performing addition or subtraction on operands in two registers takes two or more instructions in comparison with one for operands in only one register.

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

* This is true for x86, ARM, and PowerPC processors, which cover most of the processors that people program these days. This probably applies to most other processors.

+5
source

Source: https://habr.com/ru/post/983623/


All Articles