-
Notifications
You must be signed in to change notification settings - Fork 1
/
TemplateUtilities.ttinclude
287 lines (260 loc) · 8.56 KB
/
TemplateUtilities.ttinclude
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
<#+
public static int GetMinLength()
=> 2;
public static int GetMaxLength()
=> 32;
public static string GetSwapIfGreaterBranchless(string a, string b, string temp = "temp")
=> $"SwapBranchlessGreaterThan(ref {a}, ref {b});";
public static string GetSwapIfLesserBranchless(string a, string b, string temp = "temp")
=> $"SwapBranchlessLessThan(ref {a}, ref {b});";
public static string GetSwapIfGreaterComparable(string a, string b, string temp = "temp")
=> $"if (GreaterThan(ref {a}, ref {b})) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static string GetSwapIfLesserComparable(string a, string b, string temp = "temp")
=> $"if (LessThan(ref {a}, ref {b})) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static string GetSwapIfGreaterComparableLegacy(string a, string b, string temp = "temp")
=> $"if ({a}.CompareTo({b}) > 0) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static string GetSwapIfLesserComparableLegacy(string a, string b, string temp = "temp")
=> $"if ({a}.CompareTo({b}) < 0) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static string GetSwapIfGreaterComparison(string a, string b, string comparison = "comparison", string temp = "temp")
=> $"if ({comparison}.Invoke({a}, {b}) > 0) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static string GetSwapIfLesserComparison(string a, string b, string comparison = "comparison", string temp = "temp")
=> $"if ({comparison}.Invoke({a}, {b}) < 0) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static string GetSwapIfGreaterComparisonPointer(string a, string b, string comparisonPointer = "comparisonPointer", string temp = "temp")
=> $"if ({comparisonPointer}({a}, {b}) > 0) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static string GetSwapIfLesserComparisonPointer(string a, string b, string comparisonPointer = "comparisonPointer", string temp = "temp")
=> $"if ({comparisonPointer}({a}, {b}) < 0) {{ {temp} = {a}; {a} = {b}; {b} = {temp}; }}";
public static IEnumerable<string> GetBranchlessTypeNames()
{
yield return "byte";
yield return "sbyte";
yield return "ushort";
yield return "short";
yield return "uint";
yield return "int";
}
public static string GetTypeDisplayName(string value)
{
switch (value)
{
case "byte": return "Byte";
case "sbyte": return "SByte";
case "ushort": return "UInt16";
case "short": return "Int16";
case "uint": return "UInt32";
case "int": return "Int32";
case "ulong": return "UInt64";
case "long": return "Int64";
default: return "";
}
}
public static int GetOptimalBenchmarkCount(int length)
{
return length switch
{
02 => 110_000_000,
03 => 110_000_000,
04 => 110_000_000,
05 => 90_000_000,
06 => 85_000_000,
07 => 85_000_000,
08 => 55_000_000,
09 => 50_000_000,
10 => 45_000_000,
11 => 43_000_000,
12 => 36_000_000,
13 => 35_000_000,
14 => 22_000_000,
15 => 21_000_000,
16 => 21_000_000,
17 => 13_000_000,
18 => 12_000_000,
19 => 12_000_000,
20 => 11_000_000,
21 => 11_000_000,
22 => 10_000_000,
23 => 10_000_000,
24 => 10_000_000,
25 => 10_000_000,
26 => 09_000_000,
27 => 09_000_000,
28 => 09_000_000,
29 => 09_000_000,
30 => 09_000_000,
31 => 09_000_000,
32 => 09_000_000,
_ => -1,
};
}
private static IEnumerable<(int, int)> PStarBracket(int i, int x, int j, int y)
{
switch (x)
{
case 1 when y == 1:
yield return (i, j);
break;
case 1 when y == 2:
yield return (i, j + 1);
yield return (i, j);
break;
case 2 when y == 1:
yield return (i, j);
yield return (i + 1, j);
break;
default:
{
int a = x / 2;
int b = (x & 0) == 1 ? y / 2 : (y + 1) / 2;
foreach (var temp in PStarBracket(i, a, j, b)) {
yield return temp;
}
foreach (var temp in PStarBracket(i + a, x - a, j + b, y - b)) {
yield return temp;
}
foreach (var temp in PStarBracket(i + a, x - a, j, b)) {
yield return temp;
}
break;
}
}
}
private static IEnumerable<(int, int)> PStar(int i, int m)
{
if (m <= 1)
{
yield break;
}
int a = m / 2;
foreach (var temp in PStar(i, a)) {
yield return temp;
}
foreach (var temp in PStar(i + a, m - a)) {
yield return temp;
}
foreach (var temp in PStarBracket(i, a, i + a, m - a)) {
yield return temp;
}
}
public static IEnumerable<(int, int)> GetBoseNelsonSwapMacros(int length)
=> PStar(1, length);
public static IEnumerable<(int, int)> GetBestKnownSwapMacros(int length)
=> length switch
{
9 => GetBestKnownSwapMacros9(),
10 => GetBestKnownSwapMacros10(),
11 => GetBestKnownSwapMacros11(),
12 => GetBestKnownSwapMacros12(),
13 => GetBestKnownSwapMacros13(),
14 => GetBestKnownSwapMacros14(),
15 => GetBestKnownSwapMacros15(),
16 => GetBestKnownSwapMacros16(),
_ => throw new Exception($"Invalid length `{length}`."),
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros9()
=> new[]
{
(0, 1), (3, 4), (6, 7),
(1, 2), (4, 5), (7, 8),
(0, 1), (3, 4), (6, 7), (2, 5),
(0, 3), (1, 4), (5, 8),
(3, 6), (4, 7), (2, 5),
(0, 3), (1, 4), (5, 7), (2, 6),
(1, 3), (4, 6),
(2, 4), (5, 6),
(2, 3),
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros10()
=> new[]
{
(4, 9), (3, 8), (2, 7), (1, 6), (0, 5),
(1, 4), (6, 9), (0, 3), (5, 8),
(0, 2), (3, 6), (7, 9),
(0, 1), (2, 4), (5, 7), (8, 9),
(1, 2), (4, 6), (7, 8), (3, 5),
(2, 5), (6, 8), (1, 3), (4, 7),
(2, 3), (6, 7),
(3, 4), (5, 6),
(4, 5)
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros11()
=> new[]
{
(0, 1), (2, 3), (4, 5), (6, 7), (8, 9),
(1, 3), (5, 7), (0, 2), (4, 6), (8, 10),
(1, 2), (5, 6), (9, 10), (0, 4), (3, 7),
(1, 5), (6, 10), (4, 8),
(5, 9), (2, 6), (0, 4), (3, 8),
(1, 5), (6, 10), (2, 3), (8, 9),
(1, 4), (7, 10), (3, 5), (6, 8),
(2, 4), (7, 9), (5, 6),
(3, 4), (7, 8),
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros12()
=> new[]
{
(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11),
(1, 3), (5, 7), (9, 11), (0, 2), (4, 6), (8, 10),
(1, 2), (5, 6), (9, 10), (0, 4), (7, 11),
(1, 5), (6, 10), (3, 7), (4, 8),
(5, 9), (2, 6), (0, 4), (7, 11), (3, 8),
(1, 5), (6, 10), (2, 3), (8, 9),
(1, 4), (7, 10), (3, 5), (6, 8),
(2, 4), (7, 9), (5, 6),
(3, 4), (7, 8),
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros13()
=> new[]
{
(1, 7), (9, 11), (3, 4), (5, 8), (0, 12), (2, 6),
(0, 1), (2, 3), (4, 6), (8, 11), (7, 12), (5, 9),
(0, 2), (3, 7), (10, 11), (1, 4), (6, 12),
(7, 8), (11, 12), (4, 9), (6, 10),
(3, 4), (5, 6), (8, 9), (10, 11), (1, 7),
(2, 6), (9, 11), (1, 3), (4, 7), (8, 10), (0, 5),
(2, 5), (6, 8), (9, 10),
(1, 2), (3, 5), (7, 8), (4, 6),
(2, 3), (4, 5), (6, 7), (8, 9),
(3, 4), (5, 6),
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros14()
=> new[]
{
(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11), (12, 13),
(0, 2), (4, 6), (8, 10), (1, 3), (5, 7), (9, 11),
(0, 4), (8, 12), (1, 5), (9, 13), (2, 6), (3, 7),
(0, 8), (1, 9), (2, 10), (3, 11), (4, 12), (5, 13),
(5, 10), (6, 9), (3, 12), (7, 11), (1, 2), (4, 8),
(1, 4), (7, 13), (2, 8), (5, 6), (9, 10),
(2, 4), (11, 13), (3, 8), (7, 12),
(6, 8), (10, 12), (3, 5), (7, 9),
(3, 4), (5, 6), (7, 8), (9, 10), (11, 12),
(6, 7), (8, 9),
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros15()
=> new[]
{
(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11), (12, 13),
(0, 2), (4, 6), (8, 10), (12, 14), (1, 3), (5, 7), (9, 11),
(0, 4), (8, 12), (1, 5), (9, 13), (2, 6), (10, 14), (3, 7),
(0, 8), (1, 9), (2, 10), (3, 11), (4, 12), (5, 13), (6, 14),
(5, 10), (6, 9), (3, 12), (13, 14), (7, 11), (1, 2), (4, 8),
(1, 4), (7, 13), (2, 8), (11, 14), (5, 6), (9, 10),
(2, 4), (11, 13), (3, 8), (7, 12),
(6, 8), (10, 12), (3, 5), (7, 9),
(3, 4), (5, 6), (7, 8), (9, 10), (11, 12),
(6, 7), (8, 9),
};
private static IEnumerable<(int, int)> GetBestKnownSwapMacros16()
=> new[]
{
(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11), (12, 13), (14, 15),
(0, 2), (4, 6), (8, 10), (12, 14), (1, 3), (5, 7), (9, 11), (13, 15),
(0, 4), (8, 12), (1, 5), (9, 13), (2, 6), (10, 14), (3, 7), (11, 15),
(0, 8), (1, 9), (2, 10), (3, 11), (4, 12), (5, 13), (6, 14), (7, 15),
(5, 10), (6, 9), (3, 12), (13, 14), (7, 11), (1, 2), (4, 8),
(1, 4), (7, 13), (2, 8), (11, 14), (5, 6), (9, 10),
(2, 4), (11, 13), (3, 8), (7, 12),
(6, 8), (10, 12), (3, 5), (7, 9),
(3, 4), (5, 6), (7, 8), (9, 10), (11, 12),
(6, 7), (8, 9),
};
#>