-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(mul64): Add multiplication function.
- Loading branch information
1 parent
6229fa3
commit 635821d
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |