_byteswap_uint64和_byteswap_ulong的跨平台定义 [英] Cross-platform definition of _byteswap_uint64 and _byteswap_ulong

查看:1659
本文介绍了_byteswap_uint64和_byteswap_ulong的跨平台定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio定义了 _byteswap_uint64 和<$ c $ stdlib.h 中的 _byteswap_ulong

我是否可以假设这不是标准的,并且不能在Linux或Darwin上编译?

Am I right to assume, that this is not standard and won't compile on Linux or Darwin?

是否可以在十字架中定义这些包含项?平台方式?

Is there a way to define these includes in a cross-platform way?

推荐答案

Google的CityHash源代码使用以下代码:

Google's CityHash source code uses this code:

> https://github.com/google/cityhash/blob /8af9b8c2b889d80c22d6bc26ba0df1afb79a30db/src/city.cc#L50

#ifdef _MSC_VER

#include <stdlib.h>
#define bswap_32(x) _byteswap_ulong(x)
#define bswap_64(x) _byteswap_uint64(x)

#elif defined(__APPLE__)

// Mac OS X / Darwin features
#include <libkern/OSByteOrder.h>
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)

#elif defined(__sun) || defined(sun)

#include <sys/byteorder.h>
#define bswap_32(x) BSWAP_32(x)
#define bswap_64(x) BSWAP_64(x)

#elif defined(__FreeBSD__)

#include <sys/endian.h>
#define bswap_32(x) bswap32(x)
#define bswap_64(x) bswap64(x)

#elif defined(__OpenBSD__)

#include <sys/types.h>
#define bswap_32(x) swap32(x)
#define bswap_64(x) swap64(x)

#elif defined(__NetBSD__)

#include <sys/types.h>
#include <machine/bswap.h>
#if defined(__BSWAP_RENAME) && !defined(__bswap_32)
#define bswap_32(x) bswap32(x)
#define bswap_64(x) bswap64(x)
#endif

#else

#include <byteswap.h>

#endif

这篇关于_byteswap_uint64和_byteswap_ulong的跨平台定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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