我正在尝试比较64位数字的3种方式,例如结果必须类似于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);
}