x86inc.asm 29 KB

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