Skip to content

Commit

Permalink
✨ feat(mul64): Add multiplication function.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Dec 6, 2020
1 parent 6229fa3 commit 635821d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './big64' ;
export * from './compare' ;
export * from './get64' ;
export * from './limits' ;
export * from './mul64' ;
export * from './not64' ;
export * from './rotl64' ;
export * from './rotr64' ;
Expand Down
27 changes: 27 additions & 0 deletions src/mul64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {add64} from './add64';

// TODO finish need function that multiplies two 32 bit values to 64 bit
const mul3264 = (a, b) => {
const a0 = (a >>> 16) & 0xFFFF;
const a1 = a & 0xFFFF;
const b0 = (b >>> 16) & 0xFFFF;
const b1 = b & 0xFFFF;

const x = [Math.imul(a0,b0), Math.imul(a1, b1)];
const t = Math.imul(a0,b1);
const u = Math.imul(a1,b0);
const y = [(t >>> 16) | 0, (t << 16) | 0];
const z = [(u >>> 16) | 0, (u << 16) | 0];
return add64(x,add64(y,z));
};

export function mul64 (a, b) {

const [a0, a1] = a;
const [b0, b1] = b;

const x = [(Math.imul(a1, b0) + Math.imul(a0, b1)) | 0, 0];
const y = mul3264(a1, b1);
return add64(x, y);

}
22 changes: 22 additions & 0 deletions test/src/mul64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'ava';

import { mul64 , get64 } from '../../src' ;

function macro (t, A, B, EXPECTED) {
const a = get64(...A);
const b = get64(...B);
const expected = get64(...EXPECTED);
t.deepEqual(mul64(a, b), expected, 'a * b');
t.deepEqual(mul64(b, a), expected, 'b * a');
}

macro.title = (providedTitle, a, b, expected) => `${providedTitle || ''} ${a} * ${b} === ${expected}`.trim();

test(macro, [0x00000000, 0x00000000], [0x00000000, 0x00000000], [0x00000000, 0x00000000]);
test(macro, [0x00000000, 0x00000001], [0x00000000, 0x00000000], [0x00000000, 0x00000000]);
test(macro, [0x00000000, 0x00000001], [0xFFFFFFFF, 0xFFFFFFFF], [0xFFFFFFFF, 0xFFFFFFFF]);
test(macro, [0x00000000, 0x00000002], [0xFFFFFFFF, 0xFFFFFFFF], [0xFFFFFFFF, 0xFFFFFFFE]);
test(macro, [0xFFFFFFFF, 0xFFFFFFFF], [0xFFFFFFFF, 0xFFFFFFFF], [0x00000000, 0x00000001]);
test(macro, [0x80000000, 0x00000000], [0x80000000, 0x00000000], [0x00000000, 0x00000000]);
test(macro, [0x00000000, 0x80000000], [0x00000000, 0x80000000], [0x40000000, 0x00000000]);
test(macro, [0x80000000, 0x80000000], [0x80000000, 0x80000000], [0x40000000, 0x00000000]);

0 comments on commit 635821d

Please sign in to comment.