21 ms·
Big-Endian “is effectively dead”
- tempodox 12y agoI'm not much into low-level CPU architectures, so I have to ask: Is there a technical reason (performance or whatever) why LE would be objectively better than BE? I do read hex dumps & disassemblies, and there LE is a huge pain in the backend. Everything is in reverse. Why would a sane programmer voluntarily use LE? God Save the Network Byte Order!
- kahirsch 12y agoNo, it's just that Intel won, mostly because of IBM choosing it.
- mungoman2 12y agoI won't give you a technical reason, but rather respond to the second part about LE and sanity. Actually LE is the natural order and the question is why anybody sane would prefer BE. The reason you think otherwise is because you view hexdumps the wrong way. A hexdump shows hexadecimal bytes. Isolate one byte and number the bits -- 76543210, right to left. Number the nibbles -- 10, right to left. Now number the bytes -- 0123..., left to right? Display your hexdumps with addresses increasing right to left and LE makes perfect sense. This is how numbers are written, so the convention should be followed when displaying numbers.
- phkahler 12y agoWhy on earth would anyone number the bytes right to left? Do you want strings to appear backward as well? But only the portion of a string on one line, the fragments will still be in correct order. Or do you advocate numbering from bottom to top too?
- mungoman2 12y agoI am talking about the view of the hex data. That view is about showing numbers, and the common complaint about LE is that numbers are reversed, but this is only the case when you break the convention of displaying numbers right to left. For showing strings it's better to use left to right, as that's the convention for text.
- mreiland 12y agomost hex editors will show you the ascii value for hex that falls within the range of ascii, which can be useful to humans.
- SFjulie1 12y agoMoney. When Motorola 68000 was out, it was doing 32bits linear addressing. The mov operation would load directly from the memory by using a direct addressing. Wiring 32 wires is expensive. Wiring 2 overlaping address bus of 16 bits partially ovelapping was less. Intel CPU ASM works big endian because we all prefer to write AND 0xFF, DSI to get the 8 lower bit of a number. The first register would hit segment, and the second the offset. Hence you would first need to load the page (that would wire the multiplexing) to get the offset in the page/segment. The addressing bus would be accessed with a stack. So first you push the lower 16 bits, and then the highest 16 bits. Which would revert since its a FIFO in first loading the segment address, then the offset address. The cost of this "savings" in terms of CPU cycle was marginal while the gain in money was tremenduous.
- andrewla 12y agoI think the main advantage I see in LE architectures is that integral types don't have to relocate. For example, the int32 value 30 is represented in memory as "1E 00 00 00". The int16 value 30 is represented in memory as "1E 00". So casting downwards means that the pointers are the same, and you just ignore the trailing 0s. Contrast this to BE architectures, where you have "00 00 00 1E" vs "00 1E" -- casting from the larger to the smaller type means that you have to move the pointer.
- bodyfour 12y agoYes, exactly. Maybe an example that's easier to visualize is when you learned to do arithmetic as a child. If I gave you a bunch of numbers to add like 57, 9, and 318 the first thing you would do is right justify them all. For any math operation, you start at the least significant digit and work up. This is ultimately equivalent to the int32/int16 example you gave -- if you are trying to operate on numbers of different length you have to first ensure that their LSBs line up. Little-endian is like writing your decimal integers right-justified on the blackboard. So little-endian is (slightly) more convenient for machines and big-endian is (slightly) more convenient to humans reading memory dumps. The unfortunate thing is that "network byte order" is big-endian so it's still the traditional endianness to use for wire protocols or on-disk formats. In my own designs I've switched to specifying fixed-width integers as little-endian which makes more sense for 99.9% of computers today.
- cesarb 12y agoLittle-endian also feels more natural for big integers: the byte at offset x has weight 256^x. Contrast with a big-endian representation, where the byte at offset x would have weight 256^(length - 1 - x).
- KMag 12y agoExcept that it's often convenient for encodings to sort lexographically and numerically the same way. There are several multi-precision number formats with this property, but they're all either equally slow to load on any endian machine, or faster to load on big-endian machines. Also, on a 64-bit big-endian architecture, you don't need specialized string comparison instructions to get efficient string comparisons. The code is only slightly more complicated on BE architectures that only support aligned 64-bit loads.
- cliffbean 12y agoLittle-endian is generally better for compilers, to the extent that it matters. For example, with SIMD, it makes most sense to number the lanes in the same direction as addresses in memory, so that lane 0 is at offset 0 when doing a load or store. And if you want to reinterpret the bits of a SIMD value as a different type, little-endian is the only sane way to do it. And so on. I've become fond of thinking about little-endian becoming known as a universal "CPU byte order", to complement "network byte order". Each order makes the most sense for its domain.
- gecko 12y agoLittle-endian has a major advantage over big-endian...on eight-bit architectures: as you expand the size of the number, you can keep using up new digits at the end of the range, rather than the beginning. This means that you can more easily expand field widths. Beyond that, it's called after two stupid factions in Gulliver's Travels for a reason...
- KMag 12y agoThough. it's sometimes convenient to have a lexographical sort result in a numeric ordering. For instance, if you're storing timestamps as multi-precision integers using a Rice-Golomb encoding that sorts the same way lexographically and numerically, decoding is faster if your CPU is big-endian.
- marssaxman 12y agoBig-endian has always made more sense to me, since digits are laid out in memory the same way they are laid out in writing and therefore in my brain. Little-endian just seems like a cheap '70s kludge that accidentally became dominant thanks to Intel.
- antirez 12y agoI would not follow Linus advice here. To stick with a given endianess for a protocol? Sure! But why to use BE if everything is LE? If you use LE at compile time you can convert the conversion calls to no operations: zero cost. The cost will be incurred only into BE systems that are every day less common. So my suggestion is to stick to a specific endianess in protocols and serialization factors but picking LE instead of BE. Macros to convert the value that will do nothing if the host is already LE are trivial to write.
- loopbit 12y agoI might be missing something here, but I'd say that you are following exactly Linus' advice :)
- njloof 12y agoOn modern architectures the byte swap is effectively free, if it is done at the time the data is read or written. Worry more about code that touches data just to swap bytes, which may have to page data in.
- toyg 12y agoThat's what Linus is saying: hey, BE is basically dead and you shouldn't use it, but if you really really want to use it, that's fine as long as you don't go for dynamic switching.
- nudpiedo 12y agoAnother subjective opinion from someone who doesn't look much out of his box.
- olm 12y agoAs opposed to an objective opinion?
- weland 12y agoLinkbait much? Further down below, Torvalds explains precisely why BE is anything but dead. All that's dead is general-purpose CPUs using it, mostly because x86 happened. http://geekandpoke.typepad.com/geekandpoke/2011/09/simply-explained-1.html http://geekandpoke.typepad.com/geekandpoke/2011/09/simply-ex... (Edit: which is precisely why I prefer BE for anything, unless there is a pressing - usually hardware- and performance-related - requirement for the opposite. Computers are good at reading bytes in swapped order. I suck at it.)
- justincormack 12y agoWell, and IBM switched Power to little endian recently (the machines are dual endian, but all the new OSs are littl endian only). Power was the main BE architecture left. Most arm machines are dual endian, and NetBSD supports both, but almost everyone uses little endian on arm. There is a bit of big endian mips around still, eg the Cavium network appliances.
- aduitsis 12y agoSPARC is also big endian.
- bandrami 12y agoIBM also for years screamed at devs that you can't rely on a PPC system being BE. Which means all oldworld MacOS software was at least theoretically written by people who couldn't tell you what byte order they were writing for. (How seriously the devs took that admonition is a different question.)
- vonmoltke 12y agoI was writing real-time signal processing software for POWER as recently as three years ago. I'm not even aware that was IBM's position, and they were our partners on the venture. That said, it really didn't matter because all our software had to build and run on x86-64 anyway, so all the endian-specific code was wrapped in preprocessor directives. We could have changed the endianness with a single makefile change.
- JosephRedfern 12y agoGoing against what Linus suggests, The Dalvik DEX format (used by Android) has a flag representing the endianness of the enclosed bytecode: https://source.android.com/devices/tech/dalvik/dex-format.html https://source.android.com/devices/tech/dalvik/dex-format.ht... (see "ENDIAN_CONSTANT and REVERSE_ENDIAN_CONSTANT"). By default, it's Little Endian. I've always wondered WHY this flag is present - why would an implementation wish to change the byte-order? I could understand if the file contained machine code, and would vary with different architectures - but given it's running in a VM, why bother?
- davtbaum 12y agoEven though the bytecode is being executed on a VM, the VM itself has been ported to the architecture, which has a native endianness. Byte order in the dex that matches the host OS's will likely result in a performance gain as the VM can cast more efficiently.
- JosephRedfern 12y agoThat's true - do you suppose that this flag might be used if an app developer was targeting a specific CPU architecture, then? I guess it could be argued that if you're that concerned about performance and know your target architecture, that it might be worth going native rather than running on a VM.
- yincrash 12y agoThis is just a guess, but I think the odex pass on dalvik switches the endianness to the architecture's native.
- danfuzz 12y agoYou can blame me for those wasted four bytes (or, perhaps, that wasted terabyte in the aggregate). The original idea was that on big-endian machines, the resulting .odex would be endian-swapped compared to the original .dex, and this field would provide a reasonably-blatant indication. This would probably help with debugging more than anything else (maybe help with security, but not much), because there's no reason anyone would drop a little-endian .odex file on a big-endian machine (or a big-endian one on a little-endian machine). We wrote (most of?) the code to get the vm working on big-endian systems, but within Google we never shipped any big-endian hardware AFAIK. I have my doubts that anyone ever did.
- bandrami 12y agoARM still lets you switch endianness, but nobody other than me ever seems to (and they claim they will probably deprecate that going forward). What is really alarming to me is that I occasionally run into middle-endian systems on 64-bit chips (two little-endian doubles in big-endian relative order, to signify a single quad). This is an abomination and must be killed with fire.
- w8rbt 12y agoThey do. They are bi-endian and default to little on most systems I have seen. The only real big endian system I have left is an old sun Netra.
- tritium 12y agoAnd it's funny you should mention Sun Microsystems, because this is the influence behind Java's endian-ness, and subsequently Dalvik (mentioned below in another comment), which happens to be... you guessed it... big endian. So the big endian gene lives on in the JVM and its relatives.
- gradstudent 12y agoJava is seriously nasty if you need to work with bitpacked representations of data structures. Big endian nonsense, no unsigned types, booleans that cannot be converted to integer types, no typedefs... gahhhh! It's really freaking hard to write optimised code in this stupid language.
- DonHopkins 12y agoI once heard somebody jokingly refer to MIPS as SPIM when booted in "other-endian" mode. If SPARC could do that, then it would be CRAPS.
- SeanLuke 12y agoAn odd joke. SPIM is a well-known MIPS emulator, used in a great many university compiler classes. http://spimsimulator.sourceforge.net/ http://spimsimulator.sourceforge.net/
- deleted 12y ago[deleted]
- mmf 12y agoMuauahahahah
- hsivonen 12y agoThe Web is now little-endian thanks to ArrayBufferViews exposing endianness and everyone testing only the little-endian case. It doesn't make sense to make big-endian hardware that needs to run a Web browser anymore, so chances are that big-endian will never come back.
- blueflow 12y agoIP and TCP are still Big-Endian. Above that, both formats and ascii numbers are used.
- dtech 12y agoI have a hard time believing that the whole web uses ArrayBufferViews.
- bryanlarsen 12y agoWe use DataView wherever we can, but it's quite likely we do have endianness bugs in our code. AFAICT, some browsers on BE machines use LE typed buffers. Do you know of any modern browsers that use BE typed buffers with full WebGL support for a cheap BE machine that we could buy for testing? Does anybody actually use such? Is it worth the bother?
- rwmj 12y agoWhen POWER8 came out (ppc64le) the writing was pretty much on the wall. Is there any major or even minor architecture which still defaults to BE? SPARC possibly?
- sanxiyn 12y agoYes, SPARC and zSeries. Among architectures supported by recently released Debian, zSeries is the only architecture that defaults to BE. (SPARC support is dropped.)
- restalis 12y agoThis got me thinking if in general (outside computing) it was a good choice having numbers represented the way they are. What if we had 1234 read as "four thousand, three hundred and twenty one"? Or even more different than how we pronounce it today - in order to read the number left-to-right to spell it "one and twenty and three hundred and four thousand"! This way the last digit lingering in your mind would be the one representing the most significant part of the given number (unlike the current practice of hearing first then tuning off for the less interesting smaller parts). Would it have been practical?
- jerf 12y agoIf you take into account the fact that you can obtain the order-of-magnitude for most "human" numbers without actually focusing on them, then putting the most-significant digit first in the stream makes sense. 30 is easy to see, even if you look at the 3 you can at least tell there's two digits. 12,048 is pretty easy. Even 8,293,254 isn't that hard to order-of-magnitude from the near-fovea while you're focused on the 8. By the time you get into the larger numbers that are hard, well, they're hard under either order so it's a wash.
- restalis 12y agoAt reading, one may be indeed hit at first by the most significant digit ('8' in your last example) that is somewhat useful, but the reader can't easily figure out digit's order of magnitude without reading the entire number. There's actually more. The reader can't spell the number right away, as it has to be read entirely and compiled in mind first. This compilation involves counting all digits in the given number to determine the number's size, then starting again by interpreting and spelling accordingly in its order each digit and its designation. This double-pass doesn't appear that efficient to me. On the other hand, if the number from your last example would be written backwards (452'392'8), its reading and interpretation would come efficiently in one pass ("four fifty and two hundred, three ninety and two hundred thousand, and eight million"). Even when all we're interested in would be the most significant digit and its order of magnitude, that would still be faster to determine (i.e. again, in one pass).
- krazydad 12y agoFrom Wikipedia: [Gulliver's Travels] describes an intra-Lilliputian quarrel over the practice of breaking eggs. Traditionally, Lilliputians broke boiled eggs on the larger end; a few generations ago, an Emperor of Lilliput, the Present Emperor's great-grandfather, had decreed that all eggs be broken on the smaller end after his son cut himself breaking the egg on the larger end. The differences between Big-Endians (those who broke their eggs at the larger end) and Little-Endians had given rise to "six rebellions... wherein one Emperor lost his life, and another his crown". The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. The Big-Endians gained favour in Blefuscu. http://en.wikipedia.org/wiki/Lilliput_and_Blefuscu http://en.wikipedia.org/wiki/Lilliput_and_Blefuscu