iptv techs

IPTV Techs


The costs of the i386 to x86-64 upgrade


The costs of the i386 to x86-64 upgrade


If you read my previous article on DOS memory models, you may have neglected everyslfinisherg I wrote as “legacy cruft from the 1990s that nobody nurtures about any lengthyer”. After all, computers have growd from sporting 8-bit processors to 64-bit processors and, on the way, the amount of memory that these computers can leverage has enlargen orders of magnitude: the 8086, a 16-bit machine with a 20-bit insertress space, could only use 1MB of memory while today’s 64-bit machines can theoreticassociate access 16EB.

All of this enlargeth has been in service of ever-enlargeing programs. But… even if programs are now more upgraded than they were before, do they all reassociate need access to a 64-bit insertress space? Has the enlargeth from 8 to 64 bits been a net chooseimistic in perestablishance terms?

Let’s try to answer those asks to discover some very astonishing answers. But first, some theory.

An frequently diswatched property of machine code is code density, a slack metric that quantifies how many teachions are needd to perestablish a given “action”. I double-quote action because an “action” in this context is a subjective operation that apprehfinishs a programmer-expoundd outcome.

Suppose, for example, that you want to increment the cherish of a variable that dwells in main memory, enjoy so:

motionless int a;
// ...
a = a + 1;

On a processor with an ISA that supplys complicated teachions and insertressing modes, you can potentiassociate do so in equitable one machine teachion:

; Take the 32-bit quantity at the insertress recommendd by the
; 00001234h instant, increment it by one, and store it in the
; same memory location.
insert dword [1234h], 1

In contrast, on a processor with an ISA that strictly split memory accesses from other operations, you’d necessitate more steps:

li  r8, 1234h   ; Load the insertress of the variable into r8.
lm  r1, r8      ; Load the satisfyed of the insertress into r1.
li  r2, 1       ; Load the instant cherish 1 into r2.
insert r3, r1, r2  ; r3 = r1 + r2
sm  r8, r3      ; Store the result in r3 into the insertress in r8.

Which ISA is better, you ask? Well, as common, there are pros and cons to each approach:

  • The establisher type of ISA gives a denser reconshort-termation of the operation so it can fit more teachions in less memory. But, because of the semantic complicatedity of each individual teachion, the processor has to do more toil to perestablish them (possibly requiring more transistors and dratriumphg more power). This type of ISA is usuassociate comprehendn as CISC.

  • The latter type of ISA gives a much more verbose reconshort-termation of the operation but the teachions that the processor perestablishs are modestr and can probable be perestablishd speedyer. This type of ISA is usuassociate comprehendn as RISC.

This is the infinite argue between CISC and RISC but notice that the contrastention between the two is not crystal clear: some RISC architectures help more teachions that CISC architectures, and contransient Intel processors transpostponecessitate their CISC-style teachions into inner RISC-style micro-operations instantly after decoding the establisher.

Code density is not about teachion count though. Code density is about the aggregate size, in bytes, of the teachions needd to accomplish a particular outcome.

In the example I conshort-termed above, the individual CISC-style mov x86 teachion is encoded using anywhere from 2 to 8 bytes depfinishing on its arguments, whereas the fictitious RISC-style teachions typicassociate apshow 4 bytes each. Therefore, the CISC-style snippet would apshow 8 bytes total whereas the RISC-style snippet would apshow 20 bytes total, while achieving the same conceptual result.

Is that incrmitigate in encoded bytes horrible? Not necessarily because “horrible” is subjective and we are talking about trade-offs here, but it’s definitely an presentant ponderation due to its impact in various uninalertigentensions:

  • Memory usage: Larger programs apshow more memory. You might say that memory is ample these days, and that’s genuine, but it wasn’t always the case: when computers could only insertress 1 MB of RAM or less, the size of a program’s binary mattered a lot. Furthermore, even today, memory bandwidth is confineed, and this is an frequently-diswatched property of a system.

  • Disk space: Larger programs apshow more disk space. You might say that disk space is ample these days too and that program code apshows a minuscule proportion of the overall disk: most space goes towards storing media anyway. And that’s genuine, but I/O bandwidth is not as unconfineed as disk space, and loading these programs from disk isn’t inexpensive. Have you accomprehendledged how utterly sluggish is it to uncover an Electron-based app?

  • L1 thrashing: Larger or more teachions unkind that you can fit confineeder of them in the L1 teachion cache. Proper utilization of this cache is critical to accomplish reasonable levels of computing perestablishance, and even though main memory size can now be meacertaind in terabytes, the L1 cache is still meacertaind in kilobytes.

