简体   繁体   English

Facebook愚蠢的大端和小端

[英]big endian and little endian in folly of Facebook

I have read the code of folly created by Facebook,In this page https://github.com/facebook/folly/blob/master/folly/FBString.h ,I found that author considers big endian and little endian when he set some value, such as capacity_ , The code is as follows: 我已经阅读了Facebook创建的愚蠢代码,在此页面https://github.com/facebook/folly/blob/master/folly/FBString.h中 ,我发现作者在设置一些东西时会考虑大尾数和小尾数值,例如capacity_,代码如下:

void setCapacity(size_t cap, Category cat) {
  capacity_ = kIsLittleEndian
      ? cap | (static_cast<size_t>(cat) << kCategoryShift)
      : (cap << 2) | static_cast<size_t>(cat);
}

I want to know why author should consider big endian and little endian, I think we don't need to consider them in the same machine, geting and settting value are dealed with by the machines and we can ignore them 我想知道为什么作者应该考虑大尾数法和小尾数法,我认为我们不需要在同一台机器上考虑它们,获取和设置值由机器处理,我们可以忽略它们

That string implementation has some cleverness around how it allocates memory depending on string size. 该字符串实现在如何根据字符串大小分配内存方面有一些技巧。 Notably here you can find where a union is used to swap between strategies. 值得注意的是您可以在此处找到使用联合在策略之间进行交换的位置。

On a 64-bit machine with 8-bit characters, the MediumLarge struct is 24 bytes long, and can hold 24 chars. 在具有8位字符的64位计算机上,MediumLarge结构的长度为24个字节,可以容纳24个字符。 Two bits from the last byte are reserved for determining the storage strategy, though, so short strings can be up to 23 chars long. 但是,保留最后一个字节的两位来确定存储策略,因此短字符串的长度最多为23个字符。

It's that "last byte" thing that justifies the need to worry about endianness: that "last byte" is the highest address, and, therefore, on a little endian machine, and those flags are stored in the two most significant bits, and you can extract the capacity length by masking off those two bits. 正是这种“最后一个字节”的事情证明了需要担心字节序的问题:“最后一个字节”是最高地址,因此,在一个小的字节序机器上,这些标志存储在两个最高有效位中,您可以通过屏蔽这两个位来提取容量长度。 On big-endian, the last byte is the least significant byte, you store the flags in the two least significant bits, and you can extract the capacity by doing a 2-bit shift to the right. 在big-endian上,最后一个字节是最低有效字节,将标志存储在两个最低有效位中,然后可以通过向右移2位来提取容量。

Now, the fact that the code uses kIsLittleEndian and the conditional operator to swap between these behaviours seems to suggest these checks are happening at run-time. 现在,代码使用kIsLittleEndian和条件运算符在这些行为之间进行交换的事实似乎表明这些检查是在运行时进行的。 However, kIsLittleEndian is declared as constexpr , and all conditionals on it can be evaluated at compile time. 但是, kIsLittleEndian被声明为constexpr ,并且可以在编译时评估它的所有条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM