summaryrefslogtreecommitdiff
path: root/npc/functions/bitwise.txt
blob: a2e57587b20ccdb4828976a300a090ead4a440b9 (plain) (blame)
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
65
66
/**
 * Gets a bitmasked value in from an integer. If the shift is omitted, it will
 * be deduced from the mask.
 *
 * @arg 0 - the variable
 * @arg 1 - mask
 * @arg 2 - shift
 */
function	script	bitwise_get	{
    .@shift = getarg(2, 0);

    if (getargcount() < 3) {
        // guess the shift from the mask:
        for (.@shift = 0; .@shift < 32; ++.@shift) {
            if ((getarg(1) & (1 << .@shift)) != 0) {
                break;
            }
        }
    }

    return (getarg(0) & getarg(1)) >> .@shift;
}

/**
 * sets a bitmasked value in a variable
 *
 * @arg 0 - the target variable
 * @arg 1 - mask
 * @arg 2 - shift
 * @arg 3 - new value
 * @return a reference to the variable
 */
function	script	bitwise_set	{
    if (getargcount() < 4) {
        // guess the shift from the mask:
        for (.@shift = 0; .@shift < 32; ++.@shift) {
            if ((getarg(1) & (1 << .@shift)) != 0) {
                break;
            }
        }

        return set(getarg(0), (getarg(0) & ~(getarg(1))) | (getarg(2, 0) << .@shift));
    }

    return set(getarg(0), (getarg(0) & ~(getarg(1))) | (getarg(3, 0) << getarg(2, 0)));
}


// bitmask_count(<int>)
//    returns the number of bits set in <int> (up to 4096?)
function	script	bitmask_count	{
    .@n = getarg(0); // Number evaluated
    .@p=0; // Bits set/unset
    .@s=0; // Stack and Check
    .@m=0; // Memory

    // Loop only as needed
    while (.@s < .@n) {
        .@s=2**.@m;
        if (.@n & .@s)
            .@p++;
        .@m++;
    }
    return .@p;
}