<分区>
我正在尝试用 3 种方式比较 64 位数字,e.d.结果必须类似于 memcmp
或 strcmp
。
我想到了这个,但我不确定如果我输入一些非常大的数字它会表现良好。
汇编看起来不错,但我担心它是否正确,因为如果我用分支来编译,编译器不会生成相同的代码。
https://gcc.godbolt.org/z/UNtPGh
#include <cstdint>
#include <algorithm>
int f(uint64_t a, uint64_t b){
auto x = std::max(a, b);
int xa = (x - a) > 0;
int xb = (x - b) > 0;
return xb - xa;
}
inline int f1(uint64_t a, uint64_t b){
return f(b, a);
}
int main(){
return f(550u, 5555u);
}