191

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.

3
  • 7
    Supporting that quote: The JavaScript engine has to do this operation (which is called ToInt32 in the spec) whenver you apply a bitwise operator to a number and at various other times (unless the engine has been able to maintain the number as an integer as an optimization, but in many cases it cannot). Commented Jun 21, 2018 at 10:55
  • 1
    Note that the crucial difference to the usual FCVTZS instruction provided for this job is that FJCVTZS has a different behaviour on overflow. Namely, you always get the least 32 bits whereas FCVTZS seems to do something different instead if the number doesn't fit. The correct behaviour of FJCVTZS seems to be slightly tricky to implement otherwise.
    – fuz
    Commented Jun 21, 2018 at 10:58
  • 1
    @Tim I agree with TJ, minutes of the meeting are off-topic (and I suppose they're also restricted then hardly you can have them) but to run JavaScript on an ARM-powered device IS a thing, see Building IoT devices with JavaScript Commented Jun 21, 2018 at 10:59

1 Answer 1

160

It is because JS uses double precision for the numbers, but if you want to perform operations with bits, the task is nontrivial, so a specific instruction to convert JS double into integer makes the thing easier.

This ARM link explains it very well: https://community.arm.com/processors/b/blog/posts/armv8-a-architecture-2016-additions

In order to add more information regarding fuz's comment, the differences between FCVTZS and FJCVTZS (both of them convert floating point to int) are that in case of overflow, FJCVTZS value will be 0x80000000 instead of overflowing. Furthermore, FJCVTZS can generate an exception in order to indicate how the conversion was (i.e. inexact).

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

4
  • 9
    You should explain why the existing FCVTZS instruction is insufficient for the task.
    – fuz
    Commented Jun 21, 2018 at 11:02
  • 19
    It's not insufficient, it's much SLOWER (let's say at least 2/3 times) because it requires 10 different instructions to perform the same task. Of course impact on a larger algorithm is MUCH lower than that but when working on a smaller A53 both speed and memory will benefit of this.) Now let's move this to, for example, V8 compiled for ARM and it will definitely be worthy of some CPU space. Commented Jun 21, 2018 at 11:15
  • Is this instruction used only when writing JS runtime like v8? In general, is it useful in other cases?
    – wlnirvana
    Commented Oct 18, 2020 at 14:32
  • 7
    @wlnirvana It's also useful for getting x86_64 behavior on ARM stackoverflow.com/questions/66279679 Commented Feb 19, 2021 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.