字节序背后的原因? [英] The reason behind endianness?

查看:70
本文介绍了字节序背后的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想知道为什么某些体系结构使用低端字节序,而另一些使用大端字节序。我记得我在某处读到了与性能有关的内容,但是,我不了解字节序会如何影响性能。我也知道:

So, I was wondering, why some architectures use little-endian and others big-endian. I remember I read somewhere that it has to do with performance, however, I don't understand how can endianness influence it. Also I know that:

little-endian系统具有以下特性:可以从内存中以不同的长度读取相同的值,而无需使用不同的地址。

这似乎是一个不错的功能,但是即使如此,许多系统仍使用big-endian,这可能意味着big-endian具有一些优势。

Which seems a nice feature, but, even so, many systems use big-endian, which probably means big-endian has some advantages too (If so, which?).

我敢肯定还有更多东西,很可能是挖掘到硬件级别。很想知道细节。

I'm sure there's more to it, most probably digging down to hardware level. Would love to know the details.

推荐答案

我在网上四处寻找有关此问题的更多信息,并且有大量的答案和推理来解释为什么大或小端序可能更可取。我会尽力在这里解释我的发现:

I've looked around the net a bit for more information on this question and there is a quite a range of answers and reasonings to explain why big or little endian ordering may be preferrable. I'll do my best to explain here what I found:

little-endianness是您在问题中已经提到的……给定数字可以读取为来自同一内存地址的不同位数的事实。正如Wikipedia上有关该主题的文章所述:

The obvious advantage to little-endianness is what you mentioned already in your question... the fact that a given number can be read as a number of a varying number of bits from the same memory address. As the Wikipedia article on the topic states:


尽管高级程序员很少直接使用这种little-endian属性,但通常

Although this little-endian property is rarely used directly by high-level programmers, it is often employed by code optimizers as well as by assembly language programmers.

因此,涉及多种精度的数学函数更容易编写,因为字节的有效位将始终与存储器地址相对应,而对于大端数字则不是这种情况。这似乎是一遍又一遍地引用低端字节序的论点……由于它的普遍性,我不得不假设这种排序的好处是相对重要的。

Because of this, mathematical functions involving multiple precisions are easier to write because the byte significance will always correspond to the memory address, whereas with big-endian numbers this is not the case. This seems to be the argument for little-endianness that is quoted over and over again... because of its prevalence I would have to assume that the benefits of this ordering are relatively significant.

我发现的另一个有趣的解释涉及加法和减法。在添加或减去多字节数字时,必须首先获取最低有效字节,以查看是否有对更高有效字节的遗留。因为最低有效字节是从小尾数开始首先读取的,所以系统可以并行化并开始对该字节进行计算,同时获取以下字节。

Another interesting explanation that I found concerns addition and subtraction. When adding or subtracting multi-byte numbers, the least significant byte must be fetched first to see if there is a carryover to more significant bytes. Because the least-significant byte is read first in little-endian numbers, the system can parallelize and begin calculation on this byte while fetching the following byte(s).

回到Wikipedia文章,big-endian数字的优点是可以更容易地估计数字的大小,因为最高有效位是第一。与此相关的是,只需检查最低位字节的偏移量0处的位,就可以很容易地判断一个数字是正还是负。

Going back to the Wikipedia article, the stated advantage of big-endian numbers is that the size of the number can be more easily estimated because the most significant digit comes first. Related to this fact is that it is simple to tell whether a number is positive or negative by simply examining the bit at offset 0 in the lowest order byte.

还有什么在讨论big-endianness的好处时说的是,二进制数字是按顺序排序的,因为大多数人都以10为基数排序。从二进制转换为十进制时,这在性能方面是有利的。

What is also stated when discussing the benefits of big-endianness is that the binary digits are ordered as most people order base-10 digits. This is advantageous performance-wise when converting from binary to decimal.

尽管所有这些参数都很有趣(至少我认为因此),它们对现代处理器的适用性是另一回事。特别地,加/减参数在8位系统上最有效...

While all these arguments are interesting (at least I think so), their applicablility to modern processors is another matter. In particular, the addition/subtraction argument was most valid on 8 bit systems...

就我的金钱而言,小字节序似乎是最有意义的,并且到目前为止查看使用它的所有设备时最常见。我认为仍然使用大端优先的原因更多是出于传统而不是性能。也许有一次,给定体系结构的设计师认为大端优先是更可取的小端优先,并且随着体系结构的发展,多年以来保持不变。

For my money, little-endianness seems to make the most sense and is by far the most common when looking at all the devices which use it. I think that the reason why big-endianness is still used is more for reasons of legacy than performance. Perhaps at one time the designers of a given architecture decided that big-endianness was preferrable little-endianness, and as the architecture evolved over the years the endianness stayed the same.

我在这里绘制的平行线是使用JPEG(大端)的。 JPEG是大尾数格式,尽管事实上使用它的所有机器都是小尾数格式。尽管可以问一下JPEG成为big-endian有什么好处,但我敢冒险地说,就所有意图和目的而言,上面提到的性能参数并没有丝毫区别。事实是JPEG是按这种方式设计的,只要它保持使用状态,它就应该保持不变。

The parallel I draw here is with JPEG (which is big-endian). JPEG is big-endian format, despite the fact that virtually all the machines that consume it are little-endian. While one can ask what are the benefits to JPEG being big-endian, I would venture out and say that for all intents and purposes the performance arguments mentioned above don't make a shred of difference. The fact is that JPEG was designed that way, and so long as it remains in use, that way it shall stay.

这篇关于字节序背后的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