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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
;
; jsimdext.inc - common declarations
;
; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
; Copyright (C) 2010, 2016, 2018-2019, D. R. Commander.
; Copyright (C) 2018, Matthieu Darbois.
; Copyright (C) 2018, Matthias Räncker.
;
; Based on the x86 SIMD extension for IJG JPEG library - version 1.02
;
; Copyright (C) 1999-2006, MIYASAKA Masaru.
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
; ==========================================================================
; System-dependent configurations
%ifdef WIN32 ; ----(nasm -fwin32 -DWIN32 ...)--------
; * Microsoft Visual C++
; * MinGW (Minimalist GNU for Windows)
; * CygWin
; * LCC-Win32
; -- segment definition --
;
%ifdef __YASM_VER__
%define SEG_TEXT .text align=32
%define SEG_CONST .rdata align=32
%else
%define SEG_TEXT .text align=32 public use32 class=CODE
%define SEG_CONST .rdata align=32 public use32 class=CONST
%endif
%elifdef WIN64 ; ----(nasm -fwin64 -DWIN64 ...)--------
; * Microsoft Visual C++
; -- segment definition --
;
%ifdef __YASM_VER__
%define SEG_TEXT .text align=32
%define SEG_CONST .rdata align=32
%else
%define SEG_TEXT .text align=32 public use64 class=CODE
%define SEG_CONST .rdata align=32 public use64 class=CONST
%endif
%define EXTN(name) name ; foo() -> foo
%elifdef OBJ32 ; ----(nasm -fobj -DOBJ32 ...)----------
; * Borland C++ (Win32)
; -- segment definition --
;
%define SEG_TEXT _text align=32 public use32 class=CODE
%define SEG_CONST _data align=32 public use32 class=DATA
%elifdef UNIX ; ----(nasm -felf[64] -DUNIX ...)------------
; * Linux
; * *BSD family Unix using elf format
; * Unix System V, including Solaris x86, UnixWare and SCO Unix
; mark stack as non-executable
section .note.GNU-stack noalloc noexec nowrite progbits
; -- segment definition --
;
%ifdef _x86_64_
%define SEG_TEXT .text progbits align=32
%define SEG_CONST .rodata progbits align=32
%else
%define SEG_TEXT .text progbits alloc exec nowrite align=32
%define SEG_CONST .rodata progbits alloc noexec nowrite align=32
%endif
; To make the code position-independent, append -DPIC to the commandline
;
%define GOT_SYMBOL _GLOBAL_OFFSET_TABLE_ ; ELF supports PIC
%define EXTN(name) name ; foo() -> foo
%elifdef AOUT ; ----(nasm -faoutb/aout -DAOUT ...)----
; * Older Linux using a.out format (nasm -f aout -DAOUT ...)
; * *BSD family Unix using a.out format (nasm -f aoutb -DAOUT ...)
; -- segment definition --
;
%define SEG_TEXT .text
%define SEG_CONST .data
; To make the code position-independent, append -DPIC to the commandline
;
%define GOT_SYMBOL __GLOBAL_OFFSET_TABLE_ ; BSD-style a.out supports PIC
%elifdef MACHO ; ----(nasm -fmacho -DMACHO ...)--------
; * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (Mach-O format)
; -- segment definition --
;
%define SEG_TEXT .text ;align=32 ; nasm doesn't accept align=32. why?
%define SEG_CONST .rodata align=32
; The generation of position-independent code (PIC) is the default on Darwin.
;
%define PIC
%define GOT_SYMBOL _MACHO_PIC_ ; Mach-O style code-relative addressing
%else ; ----(Other case)----------------------
; -- segment definition --
;
%define SEG_TEXT .text
%define SEG_CONST .data
%endif ; ----------------------------------------------
; ==========================================================================
; --------------------------------------------------------------------------
; Common types
;
%ifdef _x86_64_
%ifnidn __OUTPUT_FORMAT__, elfx32
%define POINTER qword ; general pointer type
%define SIZEOF_POINTER SIZEOF_QWORD ; sizeof(POINTER)
%define POINTER_BIT QWORD_BIT ; sizeof(POINTER)*BYTE_BIT
%define resp resq
%define dp dq
%define raxp rax
%define rbxp rbx
%define rcxp rcx
%define rdxp rdx
%define rsip rsi
%define rdip rdi
%define rbpp rbp
%define rspp rsp
%define r8p r8
%define r9p r9
%define r10p r10
%define r11p r11
%define r12p r12
%define r13p r13
%define r14p r14
%define r15p r15
%endif
%endif
%ifndef raxp
%define POINTER dword ; general pointer type
%define SIZEOF_POINTER SIZEOF_DWORD ; sizeof(POINTER)
%define POINTER_BIT DWORD_BIT ; sizeof(POINTER)*BYTE_BIT
%define resp resd
%define dp dd
; x86_64 ILP32 ABI (x32)
%define raxp eax
%define rbxp ebx
%define rcxp ecx
%define rdxp edx
%define rsip esi
%define rdip edi
%define rbpp ebp
%define rspp esp
%define r8p r8d
%define r9p r9d
%define r10p r10d
%define r11p r11d
%define r12p r12d
%define r13p r13d
%define r14p r14d
%define r15p r15d
%endif
%define INT dword ; signed integer type
%define SIZEOF_INT SIZEOF_DWORD ; sizeof(INT)
%define INT_BIT DWORD_BIT ; sizeof(INT)*BYTE_BIT
%define FP32 dword ; IEEE754 single
%define SIZEOF_FP32 SIZEOF_DWORD ; sizeof(FP32)
%define FP32_BIT DWORD_BIT ; sizeof(FP32)*BYTE_BIT
%define MMWORD qword ; int64 (MMX register)
%define SIZEOF_MMWORD SIZEOF_QWORD ; sizeof(MMWORD)
%define MMWORD_BIT QWORD_BIT ; sizeof(MMWORD)*BYTE_BIT
; NASM is buggy and doesn't properly handle operand sizes for SSE
; instructions, so for now we have to define XMMWORD as blank.
%define XMMWORD ; int128 (SSE register)
%define SIZEOF_XMMWORD SIZEOF_OWORD ; sizeof(XMMWORD)
%define XMMWORD_BIT OWORD_BIT ; sizeof(XMMWORD)*BYTE_BIT
%define YMMWORD ; int256 (AVX register)
%define SIZEOF_YMMWORD SIZEOF_YWORD ; sizeof(YMMWORD)
%define YMMWORD_BIT YWORD_BIT ; sizeof(YMMWORD)*BYTE_BIT
; Similar hacks for when we load a dword or MMWORD into an xmm# register
%define XMM_DWORD
%define XMM_MMWORD
%define SIZEOF_BYTE 1 ; sizeof(byte)
%define SIZEOF_WORD 2 ; sizeof(word)
%define SIZEOF_DWORD 4 ; sizeof(dword)
%define SIZEOF_QWORD 8 ; sizeof(qword)
%define SIZEOF_OWORD 16 ; sizeof(oword)
%define SIZEOF_YWORD 32 ; sizeof(yword)
%define BYTE_BIT 8 ; CHAR_BIT in C
%define WORD_BIT 16 ; sizeof(word)*BYTE_BIT
%define DWORD_BIT 32 ; sizeof(dword)*BYTE_BIT
%define QWORD_BIT 64 ; sizeof(qword)*BYTE_BIT
%define OWORD_BIT 128 ; sizeof(oword)*BYTE_BIT
%define YWORD_BIT 256 ; sizeof(yword)*BYTE_BIT
; --------------------------------------------------------------------------
; External Symbol Name
;
%ifndef EXTN
%define EXTN(name) _ %+ name ; foo() -> _foo
%endif
; --------------------------------------------------------------------------
; Hidden symbols
;
%ifdef UNIX ; ----(nasm -felf[64] -DUNIX ...)--------
%define GLOBAL_FUNCTION(name) global EXTN(name):function hidden
%define GLOBAL_DATA(name) global EXTN(name):data hidden
%elifdef MACHO ; ----(nasm -fmacho -DMACHO ...)--------
%ifdef __YASM_VER__
%define GLOBAL_FUNCTION(name) global EXTN(name):private_extern
%define GLOBAL_DATA(name) global EXTN(name):private_extern
%else
%if __NASM_VERSION_ID__ >= 0x020E0000
%define GLOBAL_FUNCTION(name) global EXTN(name):private_extern
%define GLOBAL_DATA(name) global EXTN(name):private_extern
%endif
%endif
%endif
%ifndef GLOBAL_FUNCTION
%define GLOBAL_FUNCTION(name) global EXTN(name)
%endif
%ifndef GLOBAL_DATA
%define GLOBAL_DATA(name) global EXTN(name)
%endif
; --------------------------------------------------------------------------
; Macros for position-independent code (PIC) support
;
%ifndef GOT_SYMBOL
%undef PIC
%endif
%ifdef PIC ; -------------------------------------------
%ifidn GOT_SYMBOL, _MACHO_PIC_ ; --------------------
; At present, nasm doesn't seem to support PIC generation for Mach-O.
; The PIC support code below is a little tricky.
SECTION SEG_CONST
const_base:
%define GOTOFF(got, sym) (got) + (sym) - const_base
%imacro get_GOT 1
; NOTE: this macro destroys ecx resister.
call %%geteip
add ecx, byte (%%ref - $)
jmp short %%adjust
%%geteip:
mov ecx, POINTER [esp]
ret
%%adjust:
push ebp
xor ebp, ebp ; ebp = 0
%ifidni %1, ebx ; (%1 == ebx)
; db 0x8D,0x9C + jmp near const_base =
; lea ebx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,9C,E9,(offset32)
db 0x8D, 0x9C ; 8D,9C
jmp near const_base ; E9,(const_base-%%ref)
%%ref:
%else ; (%1 != ebx)
; db 0x8D,0x8C + jmp near const_base =
; lea ecx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,8C,E9,(offset32)
db 0x8D, 0x8C ; 8D,8C
jmp near const_base ; E9,(const_base-%%ref)
%%ref:
mov %1, ecx
%endif ; (%1 == ebx)
pop ebp
%endmacro
%else ; GOT_SYMBOL != _MACHO_PIC_ ----------------
%define GOTOFF(got, sym) (got) + (sym) wrt ..gotoff
%imacro get_GOT 1
extern GOT_SYMBOL
call %%geteip
add %1, GOT_SYMBOL + $$ - $ wrt ..gotpc
jmp short %%done
%%geteip:
mov %1, POINTER [esp]
ret
%%done:
%endmacro
%endif ; GOT_SYMBOL == _MACHO_PIC_ ----------------
%imacro pushpic 1.nolist
push %1
%endmacro
%imacro poppic 1.nolist
pop %1
%endmacro
%imacro movpic 2.nolist
mov %1, %2
%endmacro
%else ; !PIC -----------------------------------------
%define GOTOFF(got, sym) (sym)
%imacro get_GOT 1.nolist
%endmacro
%imacro pushpic 1.nolist
%endmacro
%imacro poppic 1.nolist
%endmacro
%imacro movpic 2.nolist
%endmacro
%endif ; PIC -----------------------------------------
; --------------------------------------------------------------------------
; Align the next instruction on {2,4,8,16,..}-byte boundary.
; ".balign n,,m" in GNU as
;
%define MSKLE(x, y) (~(((y) & 0xFFFF) - ((x) & 0xFFFF)) >> 16)
%define FILLB(b, n) (($$-(b)) & ((n)-1))
%imacro alignx 1-2.nolist 0xFFFF
%%bs: \
times MSKLE(FILLB(%%bs, %1), %2) & MSKLE(16, FILLB($, %1)) & FILLB($, %1) \
db 0x90 ; nop
times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 9 \
db 0x8D, 0x9C, 0x23, 0x00, 0x00, 0x00, 0x00 ; lea ebx,[ebx+0x00000000]
times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 7 \
db 0x8D, 0xAC, 0x25, 0x00, 0x00, 0x00, 0x00 ; lea ebp,[ebp+0x00000000]
times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 6 \
db 0x8D, 0xAD, 0x00, 0x00, 0x00, 0x00 ; lea ebp,[ebp+0x00000000]
times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 4 \
db 0x8D, 0x6C, 0x25, 0x00 ; lea ebp,[ebp+0x00]
times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 3 \
db 0x8D, 0x6D, 0x00 ; lea ebp,[ebp+0x00]
times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 2 \
db 0x8B, 0xED ; mov ebp,ebp
times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 1 \
db 0x90 ; nop
%endmacro
; Align the next data on {2,4,8,16,..}-byte boundary.
;
%imacro alignz 1.nolist
align %1, db 0 ; filling zeros
%endmacro
%ifdef _x86_64_
%ifdef WIN64
%imacro collect_args 1
sub rsp, SIZEOF_XMMWORD
movaps XMMWORD [rsp], xmm6
sub rsp, SIZEOF_XMMWORD
movaps XMMWORD [rsp], xmm7
mov r10, rcx
%if %1 > 1
mov r11, rdx
%endif
%if %1 > 2
push r12
mov r12, r8
%endif
%if %1 > 3
push r13
mov r13, r9
%endif
%if %1 > 4
push r14
mov r14, [rax+48]
%endif
%if %1 > 5
push r15
mov r15, [rax+56]
%endif
push rsi
push rdi
%endmacro
%imacro uncollect_args 1
pop rdi
pop rsi
%if %1 > 5
pop r15
%endif
%if %1 > 4
pop r14
%endif
%if %1 > 3
pop r13
%endif
%if %1 > 2
pop r12
%endif
movaps xmm7, XMMWORD [rsp]
add rsp, SIZEOF_XMMWORD
movaps xmm6, XMMWORD [rsp]
add rsp, SIZEOF_XMMWORD
%endmacro
%imacro push_xmm 1
sub rsp, %1 * SIZEOF_XMMWORD
movaps XMMWORD [rsp+0*SIZEOF_XMMWORD], xmm8
%if %1 > 1
movaps XMMWORD [rsp+1*SIZEOF_XMMWORD], xmm9
%endif
%if %1 > 2
movaps XMMWORD [rsp+2*SIZEOF_XMMWORD], xmm10
%endif
%if %1 > 3
movaps XMMWORD [rsp+3*SIZEOF_XMMWORD], xmm11
%endif
%endmacro
%imacro pop_xmm 1
movaps xmm8, XMMWORD [rsp+0*SIZEOF_XMMWORD]
%if %1 > 1
movaps xmm9, XMMWORD [rsp+1*SIZEOF_XMMWORD]
%endif
%if %1 > 2
movaps xmm10, XMMWORD [rsp+2*SIZEOF_XMMWORD]
%endif
%if %1 > 3
movaps xmm11, XMMWORD [rsp+3*SIZEOF_XMMWORD]
%endif
add rsp, %1 * SIZEOF_XMMWORD
%endmacro
%else
%imacro collect_args 1
push r10
mov r10, rdi
%if %1 > 1
push r11
mov r11, rsi
%endif
%if %1 > 2
push r12
mov r12, rdx
%endif
%if %1 > 3
push r13
mov r13, rcx
%endif
%if %1 > 4
push r14
mov r14, r8
%endif
%if %1 > 5
push r15
mov r15, r9
%endif
%endmacro
%imacro uncollect_args 1
%if %1 > 5
pop r15
%endif
%if %1 > 4
pop r14
%endif
%if %1 > 3
pop r13
%endif
%if %1 > 2
pop r12
%endif
%if %1 > 1
pop r11
%endif
pop r10
%endmacro
%imacro push_xmm 1
%endmacro
%imacro pop_xmm 1
%endmacro
%endif
%endif
; --------------------------------------------------------------------------
; Defines picked up from the C headers
;
%include "jsimdcfg.inc"
; --------------------------------------------------------------------------
|