-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarryless_div_demo_util.js
64 lines (58 loc) · 1.6 KB
/
carryless_div_demo_util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// @flow
'use strict';
/* ::
import { type ArithmeticType, impossible } from './carryless_demo_common';
*/
/* global impossible */
/* ::
type Intermediate = {
intermediate: BigInteger,
offset: number,
};
*/
// eslint-disable-next-line no-unused-vars
const computeIntermediates = (
a /* : BigInteger */,
b /* : BigInteger */,
arithmeticType /* : ArithmeticType */
) /* : Intermediate[] */ => {
const intermediates = [];
let r = a;
switch (arithmeticType) {
case 'standard':
for (let offset = 0; ; ) {
if (r.compareTo(b) < 0) {
intermediates.push({ intermediate: r, offset });
break;
}
const rLength = r.bitLength();
let shift = rLength - b.bitLength();
let bShift = b.shiftLeft(shift);
if (r.compareTo(bShift) < 0) {
shift -= 1;
bShift = bShift.shiftRight(1);
}
intermediates.push({ intermediate: r.shiftRight(shift), offset });
r = r.subtract(bShift);
offset += rLength - r.bitLength();
}
break;
case 'carry-less':
for (let offset = 0; ; ) {
if (r.bitLength() < b.bitLength()) {
intermediates.push({ intermediate: r, offset });
break;
}
const rLength = r.bitLength();
const shift = rLength - b.bitLength();
intermediates.push({ intermediate: r.shiftRight(shift), offset });
r = r.xor(b.shiftLeft(shift));
offset += rLength - r.bitLength();
}
break;
default:
return impossible(arithmeticType);
}
return intermediates;
};
/* :: export { computeIntermediates }; */