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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Copyright(c) 2011-2016 Intel Corporation All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions
; are met:
; * Redistributions of source code must retain the above copyright
; notice, this list of conditions and the following disclaimer.
; * Redistributions in binary form must reproduce the above copyright
; notice, this list of conditions and the following disclaimer in
; the documentation and/or other materials provided with the
; distribution.
; * Neither the name of Intel Corporation nor the names of its
; contributors may be used to endorse or promote products derived
; from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%ifndef STDMAC_ASM
%define STDMAC_ASM
;; internal macro used by push_all
;; push args L to R
%macro push_all_ 1-*
%xdefine _PUSH_ALL_REGS_COUNT_ %0
%rep %0
push %1
%rotate 1
%endrep
%endmacro
;; internal macro used by pop_all
;; pop args R to L
%macro pop_all_ 1-*
%rep %0
%rotate -1
pop %1
%endrep
%endmacro
%xdefine _PUSH_ALL_REGS_COUNT_ 0
%xdefine _ALLOC_STACK_VAL_ 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; STACK_OFFSET
;; Number of bytes subtracted from stack due to PUSH_ALL and ALLOC_STACK
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%define STACK_OFFSET (_PUSH_ALL_REGS_COUNT_ * 8 + _ALLOC_STACK_VAL_)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PUSH_ALL reg1, reg2, ...
;; push args L to R, remember regs for pop_all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro PUSH_ALL 1+
%xdefine _PUSH_ALL_REGS_ %1
push_all_ %1
%endmacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; POP_ALL
;; push args from prev "push_all" R to L
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro POP_ALL 0
pop_all_ _PUSH_ALL_REGS_
%xdefine _PUSH_ALL_REGS_COUNT_ 0
%endmacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ALLOC_STACK n
;; subtract n from the stack pointer and remember the value for restore_stack
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro ALLOC_STACK 1
%xdefine _ALLOC_STACK_VAL_ %1
sub rsp, %1
%endmacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; RESTORE_STACK
;; add n to the stack pointer, where n is the arg to the previous alloc_stack
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro RESTORE_STACK 0
add rsp, _ALLOC_STACK_VAL_
%xdefine _ALLOC_STACK_VAL_ 0
%endmacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NOPN n
;; Create n bytes of NOP, using nops of up to 8 bytes each
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro NOPN 1
%assign %%i %1
%rep 200
%if (%%i < 9)
nopn %%i
%exitrep
%else
nopn 8
%assign %%i (%%i - 8)
%endif
%endrep
%endmacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; nopn n
;; Create n bytes of NOP, where n is between 1 and 9
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro nopn 1
%if (%1 == 1)
nop
%elif (%1 == 2)
db 0x66
nop
%elif (%1 == 3)
db 0x0F
db 0x1F
db 0x00
%elif (%1 == 4)
db 0x0F
db 0x1F
db 0x40
db 0x00
%elif (%1 == 5)
db 0x0F
db 0x1F
db 0x44
db 0x00
db 0x00
%elif (%1 == 6)
db 0x66
db 0x0F
db 0x1F
db 0x44
db 0x00
db 0x00
%elif (%1 == 7)
db 0x0F
db 0x1F
db 0x80
db 0x00
db 0x00
db 0x00
db 0x00
%elif (%1 == 8)
db 0x0F
db 0x1F
db 0x84
db 0x00
db 0x00
db 0x00
db 0x00
db 0x00
%elif (%1 == 9)
db 0x66
db 0x0F
db 0x1F
db 0x84
db 0x00
db 0x00
db 0x00
db 0x00
db 0x00
%else
%error Invalid value to nopn
%endif
%endmacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; rolx64 dst, src, amount
;; Emulate a rolx instruction using rorx, assuming data 64 bits wide
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro rolx64 3
rorx %1, %2, (64-%3)
%endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; rolx32 dst, src, amount
;; Emulate a rolx instruction using rorx, assuming data 32 bits wide
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro rolx32 3
rorx %1, %2, (32-%3)
%endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Define a function void ssc(uint64_t x)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro DEF_SSC 0
global ssc
ssc:
mov rax, rbx
mov rbx, rcx
db 0x64
db 0x67
nop
mov rbx, rax
ret
%endm
%macro MOVDQU 2
%define %%dest %1
%define %%src %2
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vmovdqu %%dest, %%src
%else
movdqu %%dest, %%src
%endif
%endm
%macro MOVDQA 2
%define %%dest %1
%define %%src %2
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vmovdqa %%dest, %%src
%else
movdqa %%dest, %%src
%endif
%endm
%macro MOVD 2
%define %%dest %1
%define %%src %2
%if (ARCH == 02 || ARCH == 03 || ARCH == 04)
vmovd %%dest, %%src
%else
movd %%dest, %%src
%endif
%endm
%macro MOVQ 2
%define %%dest %1
%define %%src %2
%if (ARCH == 02 || ARCH == 03 || ARCH == 04)
vmovq %%dest, %%src
%else
movq %%dest, %%src
%endif
%endm
;; Move register if the src and dest are not equal
%macro MOVNIDN 2
%define dest %1
%define src %2
%ifnidn dest, src
mov dest, src
%endif
%endm
%macro MOVDQANIDN 2
%define dest %1
%define src %2
%ifnidn dest, src
MOVDQA dest, src
%endif
%endm
%macro PSHUFD 3
%define %%dest %1
%define %%src1 %2
%define %%imm8 %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpshufd %%dest, %%src1, %%imm8
%else
pshufd %%dest, %%src1, %%imm8
%endif
%endm
%macro PSHUFB 3
%define %%dest %1
%define %%src1 %2
%define %%shuf %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpshufb %%dest, %%src1, %%shuf
%else
MOVDQANIDN %%dest, %%src1
pshufb %%dest, %%shuf
%endif
%endm
%macro PBROADCASTD 2
%define %%dest %1
%define %%src %2
%if (ARCH == 04)
vpbroadcastd %%dest, %%src
%else
MOVD %%dest, %%src
PSHUFD %%dest, %%dest, 0
%endif
%endm
;; Implement BZHI instruction on older architectures
;; Clobbers rcx, unless rcx is %%index
%macro BZHI 4
%define %%dest %1
%define %%src %2
%define %%index %3
%define %%tmp1 %4
%ifdef USE_HSWNI
bzhi %%dest, %%src, %%index
%else
MOVNIDN rcx, %%index
mov %%tmp1, 1
shl %%tmp1, cl
sub %%tmp1, 1
MOVNIDN %%dest, %%src
and %%dest, %%tmp1
%endif
%endm
;; Implement shrx instruction on older architectures
;; Clobbers rcx, unless rcx is %%index
%macro SHRX 3
%define %%dest %1
%define %%src %2
%define %%index %3
%ifdef USE_HSWNI
shrx %%dest, %%src, %%index
%else
MOVNIDN rcx, %%index
MOVNIDN %%dest, %%src
shr %%dest, cl
%endif
%endm
;; Implement shlx instruction on older architectures
;; Clobbers rcx, unless rcx is %%index
%macro SHLX 3
%define %%dest %1
%define %%src %2
%define %%index %3
%ifdef USE_HSWNI
shlx %%dest, %%src, %%index
%else
MOVNIDN %%dest, %%src
MOVNIDN rcx, %%index
shl %%dest, cl
%endif
%endm
%macro PINSRD 3
%define %%dest %1
%define %%src %2
%define %%offset %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpinsrd %%dest, %%src, %%offset
%else
pinsrd %%dest, %%src, %%offset
%endif
%endm
%macro PEXTRD 3
%define %%dest %1
%define %%src %2
%define %%offset %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpextrd %%dest, %%src, %%offset
%else
pextrd %%dest, %%src, %%offset
%endif
%endm
%macro PSRLDQ 2
%define %%dest %1
%define %%offset %2
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpsrldq %%dest, %%offset
%else
psrldq %%dest, %%offset
%endif
%endm
%macro PSLLD 3
%define %%dest %1
%define %%src %2
%define %%offset %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpslld %%dest, %%src, %%offset
%else
MOVDQANIDN %%dest, %%src
pslld %%dest, %%offset
%endif
%endm
%macro PAND 3
%define %%dest %1
%define %%src1 %2
%define %%src2 %3
%if (ARCH == 02 || ARCH == 03 || ARCH == 04)
vpand %%dest, %%src1, %%src2
%else
MOVDQANIDN %%dest, %%src1
pand %%dest, %%src2
%endif
%endm
%macro POR 3
%define %%dest %1
%define %%src1 %2
%define %%src2 %3
%if (ARCH == 02 || ARCH == 03 || ARCH == 04)
vpor %%dest, %%src1, %%src2
%else
MOVDQANIDN %%dest, %%src1
por %%dest, %%src2
%endif
%endm
%macro PXOR 3
%define %%dest %1
%define %%src1 %2
%define %%src2 %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpxor %%dest, %%src1, %%src2
%else
MOVDQANIDN %%dest, %%src1
pxor %%dest, %%src2
%endif
%endm
%macro PADDD 3
%define %%dest %1
%define %%src1 %2
%define %%src2 %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpaddd %%dest, %%src1, %%src2
%else
MOVDQANIDN %%dest, %%src1
paddd %%dest, %%src2
%endif
%endm
%macro PCMPEQB 3
%define %%dest %1
%define %%src1 %2
%define %%src2 %3
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpcmpeqb %%dest, %%src1, %%src2
%else
MOVDQANIDN %%dest, %%src1
pcmpeqb %%dest, %%src2
%endif
%endm
%macro PMOVMSKB 2
%define %%dest %1
%define %%src %2
%if ((ARCH == 02) || (ARCH == 03) || (ARCH == 04))
vpmovmskb %%dest, %%src
%else
pmovmskb %%dest, %%src
%endif
%endm
%endif ;; ifndef STDMAC_ASM
|