Visual Studio确定字节顺序

Cal*_*ant 2 processor endianness audio-streaming visual-c++ visual-studio-2012

所以我有一些音频处理dll,我想知道如何确定处理器的字节顺序.我知道Visual Studio定义了一些处理器宏,但我找不到它们的完整列表.我希望这是一个编译时检查,因为我可能会为不同的处理器编译它.

编辑:我也瞄准Windows.我听说Windows只使用Little Endian,这是真的吗?

maa*_*ten 5

我找到了一种使用C预处理器确定字节序的方法.

请注意,我没有在大端机器上测试此代码

它使用了一些注册表值类型的定义[1].

REG_DWORD 在那里定义为Windows运行的体系结构的字节序.

_byteswap_TYPE函数添加到混合中,我们可以编写预处理器定义[2].

#include <Windows.h>

#if REG_DWORD == REG_DWORD_LITTLE_ENDIAN
# define le_to_host_ulong(VAL) VAL
# define be_to_host_ulong(VAL) _byteswap_ulong(VAL)
# define le_to_host_ushort(VAL) VAL
# define be_to_host_ushort(VAL) _byteswap_ushort(VAL)
# define le_to_host_uint64(VAL) VAL
# define be_to_host_uint64(VAL) _byteswap_uint64(VAL)
#else
# define le_to_host_ulong(VAL) _byteswap_ulong(VAL)
# define be_to_host_ulong(VAL) VAL
# define le_to_host_ushort(VAL) _byteswap_ushort(VAL)
# define be_to_host_ushort(VAL) VAL
# define le_to_host_uint64(VAL) _byteswap_uint64(VAL)
# define be_to_host_uint64(VAL) VAL
#endif
Run Code Online (Sandbox Code Playgroud)

[1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms724884(v=vs.85).aspx

[2] https://msdn.microsoft.com/en-us/library/a3140177.aspx