-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpmul_mk1.m4
305 lines (305 loc) · 10.5 KB
/
pmul_mk1.m4
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
dnl ## constant multiplication, variant 1
define({__},{})dnl
dnl
dnl
dnl
define({PUSH_MUL_MK1_RESET},{dnl
__{}undefine({_TOKEN_INIT})dnl
__{}undefine({_TOKEN_PUSH})dnl
__{}undefine({_TOKEN_POP})dnl
__{}undefine({_TOKEN_SAVE})dnl
__{}undefine({_TOKEN_2X})dnl
__{}undefine({_TOKEN_128X})dnl
__{}undefine({_TOKEN_256X})dnl
__{}undefine({_TOKEN_END})dnl
__{}undefine({_TOKEN_CHECK})dnl
__{}define({_COST},{0})dnl
__{}define({XMUL_BIT},{1})dnl
__{}define({XMUL_SUM},{0})dnl
})dnl
dnl
dnl
dnl
define({PUSH_MUL_MK1_LOOP},{dnl
__{}define({PUSH_MUL_MK1_PAR},eval($1))dnl
__{}define({_OUTPUT},{ _TOKEN_INIT() _TOKEN_PUSH()})dnl
__{}_PUSH_MUL_MK1_LOOP($1)dnl
__{}_ADD_OUTPUT({ _TOKEN_END() _TOKEN_CHECK() _TOKEN_POP()})dnl
})dnl
dnl
dnl
dnl
define({_PUSH_MUL_MK1_LOOP},{ifelse(eval(PUSH_MUL_MK1_PAR>1),{1},{dnl
__{}ifelse(eval(PUSH_MUL_MK1_PAR & 1),{1},dnl
__{}__{}{_ADD_OUTPUT({ _TOKEN_SAVE()})}){}dnl
__{}define({PUSH_MUL_MK1_PAR},eval(PUSH_MUL_MK1_PAR/2)){}dnl
__{}ifelse(eval(PUSH_MUL_MK1_PAR & 127),{0},{dnl
__{}__{}define({PUSH_MUL_MK1_PAR},eval(PUSH_MUL_MK1_PAR/128)){}dnl
__{}__{}define({XMUL_BIT},eval(XMUL_BIT*256)){}dnl
__{}__{}_ADD_OUTPUT({ _TOKEN_256X()}){}dnl
__{}},eval(((PUSH_MUL_MK1_PAR & 63)==0) && (XMUL_BIT > 1)),{1},{dnl
__{}__{}define({PUSH_MUL_MK1_PAR},eval(PUSH_MUL_MK1_PAR/64)){}dnl
__{}__{}define({XMUL_BIT},eval(XMUL_BIT*128)){}dnl
__{}__{}_ADD_OUTPUT({ _TOKEN_128X()}){}dnl
__{}},{dnl
__{}__{}define({XMUL_BIT},eval(2*XMUL_BIT)){}dnl
__{}__{}_ADD_OUTPUT({ _TOKEN_2X()}){}dnl
__{}}){}dnl
__{}_PUSH_MUL_MK1_LOOP($1){}dnl
})})dnl
dnl
dnl
dnl
define({PUSH_MUL_MK1_NEGLOOP},{dnl
__{}define({PUSH_MUL_MK1_PAR},eval($1-1))dnl
__{}define({_OUTPUT},{ _TOKEN_INIT() _TOKEN_PUSH()})dnl
__{}_PUSH_MUL_MK1_NEGLOOP($1)dnl
__{}_ADD_OUTPUT({ _TOKEN_END() _TOKEN_CHECK() _TOKEN_POP()})dnl
})dnl
dnl
dnl
dnl
define({_PUSH_MUL_MK1_NEGLOOP},{ifelse(eval(PUSH_MUL_MK1_PAR>=1),{1},{dnl
__{}ifelse(eval(PUSH_MUL_MK1_PAR & 1),{0},dnl
__{}__{}{_ADD_OUTPUT({ _TOKEN_SAVE()})}){}dnl
__{}define({PUSH_MUL_MK1_PAR},eval(PUSH_MUL_MK1_PAR/2)){}dnl
__{}ifelse(eval(PUSH_MUL_MK1_PAR & 127),{127},{dnl
__{}__{}define({PUSH_MUL_MK1_PAR},eval(PUSH_MUL_MK1_PAR/128)){}dnl
__{}__{}define({XMUL_BIT},eval(XMUL_BIT*256)){}dnl
__{}__{}_ADD_OUTPUT({ _TOKEN_256X()}){}dnl
__{}},eval(((PUSH_MUL_MK1_PAR & 63)==63) && (XMUL_BIT > 1)),{1},{dnl
__{}__{}define({PUSH_MUL_MK1_PAR},eval(PUSH_MUL_MK1_PAR/64)){}dnl
__{}__{}define({XMUL_BIT},eval(XMUL_BIT*128)){}dnl
__{}__{}_ADD_OUTPUT({ _TOKEN_128X()}){}dnl
__{}},{dnl
__{}__{}define({XMUL_BIT},eval(2*XMUL_BIT)){}dnl
__{}__{}_ADD_OUTPUT({ _TOKEN_2X()}){}dnl
__{}}){}dnl
__{}_PUSH_MUL_MK1_NEGLOOP($1){}dnl
})})dnl
dnl
dnl
dnl
define({_MUL_DEF_ALL},{dnl
__{}define({_TOKEN_INIT},{dnl
__{}__{}define({XMUL_SUM},{0})dnl
__{}__{}define({XMUL_BIT},{1})}){}dnl
__{}define({_TOKEN_PUSH},{}){}dnl
__{}define({_TOKEN_POP},{}){}dnl
__{}define({_TOKEN_SAVE},{dnl
__{}__{}define({XMUL_SUM},XMUL_BIT)dnl
__{}__{}_ADD_COST(2+256*8)
__{}__{} ld B{,} H ; 1:4 $1 *
__{}__{} ld C{,} L ; 1:4 $1 * [XMUL_SUM{}x]}){}dnl
__{}define({_TOKEN_2X},{dnl
__{}__{}define({XMUL_BIT},eval(2*XMUL_BIT))dnl
__{}__{}_ADD_COST(1+256*11)
__{}__{} add HL{,} HL ; 1:11 $1 * XMUL_BIT{}x}){}dnl
__{}define({_TOKEN_128X},{dnl
__{}__{}define({XMUL_BIT},eval(128*XMUL_BIT))dnl
__{}__{}_ADD_COST(7+256*27)
__{}__{} rr H ; 2:8 $1 *
__{}__{} rr L ; 2:8 $1 *
__{}__{} ld H{,} L ; 1:4 $1 *
__{}__{} ld L{,} 0x00 ; 2:7 $1 * XMUL_BIT{}x}){}dnl
__{}define({_TOKEN_256X},{dnl
__{}__{}define({XMUL_BIT},eval(256*XMUL_BIT))dnl
__{}__{}_ADD_COST(3+256*11)
__{}__{} ld H{,} L ; 1:4 $1 *
__{}__{} ld L{,} 0x00 ; 2:7 $1 * XMUL_BIT{}x}){}dnl
__{}define({_TOKEN_END},{dnl
__{}__{}define({XMUL_RESULT},eval(XMUL_BIT))}){}dnl
__{}define({_TOKEN_CHECK},{dnl
__{}__{}ifelse(eval(XMUL_RESULT!=$1),{1},{
__{}__{}__{}.error Error in mk1 constant multiplication function. The generated code does not have the correct result. XMUL_RESULT <> $1
__{}__{}})})dnl
})dnl
dnl
dnl
dnl
define({_MUL_DEF_ADD_BC},{dnl
__{}_MUL_DEF_ALL($1){}dnl
__{}define({_TOKEN_END},{dnl
__{}__{}define({XMUL_RESULT},eval(XMUL_BIT+XMUL_SUM)){}dnl
__{}__{}ifelse(eval(XMUL_SUM>0),{1},{dnl
__{}__{}__{}_ADD_COST(1+256*11)
__{}__{}__{} add HL{,} BC ; 1:11 $1 * [XMUL_RESULT{}x] = XMUL_BIT{}x + XMUL_SUM{}x})}){}dnl
})dnl
dnl
dnl
dnl
define({_MUL_DEF_SUB_BC},{dnl
__{}_MUL_DEF_ALL($1){}dnl
__{}define({_TOKEN_END},{dnl
__{}__{}define({XMUL_RESULT},eval(XMUL_BIT-XMUL_SUM)){}dnl
__{}__{}ifelse(eval(XMUL_SUM>0),{1},{dnl
__{}__{}__{}_ADD_COST(3+256*19)
__{}__{}__{} or A ; 1:4 $1 *
__{}__{}__{} sbc HL{,} BC ; 2:15 $1 * [XMUL_RESULT{}x] = XMUL_BIT{}x - XMUL_SUM{}x})}){}dnl
})dnl
dnl
dnl
dnl
define({_MUL_DEF_ADD_BA},{dnl
__{}_MUL_DEF_ALL($1){}dnl
__{}define({_TOKEN_SAVE},{dnl
__{}__{}define({XMUL_SUM},XMUL_BIT)dnl
__{}__{}_ADD_COST(2+256*8)
__{}__{} ld B{,} H ; 1:4 $1 *
__{}__{} ld A{,} L ; 1:4 $1 * [XMUL_SUM{}x]{}dnl
__{}__{}define({_TOKEN_SAVE},{dnl
__{}__{}__{}define({XMUL_SUM},eval(XMUL_SUM+XMUL_BIT))dnl
__{}__{}__{}_ADD_COST(5+256*20)
__{}__{}__{} add A{,} L ; 1:4 $1 *
__{}__{}__{} ld C{,} A ; 1:4 $1 *
__{}__{}__{} ld A{,} B ; 1:4 $1 *
__{}__{}__{} adc A{,} H ; 1:4 $1 *
__{}__{}__{} ld B{,} A ; 1:4 $1 * [XMUL_SUM{}x]})}){}dnl
__{}define({_TOKEN_END},{dnl
__{}__{}define({XMUL_RESULT},eval(XMUL_BIT+XMUL_SUM)){}dnl
__{}__{}ifelse(eval(XMUL_SUM>0),{1},{dnl
__{}__{}__{}_ADD_COST(1+256*11)
__{}__{}__{} add HL{,} BC ; 1:11 $1 * [XMUL_RESULT{}x] = XMUL_BIT{}x + XMUL_SUM{}x})}){}dnl
})dnl
dnl
dnl
dnl
define({_MUL_DEF_SUB_BA},{dnl
__{}_MUL_DEF_ADD_BA($1){}dnl
__{}define({_TOKEN_END},{dnl
__{}__{}define({XMUL_RESULT},eval(XMUL_BIT-XMUL_SUM)){}dnl
__{}__{}ifelse(eval(XMUL_SUM>0),{1},{dnl
__{}__{}__{}_ADD_COST(3+256*19)
__{}__{}__{} or A ; 1:4 $1 *
__{}__{}__{} sbc HL{,} BC ; 2:15 $1 * [XMUL_RESULT{}x] = XMUL_BIT{}x - XMUL_SUM{}x})}){}dnl
})dnl
dnl
dnl
dnl
define({_MUL_DEF_ADD_DE},{dnl
__{}_MUL_DEF_ALL($1){}dnl
__{}define({_TOKEN_PUSH},{dnl
__{}__{}_ADD_COST(2+256*8)
__{}__{} ld B{,} D ; 1:4 $1 *
__{}__{} ld C{,} E ; 1:4 $1 *}){}dnl
__{}define({_TOKEN_POP},{dnl
__{}__{}_ADD_COST(2+256*8)
__{}__{} ld D{,} B ; 1:4 $1 *
__{}__{} ld E{,} C ; 1:4 $1 *}){}dnl
__{}define({_TOKEN_SAVE},{dnl
__{}__{}define({XMUL_SUM},XMUL_BIT)dnl
__{}__{}_ADD_COST(2+256*8)
__{}__{} ld D{,} H ; 1:4 $1 *
__{}__{} ld E{,} L ; 1:4 $1 * [XMUL_SUM{}x]{}dnl
__{}__{}define({_TOKEN_SAVE},{dnl
__{}__{}__{}define({XMUL_SUM},eval(XMUL_SUM+XMUL_BIT))dnl
__{}__{}__{}_ADD_COST(3+256*19)
__{}__{}__{} ex DE{,} HL ; 1:4 $1 *
__{}__{}__{} add HL{,} DE ; 1:11 $1 * [XMUL_SUM{}x]
__{}__{}__{} ex DE{,} HL ; 1:4 $1 *})}){}dnl
__{}define({_TOKEN_END},{dnl
__{}__{}define({XMUL_RESULT},eval(XMUL_BIT+XMUL_SUM)){}dnl
__{}__{}ifelse(eval(XMUL_SUM>0),{1},{dnl
__{}__{}__{}_ADD_COST(1+256*11)
__{}__{}__{} add HL{,} DE ; 1:11 $1 * [XMUL_RESULT{}x] = XMUL_BIT{}x + XMUL_SUM{}x})}){}dnl
})dnl
dnl
dnl
dnl
define({_MUL_DEF_SUB_DE},{dnl
__{}_MUL_DEF_ADD_DE($1){}dnl
__{}define({_TOKEN_END},{dnl
__{}__{}define({XMUL_RESULT},eval(XMUL_BIT-XMUL_SUM)){}dnl
__{}__{}ifelse(eval(XMUL_SUM>0),{1},{dnl
__{}__{}__{}_ADD_COST(3+256*19)
__{}__{}__{} or A ; 1:4 $1 *
__{}__{}__{} sbc HL{,} DE ; 2:15 $1 * [XMUL_RESULT{}x] = XMUL_BIT{}x - XMUL_SUM{}x})}){}dnl
})dnl
dnl
dnl
dnl
dnl
dnl "const *"
dnl ( x1 -- const*x1 )
define({PUSH_MUL_MK1},{dnl
__{}define({PUSH_MUL_MK1_1BITS},SUM_1BITS($1)){}dnl
__{}define({PUSH_MUL_MK1_0BITS},SUM_0BITS($1-1)){}dnl
__{}PUSH_MUL_MK1_RESET{}dnl
dnl
ifelse(eval($1==0),{1},{dnl
dnl n = 0
dnl
__{}define({PUSH_MUL_MK1_OUT},{
ld HL, 0x0000 ; 3:10 0 *})dnl
__{}define({PUSH_MUL_MK1_COST},eval(3+256*10)){}dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(2563,$1,{Variant mk1: HL * 0})})},
PUSH_MUL_MK1_1BITS,{1},{dnl
dnl n = 2^a
dnl 1,2,4,8,16,32,...
__{}PUSH_MUL_MK1_LOOP($1){}dnl
__{}_MUL_DEF_ALL($1)dnl
__{}define({PUSH_MUL_MK1_OUT},_OUTPUT)dnl
__{}define({PUSH_MUL_MK1_COST},_COST)dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(}_COST{,$1,{Variant mk1: HL * 2^a})})},
PUSH_MUL_MK1_1BITS,{2},{dnl
dnl n = 2^a + 2^b
dnl 3,5,6,9,10,12,17,18,20,24,...
dnl Not all variants are optimal. For example, 258.
__{}PUSH_MUL_MK1_LOOP($1){}dnl
__{}_MUL_DEF_ADD_BC($1)dnl
__{}define({PUSH_MUL_MK1_OUT},_OUTPUT)dnl
__{}define({PUSH_MUL_MK1_COST},_COST)dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(}_COST{,$1,{Variant mk1: HL * (2^a + 2^b)})})},
PUSH_MUL_MK1_0BITS,{1},{dnl
dnl n = 2^a - 2^b, a > b
dnl 60=64-4
__{}PUSH_MUL_MK1_NEGLOOP($1){}dnl
__{}_MUL_DEF_SUB_BC($1)dnl
__{}define({PUSH_MUL_MK1_OUT},_OUTPUT)dnl
__{}define({PUSH_MUL_MK1_COST},_COST)dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(}_COST{,$1,{Variant mk1: HL * (2^a - 2^b)})})},
PUSH_MUL_MK1_1BITS,{3},{dnl
dnl
dnl n = 2^a + 2^b + 2^c
dnl 11=8+2+1,69=64+4+1
__{}PUSH_MUL_MK1_LOOP($1){}dnl
__{}_MUL_DEF_ADD_BA($1)dnl
__{}define({PUSH_MUL_MK1_OUT},_OUTPUT){}dnl
__{}define({PUSH_MUL_MK1_COST},_COST)dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(}_COST{,$1,{Variant mk1: HL * (2^a + 2^b + 2^c)})})},
PUSH_MUL_MK1_0BITS,{2},{dnl
dnl
dnl n = 2^a - 2^b - 2^c, a > b > c
dnl 27=32-4-1,54=64-8-2
__{}PUSH_MUL_MK1_NEGLOOP($1){}dnl
__{}_MUL_DEF_SUB_BA($1)dnl
__{}define({PUSH_MUL_MK1_OUT},_OUTPUT)dnl
__{}define({PUSH_MUL_MK1_COST},_COST)dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(}_COST{,$1,{Variant mk1: HL * (2^a - 2^b - 2^c)})})},
eval(PUSH_MUL_MK1_1BITS <= PUSH_MUL_MK1_0BITS + 2),{1},{dnl
dnl n = 2^a + 2^b + 2^c + ...
__{}PUSH_MUL_MK1_LOOP($1){}dnl
__{}_MUL_DEF_ADD_DE($1)dnl
__{}define({PUSH_MUL_MK1_OUT},_OUTPUT)dnl
__{}define({PUSH_MUL_MK1_COST},_COST)dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(}_COST{,$1,{Variant mk1: HL * (2^a + 2^b + 2^c + ...)})})},
{dnl
dnl n = 2^a - 2^b - 2^c - ...
dnl 187=128+32+16+8+2+1=256-64-4-1
__{}PUSH_MUL_MK1_NEGLOOP($1){}dnl
__{}_MUL_DEF_SUB_DE($1)dnl
__{}define({PUSH_MUL_MK1_OUT},_OUTPUT)dnl
__{}define({PUSH_MUL_MK1_COST},_COST)dnl
__{}define({PUSH_MUL_MK1_INFO},{
__{}__{}PUSH_MUL_INFO_PLUS(}_COST{,$1,{Variant mk1: HL * (2^a - 2^b - 2^c - ...)})})dnl
})})dnl
dnl
dnl
dnl