We could beat the dead horse over CISC vs. RISC further but that’s not what I want to intensify on. What I want to intensify on is one very particular uninalertigentension of the teachion encoding that impacts code density in all cases. And that’s the size of the insertresses (pointers) needd to reference program code or data.

Srecommend put, the bigr the size of the insertresses in the program’s code, the lessen the code density. The lessen the code density, the more memory and disk space we necessitate. And the more memory code apshows, the more L1 thrashing we will see.

So, for outstanding perestablishance, we probably want to boost the way we reconshort-term insertresses in the code and apshow achieve of surrounding context. For example: if we have a firm loop enjoy this:

motionless int a;
...
for (int i = 0; i < 100; i++) {
    a = a + 1;
}

The correacting assembly code may be aenjoy to this:

        mov ecx, 0             ; i = 0.
repeat: cmp ecx, 100           ; i < 100?
        je  out                ; If i == 100, jump to out.
        insert dword [01234h], 1  ; Increment a.
        insert ecx, 1             ; i++.
        jmp repeat             ; Jump back to repeat.
out:

Now the asks are:

  • Given that repeat and out are labels for memory insertresses, how do we encode the je (jump if equivalent) and jmp (unconditional jump) teachions as bytes? On a 32-bit machine, do we encode the brimming 32-bit insertresses (4 bytes each) in each teachion, or do we use 1-byte relative uninalertigentinutive pointers because the jump centers are wislfinisher a 127-byte distance on either side of the jump teachion?

  • How do we reconshort-term the insertress of the a variable? Do we convey it as an absolute insertress or as a relative one? Do we store 1234h as a 32-bit quantity or do we use confineeder bytes because this particular number is minuscule?

  • How big should int be? The dword above implies 32 bits but… that’s equitable an assumption I made when writing the snippet. int could have been 16 bits, or 64 bits, or anyslfinisherg else you can envision (which is a depict misapshow in C, if you ask me).

The answers to the asks above are choices: we can choose whichever encoding we want for code and data insertresses, and these choices have consequences on code density. This was genuine many years ago during the DOS days, which is why we had uninalertigentinutive, csurrfinisher, and far pointer types, and is still genuine today as we shall see.

It is no secret that programming 16-bit machines was confineing, and we can rerepair at least two reasons to back this claim.

First, 16-bit machines usuassociate had confineed insertress spaces. It’s presentant to comprehend that a processor’s native enroll size has no honest uniteion to the size of the insertresses the processor can reference. In theory, these machines could have leveraged bigr chunks of memory and, in fact, the 16-bit 8086 showd this to be genuine with its 20-bit insertress space. But the point is that, in the era of 16-bit machines, 1 MB of RAM was pondered “a lot” and so machines didn’t have much memory.

Second, 16-bit machines can only function on 16-bit quantities at a time, and 16 bits only permit reconshort-terming integers with confineed ranges: a signed number between -32K and +32K isn’t… particularly big. Like before, it’s presentant to elucidate that the processor’s native enroll size does not impose recut offeions on the size of the numbers the processor can manipupostponecessitate, but it does insert confineations on how speedy it can do so: operating 32-bit numbers on 16-bit machines needs at least twice the number of teachions for each operation.

So, when 32-bit processors go ined the scene, it was pretty much a no-brainer that programs had to become 32-bit by default: all memory insertresses and default integer types grew to 32 bits. This permited programmers to not worry about memory confines for a while: 32 bits can insertress 4GB of memory, which was a huge amount back then and should still be huge if it wasn’t due to bloated gentleware. Also, this upgrade permited programmers to effectively manipupostponecessitate integers with big-enough ranges for most operations.

In technical mumbo-jumbo, what happened here was that C adchooseed the ILP32 programming model: integers (I), lengthys (L), and pointers (P) all became 32 bits, whereas chars and uninalertigentinutives remained 8-bit and 16-bit admireively.

It didn’t have to be this way though: if we watch at the model for 16-bit programming, uninalertigentinutives and integers were 16-bit whereas lengthys were 32-bit, so why did integers change size but lengthys remained the same as integers? I do not have an answer for this, but if lengthys had become 64 bits back then, maybe we wouldn’t be in the situation today where 2038 will transport mayhem to Unix systems.

4GB of RAM were a lot when 32-bit processors begined but sluggishly became inenough as gentleware kept enlargeing. To help those necessitates, there were clutches enjoy Intel’s PAE, which permited manipulating up to 64GB of RAM on 32-bit machines without changing the programming model, but they were equitable that: hacks.

