Efficient 64 bit arithmetic

Vyacheslav Egorov vegorov at google.com
Wed Jul 9 02:36:28 PDT 2014

Hi Fabrice,

The main reason why I was proposing (hi, lo)-pair based API instead of
lower version is to

a) avoid necessity writing low-level looking code in the high-level
language which is JS;

b) avoid pattern matching in the JS code to recognize construction
built out of low-level 32-bit counterparts.

This is too much like first writing 32-bit assembly and then trying to
pattern match it to emit 64-bit one. If we are starting to add
low-level intrisics like that maybe it's time to actually agree that
we want a bytecode with a full range of numeric types and operations?

Math.H part of the proposal was an awful hack though in attempt to
avoid degrading polyfill performance while keeping API high-level and

To be honest I think the actual API for high level language like JS
should be either value-object or pair based.

Given that ES6 defines typed-arrays it might be possible to make API
based on that:

var a = new Int32Array(2),
      b = new Int32Array(2),
      c = new Int32Array(2);
Math.div64(c, a, b);

But again it has bad performance unless you pool both a, b, c in the
right way (pooling is good for unoptimized code, but pooling globally
is bad for optimized code).

Lowered version

cl = Math.div64l(al, ah, bl, bh);
ch = Math.div64h(al, ah, bl, bh);

should perform well (no allocations, potentially only some boxing if
engine requires that) and can be assembled into a single instruction
by the optimizer... but this just looks slightly bizarre.

// Vyacheslav Egorov

More information about the es-discuss mailing list