Why do ARM chips have an instruction with Javascript in the name (FJCVTZS)?

FJCVTZS is "Floating-point Javascript Convert to Signed fixed-point, rounding toward Zero". It is supported in Arm v8.3-A chips and later. Which is odd, because you don't expect to see JavaScript so close to the bare metal.

I can find explanations of what the instruction does, but not why it exists. This thread says "it exists as a single instruction is because JS's lack of an integer type means certain use cases need this operation obscenely often for no good algorithmic reason.". That's plausible but I would like a more detailed understanding.

72239 次浏览

这是因为 JS 对数字使用双精度,但是如果您希望使用比特执行操作,那么这项任务就非同小可,因此一个将 JS double 转换为整数的特定指令可以使这项工作变得更加容易。

这个 ARM 链接很好地解释了它: https://community.arm.com/processors/b/blog/posts/armv8-a-architecture-2016-additions

为了添加更多关于 fuz 评论的信息,FCVTZSFJCVTZS(两者都将浮点数转换为 int)的区别在于,在溢出的情况下,FJCVTZS的值将是0x80000000而不是溢出。此外,FJCVTZS可以生成一个异常,以指示转换是如何进行的(即不精确)。

FJCVTZS: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0801g/hko1477562192868.html

FCVTZS: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0802a/FCVTZS_float_int.html