TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The purpose of NaN Boxing

24 点作者 akg超过 12 年前

5 条评论

lmkg超过 12 年前
NaN-boxing is morally equivalent to using tagged unions to represent values of any type. It optimizes for fast floating-point operations, and for allowing a larger amount of meaningful values to be represented in 64 bits.<p>Most tagged-union approaches use some sort of punning so that at least one type of value is directly meaningful, without any bit manipulation. For example, some Common Lisp implementations will choose their tag bits for Cons cells such that NIL is represented with all zeros. Since NIL is the only falsey value and everything else is truthy, this means that any bit-pattern can be used directly for testing conditionals, with the correct semantics.<p>NaN-boxing is choosing a different type of punning for the tags, to optimize a different use case. First, all bit-patterns can be used directly as 64-bit floats with technically correct semantics--anything that's NaN-boxed is actually not a floating-point number. I'm given to understand that modern architectures are slow to mix float and bit ops, so it's nice that you don't need to mask your tags off of your float before operating on them.<p>Second, (double-precision) floats are usually the only type that require the full 64 bits to have meaning. In many applications, 50ish bits is 'good enough' for ints and pointers (with some extra ops to handle overflow), but floats are mandated by a standard and don't scale down gracefully. A tagged union wouldn't be able to contain a 64-bit float directly without spilling into an extra machine word. Unless, of course, the tags are punned to be part of the float value itself. And that's exactly what NaN-boxing is.
gus_massa超过 12 年前
In Racket, they use the alignment of the memory blocks:<p>* The even integer numbers are pointers.<p>* The odd integers represent fixnums (small integers) (n &#60;---&#62; 2*n+1).<p>With this representation the calculations with fixnums are quite fast because they don't need unboxing.
评论 #5146569 未加载
评论 #5146178 未加载
评论 #5146515 未加载
bitwize超过 12 年前
<i>I'm not really sure I understood the utility of this technique, that I see as an hack (it relies on the hardware not caring on the value of the mantissa in a NaN) but coming from a Java background I'm not used to the roughness of C.</i><p>The use of "signalling NaNs" is supported by IEEE 754. It may be a hack, but it's a hack that's in the standard.
malkia超过 12 年前
luajit was the first implementation I've seen, but it's author Mike Pall said soemewhere that this was an old technique, or more like that it could've been known before. I remember reading aome lisp paper from th 90s where sifferent encoding techniques/boxings were mentioned, and one of them was it (I thnink).
评论 #5146517 未加载
general_failure超过 12 年前
v8 doesn't use this technique - the use SMI.<p>Read also <a href="http://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations" rel="nofollow">http://wingolog.org/archives/2011/05/18/value-representation...</a>