| 7 | ; |
| 8 | ; Based on the x86 SIMD extension for IJG JPEG library - version 1.02 |
| 9 | ; |
| 10 | ; Copyright (C) 1999-2006, MIYASAKA Masaru. |
| 11 | ; |
| 12 | ; This software is provided 'as-is', without any express or implied |
| 13 | ; warranty. In no event will the authors be held liable for any damages |
| 14 | ; arising from the use of this software. |
| 15 | ; |
| 16 | ; Permission is granted to anyone to use this software for any purpose, |
| 17 | ; including commercial applications, and to alter it and redistribute it |
| 18 | ; freely, subject to the following restrictions: |
| 19 | ; |
| 20 | ; 1. The origin of this software must not be misrepresented; you must not |
| 21 | ; claim that you wrote the original software. If you use this software |
| 22 | ; in a product, an acknowledgment in the product documentation would be |
| 23 | ; appreciated but is not required. |
| 24 | ; 2. Altered source versions must be plainly marked as such, and must not be |
| 25 | ; misrepresented as being the original software. |
| 26 | ; 3. This notice may not be removed or altered from any source distribution. |
| 27 |
| 28 | ; ========================================================================== |
| 29 | ; System-dependent configurations |
| 30 |
| 31 | %ifdef WIN32 ; ----(nasm -fwin32 -DWIN32 ...)-------- |
| 32 | ; * Microsoft Visual C++ |
| 33 | ; * MinGW (Minimalist GNU for Windows) |
| 34 | ; * CygWin |
| 35 | ; * LCC-Win32 |
| 36 |
| 37 | ; -- segment definition -- |
| 38 | ; |
| 39 | %ifdef __YASM_VER__ |
| 40 | %define SEG_TEXT .text align=32 |
| 41 | %define SEG_CONST .rdata align=32 |
| 42 | %else |
| 43 | %define SEG_TEXT .text align=32 public use32 class=CODE |
| 44 | %define SEG_CONST .rdata align=32 public use32 class=CONST |
| 45 | %endif |
| 46 |
| 47 | %elifdef WIN64 ; ----(nasm -fwin64 -DWIN64 ...)-------- |
| 48 | ; * Microsoft Visual C++ |
| 49 |
| 50 | ; -- segment definition -- |
| 51 | ; |
| 52 | %ifdef __YASM_VER__ |
| 53 | %define SEG_TEXT .text align=32 |
| 54 | %define SEG_CONST .rdata align=32 |
| 55 | %else |
| 56 | %define SEG_TEXT .text align=32 public use64 class=CODE |
| 57 | %define SEG_CONST .rdata align=32 public use64 class=CONST |
| 58 | %endif |
| 59 | %define EXTN(name) name ; foo() -> foo |
| 60 |
| 61 | %elifdef OBJ32 ; ----(nasm -fobj -DOBJ32 ...)---------- |
| 62 | ; * Borland C++ (Win32) |
| 63 |
| 64 | ; -- segment definition -- |
| 65 | ; |
| 66 | %define SEG_TEXT _text align=32 public use32 class=CODE |
| 67 | %define SEG_CONST _data align=32 public use32 class=DATA |
| 68 |
| 69 | %elifdef ELF ; ----(nasm -felf[64] -DELF ...)------------ |
| 70 | ; * Linux |
| 71 | ; * *BSD family Unix using elf format |
| 72 | ; * Unix System V, including Solaris x86, UnixWare and SCO Unix |
| 73 |
| 74 | ; mark stack as non-executable |
| 75 | section .note.GNU-stack noalloc noexec nowrite progbits |
| 76 |
| 77 | ; -- segment definition -- |
| 78 | ; |
| 79 | %ifdef __x86_64__ |
| 80 | %define SEG_TEXT .text progbits align=32 |
| 81 | %define SEG_CONST .rodata progbits align=32 |
| 82 | %else |
| 83 | %define SEG_TEXT .text progbits alloc exec nowrite align=32 |
| 84 | %define SEG_CONST .rodata progbits alloc noexec nowrite align=32 |
| 85 | %endif |
| 86 |
| 87 | ; To make the code position-independent, append -DPIC to the commandline |
| 88 | ; |
| 89 | %define GOT_SYMBOL _GLOBAL_OFFSET_TABLE_ ; ELF supports PIC |
| 90 | %define EXTN(name) name ; foo() -> foo |
| 91 |
| 92 | %elifdef AOUT ; ----(nasm -faoutb/aout -DAOUT ...)---- |
| 93 | ; * Older Linux using a.out format (nasm -f aout -DAOUT ...) |
| 94 | ; * *BSD family Unix using a.out format (nasm -f aoutb -DAOUT ...) |
| 95 |
| 96 | ; -- segment definition -- |
| 97 | ; |
| 98 | %define SEG_TEXT .text |
| 99 | %define SEG_CONST .data |
| 100 |
| 101 | ; To make the code position-independent, append -DPIC to the commandline |
| 102 | ; |
| 103 | %define GOT_SYMBOL __GLOBAL_OFFSET_TABLE_ ; BSD-style a.out supports PIC |
| 104 |
| 105 | %elifdef MACHO ; ----(nasm -fmacho -DMACHO ...)-------- |
| 106 | ; * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (Mach-O format) |
| 107 |
| 108 | ; -- segment definition -- |
| 109 | ; |
| 110 | %define SEG_TEXT .text ;align=32 ; nasm doesn't accept align=32. why? |
| 111 | %define SEG_CONST .rodata align=32 |
| 112 |
| 113 | ; The generation of position-independent code (PIC) is the default on Darwin. |
| 114 | ; |
| 115 | %define PIC |
| 116 | %define GOT_SYMBOL _MACHO_PIC_ ; Mach-O style code-relative addressing |
| 117 |
| 118 | %else ; ----(Other case)---------------------- |
| 119 |
| 120 | ; -- segment definition -- |
| 121 | ; |
| 122 | %define SEG_TEXT .text |
| 123 | %define SEG_CONST .data |
| 124 |
| 125 | %endif ; ---------------------------------------------- |
| 126 |
| 127 | ; ========================================================================== |
| 128 |
| 129 | ; -------------------------------------------------------------------------- |
| 130 | ; Common types |
| 131 | ; |
| 132 | %ifdef __x86_64__ |
| 133 | %define POINTER qword ; general pointer type |
| 134 | %define SIZEOF_POINTER SIZEOF_QWORD ; sizeof(POINTER) |
| 135 | %define POINTER_BIT QWORD_BIT ; sizeof(POINTER)*BYTE_BIT |
| 136 | %else |
| 137 | %define POINTER dword ; general pointer type |
| 138 | %define SIZEOF_POINTER SIZEOF_DWORD ; sizeof(POINTER) |
| 139 | %define POINTER_BIT DWORD_BIT ; sizeof(POINTER)*BYTE_BIT |
| 140 | %endif |
| 141 |
| 142 | %define INT dword ; signed integer type |
| 143 | %define SIZEOF_INT SIZEOF_DWORD ; sizeof(INT) |
| 144 | %define INT_BIT DWORD_BIT ; sizeof(INT)*BYTE_BIT |
| 145 |
| 146 | %define FP32 dword ; IEEE754 single |
| 147 | %define SIZEOF_FP32 SIZEOF_DWORD ; sizeof(FP32) |
| 148 | %define FP32_BIT DWORD_BIT ; sizeof(FP32)*BYTE_BIT |
| 149 |
| 150 | %define MMWORD qword ; int64 (MMX register) |
| 151 | %define SIZEOF_MMWORD SIZEOF_QWORD ; sizeof(MMWORD) |
| 152 | %define MMWORD_BIT QWORD_BIT ; sizeof(MMWORD)*BYTE_BIT |
| 153 |
| 154 | ; NASM is buggy and doesn't properly handle operand sizes for SSE |
| 155 | ; instructions, so for now we have to define XMMWORD as blank. |
| 156 | %define XMMWORD ; int128 (SSE register) |
| 157 | %define SIZEOF_XMMWORD SIZEOF_OWORD ; sizeof(XMMWORD) |
| 158 | %define XMMWORD_BIT OWORD_BIT ; sizeof(XMMWORD)*BYTE_BIT |
| 159 |
| 160 | %define YMMWORD ; int256 (AVX register) |
| 161 | %define SIZEOF_YMMWORD SIZEOF_YWORD ; sizeof(YMMWORD) |
| 162 | %define YMMWORD_BIT YWORD_BIT ; sizeof(YMMWORD)*BYTE_BIT |
| 163 |
| 164 | ; Similar hacks for when we load a dword or MMWORD into an xmm# register |
| 165 | %define XMM_DWORD |
| 166 | %define XMM_MMWORD |
| 167 |
| 168 | %define SIZEOF_BYTE 1 ; sizeof(byte) |
| 169 | %define SIZEOF_WORD 2 ; sizeof(word) |
| 170 | %define SIZEOF_DWORD 4 ; sizeof(dword) |
| 171 | %define SIZEOF_QWORD 8 ; sizeof(qword) |
| 172 | %define SIZEOF_OWORD 16 ; sizeof(oword) |
| 173 | %define SIZEOF_YWORD 32 ; sizeof(yword) |
| 174 |
| 175 | %define BYTE_BIT 8 ; CHAR_BIT in C |
| 176 | %define WORD_BIT 16 ; sizeof(word)*BYTE_BIT |
| 177 | %define DWORD_BIT 32 ; sizeof(dword)*BYTE_BIT |
| 178 | %define QWORD_BIT 64 ; sizeof(qword)*BYTE_BIT |
| 179 | %define OWORD_BIT 128 ; sizeof(oword)*BYTE_BIT |
| 180 | %define YWORD_BIT 256 ; sizeof(yword)*BYTE_BIT |
| 181 |
| 182 | ; -------------------------------------------------------------------------- |
| 183 | ; External Symbol Name |
| 184 | ; |
| 185 | %ifndef EXTN |
| 186 | %define EXTN(name) _ %+ name ; foo() -> _foo |
| 187 | %endif |
| 188 |
| 189 | ; -------------------------------------------------------------------------- |
| 190 | ; Hidden symbols |
| 191 | ; |
| 192 | %ifdef ELF ; ----(nasm -felf[64] -DELF ...)-------- |
| 193 | %define GLOBAL_FUNCTION(name) global EXTN(name):function hidden |
| 194 | %define GLOBAL_DATA(name) global EXTN(name):data hidden |
| 195 | %elifdef MACHO ; ----(nasm -fmacho -DMACHO ...)-------- |
| 196 | %ifdef __YASM_VER__ |
| 197 | %define GLOBAL_FUNCTION(name) global EXTN(name):private_extern |
| 198 | %define GLOBAL_DATA(name) global EXTN(name):private_extern |
| 199 | %else |
| 200 | %if __NASM_VERSION_ID__ >= 0x020E0000 |
| 201 | %define GLOBAL_FUNCTION(name) global EXTN(name):private_extern |
| 202 | %define GLOBAL_DATA(name) global EXTN(name):private_extern |
| 203 | %endif |
| 204 | %endif |
| 205 | %endif |
| 206 |
| 207 | %ifndef GLOBAL_FUNCTION |
| 208 | %define GLOBAL_FUNCTION(name) global EXTN(name) |
| 209 | %endif |
| 210 | %ifndef GLOBAL_DATA |
| 211 | %define GLOBAL_DATA(name) global EXTN(name) |
| 212 | %endif |
| 213 |
| 214 | ; -------------------------------------------------------------------------- |
| 215 | ; Macros for position-independent code (PIC) support |
| 216 | ; |
| 217 | %ifndef GOT_SYMBOL |
| 218 | %undef PIC |
| 219 | %endif |
| 220 |
| 221 | %ifdef PIC ; ------------------------------------------- |
| 222 |
| 223 | %ifidn GOT_SYMBOL, _MACHO_PIC_ ; -------------------- |
| 224 |
| 225 | ; At present, nasm doesn't seem to support PIC generation for Mach-O. |
| 226 | ; The PIC support code below is a little tricky. |
| 227 |
| 228 | SECTION SEG_CONST |
| 229 | const_base: |
| 230 |
| 231 | %define GOTOFF(got, sym) (got) + (sym) - const_base |
| 232 |
| 233 | %imacro get_GOT 1 |
| 234 | ; NOTE: this macro destroys ecx resister. |
| 235 | call %%geteip |
| 236 | add ecx, byte (%%ref - $) |
| 237 | jmp short %%adjust |
| 238 | %%geteip: |
| 239 | mov ecx, POINTER [esp] |
| 240 | ret |
| 241 | %%adjust: |
| 242 | push ebp |
| 243 | xor ebp, ebp ; ebp = 0 |
| 244 | %ifidni %1, ebx ; (%1 == ebx) |
| 245 | ; db 0x8D,0x9C + jmp near const_base = |
| 246 | ; lea ebx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,9C,E9,(offset32) |
| 247 | db 0x8D, 0x9C ; 8D,9C |
| 248 | jmp near const_base ; E9,(const_base-%%ref) |
| 249 | %%ref: |
| 250 | %else ; (%1 != ebx) |
| 251 | ; db 0x8D,0x8C + jmp near const_base = |
| 252 | ; lea ecx, [ecx+ebp*8+(const_base-%%ref)] ; 8D,8C,E9,(offset32) |
| 253 | db 0x8D, 0x8C ; 8D,8C |
| 254 | jmp near const_base ; E9,(const_base-%%ref) |
| 255 | %%ref: |
| 256 | mov %1, ecx |
| 257 | %endif ; (%1 == ebx) |
| 258 | pop ebp |
| 259 | %endmacro |
| 260 |
| 261 | %else ; GOT_SYMBOL != _MACHO_PIC_ ---------------- |
| 262 |
| 263 | %define GOTOFF(got, sym) (got) + (sym) wrt ..gotoff |
| 264 |
| 265 | %imacro get_GOT 1 |
| 266 | extern GOT_SYMBOL |
| 267 | call %%geteip |
| 268 | add %1, GOT_SYMBOL + $$ - $ wrt ..gotpc |
| 269 | jmp short %%done |
| 270 | %%geteip: |
| 271 | mov %1, POINTER [esp] |
| 272 | ret |
| 273 | %%done: |
| 274 | %endmacro |
| 275 |
| 276 | %endif ; GOT_SYMBOL == _MACHO_PIC_ ---------------- |
| 277 |
| 278 | %imacro pushpic 1.nolist |
| 279 | push %1 |
| 280 | %endmacro |
| 281 | %imacro poppic 1.nolist |
| 282 | pop %1 |
| 283 | %endmacro |
| 284 | %imacro movpic 2.nolist |
| 285 | mov %1, %2 |
| 286 | %endmacro |
| 287 |
| 288 | %else ; !PIC ----------------------------------------- |
| 289 |
| 290 | %define GOTOFF(got, sym) (sym) |
| 291 |
| 292 | %imacro get_GOT 1.nolist |
| 293 | %endmacro |
| 294 | %imacro pushpic 1.nolist |
| 295 | %endmacro |
| 296 | %imacro poppic 1.nolist |
| 297 | %endmacro |
| 298 | %imacro movpic 2.nolist |
| 299 | %endmacro |
| 300 |
| 301 | %endif ; PIC ----------------------------------------- |
| 302 |
| 303 | ; -------------------------------------------------------------------------- |
| 304 | ; Align the next instruction on {2,4,8,16,..}-byte boundary. |
| 305 | ; ".balign n,,m" in GNU as |
| 306 | ; |
| 307 | %define MSKLE(x, y) (~(((y) & 0xFFFF) - ((x) & 0xFFFF)) >> 16) |
| 308 | %define FILLB(b, n) (($$-(b)) & ((n)-1)) |
| 309 |
| 310 | %imacro alignx 1-2.nolist 0xFFFF |
| 311 | %%bs: \ |
| 312 | times MSKLE(FILLB(%%bs, %1), %2) & MSKLE(16, FILLB($, %1)) & FILLB($, %1) \ |
| 313 | db 0x90 ; nop |
| 314 | times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 9 \ |
| 315 | db 0x8D, 0x9C, 0x23, 0x00, 0x00, 0x00, 0x00 ; lea ebx,[ebx+0x00000000] |
| 316 | times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 7 \ |
| 317 | db 0x8D, 0xAC, 0x25, 0x00, 0x00, 0x00, 0x00 ; lea ebp,[ebp+0x00000000] |
| 318 | times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 6 \ |
| 319 | db 0x8D, 0xAD, 0x00, 0x00, 0x00, 0x00 ; lea ebp,[ebp+0x00000000] |
| 320 | times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 4 \ |
| 321 | db 0x8D, 0x6C, 0x25, 0x00 ; lea ebp,[ebp+0x00] |
| 322 | times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 3 \ |
| 323 | db 0x8D, 0x6D, 0x00 ; lea ebp,[ebp+0x00] |
| 324 | times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 2 \ |
| 325 | db 0x8B, 0xED ; mov ebp,ebp |
| 326 | times MSKLE(FILLB(%%bs, %1), %2) & FILLB($, %1) / 1 \ |
| 327 | db 0x90 ; nop |
| 328 | %endmacro |
| 329 |
| 330 | ; Align the next data on {2,4,8,16,..}-byte boundary. |
| 331 | ; |
| 332 | %imacro alignz 1.nolist |
| 333 | align %1, db 0 ; filling zeros |
| 334 | %endmacro |
| 335 |
| 336 | %ifdef __x86_64__ |
| 337 |
| 338 | %ifdef WIN64 |
| 339 |
| 340 | %imacro collect_args 1 |
| 341 | sub rsp, SIZEOF_XMMWORD |
| 342 | movaps XMMWORD [rsp], xmm6 |
| 343 | sub rsp, SIZEOF_XMMWORD |
| 344 | movaps XMMWORD [rsp], xmm7 |
| 345 | mov r10, rcx |
| 346 | %if %1 > 1 |
| 347 | mov r11, rdx |
| 348 | %endif |
| 349 | %if %1 > 2 |
| 350 | push r12 |
| 351 | mov r12, r8 |
| 352 | %endif |
| 353 | %if %1 > 3 |
| 354 | push r13 |
| 355 | mov r13, r9 |
| 356 | %endif |
| 357 | %if %1 > 4 |
| 358 | push r14 |
| 359 | mov r14, [rax+48] |
| 360 | %endif |
| 361 | %if %1 > 5 |
| 362 | push r15 |
| 363 | mov r15, [rax+56] |
| 364 | %endif |
| 365 | push rsi |
| 366 | push rdi |
| 367 | %endmacro |
| 368 |
| 369 | %imacro uncollect_args 1 |
| 370 | pop rdi |
| 371 | pop rsi |
| 372 | %if %1 > 5 |
| 373 | pop r15 |
| 374 | %endif |
| 375 | %if %1 > 4 |
| 376 | pop r14 |
| 377 | %endif |
| 378 | %if %1 > 3 |
| 379 | pop r13 |
| 380 | %endif |
| 381 | %if %1 > 2 |
| 382 | pop r12 |
| 383 | %endif |
| 384 | movaps xmm7, XMMWORD [rsp] |
| 385 | add rsp, SIZEOF_XMMWORD |
| 386 | movaps xmm6, XMMWORD [rsp] |
| 387 | add rsp, SIZEOF_XMMWORD |
| 388 | %endmacro |
| 389 |
| 390 | %imacro push_xmm 1 |
| 391 | sub rsp, %1 * SIZEOF_XMMWORD |
| 392 | movaps XMMWORD [rsp+0*SIZEOF_XMMWORD], xmm8 |
| 393 | %if %1 > 1 |
| 394 | movaps XMMWORD [rsp+1*SIZEOF_XMMWORD], xmm9 |
| 395 | %endif |
| 396 | %if %1 > 2 |
| 397 | movaps XMMWORD [rsp+2*SIZEOF_XMMWORD], xmm10 |
| 398 | %endif |
| 399 | %if %1 > 3 |
| 400 | movaps XMMWORD [rsp+3*SIZEOF_XMMWORD], xmm11 |
| 401 | %endif |
| 402 | %endmacro |
| 403 |
| 404 | %imacro pop_xmm 1 |
| 405 | movaps xmm8, XMMWORD [rsp+0*SIZEOF_XMMWORD] |
| 406 | %if %1 > 1 |
| 407 | movaps xmm9, XMMWORD [rsp+1*SIZEOF_XMMWORD] |
| 408 | %endif |
| 409 | %if %1 > 2 |
| 410 | movaps xmm10, XMMWORD [rsp+2*SIZEOF_XMMWORD] |
| 411 | %endif |
| 412 | %if %1 > 3 |
| 413 | movaps xmm11, XMMWORD [rsp+3*SIZEOF_XMMWORD] |
| 414 | %endif |
| 415 | add rsp, %1 * SIZEOF_XMMWORD |
| 416 | %endmacro |
| 417 |
| 418 | %else |
| 419 |
| 420 | %imacro collect_args 1 |
| 421 | push r10 |
| 422 | mov r10, rdi |
| 423 | %if %1 > 1 |
| 424 | push r11 |
| 425 | mov r11, rsi |
| 426 | %endif |
| 427 | %if %1 > 2 |
| 428 | push r12 |
| 429 | mov r12, rdx |
| 430 | %endif |
| 431 | %if %1 > 3 |
| 432 | push r13 |
| 433 | mov r13, rcx |
| 434 | %endif |
| 435 | %if %1 > 4 |
| 436 | push r14 |
| 437 | mov r14, r8 |
| 438 | %endif |
| 439 | %if %1 > 5 |
| 440 | push r15 |
| 441 | mov r15, r9 |
| 442 | %endif |
| 443 | %endmacro |
| 444 |
| 445 | %imacro uncollect_args 1 |
| 446 | %if %1 > 5 |
| 447 | pop r15 |
| 448 | %endif |
| 449 | %if %1 > 4 |
| 450 | pop r14 |
| 451 | %endif |
| 452 | %if %1 > 3 |
| 453 | pop r13 |
| 454 | %endif |
| 455 | %if %1 > 2 |
| 456 | pop r12 |
| 457 | %endif |
| 458 | %if %1 > 1 |
| 459 | pop r11 |
| 460 | %endif |
| 461 | pop r10 |
| 462 | %endmacro |
| 463 |
| 464 | %imacro push_xmm 1 |
| 465 | %endmacro |
| 466 |
| 467 | %imacro pop_xmm 1 |
| 468 | %endmacro |
| 469 |
| 470 | %endif |
| 471 |
| 472 | %endif |
| 473 |
| 474 | ; -------------------------------------------------------------------------- |
| 475 | ; Defines picked up from the C headers |
| 476 | ; |
| 477 | %include "jsimdcfg.inc" |
| 478 |
| 479 | ; -------------------------------------------------------------------------- |
| 480 |