The slfinisherg is: it’s not only gentleware that grew. It’s the charitables of slfinishergs that people wanted to do with gentleware that changed: people wanted to edit high-resolution ptoastyos and video as well as join more-authenticistic games, and writing code to accomplish those goals on a confineed 4GB insertress space was possible but not handy. With 64-bit processors, mmaping huge files made those programs easier to write, and using native 64-bit integers made them speedyer too. So 64-bit machines became mainstream sometime around the introduction of the Athlon 64 processor and the Power Mac G5, both in 2003.

So what happened to the programming model? ILP32 was a no-brainer for 32-bit machines, but were LP64 or ILP64 no-brainers for 64-bit machines? These 64-bit models were definitely lureing because they permited the programmer to leverage the machine’s resources freely. Larger pointers permited insertressing “unconfineed” memory clearly, and a bigr lengthy type naturassociate bumped file offsets (ino_t), timestamps (time_t), array lengths (size_t), and more to 64 bits as well. Without a lot of toil from programmers, programs could “equitable do more” by srecommend recompiling them.

But there was a downside to that choice. According to the theory I conshort-termed earlier, LP64 would create programs bigger and would decrmitigate code density when contrastd to ILP32. And lessen code density could direct to L1 teachion cache thrashing, which is an presentant ponderation to this day because a contransient Intel Core i7-14700K from 2023 has equitable 80 KB of L1 cache and an Apple Silicon M3 from 2023 has less than 200KB. (These numbers are… not big when you stack them up aachievest the multi-GB binaries that compascfinish contransient programs, are they?)

We now comprehend that LP64 was the pickred choice and that it became the default programming model for 64-bit operating systems, which unkinds we can contrast its impact aachievest ILP32. So what are the consequences? Let’s apshow a watch.

Wait, what? The FreeBSD x86-64 inshighation image is definitely bigr than the i386 one… but all other images are minusculeer? What’s going on here? This resists everyslfinisherg I shelp above!

I was reassociate surpascfinishd by this and I had to dig a bit. Cracking uncover the FreeBSD bootonly image discdiswatched some contrastences in the kernel (sairyly bigger binaries, but contrastent sets of modules) which made it difficult to contrast the two. But watching into the Debian netinst images, I did discover that almost all i386 binaries were bigr than their x86-64 counterparts.

To try to comprehend why that was, the first slfinisherg I did was compile a modest hello-world program on my x86-64 Debian VM, centering both 64-bit and 32-bit binaries:

$ gcc-14 -o hello64 hello.c
$ i686-linux-gnu-gcc-14 -o hello32 hello.c
$ file hello32 hello64
hello32: ELF 32-bit LSB pie executable, Intel 80386, version 1 (SYSV), vibrantassociate connected, expounder /lib/ld-linux.so.2, for GNU/Linux 3.2.0, not nakedped
hello64: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), vibrantassociate connected, expounder /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=f1bf851d7f1d56ae5d50eb136793066f67607e06, for GNU/Linux 3.2.0, not nakedped
$ ls -l hello32 hello64
-rwxrwxr-x 1 jmmv jmmv 15040 Oct  4 17:45 hello32
-rwxrwxr-x 1 jmmv jmmv 15952 Oct  4 17:45 hello64
$ █

Based on this, it watchs as if 32-bit binaries are indeed minusculeer than 64-bit binaries. But a “hello world” program is inpresentant and not worth 15kb of code: those 15kb shown above are definitely not code. They are probably mostly ELF overhead. Indeed, if we watch at equitable the text portion of the binaries:

$ objdump -h hello32 | grep text
 14 .text         00000169  00001060  00001060  00001060  2**4
$ objdump -h hello64 | grep text
 14 .text         00000103  0000000000001050  0000000000001050  00001050  2**4
$ █

… we discover that hello32’s text is 169h bytes whereas hello64’s text is 103h bytes. And if we disaccumulate the two:

