简体   繁体   English

在小端机器上添加两个大端值

[英]Add two big endian values on little endian machine

I am currently facing a problem of which I have no idea how to avoid it.. 我目前面临的一个问题是我不知道如何避免它。
I try to process data which can be either in big endian or little endian. 我尝试处理大尾数或小尾数的数据。 This is not really a problem because it always starts with a header so I can check which endian mode I have to use but during the decoding of the values there are some operations which I dont know how to implement for big endian data. 这并不是真正的问题,因为它总是以标头开头,因此我可以检查必须使用哪种字节序模式,但是在对值进行解码期间,有些操作我不知道如何为大字节序数据实现。
The code runs on a nVidia Tegra (Cortex-A9 based on ARMv7 architecture) which is little endian (or runs in little endian mode) but sometimes I get big endian data. 该代码在nVidia Tegra(基于ARMv7架构的Cortex-A9)上运行,它的字节序很小(或在字节序小模式下运行),但是有时我会得到较大的字节序数据。
Most operations on the data are not really a problem but I dont know how to get the addition right.. 对数据进行的大多数操作并不是真正的问题,但是我不知道如何正确进行加法运算。

Example:    D5 1B EE 96    |     96 EE 1B D5
        +   AC 84 F4 D5    | +   D5 F4 84 AC
        = 1 81 A0 E3 6B    | = 1 6C E2 A0 81

As you can see, most bytes are already correct in the result but some are not. 如您所见,大多数字节在结果中已经正确,但有些则不正确。 They differ by +1 or -1 from the expected result because the addition is always made from right to left (little endian machine) and so we take the carry (if any) to the left. 它们与预期结果相差+1或-1,因为加法始终是从右到左(小字节序的机器)进行的,因此我们将进位(如果有)移到左侧。
In the case of the big endian addition on this little endian machine I would have to add from left to right and take the carry (if any) to the right. 如果在这台小端机器上添加了大端,我将不得不从左向右加,然后将进位(如果有的话)移到右边。

My question now is, whether there is a possibility (maybe using special instructions for the processor?) to get the right result? 我现在的问题是,是否有可能(可能对处理器使用特殊的指令?)获得正确的结果? Maybe ther are further operations I can make on the result to get rid of these +1/-1 differences which are "cheaper" than to revert both operands and also the result? 也许我还可以对结果做进一步的操作,以消除这些“更便宜”的+ 1 / -1差异,而不是还原两个操作数和结果?

Best Regards, Tobias 最好的问候,托比亚斯

The most logical way to do this is to simply convert the numbers to the correct endianness, then perform the calculation, then (if needed) convert back again. 这样做的最合乎逻辑的方法是,将数字简单地转换为正确的字节序,然后执行计算,然后(如果需要)再次转换回去。

You could of course use a loop to do the byte-by-byte backwards caclulation and handle the carry - but it's more complicated, and I'm pretty certain that it won't be faster either, because there are more conditionals and processors are pretty good at "byteswapping". 您当然可以使用循环来逐字节向后计算并处理进位-但它更复杂,而且我可以肯定它也不会更快,因为有更多的条件和处理器相当擅长“字节交换”。

You should be able to use the ntohl and htons networking functions to convert the numbers. 您应该能够使用ntohlhtons网络功能来转换数字。

Something like this: 像这样:

int add_big_endian(int a, int b)
{
   x = ntohl(a);
   y = ntohl(b);

   z = x + y;

   return htonl(z);
}

You have two options: you can write two sets of code, one for each endianness, and try to keep track of what's going on where, or you can use a single internal representation and convert incoming and outgoing values appropriately. 您有两种选择:可以编写两组代码,每种字节序一组,并尝试跟踪发生在哪里的情况,或者可以使用单个内部表示形式并适当地转换传入和传出的值。 The latter is much simpler. 后者要简单得多。

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

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