6

I'm writing an app, and I am wondering whether I should compile it in 32-bit mode or 64-bit mode. The program doesn't use any 64-bit types and doesn't access over 4 gigabytes of memory (and I don't plan on it ever accessing that much memory). The only advantage that I can see in using 64-bit mode over 32-bit mode is that 64-bit mode allows more registers to be accessed, but there must be some benefit to using 32-bit mode over 64-bit mode when most of the 64-bit features are not being used (other than smaller instructions and pointers, since I am not worried about my program overflowing the cache). Is there some benefit to using 32-bit such as faster instructions or less energy consumption that should lead me to compiling for 32-bit instead. Thank you.

Cpp plus 1
  • 990
  • 8
  • 25

5 Answers5

3

Depends on the application type.

As others mentioned, 32-bit platforms are considered deprecated, and running 32-bit applications on 64-bit platforms imposes some overhead. In addition, as you mentioned, there are more registers accessible to the compiler and more advanced instruction set.

OTOH there are scenarios where code compiled for 32-bit would have a significant advantage, due to the smaller native pointers size.

For instance I have an application with "algorithmic" code, which deals with large data set stored in complex data structures with cross-pointers. The same application compiled for 32-bit platform has significantly smaller memory footprint, and runs considerably faster.

So that if your application doesn't approach the 32-bit limitation, works with complex data structures with native pointers, and the performance is critical - 32-bit may be a way to go. Otherwise I'd recommend switching to 64-bit.

valdo
  • 12,632
  • 2
  • 37
  • 67
  • *the performance is critical - 32-bit may be a way to go* If performance matters, 32-bit x86 only has 8 32-bit registers, of which three are program counter, stack pointer, and frame pointer. If you use the frame pointer as a general-purpose register, you get all the way up to an *entire* six registers. If you run 64-bit, [you can access double the number of registers - 16.](https://en.wikipedia.org/wiki/X86#64-bit) – Andrew Henle Feb 24 '18 at 16:41
  • 1
    @AndrewHenle: oh yes, there are plenty of registers. But performance isn't always bounded by the number of registers. I definitely observed "algorithmic" code that tends to be faster on 32-bit due to less memory I/O. – valdo Feb 24 '18 at 16:51
  • That's why benchmarking matters - and that includes benchmarking hardware, too. I've had brand-new HP servers that weren't any faster running real code than the five-year-old Sun-built x86 servers they replaced. There's more to system performance than CPU specs and how much RAM gets crammed onto the MB. But since purchases tend to be made based on CPU specs and amount of RAM, vendors tend to skimp on things like memory latency and IO bandwidth. – Andrew Henle Feb 24 '18 at 16:57
  • @AndrewHenle: In Linux, a `x32` architecture exists. It is `amd64`, but with 32-bit pointers (in other words, 64-bit architecture, but with smaller pointers). So you get the registers of `amd64`, but the small pointer size of `x86`. – Tim Čas Feb 24 '18 at 19:15
3

You have the register access improvement in 64-bit mode correct, but you may not know how dramatic the difference is.

Per Wikipedia:

Starting with the AMD Opteron processor, the x86 architecture extended the 32-bit registers into 64-bit registers in a way similar to how the 16 to 32-bit extension took place. An R-prefix identifies the 64-bit registers (RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP, RFLAGS, RIP), and eight additional 64-bit general registers (R8-R15) were also introduced in the creation of x86-64. However, these extensions are only usable in 64-bit mode ...

On x86, running in 64-bit mode gives you access to 16 64-bit registers. In 32-bit mode, you can only access 8 32-bit registers.

And since three of the registers in any case are the program counter, stack pointer, and frame pointer, the difference in available general-purpose registers is even more dramatic.

So you effectively go from five or six 32-bit registers to 13 or 14 64-bit registers just by changing your compile flag from -m32 to -m64.

Unless you have specific needs to deploy on older 32-bit hardware or operating systems, there's really no reason to compile a 32-bit x86 application.

Just be careful in porting code to 64-bits. There are still way too many programmers who conflate pointers and ints and longs.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
2

WoW64 on Intel 64 (AMD64 / x64) does not require instruction emulation. In this case the WoW64 subsystem emulates only the 32-bit environment through an additional layer between a 32-bit application, and the 64-bit Windows API. In some places this layer is thin, in others a bit thicker. For an average program, you may expect 2% performance penalty because of this layer. For some programs, it can be larger. Two per cent is not very much, but keep in mind that 32-bit applications work a bit slower under the 64-bit Windows than in the 32-bit environment.

Compilation of 64-bit code does not only allow you to avoid using WoW64, but also gives you an additional performance gain. This is explained by architectural modifications in the microprocessor, such as an increased number of general-purpose registers. For an average program, you may expect a 5-15% performance gain after recompilation alone.

Because of the WoW64 layer, 32-bit programs are less efficient in the 64-bit environment than in their native 32-bit one. However, simple 32-bit applications can still get one benefit of being executed in the 64-bit environment. Maybe you know that a program built with the switch "/LARGEADDRESSAWARE:YES" can allocate up to 3 Gbytes of memory, if a 32-bit Windows is launched with the switch "/3gb". Well, the same 32-bit program built on a 64-bit system can allocate almost 4 Gbytes of memory (in practice it is usually about 3.5 Gbytes).

P.S. See also "Lesson 2. Support of 32-bit applications in the 64-bit Windows environment".

AndreyKarpov
  • 1,083
  • 6
  • 17
1

Use 64 bit because running 32 bit in a 64 bit environment has overhead (albeit small).

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
1

Why not compile it in both?

I recommend 64 bit (AMD 64). There are no advantages of 32 bit (x86) applications other than compatibility for really old computers. I am sure someone more knowledgeable than me can point some technical advantages of AMD 64 over x86, but I suspect those won't make a real difference on your average application.

But nothing is stopping you to compile and distribute both.

bolov
  • 72,283
  • 15
  • 145
  • 224