问题描述
从BigEndian转换__int64类型的最快方法是什么?
Endian?
我写了这个函数:
---
uint64 EndianReverse(uint64 number){
typedef unsigned __int8 uint8;
const int size = sizeof(uint64);
uint8 polje [size] = {0};
uint8 * ptr = reinterpret_cast< uint8 *>(& number);
int index = 0;
for(int i = size-1; i> = 0; i--){
polje [index] = *(ptr + i);
index ++;
}
uint64 * retPtr = reinterpret_cast< uint64 *>(polje);
return * retPtr;
}
---
但是我知道有更快的逻辑和移位操作方式,所以如果有人
知道我会很感激。
What is the fastest way to convert __int64 type from BigEndian To Little
Endian?
I wrote this function:
---
uint64 EndianReverse(uint64 number) {
typedef unsigned __int8 uint8;
const int size = sizeof(uint64);
uint8 polje[size] = {0};
uint8 *ptr = reinterpret_cast<uint8*>(&number);
int index = 0;
for (int i=size-1 ; i>=0 ; i--) {
polje[index] = *(ptr+i);
index++;
}
uint64 *retPtr = reinterpret_cast<uint64*>(polje);
return *retPtr;
}
---
but i know there is faster way with logical and shift opertors, so if anyone
knows it i would be appreciated.
推荐答案
牢度必须_measured_,而不是从使用的
操作的类型推断出来。也就是说,我希望这会更快一点:
模板< class T> T EndianReverse(T t)
{
unsigned char uc [sizeof t];
memcpy(uc,& t,sizeof t) ;
for(unsigned char * b = uc,* e = uc + sizeof(T) - 1; b< e; ++ b, - e)
std :: swap(* b,* e);
memcpy(& t,uc,sizeof t);
返回t;
}
....它在我的机器上(刚刚测量)大约20%。
V
-
请在邮寄回复时从我的地址删除资金
The "fastness" has to be _measured_, not deduced from the types of
operations used. That said, I''d expect this to be a bit faster:
template<class T> T EndianReverse(T t)
{
unsigned char uc[sizeof t];
memcpy(uc, &t, sizeof t);
for (unsigned char *b = uc, *e = uc + sizeof(T) - 1; b < e; ++b, --e)
std::swap(*b, *e);
memcpy(&t, uc, sizeof t);
return t;
}
.... and it is (just measured), by about 20%, on my machine.
V
--
Please remove capital As from my address when replying by mail
< snip code>
既然你看起来像是在视觉工作室,那么尝试
_byteswap_uint64内在如何:
<snip code>
Since it looks like you''re on visual studio, how about trying the
_byteswap_uint64 intrinsic:
http://msdn2.microsoft.com/en-us/lib...77(VS.80).aspx
< snip code>
因为看起来你在视觉工作室,尝试
_byteswap_uint64内在如何:
非常感谢。
Thank you very much.
这篇关于Endian逆转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!