aboutsummaryrefslogtreecommitdiffgithub
path: root/packages/novice/novice/util.ts
blob: 8a8dc6f37d334e5b8a1ea8648e97e6e5483ee34b (plain)
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
export function maskTo(val: number, bits: number): number {
    return val & ((bits === 32) ? -1 : ~(-1 << bits));
}

export function sextTo(val: number, bits: number): number {
    return (bits === 32 || (val & (1 << (bits - 1))) === 0)
           ? val
           : val | (-1 << bits);
}

export function maxUnsignedVal(bits: number): number {
    // Need to do this instead of using bitwise operators to get around
    // the fact that js bitwise operators work on 32-bit two's complement ints
    return Math.pow(2, bits) - 1;
}

// 0x80000001 & 0xffffffff = -0x7fffffff in js. This is pretty confusing
// behavior when dealing with 32-bit addresses, so get the expected
// value by clearing the MSB and then adding 2^31 to set it again.
export function forceUnsigned(val: number, bits: number): number {
    return (bits < 32 || !(val & (1 << 31))) ? val : (val & ~(1 << 31)) + Math.pow(2, 31);
}

export function padStr(str: string, to: number, withChar: string, right?: boolean) {
    const need = to - str.length;
    for (let i = 0; i < need; i++) {
        str = right ? str + withChar : withChar + str;
    }
    return str;
}

export function fmtHex(val: number, bits: number): string {
    return '0x' + padStr(forceUnsigned(val, bits).toString(16),
                         Math.ceil(bits / 4), '0');
}

export function fmtBin(val: number, bits: number): string {
    return '0b' + padStr(forceUnsigned(val, bits).toString(2), bits, '0');
}

export function fmtBinOrHex(val: number, bits: number): string {
    return (bits <= 4) ? fmtBin(val, bits) : fmtHex(val, bits);
}

export function range(n: number): number[] {
    return [...new Array(n).keys()];
}