-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_map_base.py
124 lines (114 loc) · 4.4 KB
/
get_map_base.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
def get_map_base(name):
"""
Returns a mapping base for use in stochastic models.
Parameters:
- name: A string containing the name of the mapping model to be used.
The mapping base returned is a list of functions, where each function
takes a value `v` and returns the mapping for that base.
Available mappings:
- 'F9': The standard 8-bit model. The first base is constant 1, followed by each bit (from MSB to LSB).
- 'F17': Similar to F9 but for a 16-bit model.
- 'F17xor': Model F17 plus 8 bits corresponding to the XOR between the two bytes in the 16-bit value.
- 'F17tran': Model F17 plus 8 bits corresponding to transitions 0->1 and 1->0.
"""
def bitget(v, pos):
return (int(v) >> (pos - 1)) & 1
if name == 'F9':
map_base = [
lambda v: 1,
lambda v: bitget(v, 8),
lambda v: bitget(v, 7),
lambda v: bitget(v, 6),
lambda v: bitget(v, 5),
lambda v: bitget(v, 4),
lambda v: bitget(v, 3),
lambda v: bitget(v, 2),
lambda v: bitget(v, 1)
]
elif name == 'F17':
map_base = [
lambda v: 1,
lambda v: bitget(v, 16),
lambda v: bitget(v, 15),
lambda v: bitget(v, 14),
lambda v: bitget(v, 13),
lambda v: bitget(v, 12),
lambda v: bitget(v, 11),
lambda v: bitget(v, 10),
lambda v: bitget(v, 9),
lambda v: bitget(v, 8),
lambda v: bitget(v, 7),
lambda v: bitget(v, 6),
lambda v: bitget(v, 5),
lambda v: bitget(v, 4),
lambda v: bitget(v, 3),
lambda v: bitget(v, 2),
lambda v: bitget(v, 1)
]
elif name == 'F17xor':
map_base = [
lambda v: 1,
lambda v: bitget(v, 16),
lambda v: bitget(v, 15),
lambda v: bitget(v, 14),
lambda v: bitget(v, 13),
lambda v: bitget(v, 12),
lambda v: bitget(v, 11),
lambda v: bitget(v, 10),
lambda v: bitget(v, 9),
lambda v: bitget(v, 8),
lambda v: bitget(v, 7),
lambda v: bitget(v, 6),
lambda v: bitget(v, 5),
lambda v: bitget(v, 4),
lambda v: bitget(v, 3),
lambda v: bitget(v, 2),
lambda v: bitget(v, 1),
lambda v: bitget(v, 16) ^ bitget(v, 8),
lambda v: bitget(v, 15) ^ bitget(v, 7),
lambda v: bitget(v, 14) ^ bitget(v, 6),
lambda v: bitget(v, 13) ^ bitget(v, 5),
lambda v: bitget(v, 12) ^ bitget(v, 4),
lambda v: bitget(v, 11) ^ bitget(v, 3),
lambda v: bitget(v, 10) ^ bitget(v, 2),
lambda v: bitget(v, 9) ^ bitget(v, 1)
]
elif name == 'F17tran':
map_base = [
lambda v: 1,
lambda v: bitget(v, 16),
lambda v: bitget(v, 15),
lambda v: bitget(v, 14),
lambda v: bitget(v, 13),
lambda v: bitget(v, 12),
lambda v: bitget(v, 11),
lambda v: bitget(v, 10),
lambda v: bitget(v, 9),
lambda v: bitget(v, 8),
lambda v: bitget(v, 7),
lambda v: bitget(v, 6),
lambda v: bitget(v, 5),
lambda v: bitget(v, 4),
lambda v: bitget(v, 3),
lambda v: bitget(v, 2),
lambda v: bitget(v, 1),
lambda v: (~bitget(v, 16) & bitget(v, 8)),
lambda v: (~bitget(v, 15) & bitget(v, 7)),
lambda v: (~bitget(v, 14) & bitget(v, 6)),
lambda v: (~bitget(v, 13) & bitget(v, 5)),
lambda v: (~bitget(v, 12) & bitget(v, 4)),
lambda v: (~bitget(v, 11) & bitget(v, 3)),
lambda v: (~bitget(v, 10) & bitget(v, 2)),
lambda v: (~bitget(v, 9) & bitget(v, 1)),
lambda v: (bitget(v, 16) & ~bitget(v, 8)),
lambda v: (bitget(v, 15) & ~bitget(v, 7)),
lambda v: (bitget(v, 14) & ~bitget(v, 6)),
lambda v: (bitget(v, 13) & ~bitget(v, 5)),
lambda v: (bitget(v, 12) & ~bitget(v, 4)),
lambda v: (bitget(v, 11) & ~bitget(v, 3)),
lambda v: (bitget(v, 10) & ~bitget(v, 2)),
lambda v: (bitget(v, 9) & ~bitget(v, 1))
]
else:
raise ValueError(f"Unknown base name: {name}")
return map_base