GHC Int 类型的位大小
为什么 GHC 的 Int type
不保证使用精确的 32 位精度? 此文档声称它有至少 30 位有符号精度。它是否与将 Maybe Int
或类似的内容装入 32 位有关?
Why is GHC's Int type
not guaranteed to use exactly 32 bits of precision? This document claim it has at least 30-bit signed precision. Is it somehow related to fitting Maybe Int
or similar into 32-bits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是为了允许使用标记的 Haskell 实现。使用标记时,您需要一些位作为标记(至少一个,两个更好)。我不确定目前是否有这样的实现,但我似乎记得耶鲁哈斯克尔使用过它。
标记可以在一定程度上避免装箱的缺点,因为您不再需要对所有内容进行装箱;相反,标签位会告诉您是否对其进行了评估等。
It is to allow implementations of Haskell that use tagging. When using tagging you need a few bits as tags (at least one, two is better). I'm not sure there currently are any such implementations, but I seem to remember Yale Haskell used it.
Tagging can somewhat avoid the disadvantages of boxing, since you no longer have to box everything; instead the tag bit will tell you if it's evaluated etc.
Haskell 语言定义指出 类型
Int
涵盖至少范围 [−229, 229−1]。还有其他编译器/解释器使用此属性来提高结果程序的执行时间。
在 32 位(64 位)系统上,对(对齐的)Haskell 数据的所有内部引用都指向 4(8) 倍数的内存地址。因此,引用仅需要 30 位(61 位),因此允许 2(3) 位用于“指针标记”。
对于数据,GHC 使用这些标签来存储有关该引用数据的信息,即该值是否已被评估,如果是,则它具有哪个构造函数。
如果是 30 位
Int
(因此,不是 GHC),您可以使用一位来决定它是指向未计算的Int
的指针,还是指向该Int
的指针。 >Int 本身。指针标记可用于一位引用计数,这可以加快垃圾收集过程。这在运行时创建直接的一对一生产者-消费者关系的情况下非常有用:它将直接导致内存重用,而不是垃圾收集器馈送。
因此,使用 2 位进行指针标记,可能会出现一些强烈优化的疯狂组合......
对于 Ints,我可以想象这 4 个标签:
Int
的单个引用Int
的许多引用的 30 位Int
本身Int
的引用(可能有许多引用)。The Haskell language definition states that the type
Int
covers at least the range [−229, 229−1].There are other compilers/interpreters that use this property to boost the execution time of the resulting program.
All internal references to (aligned) Haskell data point to memory addresses that are multiple of 4(8) on 32-bit(64-bit) systems. So, references need only 30bits(61bits) and therefore allow 2(3) bits for "pointer tagging".
In case of data, the GHC uses those tags to store information about that referenced data, i.e. whether that value is already evaluated and if so which constructor it has.
In case of 30-bit
Int
s (so, not GHC), you could use one bit to decide if it is either a pointer to an unevaluatedInt
or thatInt
itself.Pointer tagging could be used for one-bit reference counting, which can speed up the garbage collection process. That can be useful in cases where a direct one-to-one producer-consumer relationship was created at runtime: It would result directly in memory reuse instead of a garbage collector feeding.
So, using 2 bits for pointer tagging, there could be some wild combination of intense optimisation...
In case of Ints I could imagine these 4 tags:
Int
Int
Int
itselfInt
.我认为这是因为早期实现 GC 和所有其他东西的方法。如果您有 32 位可用,而您只需要 30 位,则可以使用这两个备用位来实现有趣的事情,例如在最低有效位中使用 0 表示值,使用 1 表示指针。
如今,实现不使用这些位,因此
Int
在 GHC 上至少有 32 位。 (这并不完全正确。IIRC 可以将一些标志设置为 30 或 31 位 Int)I think this is because of early ways to implement GC and all that stuff. If you have 32 bits available and you only need 30, you could use those two spare bits to implement interesting things, for instance using a zero in the least significant bit to denote a value and a one for a pointer.
Today the implementations don't use those bits so an
Int
has at least 32 bits on GHC. (That's not entirely true. IIRC one can set some flags to have 30 or 31 bitInt
s)