C ++/Boost ASIO中的内联ntohs()/ntohl() [英] Inline ntohs() / ntohl() in C++ / Boost ASIO

查看:202
本文介绍了C ++/Boost ASIO中的内联ntohs()/ntohl()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++/Boost ASIO,出于性能原因,我必须内联ntohl().每个数据包包含256个int32,因此对ntohl()的调用很多.有人做过吗?

Hi I'm using C++ / Boost ASIO and I have to inline ntohl() for performance reasons. Each data packet contains 256 int32s, hence a lot of calls to ntohl(). Has anyone done this?

以下是VC10 ++编译的程序集输出,其中启用了所有优化功能:

Here is the compiled assembly output out of VC10++ with all optimizations turned on:

;  int32_t d = boost::asio::detail::socket_ops::network_to_host_long(*pdw++);
mov      esi, DWORD PTR _pdw$[esp+64]
mov      eax, DWORD PTR [esi]
push     eax
call     DWORD PTR __imp__ntohl@4

我还尝试了winsock提供的常规ntohl().任何帮助将不胜感激.

I've also tried the regular ntohl() provided by winsock. Any help would be greatly appreciated.

此外,我一直在考虑C语言中使用#define宏进行简单的int32桶移位(如果网络顺序与编译时的机器顺序不匹配)的方法.而且,如果有人知道并且可以在x86/x64体系结构上为ntohl()提供最有效的汇编,那将是非常棒的.最终,我的代码也需要移植到ARM.

Also, I've been thinking the C way of having a #define macro that does simple int32 barrel shifts (if the network order doesn't match the machines order at compile time). And if anyone knows and can provide the most efficient assembly for ntohl() on a x86 / x64 architecture, that would be awesome. Eventually my code needs to be portable to ARM as well.

推荐答案

x86-32和x86-64平台具有32位"bswap"汇编指令.我认为您不会比一项手术做得更好.

The x86-32 and x86-64 platforms have a 32-bit 'bswap' assembly instruction. I don't think you'll do better than one operation.

uint32_t asm_ntohl(uint32_t a)
{
   __asm
    {
       mov eax, a;
       bswap eax; 
    }
}

这篇关于C ++/Boost ASIO中的内联ntohs()/ntohl()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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