- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一些代码以了解有关汇编和 JIT 编译器等内容的更多信息。到目前为止,我已经能够想出一个 XOR 函数,理论上应该可以在 Windows 和 Linux 环境中的 x86 或 x64 机器上运行。
假设我理解正确,[RE]AX
寄存器用于保存整数返回值,而 [RE]DX
是用于传递的可用寄存器之一函数之间的整数。我选择不严格遵循 ABI 并使用 [RE]AX
传递第一个参数,因为它节省了 MOV
指令而不影响结果。
是否有更好(更优雅或更高效)的方式来发出跨平台程序集,或者我在开发此程序时是否犯过任何错误?
#include <cstdint>
#include <iostream>
template<typename TInput>
static auto Xor(TInput const highPart, TInput const lowPart) {
constexpr bool is16Bit = (std::is_same<TInput, int16_t>::value || std::is_same<TInput, uint16_t>::value);
constexpr bool is32Bit = (std::is_same<TInput, int32_t>::value || std::is_same<TInput, uint32_t>::value);
static_assert(is16Bit || is32Bit, "type must be a member of the type family: [u]int{16, 32}_t");
if constexpr (is16Bit) {
uint16_t result;
#if (defined(__linux__) || defined(__unix__) || defined(_WIN32))
asm volatile ("xorw %%dx, %%ax;" : "=a" (result) : "a" (highPart), "d" (lowPart));
#else
#error "Unsupported platform detected."
#endif
return result;
}
else if constexpr (is32Bit) {
uint32_t result;
#if (defined(__linux__) || defined(__unix__) || defined(_WIN32))
asm volatile ("xorl %%edx, %%eax;" : "=a" (result) : "a" (highPart), "d" (lowPart));
#else
#error "Unsupported platform detected."
#endif
return result;
}
}
#define HIGH_PART 4;
#define LOW_PART 8;
int main() {
int16_t const a = HIGH_PART;
int16_t const b = LOW_PART;
int16_t const c = Xor(a, b);
uint32_t const x = HIGH_PART;
uint32_t const y = LOW_PART;
uint32_t const z = Xor(x, y);
std::cout << c << "\n";
std::cout << z << "\n";
getchar();
return 0;
}
以下是如何改进的示例;通过“提升”result
变量和 if defined(...)
检查高于 constexpr
检查,我们可以使事情更通用。
template<typename T>
static auto Xor(T const highPart, T const lowPart) {
constexpr bool is16Bit = (std::is_same<T, int16_t>::value || std::is_same<T, uint16_t>::value);
constexpr bool is32Bit = (std::is_same<T, int32_t>::value || std::is_same<T, uint32_t>::value);
static_assert(is16Bit || is32Bit, "type must be a member of the type family: [u]int{16, 32}_t");
#if !(defined(__linux__) || defined(__unix__) || defined(_WIN32))
#error "Unsupported platform detected."
#endif
T result;
if constexpr (is16Bit) {
asm volatile ("xorw %%dx, %%ax;" : "=a" (result) : "a" (highPart), "d" (lowPart));
}
else if constexpr (is32Bit) {
asm volatile ("xorl %%edx, %%eax;" : "=a" (result) : "a" (highPart), "d" (lowPart));
}
return result;
}
最佳答案
您不能让编译器在 64 位模式下在 EAX/RAX 中传递函数 arg。在 32 位模式下,您可以使用 gcc "regparm"调用约定,如 __attribute__((regparm(3))) int my_func(int,int);
在 EAX、ECX 中传递参数, EDX 的顺序。 (所以编译器将在内联 asm 之前需要一个 mov
,它在 EAX 中有一个函数 arg)。
或者您可以使用 __attribute__((sysv_abi))
声明您的函数以始终使用 SysV ABI,即使在 Windows 上编译时也是如此。但这只有在所有调用者都由 GCC/clang/ICC 而不是 MSVC 编译时才有效。在 32 位模式下更糟; i386 System V 调用约定很糟糕:在堆栈上传递所有参数,并且在 edx:eax 中仅返回 int64_t,而不是 2 成员 64 位结构。
调用 sysv_abi
函数可能也会调用 ms_abi
函数来保存/恢复所有 xmm6..15,除非 sysv_abi 函数调用可以内联并优化掉。因此,如果函数尚未大量使用 XMM regs 并保存/恢复其中的大部分,那么总的来说这可能是一个糟糕的计划。
使用固定寄存器输入/输出约束通常没有用,除非您使用带有隐式寄存器的指令(如 cl
中的移位计数,如果您不能使用 BMI2 shlx
/shrx
).
让编译器使用"r"
和"+r"
约束进行寄存器分配。 (或 "=r"
和 "0"
匹配约束)因此无论值在哪里,您的函数都可以有效地内联。对于可以是寄存器或 32 位立即数的输入,也可以使用 "re"
。甚至 "rem"
也可以是内存输入。但是,如果您重复使用输入,最好让编译器在 asm 之前为您加载它。
另见 https://stackoverflow.com/tags/inline-assembly/info
对寄存器分配进行硬编码部分违背了使用内联 asm 而不是编译器必须调用而不是内联的独立 asm 函数的目的。
查看编译器为您的代码生成的 asm,了解它生成的周围代码,以及它如何通过选择操作数填充模板。
另请注意,"r"
为 16 位类型选择 16 位寄存器,为 32 位类型选择 32 位寄存器,因此所有这些调整类型大小的东西基本上都是不必要的。 (尽管取决于输入的写入方式,使用 32 位 xor
可能比 16 位 xor 更好,如果稍后读取完整的 32 位或 64 位寄存器,可能会避免部分寄存器停顿。但是如果您的输入寄存器是用 16 位操作数大小编写的,那么在 P6 系列 CPU 上,一个 32 位异或将创建一个部分寄存器停顿。)您可以覆盖为 "xor %0 填充的大小"
模板替换为 "%k0"
32 位大小等。参见 x86 Operand Modifiers in the GCC manual .
关于c++ - 跨平台汇编 ((x64 || x86) && (Microsoft x64 || SystemV)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54872021/
我正在开发一组应用程序,它们协同工作以创建一个用于处理计量数据的系统。我想让它们松散耦合有几个原因,并且系统应该可以由第三方扩展,因此应用程序将通过消息传递绑定(bind)在一起。 我正在寻找一种消息
如果我使用 python 2.6 和 Qt 为 windows 创建一个 gui,然后想在 solaris 或 linux 世界中运行。 我在两个系统上都需要什么,我猜两个平台都需要 2.6 和 Qt
我正在运行 Linux Ubuntu 10.04,我有一台 Windows 7 机器和一台运行 Mac OS X 10.6.4 的 MacBook。我如何编写一个简单的 C 程序(如 不是 QT!),
前言 今天给大家分享一个.NET开源(MIT License)、免费、跨平台(适用于 Windows、MacOS 和 Linux )、使用简单的面部识别库:FaceRecognitionDotNet
我知道汇编语言通常不是跨平台的。即使使用像 NASM 这样的东西,对于不同的架构和平台,您仍然需要不同的代码,这些架构和平台具有不同的运行时和诸如中断之类的东西的实现。但是如果有人因为喜欢汇编语言而想
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 8年前关闭。 Improve thi
This question already has answers here: How to play audio? (20个答案) 12天前关闭。 我正在编写一个dhtml应用程序,该应用程序创建系
我正在使用MonoGame开发一款游戏,该游戏使用SoundEffectInstance来实现游戏中的音效。在可以创建的SoundEffect实例数量以及可以同时播放的最重要实例方面,不同平台受到什么
如何在 java 中找到并运行默认计算器而不使我的程序依赖于平台。我可以对每个主要操作系统的所有路径进行硬编码,然后找到该人正在使用的操作系统,然后使用该操作系统各自的路径。但是,我不想那样做,有没有
有没有办法使用捏合来放大共享的Xamarin Forms,我只找到了每个平台的实现。 最佳答案 您可以使用 Pan Gesture 来实现它。这里有一个很好的在 PanContainer 中包装图像的
是否有最好/好的方法来同时为 Android 和 iOS(以及 Mac OS X)开发跨平台应用程序,例如游戏? 只是简单地开发一个仅使用 OpenGL 和 C/C++ 的应用程序应该很简单,不是吗?
我想知道 CSS 行为是否会随着操作系统的不同而变化。我的 css 对于 vista 和 win xp proffesional 对于相同版本的浏览器来说有点不同。我在 vista + ie8 和 x
我需要一种跨平台的方式来检查给定的文件路径是否是绝对的而不使用Pathname 库。为此,我唯一希望依赖的库是 File。它需要跨平台和跨实现,因此无论是 Ruby MRI、JRuby、Rubiniu
我正在尝试寻找一种跨平台的方法来从文件末尾删除 X 字节。 目前我发现: 特定于平台的解决方案(例如 truncate for posix):这是我不想要的,因为我希望 C++ 程序可以在多个平台上运
我想将可执行文件从一个目录复制到另一个目录。 源目录包括各种我不关心的文件(构建工件)。我只想使用适用于 OS X 和 Linux 的 bash 脚本获取可执行文件。 可执行文件是指具有可执行权限的文
我需要能够在服务器端处理简单 HTTP GET/PUT 请求的库。有一些特殊要求: 应该是跨平台的(这是主要因素) 支持多线程。 高性能 支持 HTTPS 许可证应允许链接专有代码(LGPL、BSD)
我正在寻找一种可用于从 Windows 和 Mac OSX 计算机上传大型 (~2GB) 文件的技术。应该可以根据请求/错误暂停和恢复上传。具有基本计算机技能的用户应该可以轻松安装最终产品。 我正在考
我正在寻找一种使用 java 播放 .flv 文件的方法。这意味着我需要一个跨平台的 ffmpeg 库。我一直在玩弄 jmf 和 fobs4jmf,但我无法在 linux 中播放,因为我需要一个本地库
我想在基于浏览器的应用程序中做的一件事是允许用户选择一些文本(不是在 中,只是普通的文本!),然后让我的应用程序弹出一个然后可以与下一个交互的小工具栏(在我的例子中,添加注释)。 我在 google
对于 PyInstaller 中的新功能,我们需要一个命令行选项来接收其中包含任何分隔符的字符串。这是讨论:https://github.com/pyinstaller/pyinstaller/pul
我是一名优秀的程序员,十分优秀!