起初,我想研究一下如何Integer
从类中派生出来Ord
我在 GHC.Classes 中得到了这个定义
\ninstance Ord Integer where\n (<=) = leInteger\n (>) = gtInteger\n (<) = ltInteger\n (>=) = geInteger\n compare = compareInteger\n
Run Code Online (Sandbox Code Playgroud)\ncompareInteger
指向另一个源文件,GHC.Integer.Type
. 它的定义如下\xe2\x80\xaf:
compareInteger :: Integer -> Integer -> Ordering\ncompareInteger (Jn# x) (Jn# y) = compareBigNat y x\ncompareInteger (S# x) (S# y) = compareInt# x y\ncompareInteger (Jp# x) (Jp# y) = compareBigNat x y\ncompareInteger (Jn# _) _ = LT\ncompareInteger (S# _) (Jp# _) = LT\ncompareInteger (S# _) (Jn# …
Run Code Online (Sandbox Code Playgroud) 这与为什么 GCC 无法为两个 int32 的结构生成最佳运算符==有关?. 我正在玩 godbolt.org 上那个问题的代码,并注意到这种奇怪的行为。
struct Point {
int x, y;
};
bool nonzero_ptr(Point const* a) {
return a->x || a->y;
}
bool nonzero_ref(Point const& a) {
return a.x || a.y;
}
Run Code Online (Sandbox Code Playgroud)
对于nonzero_ptr
, clang -O3 (所有版本) 产生这个或类似的代码:
struct Point {
int x, y;
};
bool nonzero_ptr(Point const* a) {
return a->x || a->y;
}
bool nonzero_ref(Point const& a) {
return a.x || a.y;
}
Run Code Online (Sandbox Code Playgroud)
这严格实现了 C++ 函数的短路行为,y
仅当x
字段为零时才加载该字段。
对于 …