+/TECS

git clone https://git.julia.blue/+/TECS

P1n3appl3Better comparison support for jackb17cc4b

master
4.7 KiB172 linesraw
1/**
2 * A library of commonly used mathematical functions.
3 * Note: Jack compilers implement multiplication and division using OS method calls.
4 */
5class Math {
6    static Array mask;
7    static int seed; // For rand()
8
9    /** Initializes the library. */
10    function void init() {
11        var int temp, i;
12        let mask = Array.new(16);
13        let temp = 1;
14        let i = 0;
15        while(i < 16){
16            let mask[i] = temp;
17            let temp = temp + temp;
18            let i = i + 1;
19        }
20        return;
21    }
22
23    /** Returns the absolute value of x. */
24    function int abs(int x) {
25        if(x < 0){
26            let x = -x;
27        }
28        return x;
29    }
30
31    /** Returns the product of x and y.
32     *  When a Jack compiler detects the multiplication operator '*' in the
33     *  program's code, it handles it by invoking this method. In other words,
34     *  the Jack expressions x*y and multiply(x,y) return the same value.
35     */
36    function int multiply(int x, int y) {
37        var boolean negative;
38        var int sum, shiftedX, i;
39        let negative = false;
40        let sum = 0;
41        let i = 0;
42        if(x < 0){
43            let negative = true;
44        }
45        if(y < 0){
46            let negative = ~negative;
47        }
48        let x = Math.abs(x);
49        let y = Math.abs(y);
50        let shiftedX = x;
51        while(i < 16){
52            if((mask[i] & y) ~= 0){
53                let sum = sum + shiftedX;
54            }
55            let shiftedX = shiftedX + shiftedX;
56            let i = i + 1;
57        }
58        if(negative){
59            let sum = -sum;
60        }
61        return sum;
62    }
63
64    /** Returns the integer part of n/d.
65     *  When a Jack compiler detects the multiplication operator '/' in the
66     *  program's code, it handles it by invoking this method. In other words,
67     *  the Jack expressions n/d and divide(n,d) return the same value.
68     */
69    function int divide(int n, int d){
70        var int result, a, b;
71        var boolean negative;
72        if(d == 0){
73            do Sys.error(3);
74        }
75        let negative = false;
76        if(n < 0){
77            let negative = true;
78        }
79        if(d < 0){
80            let negative = ~negative;
81        }
82        let n = Math.abs(n);
83        let d = Math.abs(d);
84        let result = 0;
85        while(n >= d){
86            let a = d;
87            let b = 1;
88            while((a < 16384) & ((a + a) < n)){
89                let a = a + a;
90                let b = b + b;
91            }
92            let result = result + b;
93            let n = n - a;
94        }
95        if(negative){
96            let result = -result;
97        }
98        return result;
99    }
100
101    /** Returns the integer part of the square root of x. */
102    function int sqrt(int x) {
103        var int i, y, temp;
104        if(x < 0){
105            do Sys.error(4);
106        }
107        let i = 7;
108        let y = 0;
109        while(i >= 0){
110            let temp = (y + mask[i]) * (y + mask[i]);
111            if(((temp < x) | (temp == x)) & (temp > 0)){
112                let y = y + mask[i];
113            }
114            let i = i - 1;
115        }
116        return y;
117    }
118
119    /** Returns the product of a%b.
120     *  When a Jack compiler detects the modulus operator '%' in the
121     *  program's code, it handles it by invoking this method. In other words,
122     *  the Jack expressions n%d and mod(n,d) return the same value.
123     */
124    function int mod(int a, int b){
125        return a - ((a / b) * b);
126    }
127
128    /** Returns the greater number. */
129    function int max(int a, int b) {
130        if(a > b){
131            return a;
132        }
133        return b;
134    }
135
136    /** Returns the smaller number. */
137    function int min(int a, int b) {
138        if(a < b){
139            return a;
140        }
141        return b;
142    }
143
144    /** Seeds the pseudo-random number generator. */
145    function void seedRandom(int s){
146        let seed = s;
147        return;
148    }
149
150    /** Returns a pseudo-random positive integer in the range
151     *  [0, 32749). The upper bound is the largest prime smaller
152     *  than the MAX_INT of the HACK platform. The constants 19
153     *  and 1 are some constants experimentally verified to give
154     *  a decently irregular distribution. Note that due to the
155     *  nature of linear congruential generators, certain bits
156     *  of the result will be more "random" than others.
157     */
158    function int rand(){
159        var int temp;
160        let temp = ((seed * 19) + 1) % 32749;
161        let seed = temp;
162        return Math.abs(temp);
163    }
164
165    /** Returns a pseudo-random integer in range [min, max).
166     *  Warning: behavior is undefined if max <= min or either min
167     *  or max are negative
168     */
169    function int randRange(int min, int max){
170        return (Math.rand() % ((max - min) - 1)) + min;
171    }
172}