-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfunc_search.py
executable file
·428 lines (317 loc) · 8.76 KB
/
func_search.py
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
"""
func_search.py -- Search for functions to replace a lookup table.
See http://www.oilshell.org/blog/2016/12/23.html
"""
from collections import Counter, defaultdict
import inspect
import json
import sys
DESIRED_DIST = sorted([1, 1, 3, 3, 1, 15, 12, 10, 11, 12, 10, 8, 8, 4, 43, 20, 1, 16, 4, 25, 13], reverse=True)
def ik1(tok):
return tok & (-tok - 23)
def ik2(tok):
return (tok & 230) & (tok + 13)
def ik3(tok):
return 12 | tok | (tok + 15)
def ik4(tok):
return (tok + 108) | (tok ^ 163)
def ik5(tok):
return (tok ^ 35) & (tok + 20)
def ik6(tok):
return (12 -tok) & (tok ^ 6) & (tok + 14)
def ik7(tok):
return 175 & tok & ((tok ^ 173) + 11)
# Blog post code
def LookupKind1(id):
kind = id & (-id - 23)
if kind: return kind
if id <= 1: return 20
if id <= 9: return 44
if id <= 40: return 48
if id <= 72: return 50
if id <= 105: return 4
return 2
def LookupKind2(id):
return (id & 230) & (id + 13) if id else 32
def Deficit(want, need):
n = min(len(want), len(need))
deficit = []
for i in range(n):
d = need[i] - want[i]
deficit.append(d if d < 0 else 0)
return deficit
def Show(label, a):
sys.stdout.write('%12s' % label)
print(''.join('%4d' % item for item in a))
def Hist(f):
h = Counter(f(tok) for tok in range(256))
pairs = sorted(h.items(), key=lambda p: p[1], reverse=True)
return pairs
def FunctionBody(f):
lines, _ = inspect.getsourcelines(f)
return lines[1]
def TestFunc(f):
"""See if a given function has the desired distribution of return values."""
print(f)
print(FunctionBody(f))
hist = Hist(f)
#print(hist)
#if any(k > 255 for k in hist.keys()):
# raise AssertionError('OUT OF RANGE')
# Compare the two
# TODO: sort in the same order?
Show('func range', [result for result, _ in hist])
#pad = [0] * offset
pad = []
want = pad + DESIRED_DIST
need = [freq for _, freq in hist]
d = Deficit(want, need)
Show('want dist', want)
Show('have dist', need)
Show('deficit', d)
print('Unique values:', len(hist))
print()
def NumDeficits(dist, desired_dist):
"""Return the number of kinds that are too small.
Args:
dist: array sorted in reverse order
desired_dist: ditto
"""
#need = set(desired_dist)
n = len(desired_dist)
num_deficits = 0
for i in range(n):
if dist[i] < desired_dist[i]:
num_deficits += 1
return num_deficits
def ScoreFunction(hist):
func_range_size = len(hist)
if func_range_size < 21:
#print('%d - %d values not enough' % (i, func_range_size))
return
dist = [freq for _, freq in hist]
func_range = [result for result, _ in hist]
if any(v > 255 for v in func_range):
#print('%d - %d is too big' % (0, max(func_range)))
return
if any(v < 0 for v in func_range):
#print('%d - %d is too small' % (0, min(func_range)))
return
num_deficits = NumDeficits(dist, DESIRED_DIST)
#print('n = %d, deficit %d' % (i, deficit))
if num_deficits <= 1:
print("Kinds that don't fit: %d" % num_deficits)
d = Deficit(DESIRED_DIST, dist)
Show('func range', func_range)
Show('want dist', DESIRED_DIST)
Show('have dist', dist)
Show('deficit', d)
return num_deficits
def f1(tok, n):
return tok & (-tok - n)
def g1(tok, n):
return tok & (tok - n)
def Form1(f):
#return
min_deficit = 256
for i in range(256):
#hist = Hist(lambda tok: f(tok, i))
hist = Hist(lambda tok: f(tok, i))
num_deficits = ScoreFunction(hist)
if num_deficits is not None:
min_deficit = min(min_deficit, num_deficits)
print('Min deficit: %d'% min_deficit)
def f2_1(tok, i, j):
return (tok & i) & (tok + j)
def f2_2(tok, i, j):
return (tok ^ i) & (tok + j)
# This is horrible
def f2_3(tok, i, j):
return (tok ^ i) & (tok & j)
# This is bad too -- most are too big
def f2_4(tok, i, j):
return (tok + i) & (tok - j)
# min deficit: 3
def f2_5(tok, i, j):
return (tok | i) & (tok + j)
# These are all bad. minimum 11 deficits!
def f2_6(tok, i, j):
return (tok | i) & (tok | j)
def f2_7(tok, i, j):
return (tok & i) | (tok & j)
def f2_8(tok, i, j):
return (tok ^ i) & (tok ^ j)
# not bad, but not good. min deficit 2
def f2_9(tok, i, j):
return (tok & i) ^ (-tok - j)
def f2_10(tok, i, j):
return (tok & i) ^ (-tok & j)
def f2_11(tok, i, j):
return (i - tok) & (j - tok)
def f2_12(tok, i, j):
return (i - tok) | (j - tok)
# Too many negative results
def f2_13(tok, i, j):
return i | tok | (j - tok)
# Doesn't work either
def f2_14(tok, i, j):
return - (i | tok | (j - tok))
def f2_15(tok, i, j):
return (i & tok) & (tok | j)
def f2_16(tok, i, j):
return (tok ^ i) & (tok | j)
#
# Functions involving shifts
#
def f3_1(tok, s, j):
return (tok >> s) & (- j - tok)
def f3_2(tok, s, j):
return (tok >> s) & (tok - j)
def f3_3(tok, s, j):
return (tok >> s) & (tok + j)
def f3_4(tok, s, j):
return (tok >> s) & (tok ^ j)
def f3_5(tok, s, j):
return (tok >> s) & (tok | j)
def f3_6(tok, s, j):
return (tok | j) >> s
def f3_7(tok, s, j):
return (tok & j) >> s
def f3_8(tok, s, j):
return (tok ^ j) >> s
def Form2(f):
#return
deficit1 = 0
min_deficit = 256
for i in range(256):
for j in range(256):
#hist = Hist(lambda tok: f(tok, i))
hist = Hist(lambda tok: f(tok, i, j))
num_deficits = ScoreFunction(hist)
if num_deficits == 1:
deficit1 += 1
print('i = %d, j = %d' % (i, j))
if num_deficits is not None:
min_deficit = min(min_deficit, num_deficits)
print('Min deficit: %d' % min_deficit)
print('number of solutions with a deficit of 1: %d' % deficit1)
def Form3(f):
#return
deficit1 = 0
min_deficit = 256
for s in range(8):
for j in range(256):
#hist = Hist(lambda tok: f(tok, i))
hist = Hist(lambda tok: f(tok, s, j))
num_deficits = ScoreFunction(hist)
if num_deficits == 1:
deficit1 += 1
print('i = %d, j = %d' % (s, j))
if num_deficits is not None:
min_deficit = min(min_deficit, num_deficits)
print('Min deficit: %d' % min_deficit)
print('number of solutions with a deficit of 1: %d' % deficit1)
def f4_1(tok, i, j, k):
return (tok ^ i) & (tok + j) & (tok + k)
def f4_2(tok, i, j, k):
return (tok ^ i) & (tok ^ j) & (tok + k)
# FOUND A deficit of 1 here! (Actually 12 of them)
def f4_3(tok, i, j, k):
return (-tok + i) & (tok + j) & (tok + k)
def f4_4(tok, i, j, k):
return (-tok + i) & (-tok + j) & (tok + k)
# WOWWWW
# num deficits 0, i = 12, j = 6, k = 14
# Mysterious.
#
# (-tok + 12) & (tok ^ 6) & (tok + 14)
def f4_5(tok, i, j, k):
return (-tok + i) & (tok ^ j) & (tok + k)
def Form4(f):
deficit1 = 0
min_deficit = 256
# 2^5 bits * 3 -- less than 2^8 * 2
for i in range(-16, 16):
for j in range(-16, 16):
for k in range(-16, 16):
hist = Hist(lambda tok: f(tok, i, j, k))
num_deficits = ScoreFunction(hist)
if num_deficits == 1:
deficit1 += 1
if num_deficits in (0, 1):
print('num deficits %d, i = %d, j = %d, k = %d' % (num_deficits, i, j, k))
print('')
if num_deficits == 0:
raise RuntimeError('Found exact solution')
if num_deficits is not None:
min_deficit = min(min_deficit, num_deficits)
print('Min deficit: %d' % min_deficit)
print('number of solutions with a deficit of 1: %d' % deficit1)
def Search():
print(sorted(DESIRED_DIST))
print(sum(DESIRED_DIST))
print('-'* 70)
# ik3 and ik4 are out of range
TestFunc(ik1)
TestFunc(ik2)
TestFunc(ik5)
TestFunc(ik6)
TestFunc(ik7)
TestFunc(LookupKind1)
TestFunc(LookupKind2)
Form1(f1)
Form1(g1)
#Form2(f2)
#Form2(g2)
#Form2(h2) BAD
#Form2(j2)
#Form2(f2_1)
#Form2(f2_6)
#Form2(f2_7)
#Form2(f2_8)
#Form2(f2_9)
#Form2(f2_10)
#Form2(f2_11)
#Form2(f2_12)
#Form2(f2_16)
#Form3(f3_1)
#Form3(f3_2)
#Form3(f3_5)
#Form3(f3_6)
#Form3(f3_7)
#Form3(f3_8)
#Form4(f4_3) # DEFICIT 1
#Form4(f4_4)
# EXACT ONE
Form4(f4_5)
# Then search for functions with a similar distribution?
def PrintSolution(name):
f = getattr(sys.modules[__name__], name)
#print(FunctionBody(f))
hist = defaultdict(list)
for i in range(256):
k = f(i)
hist[k].append(i)
solution = {}
items = sorted(hist.items(), key=lambda p: len(p[1]), reverse=True)
# TODO: Assign it to desired_dist
i = 0
for k, id_list in items:
d = DESIRED_DIST[i] if i < len(DESIRED_DIST) else 0
print(d, ':', len(id_list), 'ids-of-kind', k, ':',
' '.join(str(i) for i in id_list))
i += 1
#json.dump(solution, sys.stdout, indent=None)
def main(argv):
action = argv[1]
if action == 'search':
Search()
elif action == 'print':
PrintSolution(argv[2])
if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print('ABORT: %s' % e, file=sys.stderr)
sys.exit(1)