小编ben*_*nrg的帖子

GHC 中如何实现整数比较?

起初,我想研究一下如何Integer从类中派生出来Ord

\n

我在 GHC.Classes 中得到了这个定义

\n
instance Ord Integer where\n    (<=) = leInteger\n    (>)  = gtInteger\n    (<)  = ltInteger\n    (>=) = geInteger\n    compare = compareInteger\n
Run Code Online (Sandbox Code Playgroud)\n

compareInteger指向另一个源文件,GHC.Integer.Type. 它的定义如下\xe2\x80\xaf:

\n
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)

haskell ghc

14
推荐指数
1
解决办法
510
查看次数

为什么 Clang 为引用和非空指针参数生成不同的代码?

这与为什么 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)

https://godbolt.org/z/e49h6d

对于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字段为零时才加载该字段。

对于 …

c++ x86-64 clang pass-by-reference language-lawyer

9
推荐指数
1
解决办法
219
查看次数

标签 统计

c++ ×1

clang ×1

ghc ×1

haskell ×1

language-lawyer ×1

pass-by-reference ×1

x86-64 ×1