首页 > buildin 字节交换

buildin 字节交换

为什么80%的码农都做不了架构师?>>>   hot3.png

template T EndianScalar(T t) {#if FLATBUFFERS_LITTLEENDIANreturn t;#else#if defined(_MSC_VER)#pragma push_macro("__builtin_bswap16")#pragma push_macro("__builtin_bswap32")#pragma push_macro("__builtin_bswap64")#define __builtin_bswap16 _byteswap_ushort#define __builtin_bswap32 _byteswap_ulong#define __builtin_bswap64 _byteswap_uint64#endif// If you're on the few remaining big endian platforms, we make the bold// assumption you're also on gcc/clang, and thus have bswap intrinsics:if (sizeof(T) == 1) {   // Compile-time if-then's.return t;} else if (sizeof(T) == 2) {auto r = __builtin_bswap16(*reinterpret_cast(&t));return *reinterpret_cast(&r);} else if (sizeof(T) == 4) {auto r = __builtin_bswap32(*reinterpret_cast(&t));return *reinterpret_cast(&r);} else if (sizeof(T) == 8) {auto r = __builtin_bswap64(*reinterpret_cast(&t));return *reinterpret_cast(&r);} else {assert(0);}#if defined(_MSC_VER)#pragma pop_macro("__builtin_bswap16")#pragma pop_macro("__builtin_bswap32")#pragma pop_macro("__builtin_bswap64")#endif#endif
}





转载于:https://my.oschina.net/lyr/blog/317248

更多相关:

  •  这是我偶然在 http://stackoverflow.com/questions/ 网页上发现的一个问题(类似博客园的博问),问题主要是关于询问应该怎样使用,以及何时使用C++里面的这几种类型转换操作符:static_case, dynamic_cast,以及 reinterpret_cast 。我想这是一个非常典型的问题,因此...