x86inc.asm 30 KB

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