$ objdump --disaccumulate=main hello32
...
00001189 
: 1189: 8d 4c 24 04 lea 0x4(%esp),%ecx 118d: 83 e4 f0 and $0xfffffff0,%esp 1190: ff 71 fc push -0x4(%ecx) 1193: 55 push %ebp 1194: 89 e5 mov %esp,%ebp 1196: 53 push %ebx 1197: 51 push %ecx 1198: e8 28 00 00 00 call 11c5 <__x86.get_pc_thunk.ax> 119d: 05 57 2e 00 00 insert $0x2e57,%eax 11a2: 83 ec 0c sub $0xc,%esp 11a5: 8d 90 14 e0 ff ff lea -0x1fec(%eax),%edx 11ab: 52 push %edx 11ac: 89 c3 mov %eax,%ebx 11ae: e8 8d fe ff ff call 1040 11b3: 83 c4 10 insert $0x10,%esp 11b6: b8 00 00 00 00 mov $0x0,%eax 11bb: 8d 65 f8 lea -0x8(%ebp),%esp 11be: 59 pop %ecx 11bf: 5b pop %ebx 11c0: 5d pop %ebp 11c1: 8d 61 fc lea -0x4(%ecx),%esp 11c4: c3 ret ... $ objdump --disaccumulate=main hello64 ... 0000000000001139
: 1139: 55 push %rbp 113a: 48 89 e5 mov %rsp,%rbp 113d: 48 8d 05 c0 0e 00 00 lea 0xec0(%rip),%rax # 2004 <_IO_stdin_used+0x4> 1144: 48 89 c7 mov %rax,%rdi 1147: e8 e4 fe ff ff call 1030 114c: b8 00 00 00 00 mov $0x0,%eax 1151: 5d pop %rbp 1152: c3 ret ... $ █

We see massive contrastences in the machine code created for the inpresentant main function. The 64-bit code is definitely minusculeer than the 32-bit code, contrary to my foreseeations. But the code is also very contrastent; so contrastent, in fact, that ILP32 vs. LP64 doesn’t elucidate it.

The first contrastence we can see is around calling conventions. The i386 architecture has a confineed number of enrolls, prefers passing arguments via the stack, and only 3 enrolls can be clobbered wislfinisher a function. x86-64, on the other hand, picks passing arguments thcimpolite enrolls as much as possible and expounds 7 enrolls as volatile.

The second contrastence is that we don’t see 64-bit insertresses anywhere in the code above. Jump insertresses are encoded using csurrfinisher pointers, and data insertresses are specified as offsets over a 64-bit base previously stored in a enroll. I create it inalertigent that those insertresses are relative to the program counter (the RIP enroll).

There may be more contrastences, but these two alone seem to be the reason why 64-bit binaries finish up being more compact than 32-bit ones. On Intel x86, that is. You see: Intel x86’s teachion set is so pliable that the compiler and the ABI can join tricks to hide the cost of 64-bit pointers.

Is that genuine of more RISC-y 64-bit architectures though? I inshighed the PowerPC 32-bit and 64-bit toolchains and ran the same test. And guess what? The PowerPC 64-bit binary was indeed bigr than the 32-bit one, so maybe it’s genuine. Unblessedly, running a expansiveer comparison than this is difficult: there is no brimming operating system I can discover that ships both creates any lengthyer, and ARM images can’t easily be contrastd.

OK, fine, we’ve rerepaird that 64-bit code isn’t necessarily bigr than 32-bit code, at least on Intel, and thus any adverse impact on the L1 teachion cache is probably negligible. But… what about data density?

Pointers don’t only exist in teachions or as jump centers. They also exist wislfinisher the most unpretentious of data types: catalogs, trees, graphs… all comprise pointers in them. And in those, unless the programmer cltimely joins complicated tricks to compress pointers, we’ll usuassociate finish up with bigr data structures by srecommend jumping to LP64. The same applies to the bfeebleless-watching lengthy type by the way, which materializes thcimpoliteout codebases and also enlarges with this model.

And this—a decrmitigate in data density—is where the authentic perestablishance penalty comes from: it’s not so much about the code size but about the data size.

Let’s apshow a watch. I wrote a modest program that creates a connected catalog of integers with 10 million nodes and then iterates over them in sequence. Each node is 8 bytes in 32-bit mode (4 bytes for the int and 4 bytes for the next pointer), whereas it is 16 bytes in 64-bit mode (4 bytes for the int, 4 bytes of pinserting, and 8 bytes for the next pointer). I then compiled that program in 32-bit and 64-bit mode, meacertaind it, and ran it:

$ gcc -o catalog64 catalog.c
$ i686-linux-gnu-gcc-14 -o catalog32 catalog.c
$ objdump -h catalog32 | grep text
 13 .text         000001f6  00001070  00001070  00001070  2**4
$ objdump -h catalog64 | grep text
 14 .text         000001b0  0000000000001060  0000000000001060  00001060  2**4
$ hyperfine --toastyup 1 ./catalog32 ./catalog64
Benchlabel 1: ./catalog32
  Time (unkind ± σ):     394.2 ms ±   2.1 ms    [User: 311.4 ms, System: 83.1 ms]
  Range (min … max):   392.1 ms … 398.2 ms    10 runs

