-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomprakt_bench_safe_arrays.java
231 lines (198 loc) · 5.24 KB
/
comprakt_bench_safe_arrays.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
class Main {
public void div_by_constant() {
int i = 0;
int prod = 0;
while (i < 1024 * 400) {
SafeIntMatrix dividend = new SafeIntMatrix(); dividend.init(16, 16);
{
int x = 0;
while (x < dividend.len) {
int y = 0;
while (y < dividend.len2(x)) {
LehmerRandom gen = new LehmerRandom().initWithDefault();
dividend.set(x, y, gen.random());
y = y + 1;
}
x = x + 1;
}
}
LehmerRandom gen = new LehmerRandom().initWithDefault();
int x = gen.random() % dividend.len;
int y = gen.random() % dividend.len2(0);
int divisor = dividend.get(x, y);
dividend.div_by(divisor);
prod = prod * dividend.get(x, y);
i = i + 1;
}
System.out.println(prod); /* Should be 1 */
}
public static void main(String[] args) {
Main main = new Main();
main.div_by_constant();
}
}
/* Adapted from: https://en.wikipedia.org/wiki/Lehmer_random_number_generator */
class LehmerRandom {
public int M; /* 2^31 - 1 (A large prime number) */
public int A; /* Prime root of M, passes statistical tests and produces a full cycle */
public int Q; /* M / A (To avoid overflow on A * seed) */
public int R; /* M % A (To avoid overflow on A * seed) */
public int seed;
public LehmerRandom init(int seed){
this.M = 2147483647;
this.A = 16807;
this.Q = 127773;
this.R = 2836;
this.seed = seed;
return this;
}
public LehmerRandom initWithDefault(){
return init(2147480677);
}
public int random() {
int hi = seed / Q;
int lo = seed % Q;
int test = A * lo - R * hi;
if (test <= 0)
test = test + M;
seed = test;
return test;
}
public int next(){
return random();
}
/**
* @param min minimum number
* @param max exclusive range end
*/
public int nextRange(int min, int max){
return next() % (max - min) + min;
}
public int[] intArray(int size, int min, int maxEx){
int[] arr = new int[size];
int i = 0;
while (i < size){
arr[i] = nextRange(min, maxEx);
i = i + 1;
}
return arr;
}
public boolean nextBoolean(){
return next() % 2 == 0;
}
public boolean[] booleanArray(int size){
boolean[] arr = new boolean[size];
int i = 0;
while (i < size){
arr[i] = nextBoolean();
i = i + 1;
}
return arr;
}
public void shuffleIntArray(int[] arr, int size) {
int i = size - 1;
while (i > 0){
int index = nextRange(0, i + 1);
int a = arr[index];
arr[index] = arr[i];
arr[i] = a;
i = i - 1;
}
}
}
class Error {
public void raise() {
/* This results in an abort by our runtime */
int[] panic = new int[-1];
}
}
class SafeIntArray {
public int len;
public int[] data;
public SafeIntArray init(int len) {
this.data = new int[len];
this.len = len;
return this;
}
public int get(int idx) {
if (0 <= idx && idx < this.len) {
return data[idx];
} else {
new Error().raise();
return -1; /* Unreachable */
}
}
public void set(int idx, int value) {
if (0 <= idx && idx < this.len) {
data[idx] = value;
} else {
new Error().raise();
}
}
}
class SafeIntMatrix {
public int len;
public SafeIntArray[] data;
public SafeIntMatrix init(int len, int len2) {
this.data = new SafeIntArray[len];
this.len = len;
int i = 0;
while (i < len) {
this.data[i] = new SafeIntArray();
this.data[i].init(len2);
i = i+1;
}
return this;
}
public int get(int idx, int idx2) {
if (0 <= idx && idx < this.len) {
return data[idx].get(idx2);
} else {
new Error().raise();
return -1; /* Unreachable */
}
}
public int len2(int idx) {
if (0 <= idx && idx < this.len) {
return data[idx].len;
} else {
new Error().raise();
return -1; /* Unreachable */
}
}
public void set(int idx, int idx2, int value) {
if (0 <= idx && idx < this.len) {
data[idx].set(idx2, value);
} else {
new Error().raise();
}
}
public void div_by(int divisor) {
int x = 0;
while (x < len) {
int y = 0;
while (y < len2(x)) {
set(x, y, get(x, y) / divisor);
y = y + 1;
}
x = x + 1;
}
}
public void add(SafeIntMatrix that) {
if (this.len != that.len) {
new Error().raise();
}
int x = 0;
while (x < this.len) {
if (this.len != that.len) {
new Error().raise();
}
int y = 0;
while (y < this.len2(x)) {
this.set(x, y, this.get(x, y) + that.get(x, y));
y = y + 1;
}
x = x + 1;
}
}
}