gpt4 book ai didi

visual-c++ - Visual Studio 确定字节顺序

转载 作者:行者123 更新时间:2023-12-04 00:08:02 25 4
gpt4 key购买 nike

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

编辑:我也是针对 windows 的。听说 Windows 只使用小尾数,是真的吗?

最佳答案

我找到了一种使用 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

[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

关于visual-c++ - Visual Studio 确定字节顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29113768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com