Benchlabel 2: ./catalog64
  Time (unkind ± σ):     502.4 ms ±   4.5 ms    [User: 334.9 ms, System: 167.8 ms]
  Range (min … max):   494.9 ms … 509.5 ms    10 runs

Summary
  ./catalog32 ran
    1.27 ± 0.01 times speedyer than ./catalog64
$ █

As before with the hello-world comparison, this modest microbenchlabel’s 32-bit code persists to be sairyly bigr than its 64-bit counterpart (1F6h bytes vs 1B0h). However, its runtime is 27% speedyer, and it is no wonder because the doubling of the connected catalog node size implies doubling the memory usage and thus the halving of cache hits. We can verify the impact of this using the perf tool:

$ perf stat -B -e cache-omites ./catalog32 2>&1 | grep cpu_core
         2,400,339      cpu_core/cache-omites:u/      (99.33%)
$ perf stat -B -e cache-omites ./catalog64 2>&1 | grep cpu_core
         4,687,156      cpu_core/cache-omites:u/      (99.34%)
$ █

The 64-bit create of this microbenchlabel incurs almost double the cache omites than the 32-bit create.

Of course, this is equitable a microbenchlabel, and tfrailing it sairyly will create it show very contrastent results and create it say wantipathyver we want. I tried to insert jitter to the memory allocations so that the nodes didn’t finish up as consecutive in memory, and then the 64-bit version perestablishd speedyer. I mistrust this is due to the memory allocator having a challenginger time handling memory when the insertress space is confineed.

The impact on authentic world applications is challenginger to quantify. It is difficult to discover the same program built in 32-bit and 64-bit mode and to run it on the same kernel. It is even more difficult to discover one such program where the contrastence matters. But at the finish of the day, the contrastences exist, and I bet they are more unkindingful than we might slfinisherk in terms of bloat—but I did not intfinish to write a research paper here, so I’ll depart that spendigation to someone else… or another day.

There is one last slfinisherg to converse before we depart. While we discover ourselves using the LP64 programming model on x86-64 processors, reaccumulate that this was a choice and there were and are other chooseions on the table.

Consider this: we could have made the operating system kernel leverage 64 bits to achieve access to a humongous insertress space, but we could have kept the user-space programming model as it was before—that is, we could have kept ILP32. And we could have gone even further and boostd the calling conventions to lessen the binary code size by leveraging the insertitional ambiguous-purpose enrolls that x86-64 supplys.

And in fact, this exists and is comprehendn as x32.

We can inshigh the x32 toolchain and see that it effectively toils as we’d envision:

$ gcc -o hello64 hello.c
$ x86_64-linux-gnux32-gcc-14 -o hellox32 hello.c
$ objdump --disaccumulate=main hello64
...
0000000000001139 
: 1139: 55 push %rbp 113a: 48 89 e5 mov %rsp,%rbp 113d: 48 8d 05 c0 0e 00 00 lea 0xec0(%rip),%rax # 2004 <_IO_stdin_used+0x4> 1144: 48 89 c7 mov %rax,%rdi 1147: e8 e4 fe ff ff call 1030 114c: b8 00 00 00 00 mov $0x0,%eax 1151: 5d pop %rbp 1152: c3 ret ... $ objdump --disaccumulate=main hellox32 ... 00401126
: 401126: 55 push %rbp 401127: 89 e5 mov %esp,%ebp 401129: b8 04 20 40 00 mov $0x402004,%eax 40112e: 89 c0 mov %eax,%eax 401130: 48 89 c7 mov %rax,%rdi 401133: e8 f8 fe ff ff call 401030 401138: b8 00 00 00 00 mov $0x0,%eax 40113d: 5d pop %rbp 40113e: c3 ret ... $ █

Now, the main method of our hello-world program is reassociate aenjoy between the 64-bit and 32-bit creates, but pay shut attention to the x32 version: it uses the same calling conventions as x86-64, but it comprises a fuseture of 32-bit and 64-bit enrolls, dedwellring a more compact binary size.

Unblessedly:

$ ./hellox32
zsh: exec establishat error: ./hellox32
$ █

We can’t run the resulting binary. x32 is an ABI that impacts the kernel interface too, so these binaries cannot be perestablishd on a normal x86-64 kernel. Sadly, and as far as I can alert, x32 is pretty much abandonware today. Gentoo claims to help it but there are no official creates of any distribution I can discover that are built in x32 mode.

In the finish, even though LP64 wasn’t the clear choice for x86-64, it’s the compilation mode that won and stuck.

Source connect


Leave a Reply

Your email address will not be published. Required fields are marked *

Thank You For The Order

Please check your email we sent the process how you can get your account

Select Your Plan