Endian逆转? [英] Endian Reversal?

查看:78
本文介绍了Endian逆转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从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.

推荐答案

Tosha写道:
最快的方式是什么?将__int64类型从BigEndian转换为Little
Endian?
我写了这个有趣的ction:
---
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);
返回* 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


Tosha写道:
将__int64类型从BigEndian转换为Little的最快方法是什么?
What is the fastest way to convert __int64 type from BigEndian To Little
Endian?



< snip code>


既然你看起来像是在视觉工作室,那么尝试

_byteswap_uint64内在如何:

http://msdn2.microsoft.com/en-us/lib...77(VS。 80)的.aspx


<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




" Me" <一个***************** @ yahoo.com>在消息中写道

news:11 ********************** @ p10g2000cwp.googlegr oups.com ...

"Me" <an*****************@yahoo.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
Tosha写道:
将__int64类型从BigEndian转换为Little的最快方法是什么?
What is the fastest way to convert __int64 type from BigEndian To Little
Endian?


< snip code>

因为看起来你在视觉工作室,尝试
_byteswap_uint64内在如何:

http://msdn2.microsoft.com/en-us/lib...77(VS.80 ).aspx




非常感谢。



Thank you very much.


这篇关于Endian逆转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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