You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1192 lines
30KB

  1. ;*****************************************************************************
  2. ;* x86inc.asm: x264asm abstraction layer
  3. ;*****************************************************************************
  4. ;* Copyright (C) 2005-2012 x264 project
  5. ;*
  6. ;* Authors: Loren Merritt <lorenm@u.washington.edu>
  7. ;* Anton Mitrofanov <BugMaster@narod.ru>
  8. ;* Jason Garrett-Glaser <darkshikari@gmail.com>
  9. ;* Henrik Gramner <hengar-6@student.ltu.se>
  10. ;*
  11. ;* Permission to use, copy, modify, and/or distribute this software for any
  12. ;* purpose with or without fee is hereby granted, provided that the above
  13. ;* copyright notice and this permission notice appear in all copies.
  14. ;*
  15. ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  16. ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  17. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  18. ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  21. ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. ;*****************************************************************************
  23. ; This is a header file for the x264ASM assembly language, which uses
  24. ; NASM/YASM syntax combined with a large number of macros to provide easy
  25. ; abstraction between different calling conventions (x86_32, win64, linux64).
  26. ; It also has various other useful features to simplify writing the kind of
  27. ; DSP functions that are most often used in x264.
  28. ; Unlike the rest of x264, this file is available under an ISC license, as it
  29. ; has significant usefulness outside of x264 and we want it to be available
  30. ; to the largest audience possible. Of course, if you modify it for your own
  31. ; purposes to add a new feature, we strongly encourage contributing a patch
  32. ; as this feature might be useful for others as well. Send patches or ideas
  33. ; to x264-devel@videolan.org .
  34. %define program_name ff
  35. %define WIN64 0
  36. %define UNIX64 0
  37. %if ARCH_X86_64
  38. %ifidn __OUTPUT_FORMAT__,win32
  39. %define WIN64 1
  40. %elifidn __OUTPUT_FORMAT__,win64
  41. %define WIN64 1
  42. %else
  43. %define UNIX64 1
  44. %endif
  45. %endif
  46. %ifdef PREFIX
  47. %define mangle(x) _ %+ x
  48. %else
  49. %define mangle(x) x
  50. %endif
  51. ; Name of the .rodata section.
  52. %macro SECTION_RODATA 0-1 16
  53. ; Kludge: Something on OS X fails to align .rodata even given an align
  54. ; attribute, so use a different read-only section. This has been fixed in
  55. ; yasm 0.8.0 and nasm 2.6.
  56. %ifdef __YASM_VERSION_ID__
  57. %if __YASM_VERSION_ID__ < 00080000h
  58. %define NEED_MACHO_RODATA_KLUDGE
  59. %endif
  60. %elifdef __NASM_VERSION_ID__
  61. %if __NASM_VERSION_ID__ < 02060000h
  62. %define NEED_MACHO_RODATA_KLUDGE
  63. %endif
  64. %endif
  65. %ifidn __OUTPUT_FORMAT__,aout
  66. section .text
  67. %else
  68. %ifndef NEED_MACHO_RODATA_KLUDGE
  69. SECTION .rodata align=%1
  70. %else
  71. %ifidn __OUTPUT_FORMAT__,macho64
  72. SECTION .text align=%1
  73. %elifidn __OUTPUT_FORMAT__,macho
  74. SECTION .text align=%1
  75. fakegot:
  76. %else
  77. SECTION .rodata align=%1
  78. %endif
  79. %endif
  80. %endif
  81. %undef NEED_MACHO_RODATA_KLUDGE
  82. %endmacro
  83. ; aout does not support align=
  84. %macro SECTION_TEXT 0-1 16
  85. %ifidn __OUTPUT_FORMAT__,aout
  86. SECTION .text
  87. %else
  88. SECTION .text align=%1
  89. %endif
  90. %endmacro
  91. %if WIN64
  92. %define PIC
  93. %elif ARCH_X86_64 == 0
  94. ; x86_32 doesn't require PIC.
  95. ; Some distros prefer shared objects to be PIC, but nothing breaks if
  96. ; the code contains a few textrels, so we'll skip that complexity.
  97. %undef PIC
  98. %endif
  99. %ifdef PIC
  100. default rel
  101. %endif
  102. ; Always use long nops (reduces 0x90 spam in disassembly on x86_32)
  103. ; Not supported by NASM (except via smartalign package + ALIGNMODE k8,
  104. ; however that fails when used together with the -M option)
  105. %ifdef __YASM_VER__
  106. CPU amdnop
  107. %endif
  108. ; Macros to eliminate most code duplication between x86_32 and x86_64:
  109. ; Currently this works only for leaf functions which load all their arguments
  110. ; into registers at the start, and make no other use of the stack. Luckily that
  111. ; covers most of x264's asm.
  112. ; PROLOGUE:
  113. ; %1 = number of arguments. loads them from stack if needed.
  114. ; %2 = number of registers used. pushes callee-saved regs if needed.
  115. ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
  116. ; %4 = list of names to define to registers
  117. ; PROLOGUE can also be invoked by adding the same options to cglobal
  118. ; e.g.
  119. ; cglobal foo, 2,3,0, dst, src, tmp
  120. ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
  121. ; TODO Some functions can use some args directly from the stack. If they're the
  122. ; last args then you can just not declare them, but if they're in the middle
  123. ; we need more flexible macro.
  124. ; RET:
  125. ; Pops anything that was pushed by PROLOGUE, and returns.
  126. ; REP_RET:
  127. ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
  128. ; which are slow when a normal ret follows a branch.
  129. ; registers:
  130. ; rN and rNq are the native-size register holding function argument N
  131. ; rNd, rNw, rNb are dword, word, and byte size
  132. ; rNh is the high 8 bits of the word size
  133. ; rNm is the original location of arg N (a register or on the stack), dword
  134. ; rNmp is native size
  135. %macro DECLARE_REG 2-3
  136. %define r%1q %2
  137. %define r%1d %2d
  138. %define r%1w %2w
  139. %define r%1b %2b
  140. %define r%1h %2h
  141. %if %0 == 2
  142. %define r%1m %2d
  143. %define r%1mp %2
  144. %elif ARCH_X86_64 ; memory
  145. %define r%1m [rsp + stack_offset + %3]
  146. %define r%1mp qword r %+ %1 %+ m
  147. %else
  148. %define r%1m [esp + stack_offset + %3]
  149. %define r%1mp dword r %+ %1 %+ m
  150. %endif
  151. %define r%1 %2
  152. %endmacro
  153. %macro DECLARE_REG_SIZE 3
  154. %define r%1q r%1
  155. %define e%1q r%1
  156. %define r%1d e%1
  157. %define e%1d e%1
  158. %define r%1w %1
  159. %define e%1w %1
  160. %define r%1h %3
  161. %define e%1h %3
  162. %define r%1b %2
  163. %define e%1b %2
  164. %if ARCH_X86_64 == 0
  165. %define r%1 e%1
  166. %endif
  167. %endmacro
  168. DECLARE_REG_SIZE ax, al, ah
  169. DECLARE_REG_SIZE bx, bl, bh
  170. DECLARE_REG_SIZE cx, cl, ch
  171. DECLARE_REG_SIZE dx, dl, dh
  172. DECLARE_REG_SIZE si, sil, null
  173. DECLARE_REG_SIZE di, dil, null
  174. DECLARE_REG_SIZE bp, bpl, null
  175. ; t# defines for when per-arch register allocation is more complex than just function arguments
  176. %macro DECLARE_REG_TMP 1-*
  177. %assign %%i 0
  178. %rep %0
  179. CAT_XDEFINE t, %%i, r%1
  180. %assign %%i %%i+1
  181. %rotate 1
  182. %endrep
  183. %endmacro
  184. %macro DECLARE_REG_TMP_SIZE 0-*
  185. %rep %0
  186. %define t%1q t%1 %+ q
  187. %define t%1d t%1 %+ d
  188. %define t%1w t%1 %+ w
  189. %define t%1h t%1 %+ h
  190. %define t%1b t%1 %+ b
  191. %rotate 1
  192. %endrep
  193. %endmacro
  194. DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
  195. %if ARCH_X86_64
  196. %define gprsize 8
  197. %else
  198. %define gprsize 4
  199. %endif
  200. %macro PUSH 1
  201. push %1
  202. %assign stack_offset stack_offset+gprsize
  203. %endmacro
  204. %macro POP 1
  205. pop %1
  206. %assign stack_offset stack_offset-gprsize
  207. %endmacro
  208. %macro PUSH_IF_USED 1-*
  209. %rep %0
  210. %if %1 < regs_used
  211. PUSH r%1
  212. %endif
  213. %rotate 1
  214. %endrep
  215. %endmacro
  216. %macro POP_IF_USED 1-*
  217. %rep %0
  218. %if %1 < regs_used
  219. pop r%1
  220. %endif
  221. %rotate 1
  222. %endrep
  223. %endmacro
  224. %macro LOAD_IF_USED 1-*
  225. %rep %0
  226. %if %1 < num_args
  227. mov r%1, r %+ %1 %+ mp
  228. %endif
  229. %rotate 1
  230. %endrep
  231. %endmacro
  232. %macro SUB 2
  233. sub %1, %2
  234. %ifidn %1, rsp
  235. %assign stack_offset stack_offset+(%2)
  236. %endif
  237. %endmacro
  238. %macro ADD 2
  239. add %1, %2
  240. %ifidn %1, rsp
  241. %assign stack_offset stack_offset-(%2)
  242. %endif
  243. %endmacro
  244. %macro movifnidn 2
  245. %ifnidn %1, %2
  246. mov %1, %2
  247. %endif
  248. %endmacro
  249. %macro movsxdifnidn 2
  250. %ifnidn %1, %2
  251. movsxd %1, %2
  252. %endif
  253. %endmacro
  254. %macro ASSERT 1
  255. %if (%1) == 0
  256. %error assert failed
  257. %endif
  258. %endmacro
  259. %macro DEFINE_ARGS 0-*
  260. %ifdef n_arg_names
  261. %assign %%i 0
  262. %rep n_arg_names
  263. CAT_UNDEF arg_name %+ %%i, q
  264. CAT_UNDEF arg_name %+ %%i, d
  265. CAT_UNDEF arg_name %+ %%i, w
  266. CAT_UNDEF arg_name %+ %%i, h
  267. CAT_UNDEF arg_name %+ %%i, b
  268. CAT_UNDEF arg_name %+ %%i, m
  269. CAT_UNDEF arg_name %+ %%i, mp
  270. CAT_UNDEF arg_name, %%i
  271. %assign %%i %%i+1
  272. %endrep
  273. %endif
  274. %xdefine %%stack_offset stack_offset
  275. %undef stack_offset ; so that the current value of stack_offset doesn't get baked in by xdefine
  276. %assign %%i 0
  277. %rep %0
  278. %xdefine %1q r %+ %%i %+ q
  279. %xdefine %1d r %+ %%i %+ d
  280. %xdefine %1w r %+ %%i %+ w
  281. %xdefine %1h r %+ %%i %+ h
  282. %xdefine %1b r %+ %%i %+ b
  283. %xdefine %1m r %+ %%i %+ m
  284. %xdefine %1mp r %+ %%i %+ mp
  285. CAT_XDEFINE arg_name, %%i, %1
  286. %assign %%i %%i+1
  287. %rotate 1
  288. %endrep
  289. %xdefine stack_offset %%stack_offset
  290. %assign n_arg_names %0
  291. %endmacro
  292. %if WIN64 ; Windows x64 ;=================================================
  293. DECLARE_REG 0, rcx
  294. DECLARE_REG 1, rdx
  295. DECLARE_REG 2, R8
  296. DECLARE_REG 3, R9
  297. DECLARE_REG 4, R10, 40
  298. DECLARE_REG 5, R11, 48
  299. DECLARE_REG 6, rax, 56
  300. DECLARE_REG 7, rdi, 64
  301. DECLARE_REG 8, rsi, 72
  302. DECLARE_REG 9, rbx, 80
  303. DECLARE_REG 10, rbp, 88
  304. DECLARE_REG 11, R12, 96
  305. DECLARE_REG 12, R13, 104
  306. DECLARE_REG 13, R14, 112
  307. DECLARE_REG 14, R15, 120
  308. %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
  309. %assign num_args %1
  310. %assign regs_used %2
  311. ASSERT regs_used >= num_args
  312. ASSERT regs_used <= 15
  313. PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
  314. %if mmsize == 8
  315. %assign xmm_regs_used 0
  316. %else
  317. WIN64_SPILL_XMM %3
  318. %endif
  319. LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  320. DEFINE_ARGS %4
  321. %endmacro
  322. %macro WIN64_SPILL_XMM 1
  323. %assign xmm_regs_used %1
  324. ASSERT xmm_regs_used <= 16
  325. %if xmm_regs_used > 6
  326. SUB rsp, (xmm_regs_used-6)*16+16
  327. %assign %%i xmm_regs_used
  328. %rep (xmm_regs_used-6)
  329. %assign %%i %%i-1
  330. movdqa [rsp + (%%i-6)*16+(~stack_offset&8)], xmm %+ %%i
  331. %endrep
  332. %endif
  333. %endmacro
  334. %macro WIN64_RESTORE_XMM_INTERNAL 1
  335. %if xmm_regs_used > 6
  336. %assign %%i xmm_regs_used
  337. %rep (xmm_regs_used-6)
  338. %assign %%i %%i-1
  339. movdqa xmm %+ %%i, [%1 + (%%i-6)*16+(~stack_offset&8)]
  340. %endrep
  341. add %1, (xmm_regs_used-6)*16+16
  342. %endif
  343. %endmacro
  344. %macro WIN64_RESTORE_XMM 1
  345. WIN64_RESTORE_XMM_INTERNAL %1
  346. %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
  347. %assign xmm_regs_used 0
  348. %endmacro
  349. %define has_epilogue regs_used > 7 || xmm_regs_used > 6 || mmsize == 32
  350. %macro RET 0
  351. WIN64_RESTORE_XMM_INTERNAL rsp
  352. POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
  353. %if mmsize == 32
  354. vzeroupper
  355. %endif
  356. ret
  357. %endmacro
  358. %elif ARCH_X86_64 ; *nix x64 ;=============================================
  359. DECLARE_REG 0, rdi
  360. DECLARE_REG 1, rsi
  361. DECLARE_REG 2, rdx
  362. DECLARE_REG 3, rcx
  363. DECLARE_REG 4, R8
  364. DECLARE_REG 5, R9
  365. DECLARE_REG 6, rax, 8
  366. DECLARE_REG 7, R10, 16
  367. DECLARE_REG 8, R11, 24
  368. DECLARE_REG 9, rbx, 32
  369. DECLARE_REG 10, rbp, 40
  370. DECLARE_REG 11, R12, 48
  371. DECLARE_REG 12, R13, 56
  372. DECLARE_REG 13, R14, 64
  373. DECLARE_REG 14, R15, 72
  374. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  375. %assign num_args %1
  376. %assign regs_used %2
  377. ASSERT regs_used >= num_args
  378. ASSERT regs_used <= 15
  379. PUSH_IF_USED 9, 10, 11, 12, 13, 14
  380. LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
  381. DEFINE_ARGS %4
  382. %endmacro
  383. %define has_epilogue regs_used > 9 || mmsize == 32
  384. %macro RET 0
  385. POP_IF_USED 14, 13, 12, 11, 10, 9
  386. %if mmsize == 32
  387. vzeroupper
  388. %endif
  389. ret
  390. %endmacro
  391. %else ; X86_32 ;==============================================================
  392. DECLARE_REG 0, eax, 4
  393. DECLARE_REG 1, ecx, 8
  394. DECLARE_REG 2, edx, 12
  395. DECLARE_REG 3, ebx, 16
  396. DECLARE_REG 4, esi, 20
  397. DECLARE_REG 5, edi, 24
  398. DECLARE_REG 6, ebp, 28
  399. %define rsp esp
  400. %macro DECLARE_ARG 1-*
  401. %rep %0
  402. %define r%1m [esp + stack_offset + 4*%1 + 4]
  403. %define r%1mp dword r%1m
  404. %rotate 1
  405. %endrep
  406. %endmacro
  407. DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
  408. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  409. %assign num_args %1
  410. %assign regs_used %2
  411. %if num_args > 7
  412. %assign num_args 7
  413. %endif
  414. %if regs_used > 7
  415. %assign regs_used 7
  416. %endif
  417. ASSERT regs_used >= num_args
  418. PUSH_IF_USED 3, 4, 5, 6
  419. LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
  420. DEFINE_ARGS %4
  421. %endmacro
  422. %define has_epilogue regs_used > 3 || mmsize == 32
  423. %macro RET 0
  424. POP_IF_USED 6, 5, 4, 3
  425. %if mmsize == 32
  426. vzeroupper
  427. %endif
  428. ret
  429. %endmacro
  430. %endif ;======================================================================
  431. %if WIN64 == 0
  432. %macro WIN64_SPILL_XMM 1
  433. %endmacro
  434. %macro WIN64_RESTORE_XMM 1
  435. %endmacro
  436. %endif
  437. %macro REP_RET 0
  438. %if has_epilogue
  439. RET
  440. %else
  441. rep ret
  442. %endif
  443. %endmacro
  444. %macro TAIL_CALL 2 ; callee, is_nonadjacent
  445. %if has_epilogue
  446. call %1
  447. RET
  448. %elif %2
  449. jmp %1
  450. %endif
  451. %endmacro
  452. ;=============================================================================
  453. ; arch-independent part
  454. ;=============================================================================
  455. %assign function_align 16
  456. ; Begin a function.
  457. ; Applies any symbol mangling needed for C linkage, and sets up a define such that
  458. ; subsequent uses of the function name automatically refer to the mangled version.
  459. ; Appends cpuflags to the function name if cpuflags has been specified.
  460. %macro cglobal 1-2+ ; name, [PROLOGUE args]
  461. %if %0 == 1
  462. ; HACK: work around %+ broken with empty SUFFIX for nasm 2.09.10
  463. %ifndef cpuname
  464. cglobal_internal %1
  465. %else
  466. cglobal_internal %1 %+ SUFFIX
  467. %endif
  468. %else
  469. ; HACK: work around %+ broken with empty SUFFIX for nasm 2.09.10
  470. %ifndef cpuname
  471. cglobal_internal %1, %2
  472. %else
  473. cglobal_internal %1 %+ SUFFIX, %2
  474. %endif
  475. %endif
  476. %endmacro
  477. %macro cglobal_internal 1-2+
  478. %ifndef cglobaled_%1
  479. %xdefine %1 mangle(program_name %+ _ %+ %1)
  480. %xdefine %1.skip_prologue %1 %+ .skip_prologue
  481. CAT_XDEFINE cglobaled_, %1, 1
  482. %endif
  483. %xdefine current_function %1
  484. %ifidn __OUTPUT_FORMAT__,elf
  485. global %1:function hidden
  486. %else
  487. global %1
  488. %endif
  489. align function_align
  490. %1:
  491. RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
  492. %assign stack_offset 0
  493. %if %0 > 1
  494. PROLOGUE %2
  495. %endif
  496. %endmacro
  497. %macro cextern 1
  498. %xdefine %1 mangle(program_name %+ _ %+ %1)
  499. CAT_XDEFINE cglobaled_, %1, 1
  500. extern %1
  501. %endmacro
  502. ; like cextern, but without the prefix
  503. %macro cextern_naked 1
  504. %xdefine %1 mangle(%1)
  505. CAT_XDEFINE cglobaled_, %1, 1
  506. extern %1
  507. %endmacro
  508. %macro const 2+
  509. %xdefine %1 mangle(program_name %+ _ %+ %1)
  510. global %1
  511. %1: %2
  512. %endmacro
  513. ; This is needed for ELF, otherwise the GNU linker assumes the stack is
  514. ; executable by default.
  515. %ifidn __OUTPUT_FORMAT__,elf
  516. SECTION .note.GNU-stack noalloc noexec nowrite progbits
  517. %endif
  518. ; cpuflags
  519. %assign cpuflags_mmx (1<<0)
  520. %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
  521. %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
  522. %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
  523. %assign cpuflags_sse (1<<4) | cpuflags_mmx2
  524. %assign cpuflags_sse2 (1<<5) | cpuflags_sse
  525. %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
  526. %assign cpuflags_sse3 (1<<7) | cpuflags_sse2
  527. %assign cpuflags_ssse3 (1<<8) | cpuflags_sse3
  528. %assign cpuflags_sse4 (1<<9) | cpuflags_ssse3
  529. %assign cpuflags_sse42 (1<<10)| cpuflags_sse4
  530. %assign cpuflags_avx (1<<11)| cpuflags_sse42
  531. %assign cpuflags_xop (1<<12)| cpuflags_avx
  532. %assign cpuflags_fma4 (1<<13)| cpuflags_avx
  533. %assign cpuflags_avx2 (1<<14)| cpuflags_avx
  534. %assign cpuflags_fma3 (1<<15)| cpuflags_avx
  535. %assign cpuflags_cache32 (1<<16)
  536. %assign cpuflags_cache64 (1<<17)
  537. %assign cpuflags_slowctz (1<<18)
  538. %assign cpuflags_lzcnt (1<<19)
  539. %assign cpuflags_misalign (1<<20)
  540. %assign cpuflags_aligned (1<<21) ; not a cpu feature, but a function variant
  541. %assign cpuflags_atom (1<<22)
  542. %assign cpuflags_bmi1 (1<<23)
  543. %assign cpuflags_bmi2 (1<<24)|cpuflags_bmi1
  544. %assign cpuflags_tbm (1<<25)|cpuflags_bmi1
  545. %define cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
  546. %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
  547. ; Takes up to 2 cpuflags from the above list.
  548. ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
  549. ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
  550. %macro INIT_CPUFLAGS 0-2
  551. %ifdef __YASM_VER__
  552. CPU amdnop
  553. %endif
  554. %if %0 >= 1
  555. %xdefine cpuname %1
  556. %assign cpuflags cpuflags_%1
  557. %if %0 >= 2
  558. %xdefine cpuname %1_%2
  559. %assign cpuflags cpuflags | cpuflags_%2
  560. %endif
  561. %xdefine SUFFIX _ %+ cpuname
  562. %if cpuflag(avx)
  563. %assign avx_enabled 1
  564. %endif
  565. %if mmsize == 16 && notcpuflag(sse2)
  566. %define mova movaps
  567. %define movu movups
  568. %define movnta movntps
  569. %endif
  570. %if cpuflag(aligned)
  571. %define movu mova
  572. %elifidn %1, sse3
  573. %define movu lddqu
  574. %endif
  575. %ifdef __YASM_VER__
  576. %if notcpuflag(mmx2)
  577. CPU basicnop
  578. %endif
  579. %endif
  580. %else
  581. %xdefine SUFFIX
  582. %undef cpuname
  583. %undef cpuflags
  584. %endif
  585. %endmacro
  586. ; merge mmx and sse*
  587. %macro CAT_XDEFINE 3
  588. %xdefine %1%2 %3
  589. %endmacro
  590. %macro CAT_UNDEF 2
  591. %undef %1%2
  592. %endmacro
  593. %macro INIT_MMX 0-1+
  594. %assign avx_enabled 0
  595. %define RESET_MM_PERMUTATION INIT_MMX %1
  596. %define mmsize 8
  597. %define num_mmregs 8
  598. %define mova movq
  599. %define movu movq
  600. %define movh movd
  601. %define movnta movntq
  602. %assign %%i 0
  603. %rep 8
  604. CAT_XDEFINE m, %%i, mm %+ %%i
  605. CAT_XDEFINE nmm, %%i, %%i
  606. %assign %%i %%i+1
  607. %endrep
  608. %rep 8
  609. CAT_UNDEF m, %%i
  610. CAT_UNDEF nmm, %%i
  611. %assign %%i %%i+1
  612. %endrep
  613. INIT_CPUFLAGS %1
  614. %endmacro
  615. %macro INIT_XMM 0-1+
  616. %assign avx_enabled 0
  617. %define RESET_MM_PERMUTATION INIT_XMM %1
  618. %define mmsize 16
  619. %define num_mmregs 8
  620. %if ARCH_X86_64
  621. %define num_mmregs 16
  622. %endif
  623. %define mova movdqa
  624. %define movu movdqu
  625. %define movh movq
  626. %define movnta movntdq
  627. %assign %%i 0
  628. %rep num_mmregs
  629. CAT_XDEFINE m, %%i, xmm %+ %%i
  630. CAT_XDEFINE nxmm, %%i, %%i
  631. %assign %%i %%i+1
  632. %endrep
  633. INIT_CPUFLAGS %1
  634. %endmacro
  635. ; FIXME: INIT_AVX can be replaced by INIT_XMM avx
  636. %macro INIT_AVX 0
  637. INIT_XMM
  638. %assign avx_enabled 1
  639. %define PALIGNR PALIGNR_SSSE3
  640. %define RESET_MM_PERMUTATION INIT_AVX
  641. %endmacro
  642. %macro INIT_YMM 0-1+
  643. %assign avx_enabled 1
  644. %define RESET_MM_PERMUTATION INIT_YMM %1
  645. %define mmsize 32
  646. %define num_mmregs 8
  647. %if ARCH_X86_64
  648. %define num_mmregs 16
  649. %endif
  650. %define mova vmovaps
  651. %define movu vmovups
  652. %undef movh
  653. %define movnta vmovntps
  654. %assign %%i 0
  655. %rep num_mmregs
  656. CAT_XDEFINE m, %%i, ymm %+ %%i
  657. CAT_XDEFINE nymm, %%i, %%i
  658. %assign %%i %%i+1
  659. %endrep
  660. INIT_CPUFLAGS %1
  661. %endmacro
  662. INIT_XMM
  663. ; I often want to use macros that permute their arguments. e.g. there's no
  664. ; efficient way to implement butterfly or transpose or dct without swapping some
  665. ; arguments.
  666. ;
  667. ; I would like to not have to manually keep track of the permutations:
  668. ; If I insert a permutation in the middle of a function, it should automatically
  669. ; change everything that follows. For more complex macros I may also have multiple
  670. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  671. ;
  672. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  673. ; permutes its arguments. It's equivalent to exchanging the contents of the
  674. ; registers, except that this way you exchange the register names instead, so it
  675. ; doesn't cost any cycles.
  676. %macro PERMUTE 2-* ; takes a list of pairs to swap
  677. %rep %0/2
  678. %xdefine tmp%2 m%2
  679. %xdefine ntmp%2 nm%2
  680. %rotate 2
  681. %endrep
  682. %rep %0/2
  683. %xdefine m%1 tmp%2
  684. %xdefine nm%1 ntmp%2
  685. %undef tmp%2
  686. %undef ntmp%2
  687. %rotate 2
  688. %endrep
  689. %endmacro
  690. %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
  691. %rep %0-1
  692. %ifdef m%1
  693. %xdefine tmp m%1
  694. %xdefine m%1 m%2
  695. %xdefine m%2 tmp
  696. CAT_XDEFINE n, m%1, %1
  697. CAT_XDEFINE n, m%2, %2
  698. %else
  699. ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
  700. ; Be careful using this mode in nested macros though, as in some cases there may be
  701. ; other copies of m# that have already been dereferenced and don't get updated correctly.
  702. %xdefine %%n1 n %+ %1
  703. %xdefine %%n2 n %+ %2
  704. %xdefine tmp m %+ %%n1
  705. CAT_XDEFINE m, %%n1, m %+ %%n2
  706. CAT_XDEFINE m, %%n2, tmp
  707. CAT_XDEFINE n, m %+ %%n1, %%n1
  708. CAT_XDEFINE n, m %+ %%n2, %%n2
  709. %endif
  710. %undef tmp
  711. %rotate 1
  712. %endrep
  713. %endmacro
  714. ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
  715. ; calls to that function will automatically load the permutation, so values can
  716. ; be returned in mmregs.
  717. %macro SAVE_MM_PERMUTATION 0-1
  718. %if %0
  719. %xdefine %%f %1_m
  720. %else
  721. %xdefine %%f current_function %+ _m
  722. %endif
  723. %assign %%i 0
  724. %rep num_mmregs
  725. CAT_XDEFINE %%f, %%i, m %+ %%i
  726. %assign %%i %%i+1
  727. %endrep
  728. %endmacro
  729. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  730. %ifdef %1_m0
  731. %assign %%i 0
  732. %rep num_mmregs
  733. CAT_XDEFINE m, %%i, %1_m %+ %%i
  734. CAT_XDEFINE n, m %+ %%i, %%i
  735. %assign %%i %%i+1
  736. %endrep
  737. %endif
  738. %endmacro
  739. ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
  740. %macro call 1
  741. ; HACK: work around %+ broken with empty SUFFIX for nasm 2.09.10
  742. %ifndef cpuname
  743. call_internal %1, %1
  744. %else
  745. call_internal %1, %1 %+ SUFFIX
  746. %endif
  747. %endmacro
  748. %macro call_internal 2
  749. %xdefine %%i %1
  750. %ifndef cglobaled_%1
  751. %ifdef cglobaled_%2
  752. %xdefine %%i %2
  753. %endif
  754. %endif
  755. call %%i
  756. LOAD_MM_PERMUTATION %%i
  757. %endmacro
  758. ; Substitutions that reduce instruction size but are functionally equivalent
  759. %macro add 2
  760. %ifnum %2
  761. %if %2==128
  762. sub %1, -128
  763. %else
  764. add %1, %2
  765. %endif
  766. %else
  767. add %1, %2
  768. %endif
  769. %endmacro
  770. %macro sub 2
  771. %ifnum %2
  772. %if %2==128
  773. add %1, -128
  774. %else
  775. sub %1, %2
  776. %endif
  777. %else
  778. sub %1, %2
  779. %endif
  780. %endmacro
  781. ;=============================================================================
  782. ; AVX abstraction layer
  783. ;=============================================================================
  784. %assign i 0
  785. %rep 16
  786. %if i < 8
  787. CAT_XDEFINE sizeofmm, i, 8
  788. %endif
  789. CAT_XDEFINE sizeofxmm, i, 16
  790. CAT_XDEFINE sizeofymm, i, 32
  791. %assign i i+1
  792. %endrep
  793. %undef i
  794. %macro CHECK_AVX_INSTR_EMU 3-*
  795. %xdefine %%opcode %1
  796. %xdefine %%dst %2
  797. %rep %0-2
  798. %ifidn %%dst, %3
  799. %error non-avx emulation of ``%%opcode'' is not supported
  800. %endif
  801. %rotate 1
  802. %endrep
  803. %endmacro
  804. ;%1 == instruction
  805. ;%2 == 1 if float, 0 if int
  806. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
  807. ;%4 == number of operands given
  808. ;%5+: operands
  809. %macro RUN_AVX_INSTR 6-7+
  810. %ifid %6
  811. %define %%sizeofreg sizeof%6
  812. %elifid %5
  813. %define %%sizeofreg sizeof%5
  814. %else
  815. %define %%sizeofreg mmsize
  816. %endif
  817. %if %%sizeofreg==32
  818. %if %4>=3
  819. v%1 %5, %6, %7
  820. %else
  821. v%1 %5, %6
  822. %endif
  823. %else
  824. %if %%sizeofreg==8
  825. %define %%regmov movq
  826. %elif %2
  827. %define %%regmov movaps
  828. %else
  829. %define %%regmov movdqa
  830. %endif
  831. %if %4>=3+%3
  832. %ifnidn %5, %6
  833. %if avx_enabled && %%sizeofreg==16
  834. v%1 %5, %6, %7
  835. %else
  836. CHECK_AVX_INSTR_EMU {%1 %5, %6, %7}, %5, %7
  837. %%regmov %5, %6
  838. %1 %5, %7
  839. %endif
  840. %else
  841. %1 %5, %7
  842. %endif
  843. %elif %4>=3
  844. %1 %5, %6, %7
  845. %else
  846. %1 %5, %6
  847. %endif
  848. %endif
  849. %endmacro
  850. ; 3arg AVX ops with a memory arg can only have it in src2,
  851. ; whereas SSE emulation of 3arg prefers to have it in src1 (i.e. the mov).
  852. ; So, if the op is symmetric and the wrong one is memory, swap them.
  853. %macro RUN_AVX_INSTR1 8
  854. %assign %%swap 0
  855. %if avx_enabled
  856. %ifnid %6
  857. %assign %%swap 1
  858. %endif
  859. %elifnidn %5, %6
  860. %ifnid %7
  861. %assign %%swap 1
  862. %endif
  863. %endif
  864. %if %%swap && %3 == 0 && %8 == 1
  865. RUN_AVX_INSTR %1, %2, %3, %4, %5, %7, %6
  866. %else
  867. RUN_AVX_INSTR %1, %2, %3, %4, %5, %6, %7
  868. %endif
  869. %endmacro
  870. ;%1 == instruction
  871. ;%2 == 1 if float, 0 if int
  872. ;%3 == 1 if 4-operand (xmm, xmm, xmm, imm), 0 if 2- or 3-operand (xmm, xmm, xmm)
  873. ;%4 == 1 if symmetric (i.e. doesn't matter which src arg is which), 0 if not
  874. %macro AVX_INSTR 4
  875. %macro %1 2-9 fnord, fnord, fnord, %1, %2, %3, %4
  876. %ifidn %3, fnord
  877. RUN_AVX_INSTR %6, %7, %8, 2, %1, %2
  878. %elifidn %4, fnord
  879. RUN_AVX_INSTR1 %6, %7, %8, 3, %1, %2, %3, %9
  880. %elifidn %5, fnord
  881. RUN_AVX_INSTR %6, %7, %8, 4, %1, %2, %3, %4
  882. %else
  883. RUN_AVX_INSTR %6, %7, %8, 5, %1, %2, %3, %4, %5
  884. %endif
  885. %endmacro
  886. %endmacro
  887. AVX_INSTR addpd, 1, 0, 1
  888. AVX_INSTR addps, 1, 0, 1
  889. AVX_INSTR addsd, 1, 0, 1
  890. AVX_INSTR addss, 1, 0, 1
  891. AVX_INSTR addsubpd, 1, 0, 0
  892. AVX_INSTR addsubps, 1, 0, 0
  893. AVX_INSTR andpd, 1, 0, 1
  894. AVX_INSTR andps, 1, 0, 1
  895. AVX_INSTR andnpd, 1, 0, 0
  896. AVX_INSTR andnps, 1, 0, 0
  897. AVX_INSTR blendpd, 1, 0, 0
  898. AVX_INSTR blendps, 1, 0, 0
  899. AVX_INSTR blendvpd, 1, 0, 0
  900. AVX_INSTR blendvps, 1, 0, 0
  901. AVX_INSTR cmppd, 1, 0, 0
  902. AVX_INSTR cmpps, 1, 0, 0
  903. AVX_INSTR cmpsd, 1, 0, 0
  904. AVX_INSTR cmpss, 1, 0, 0
  905. AVX_INSTR cvtdq2ps, 1, 0, 0
  906. AVX_INSTR cvtps2dq, 1, 0, 0
  907. AVX_INSTR divpd, 1, 0, 0
  908. AVX_INSTR divps, 1, 0, 0
  909. AVX_INSTR divsd, 1, 0, 0
  910. AVX_INSTR divss, 1, 0, 0
  911. AVX_INSTR dppd, 1, 1, 0
  912. AVX_INSTR dpps, 1, 1, 0
  913. AVX_INSTR haddpd, 1, 0, 0
  914. AVX_INSTR haddps, 1, 0, 0
  915. AVX_INSTR hsubpd, 1, 0, 0
  916. AVX_INSTR hsubps, 1, 0, 0
  917. AVX_INSTR maxpd, 1, 0, 1
  918. AVX_INSTR maxps, 1, 0, 1
  919. AVX_INSTR maxsd, 1, 0, 1
  920. AVX_INSTR maxss, 1, 0, 1
  921. AVX_INSTR minpd, 1, 0, 1
  922. AVX_INSTR minps, 1, 0, 1
  923. AVX_INSTR minsd, 1, 0, 1
  924. AVX_INSTR minss, 1, 0, 1
  925. AVX_INSTR movhlps, 1, 0, 0
  926. AVX_INSTR movlhps, 1, 0, 0
  927. AVX_INSTR movsd, 1, 0, 0
  928. AVX_INSTR movss, 1, 0, 0
  929. AVX_INSTR mpsadbw, 0, 1, 0
  930. AVX_INSTR mulpd, 1, 0, 1
  931. AVX_INSTR mulps, 1, 0, 1
  932. AVX_INSTR mulsd, 1, 0, 1
  933. AVX_INSTR mulss, 1, 0, 1
  934. AVX_INSTR orpd, 1, 0, 1
  935. AVX_INSTR orps, 1, 0, 1
  936. AVX_INSTR pabsb, 0, 0, 0
  937. AVX_INSTR pabsw, 0, 0, 0
  938. AVX_INSTR pabsd, 0, 0, 0
  939. AVX_INSTR packsswb, 0, 0, 0
  940. AVX_INSTR packssdw, 0, 0, 0
  941. AVX_INSTR packuswb, 0, 0, 0
  942. AVX_INSTR packusdw, 0, 0, 0
  943. AVX_INSTR paddb, 0, 0, 1
  944. AVX_INSTR paddw, 0, 0, 1
  945. AVX_INSTR paddd, 0, 0, 1
  946. AVX_INSTR paddq, 0, 0, 1
  947. AVX_INSTR paddsb, 0, 0, 1
  948. AVX_INSTR paddsw, 0, 0, 1
  949. AVX_INSTR paddusb, 0, 0, 1
  950. AVX_INSTR paddusw, 0, 0, 1
  951. AVX_INSTR palignr, 0, 1, 0
  952. AVX_INSTR pand, 0, 0, 1
  953. AVX_INSTR pandn, 0, 0, 0
  954. AVX_INSTR pavgb, 0, 0, 1
  955. AVX_INSTR pavgw, 0, 0, 1
  956. AVX_INSTR pblendvb, 0, 0, 0
  957. AVX_INSTR pblendw, 0, 1, 0
  958. AVX_INSTR pcmpestri, 0, 0, 0
  959. AVX_INSTR pcmpestrm, 0, 0, 0
  960. AVX_INSTR pcmpistri, 0, 0, 0
  961. AVX_INSTR pcmpistrm, 0, 0, 0
  962. AVX_INSTR pcmpeqb, 0, 0, 1
  963. AVX_INSTR pcmpeqw, 0, 0, 1
  964. AVX_INSTR pcmpeqd, 0, 0, 1
  965. AVX_INSTR pcmpeqq, 0, 0, 1
  966. AVX_INSTR pcmpgtb, 0, 0, 0
  967. AVX_INSTR pcmpgtw, 0, 0, 0
  968. AVX_INSTR pcmpgtd, 0, 0, 0
  969. AVX_INSTR pcmpgtq, 0, 0, 0
  970. AVX_INSTR phaddw, 0, 0, 0
  971. AVX_INSTR phaddd, 0, 0, 0
  972. AVX_INSTR phaddsw, 0, 0, 0
  973. AVX_INSTR phsubw, 0, 0, 0
  974. AVX_INSTR phsubd, 0, 0, 0
  975. AVX_INSTR phsubsw, 0, 0, 0
  976. AVX_INSTR pmaddwd, 0, 0, 1
  977. AVX_INSTR pmaddubsw, 0, 0, 0
  978. AVX_INSTR pmaxsb, 0, 0, 1
  979. AVX_INSTR pmaxsw, 0, 0, 1
  980. AVX_INSTR pmaxsd, 0, 0, 1
  981. AVX_INSTR pmaxub, 0, 0, 1
  982. AVX_INSTR pmaxuw, 0, 0, 1
  983. AVX_INSTR pmaxud, 0, 0, 1
  984. AVX_INSTR pminsb, 0, 0, 1
  985. AVX_INSTR pminsw, 0, 0, 1
  986. AVX_INSTR pminsd, 0, 0, 1
  987. AVX_INSTR pminub, 0, 0, 1
  988. AVX_INSTR pminuw, 0, 0, 1
  989. AVX_INSTR pminud, 0, 0, 1
  990. AVX_INSTR pmovmskb, 0, 0, 0
  991. AVX_INSTR pmulhuw, 0, 0, 1
  992. AVX_INSTR pmulhrsw, 0, 0, 1
  993. AVX_INSTR pmulhw, 0, 0, 1
  994. AVX_INSTR pmullw, 0, 0, 1
  995. AVX_INSTR pmulld, 0, 0, 1
  996. AVX_INSTR pmuludq, 0, 0, 1
  997. AVX_INSTR pmuldq, 0, 0, 1
  998. AVX_INSTR por, 0, 0, 1
  999. AVX_INSTR psadbw, 0, 0, 1
  1000. AVX_INSTR pshufb, 0, 0, 0
  1001. AVX_INSTR pshufd, 0, 1, 0
  1002. AVX_INSTR pshufhw, 0, 1, 0
  1003. AVX_INSTR pshuflw, 0, 1, 0
  1004. AVX_INSTR psignb, 0, 0, 0
  1005. AVX_INSTR psignw, 0, 0, 0
  1006. AVX_INSTR psignd, 0, 0, 0
  1007. AVX_INSTR psllw, 0, 0, 0
  1008. AVX_INSTR pslld, 0, 0, 0
  1009. AVX_INSTR psllq, 0, 0, 0
  1010. AVX_INSTR pslldq, 0, 0, 0
  1011. AVX_INSTR psraw, 0, 0, 0
  1012. AVX_INSTR psrad, 0, 0, 0
  1013. AVX_INSTR psrlw, 0, 0, 0
  1014. AVX_INSTR psrld, 0, 0, 0
  1015. AVX_INSTR psrlq, 0, 0, 0
  1016. AVX_INSTR psrldq, 0, 0, 0
  1017. AVX_INSTR psubb, 0, 0, 0
  1018. AVX_INSTR psubw, 0, 0, 0
  1019. AVX_INSTR psubd, 0, 0, 0
  1020. AVX_INSTR psubq, 0, 0, 0
  1021. AVX_INSTR psubsb, 0, 0, 0
  1022. AVX_INSTR psubsw, 0, 0, 0
  1023. AVX_INSTR psubusb, 0, 0, 0
  1024. AVX_INSTR psubusw, 0, 0, 0
  1025. AVX_INSTR ptest, 0, 0, 0
  1026. AVX_INSTR punpckhbw, 0, 0, 0
  1027. AVX_INSTR punpckhwd, 0, 0, 0
  1028. AVX_INSTR punpckhdq, 0, 0, 0
  1029. AVX_INSTR punpckhqdq, 0, 0, 0
  1030. AVX_INSTR punpcklbw, 0, 0, 0
  1031. AVX_INSTR punpcklwd, 0, 0, 0
  1032. AVX_INSTR punpckldq, 0, 0, 0
  1033. AVX_INSTR punpcklqdq, 0, 0, 0
  1034. AVX_INSTR pxor, 0, 0, 1
  1035. AVX_INSTR shufps, 1, 1, 0
  1036. AVX_INSTR subpd, 1, 0, 0
  1037. AVX_INSTR subps, 1, 0, 0
  1038. AVX_INSTR subsd, 1, 0, 0
  1039. AVX_INSTR subss, 1, 0, 0
  1040. AVX_INSTR unpckhpd, 1, 0, 0
  1041. AVX_INSTR unpckhps, 1, 0, 0
  1042. AVX_INSTR unpcklpd, 1, 0, 0
  1043. AVX_INSTR unpcklps, 1, 0, 0
  1044. AVX_INSTR xorpd, 1, 0, 1
  1045. AVX_INSTR xorps, 1, 0, 1
  1046. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  1047. AVX_INSTR pfadd, 1, 0, 1
  1048. AVX_INSTR pfsub, 1, 0, 0
  1049. AVX_INSTR pfmul, 1, 0, 1
  1050. ; base-4 constants for shuffles
  1051. %assign i 0
  1052. %rep 256
  1053. %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
  1054. %if j < 10
  1055. CAT_XDEFINE q000, j, i
  1056. %elif j < 100
  1057. CAT_XDEFINE q00, j, i
  1058. %elif j < 1000
  1059. CAT_XDEFINE q0, j, i
  1060. %else
  1061. CAT_XDEFINE q, j, i
  1062. %endif
  1063. %assign i i+1
  1064. %endrep
  1065. %undef i
  1066. %undef j
  1067. %macro FMA_INSTR 3
  1068. %macro %1 5-8 %1, %2, %3
  1069. %if cpuflag(xop) || cpuflag(fma4)
  1070. v%6 %1, %2, %3, %4
  1071. %else
  1072. %ifidn %1, %4
  1073. %7 %5, %2, %3
  1074. %8 %1, %4, %5
  1075. %else
  1076. %7 %1, %2, %3
  1077. %8 %1, %4
  1078. %endif
  1079. %endif
  1080. %endmacro
  1081. %endmacro
  1082. FMA_INSTR fmaddps, mulps, addps
  1083. FMA_INSTR pmacsdd, pmulld, paddd
  1084. FMA_INSTR pmacsww, pmullw, paddw
  1085. FMA_INSTR pmadcswd, pmaddwd, paddd
  1086. ; tzcnt is equivalent to "rep bsf" and is backwards-compatible with bsf.
  1087. ; This lets us use tzcnt without bumping the yasm version requirement yet.
  1088. %define tzcnt rep bsf