x86inc.asm 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. ;*****************************************************************************
  2. ;* x86inc.asm: x264asm abstraction layer
  3. ;*****************************************************************************
  4. ;* Copyright (C) 2005-2015 x264 project
  5. ;*
  6. ;* Authors: Loren Merritt <lorenm@u.washington.edu>
  7. ;* Anton Mitrofanov <BugMaster@narod.ru>
  8. ;* Fiona Glaser <fiona@x264.com>
  9. ;* Henrik Gramner <henrik@gramner.com>
  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. %ifndef private_prefix
  35. %define private_prefix x264
  36. %endif
  37. %ifndef public_prefix
  38. %define public_prefix private_prefix
  39. %endif
  40. %define WIN64 0
  41. %define UNIX64 0
  42. %if ARCH_X86_64
  43. %ifidn __OUTPUT_FORMAT__,win32
  44. %define WIN64 1
  45. %elifidn __OUTPUT_FORMAT__,win64
  46. %define WIN64 1
  47. %elifidn __OUTPUT_FORMAT__,x64
  48. %define WIN64 1
  49. %else
  50. %define UNIX64 1
  51. %endif
  52. %endif
  53. %define FORMAT_ELF 0
  54. %ifidn __OUTPUT_FORMAT__,elf
  55. %define FORMAT_ELF 1
  56. %elifidn __OUTPUT_FORMAT__,elf32
  57. %define FORMAT_ELF 1
  58. %elifidn __OUTPUT_FORMAT__,elf64
  59. %define FORMAT_ELF 1
  60. %endif
  61. %ifdef PREFIX
  62. %define mangle(x) _ %+ x
  63. %else
  64. %define mangle(x) x
  65. %endif
  66. ; aout does not support align=
  67. ; NOTE: This section is out of sync with x264, in order to
  68. ; keep supporting OS/2.
  69. %macro SECTION_RODATA 0-1 16
  70. %ifidn __OUTPUT_FORMAT__,aout
  71. section .text
  72. %else
  73. SECTION .rodata align=%1
  74. %endif
  75. %endmacro
  76. %macro SECTION_TEXT 0-1 16
  77. %ifidn __OUTPUT_FORMAT__,aout
  78. SECTION .text
  79. %else
  80. SECTION .text align=%1
  81. %endif
  82. %endmacro
  83. %if WIN64
  84. %define PIC
  85. %elif ARCH_X86_64 == 0
  86. ; x86_32 doesn't require PIC.
  87. ; Some distros prefer shared objects to be PIC, but nothing breaks if
  88. ; the code contains a few textrels, so we'll skip that complexity.
  89. %undef PIC
  90. %endif
  91. %ifdef PIC
  92. default rel
  93. %endif
  94. %macro CPUNOP 1
  95. %if HAVE_CPUNOP
  96. CPU %1
  97. %endif
  98. %endmacro
  99. ; Macros to eliminate most code duplication between x86_32 and x86_64:
  100. ; Currently this works only for leaf functions which load all their arguments
  101. ; into registers at the start, and make no other use of the stack. Luckily that
  102. ; covers most of x264's asm.
  103. ; PROLOGUE:
  104. ; %1 = number of arguments. loads them from stack if needed.
  105. ; %2 = number of registers used. pushes callee-saved regs if needed.
  106. ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
  107. ; %4 = (optional) stack size to be allocated. If not aligned (x86-32 ICC 10.x,
  108. ; MSVC or YMM), the stack will be manually aligned (to 16 or 32 bytes),
  109. ; and an extra register will be allocated to hold the original stack
  110. ; pointer (to not invalidate r0m etc.). To prevent the use of an extra
  111. ; register as stack pointer, request a negative stack size.
  112. ; %4+/%5+ = list of names to define to registers
  113. ; PROLOGUE can also be invoked by adding the same options to cglobal
  114. ; e.g.
  115. ; cglobal foo, 2,3,0, dst, src, tmp
  116. ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
  117. ; TODO Some functions can use some args directly from the stack. If they're the
  118. ; last args then you can just not declare them, but if they're in the middle
  119. ; we need more flexible macro.
  120. ; RET:
  121. ; Pops anything that was pushed by PROLOGUE, and returns.
  122. ; REP_RET:
  123. ; Use this instead of RET if it's a branch target.
  124. ; registers:
  125. ; rN and rNq are the native-size register holding function argument N
  126. ; rNd, rNw, rNb are dword, word, and byte size
  127. ; rNh is the high 8 bits of the word size
  128. ; rNm is the original location of arg N (a register or on the stack), dword
  129. ; rNmp is native size
  130. %macro DECLARE_REG 2-3
  131. %define r%1q %2
  132. %define r%1d %2d
  133. %define r%1w %2w
  134. %define r%1b %2b
  135. %define r%1h %2h
  136. %define %2q %2
  137. %if %0 == 2
  138. %define r%1m %2d
  139. %define r%1mp %2
  140. %elif ARCH_X86_64 ; memory
  141. %define r%1m [rstk + stack_offset + %3]
  142. %define r%1mp qword r %+ %1 %+ m
  143. %else
  144. %define r%1m [rstk + stack_offset + %3]
  145. %define r%1mp dword r %+ %1 %+ m
  146. %endif
  147. %define r%1 %2
  148. %endmacro
  149. %macro DECLARE_REG_SIZE 3
  150. %define r%1q r%1
  151. %define e%1q r%1
  152. %define r%1d e%1
  153. %define e%1d e%1
  154. %define r%1w %1
  155. %define e%1w %1
  156. %define r%1h %3
  157. %define e%1h %3
  158. %define r%1b %2
  159. %define e%1b %2
  160. %if ARCH_X86_64 == 0
  161. %define r%1 e%1
  162. %endif
  163. %endmacro
  164. DECLARE_REG_SIZE ax, al, ah
  165. DECLARE_REG_SIZE bx, bl, bh
  166. DECLARE_REG_SIZE cx, cl, ch
  167. DECLARE_REG_SIZE dx, dl, dh
  168. DECLARE_REG_SIZE si, sil, null
  169. DECLARE_REG_SIZE di, dil, null
  170. DECLARE_REG_SIZE bp, bpl, null
  171. ; t# defines for when per-arch register allocation is more complex than just function arguments
  172. %macro DECLARE_REG_TMP 1-*
  173. %assign %%i 0
  174. %rep %0
  175. CAT_XDEFINE t, %%i, r%1
  176. %assign %%i %%i+1
  177. %rotate 1
  178. %endrep
  179. %endmacro
  180. %macro DECLARE_REG_TMP_SIZE 0-*
  181. %rep %0
  182. %define t%1q t%1 %+ q
  183. %define t%1d t%1 %+ d
  184. %define t%1w t%1 %+ w
  185. %define t%1h t%1 %+ h
  186. %define t%1b t%1 %+ b
  187. %rotate 1
  188. %endrep
  189. %endmacro
  190. DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
  191. %if ARCH_X86_64
  192. %define gprsize 8
  193. %else
  194. %define gprsize 4
  195. %endif
  196. %macro PUSH 1
  197. push %1
  198. %ifidn rstk, rsp
  199. %assign stack_offset stack_offset+gprsize
  200. %endif
  201. %endmacro
  202. %macro POP 1
  203. pop %1
  204. %ifidn rstk, rsp
  205. %assign stack_offset stack_offset-gprsize
  206. %endif
  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, rstk
  235. %assign stack_offset stack_offset+(%2)
  236. %endif
  237. %endmacro
  238. %macro ADD 2
  239. add %1, %2
  240. %ifidn %1, rstk
  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. %macro ALLOC_STACK 1-2 0 ; stack_size, n_xmm_regs (for win64 only)
  293. %ifnum %1
  294. %if %1 != 0
  295. %assign %%stack_alignment ((mmsize + 15) & ~15)
  296. %assign stack_size %1
  297. %if stack_size < 0
  298. %assign stack_size -stack_size
  299. %endif
  300. %assign stack_size_padded stack_size
  301. %if WIN64
  302. %assign stack_size_padded stack_size_padded + 32 ; reserve 32 bytes for shadow space
  303. %if mmsize != 8
  304. %assign xmm_regs_used %2
  305. %if xmm_regs_used > 8
  306. %assign stack_size_padded stack_size_padded + (xmm_regs_used-8)*16
  307. %endif
  308. %endif
  309. %endif
  310. %if mmsize <= 16 && HAVE_ALIGNED_STACK
  311. %assign stack_size_padded stack_size_padded + %%stack_alignment - gprsize - (stack_offset & (%%stack_alignment - 1))
  312. SUB rsp, stack_size_padded
  313. %else
  314. %assign %%reg_num (regs_used - 1)
  315. %xdefine rstk r %+ %%reg_num
  316. ; align stack, and save original stack location directly above
  317. ; it, i.e. in [rsp+stack_size_padded], so we can restore the
  318. ; stack in a single instruction (i.e. mov rsp, rstk or mov
  319. ; rsp, [rsp+stack_size_padded])
  320. mov rstk, rsp
  321. %if %1 < 0 ; need to store rsp on stack
  322. sub rsp, gprsize+stack_size_padded
  323. and rsp, ~(%%stack_alignment-1)
  324. %xdefine rstkm [rsp+stack_size_padded]
  325. mov rstkm, rstk
  326. %else ; can keep rsp in rstk during whole function
  327. sub rsp, stack_size_padded
  328. and rsp, ~(%%stack_alignment-1)
  329. %xdefine rstkm rstk
  330. %endif
  331. %endif
  332. WIN64_PUSH_XMM
  333. %endif
  334. %endif
  335. %endmacro
  336. %macro SETUP_STACK_POINTER 1
  337. %ifnum %1
  338. %if %1 != 0 && (HAVE_ALIGNED_STACK == 0 || mmsize == 32)
  339. %if %1 > 0
  340. %assign regs_used (regs_used + 1)
  341. %elif ARCH_X86_64 && regs_used == num_args && num_args <= 4 + UNIX64 * 2
  342. %warning "Stack pointer will overwrite register argument"
  343. %endif
  344. %endif
  345. %endif
  346. %endmacro
  347. %macro DEFINE_ARGS_INTERNAL 3+
  348. %ifnum %2
  349. DEFINE_ARGS %3
  350. %elif %1 == 4
  351. DEFINE_ARGS %2
  352. %elif %1 > 4
  353. DEFINE_ARGS %2, %3
  354. %endif
  355. %endmacro
  356. %if WIN64 ; Windows x64 ;=================================================
  357. DECLARE_REG 0, rcx
  358. DECLARE_REG 1, rdx
  359. DECLARE_REG 2, R8
  360. DECLARE_REG 3, R9
  361. DECLARE_REG 4, R10, 40
  362. DECLARE_REG 5, R11, 48
  363. DECLARE_REG 6, rax, 56
  364. DECLARE_REG 7, rdi, 64
  365. DECLARE_REG 8, rsi, 72
  366. DECLARE_REG 9, rbx, 80
  367. DECLARE_REG 10, rbp, 88
  368. DECLARE_REG 11, R12, 96
  369. DECLARE_REG 12, R13, 104
  370. DECLARE_REG 13, R14, 112
  371. DECLARE_REG 14, R15, 120
  372. %macro PROLOGUE 2-5+ 0 ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  373. %assign num_args %1
  374. %assign regs_used %2
  375. ASSERT regs_used >= num_args
  376. SETUP_STACK_POINTER %4
  377. ASSERT regs_used <= 15
  378. PUSH_IF_USED 7, 8, 9, 10, 11, 12, 13, 14
  379. ALLOC_STACK %4, %3
  380. %if mmsize != 8 && stack_size == 0
  381. WIN64_SPILL_XMM %3
  382. %endif
  383. LOAD_IF_USED 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  384. DEFINE_ARGS_INTERNAL %0, %4, %5
  385. %endmacro
  386. %macro WIN64_PUSH_XMM 0
  387. ; Use the shadow space to store XMM6 and XMM7, the rest needs stack space allocated.
  388. %if xmm_regs_used > 6
  389. movaps [rstk + stack_offset + 8], xmm6
  390. %endif
  391. %if xmm_regs_used > 7
  392. movaps [rstk + stack_offset + 24], xmm7
  393. %endif
  394. %if xmm_regs_used > 8
  395. %assign %%i 8
  396. %rep xmm_regs_used-8
  397. movaps [rsp + (%%i-8)*16 + stack_size + 32], xmm %+ %%i
  398. %assign %%i %%i+1
  399. %endrep
  400. %endif
  401. %endmacro
  402. %macro WIN64_SPILL_XMM 1
  403. %assign xmm_regs_used %1
  404. ASSERT xmm_regs_used <= 16
  405. %if xmm_regs_used > 8
  406. %assign stack_size_padded (xmm_regs_used-8)*16 + (~stack_offset&8) + 32
  407. SUB rsp, stack_size_padded
  408. %endif
  409. WIN64_PUSH_XMM
  410. %endmacro
  411. %macro WIN64_RESTORE_XMM_INTERNAL 1
  412. %assign %%pad_size 0
  413. %if xmm_regs_used > 8
  414. %assign %%i xmm_regs_used
  415. %rep xmm_regs_used-8
  416. %assign %%i %%i-1
  417. movaps xmm %+ %%i, [%1 + (%%i-8)*16 + stack_size + 32]
  418. %endrep
  419. %endif
  420. %if stack_size_padded > 0
  421. %if stack_size > 0 && (mmsize == 32 || HAVE_ALIGNED_STACK == 0)
  422. mov rsp, rstkm
  423. %else
  424. add %1, stack_size_padded
  425. %assign %%pad_size stack_size_padded
  426. %endif
  427. %endif
  428. %if xmm_regs_used > 7
  429. movaps xmm7, [%1 + stack_offset - %%pad_size + 24]
  430. %endif
  431. %if xmm_regs_used > 6
  432. movaps xmm6, [%1 + stack_offset - %%pad_size + 8]
  433. %endif
  434. %endmacro
  435. %macro WIN64_RESTORE_XMM 1
  436. WIN64_RESTORE_XMM_INTERNAL %1
  437. %assign stack_offset (stack_offset-stack_size_padded)
  438. %assign xmm_regs_used 0
  439. %endmacro
  440. %define has_epilogue regs_used > 7 || xmm_regs_used > 6 || mmsize == 32 || stack_size > 0
  441. %macro RET 0
  442. WIN64_RESTORE_XMM_INTERNAL rsp
  443. POP_IF_USED 14, 13, 12, 11, 10, 9, 8, 7
  444. %if mmsize == 32
  445. vzeroupper
  446. %endif
  447. AUTO_REP_RET
  448. %endmacro
  449. %elif ARCH_X86_64 ; *nix x64 ;=============================================
  450. DECLARE_REG 0, rdi
  451. DECLARE_REG 1, rsi
  452. DECLARE_REG 2, rdx
  453. DECLARE_REG 3, rcx
  454. DECLARE_REG 4, R8
  455. DECLARE_REG 5, R9
  456. DECLARE_REG 6, rax, 8
  457. DECLARE_REG 7, R10, 16
  458. DECLARE_REG 8, R11, 24
  459. DECLARE_REG 9, rbx, 32
  460. DECLARE_REG 10, rbp, 40
  461. DECLARE_REG 11, R12, 48
  462. DECLARE_REG 12, R13, 56
  463. DECLARE_REG 13, R14, 64
  464. DECLARE_REG 14, R15, 72
  465. %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  466. %assign num_args %1
  467. %assign regs_used %2
  468. ASSERT regs_used >= num_args
  469. SETUP_STACK_POINTER %4
  470. ASSERT regs_used <= 15
  471. PUSH_IF_USED 9, 10, 11, 12, 13, 14
  472. ALLOC_STACK %4
  473. LOAD_IF_USED 6, 7, 8, 9, 10, 11, 12, 13, 14
  474. DEFINE_ARGS_INTERNAL %0, %4, %5
  475. %endmacro
  476. %define has_epilogue regs_used > 9 || mmsize == 32 || stack_size > 0
  477. %macro RET 0
  478. %if stack_size_padded > 0
  479. %if mmsize == 32 || HAVE_ALIGNED_STACK == 0
  480. mov rsp, rstkm
  481. %else
  482. add rsp, stack_size_padded
  483. %endif
  484. %endif
  485. POP_IF_USED 14, 13, 12, 11, 10, 9
  486. %if mmsize == 32
  487. vzeroupper
  488. %endif
  489. AUTO_REP_RET
  490. %endmacro
  491. %else ; X86_32 ;==============================================================
  492. DECLARE_REG 0, eax, 4
  493. DECLARE_REG 1, ecx, 8
  494. DECLARE_REG 2, edx, 12
  495. DECLARE_REG 3, ebx, 16
  496. DECLARE_REG 4, esi, 20
  497. DECLARE_REG 5, edi, 24
  498. DECLARE_REG 6, ebp, 28
  499. %define rsp esp
  500. %macro DECLARE_ARG 1-*
  501. %rep %0
  502. %define r%1m [rstk + stack_offset + 4*%1 + 4]
  503. %define r%1mp dword r%1m
  504. %rotate 1
  505. %endrep
  506. %endmacro
  507. DECLARE_ARG 7, 8, 9, 10, 11, 12, 13, 14
  508. %macro PROLOGUE 2-5+ ; #args, #regs, #xmm_regs, [stack_size,] arg_names...
  509. %assign num_args %1
  510. %assign regs_used %2
  511. ASSERT regs_used >= num_args
  512. %if num_args > 7
  513. %assign num_args 7
  514. %endif
  515. %if regs_used > 7
  516. %assign regs_used 7
  517. %endif
  518. SETUP_STACK_POINTER %4
  519. ASSERT regs_used <= 7
  520. PUSH_IF_USED 3, 4, 5, 6
  521. ALLOC_STACK %4
  522. LOAD_IF_USED 0, 1, 2, 3, 4, 5, 6
  523. DEFINE_ARGS_INTERNAL %0, %4, %5
  524. %endmacro
  525. %define has_epilogue regs_used > 3 || mmsize == 32 || stack_size > 0
  526. %macro RET 0
  527. %if stack_size_padded > 0
  528. %if mmsize == 32 || HAVE_ALIGNED_STACK == 0
  529. mov rsp, rstkm
  530. %else
  531. add rsp, stack_size_padded
  532. %endif
  533. %endif
  534. POP_IF_USED 6, 5, 4, 3
  535. %if mmsize == 32
  536. vzeroupper
  537. %endif
  538. AUTO_REP_RET
  539. %endmacro
  540. %endif ;======================================================================
  541. %if WIN64 == 0
  542. %macro WIN64_SPILL_XMM 1
  543. %endmacro
  544. %macro WIN64_RESTORE_XMM 1
  545. %endmacro
  546. %macro WIN64_PUSH_XMM 0
  547. %endmacro
  548. %endif
  549. ; On AMD cpus <=K10, an ordinary ret is slow if it immediately follows either
  550. ; a branch or a branch target. So switch to a 2-byte form of ret in that case.
  551. ; We can automatically detect "follows a branch", but not a branch target.
  552. ; (SSSE3 is a sufficient condition to know that your cpu doesn't have this problem.)
  553. %macro REP_RET 0
  554. %if has_epilogue
  555. RET
  556. %else
  557. rep ret
  558. %endif
  559. %endmacro
  560. %define last_branch_adr $$
  561. %macro AUTO_REP_RET 0
  562. %ifndef cpuflags
  563. times ((last_branch_adr-$)>>31)+1 rep ; times 1 iff $ != last_branch_adr.
  564. %elif notcpuflag(ssse3)
  565. times ((last_branch_adr-$)>>31)+1 rep
  566. %endif
  567. ret
  568. %endmacro
  569. %macro BRANCH_INSTR 0-*
  570. %rep %0
  571. %macro %1 1-2 %1
  572. %2 %1
  573. %%branch_instr:
  574. %xdefine last_branch_adr %%branch_instr
  575. %endmacro
  576. %rotate 1
  577. %endrep
  578. %endmacro
  579. BRANCH_INSTR jz, je, jnz, jne, jl, jle, jnl, jnle, jg, jge, jng, jnge, ja, jae, jna, jnae, jb, jbe, jnb, jnbe, jc, jnc, js, jns, jo, jno, jp, jnp
  580. %macro TAIL_CALL 2 ; callee, is_nonadjacent
  581. %if has_epilogue
  582. call %1
  583. RET
  584. %elif %2
  585. jmp %1
  586. %endif
  587. %endmacro
  588. ;=============================================================================
  589. ; arch-independent part
  590. ;=============================================================================
  591. %assign function_align 16
  592. ; Begin a function.
  593. ; Applies any symbol mangling needed for C linkage, and sets up a define such that
  594. ; subsequent uses of the function name automatically refer to the mangled version.
  595. ; Appends cpuflags to the function name if cpuflags has been specified.
  596. ; The "" empty default parameter is a workaround for nasm, which fails if SUFFIX
  597. ; is empty and we call cglobal_internal with just %1 %+ SUFFIX (without %2).
  598. %macro cglobal 1-2+ "" ; name, [PROLOGUE args]
  599. cglobal_internal 1, %1 %+ SUFFIX, %2
  600. %endmacro
  601. %macro cvisible 1-2+ "" ; name, [PROLOGUE args]
  602. cglobal_internal 0, %1 %+ SUFFIX, %2
  603. %endmacro
  604. %macro cglobal_internal 2-3+
  605. %if %1
  606. %xdefine %%FUNCTION_PREFIX private_prefix
  607. %xdefine %%VISIBILITY hidden
  608. %else
  609. %xdefine %%FUNCTION_PREFIX public_prefix
  610. %xdefine %%VISIBILITY
  611. %endif
  612. %ifndef cglobaled_%2
  613. %xdefine %2 mangle(%%FUNCTION_PREFIX %+ _ %+ %2)
  614. %xdefine %2.skip_prologue %2 %+ .skip_prologue
  615. CAT_XDEFINE cglobaled_, %2, 1
  616. %endif
  617. %xdefine current_function %2
  618. %if FORMAT_ELF
  619. global %2:function %%VISIBILITY
  620. %else
  621. global %2
  622. %endif
  623. align function_align
  624. %2:
  625. RESET_MM_PERMUTATION ; needed for x86-64, also makes disassembly somewhat nicer
  626. %xdefine rstk rsp ; copy of the original stack pointer, used when greater alignment than the known stack alignment is required
  627. %assign stack_offset 0 ; stack pointer offset relative to the return address
  628. %assign stack_size 0 ; amount of stack space that can be freely used inside a function
  629. %assign stack_size_padded 0 ; total amount of allocated stack space, including space for callee-saved xmm registers on WIN64 and alignment padding
  630. %assign xmm_regs_used 0 ; number of XMM registers requested, used for dealing with callee-saved registers on WIN64
  631. %ifnidn %3, ""
  632. PROLOGUE %3
  633. %endif
  634. %endmacro
  635. %macro cextern 1
  636. %xdefine %1 mangle(private_prefix %+ _ %+ %1)
  637. CAT_XDEFINE cglobaled_, %1, 1
  638. extern %1
  639. %endmacro
  640. ; like cextern, but without the prefix
  641. %macro cextern_naked 1
  642. %ifdef PREFIX
  643. %xdefine %1 mangle(%1)
  644. %endif
  645. CAT_XDEFINE cglobaled_, %1, 1
  646. extern %1
  647. %endmacro
  648. %macro const 1-2+
  649. %xdefine %1 mangle(private_prefix %+ _ %+ %1)
  650. %if FORMAT_ELF
  651. global %1:data hidden
  652. %else
  653. global %1
  654. %endif
  655. %1: %2
  656. %endmacro
  657. ; This is needed for ELF, otherwise the GNU linker assumes the stack is executable by default.
  658. %if FORMAT_ELF
  659. [SECTION .note.GNU-stack noalloc noexec nowrite progbits]
  660. %endif
  661. ; Overrides the default .text section.
  662. ; Silences warnings when defining structures.
  663. %define __SECT__
  664. ; cpuflags
  665. %assign cpuflags_mmx (1<<0)
  666. %assign cpuflags_mmx2 (1<<1) | cpuflags_mmx
  667. %assign cpuflags_3dnow (1<<2) | cpuflags_mmx
  668. %assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow
  669. %assign cpuflags_sse (1<<4) | cpuflags_mmx2
  670. %assign cpuflags_sse2 (1<<5) | cpuflags_sse
  671. %assign cpuflags_sse2slow (1<<6) | cpuflags_sse2
  672. %assign cpuflags_sse3 (1<<7) | cpuflags_sse2
  673. %assign cpuflags_ssse3 (1<<8) | cpuflags_sse3
  674. %assign cpuflags_sse4 (1<<9) | cpuflags_ssse3
  675. %assign cpuflags_sse42 (1<<10)| cpuflags_sse4
  676. %assign cpuflags_avx (1<<11)| cpuflags_sse42
  677. %assign cpuflags_xop (1<<12)| cpuflags_avx
  678. %assign cpuflags_fma4 (1<<13)| cpuflags_avx
  679. %assign cpuflags_fma3 (1<<14)| cpuflags_avx
  680. %assign cpuflags_avx2 (1<<15)| cpuflags_fma3
  681. %assign cpuflags_cache32 (1<<16)
  682. %assign cpuflags_cache64 (1<<17)
  683. %assign cpuflags_slowctz (1<<18)
  684. %assign cpuflags_lzcnt (1<<19)
  685. %assign cpuflags_aligned (1<<20) ; not a cpu feature, but a function variant
  686. %assign cpuflags_atom (1<<21)
  687. %assign cpuflags_bmi1 (1<<22)|cpuflags_lzcnt
  688. %assign cpuflags_bmi2 (1<<23)|cpuflags_bmi1
  689. %define cpuflag(x) ((cpuflags & (cpuflags_ %+ x)) == (cpuflags_ %+ x))
  690. %define notcpuflag(x) ((cpuflags & (cpuflags_ %+ x)) != (cpuflags_ %+ x))
  691. ; Takes an arbitrary number of cpuflags from the above list.
  692. ; All subsequent functions (up to the next INIT_CPUFLAGS) is built for the specified cpu.
  693. ; You shouldn't need to invoke this macro directly, it's a subroutine for INIT_MMX &co.
  694. %macro INIT_CPUFLAGS 0-*
  695. %xdefine SUFFIX
  696. %undef cpuname
  697. %assign cpuflags 0
  698. %if %0 >= 1
  699. %rep %0
  700. %ifdef cpuname
  701. %xdefine cpuname cpuname %+ _%1
  702. %else
  703. %xdefine cpuname %1
  704. %endif
  705. %assign cpuflags cpuflags | cpuflags_%1
  706. %rotate 1
  707. %endrep
  708. %xdefine SUFFIX _ %+ cpuname
  709. %if cpuflag(avx)
  710. %assign avx_enabled 1
  711. %endif
  712. %if (mmsize == 16 && notcpuflag(sse2)) || (mmsize == 32 && notcpuflag(avx2))
  713. %define mova movaps
  714. %define movu movups
  715. %define movnta movntps
  716. %endif
  717. %if cpuflag(aligned)
  718. %define movu mova
  719. %elif cpuflag(sse3) && notcpuflag(ssse3)
  720. %define movu lddqu
  721. %endif
  722. %endif
  723. %if ARCH_X86_64 || cpuflag(sse2)
  724. CPUNOP amdnop
  725. %else
  726. CPUNOP basicnop
  727. %endif
  728. %endmacro
  729. ; Merge mmx and sse*
  730. ; m# is a simd register of the currently selected size
  731. ; xm# is the corresponding xmm register if mmsize >= 16, otherwise the same as m#
  732. ; ym# is the corresponding ymm register if mmsize >= 32, otherwise the same as m#
  733. ; (All 3 remain in sync through SWAP.)
  734. %macro CAT_XDEFINE 3
  735. %xdefine %1%2 %3
  736. %endmacro
  737. %macro CAT_UNDEF 2
  738. %undef %1%2
  739. %endmacro
  740. %macro INIT_MMX 0-1+
  741. %assign avx_enabled 0
  742. %define RESET_MM_PERMUTATION INIT_MMX %1
  743. %define mmsize 8
  744. %define num_mmregs 8
  745. %define mova movq
  746. %define movu movq
  747. %define movh movd
  748. %define movnta movntq
  749. %assign %%i 0
  750. %rep 8
  751. CAT_XDEFINE m, %%i, mm %+ %%i
  752. CAT_XDEFINE nnmm, %%i, %%i
  753. %assign %%i %%i+1
  754. %endrep
  755. %rep 8
  756. CAT_UNDEF m, %%i
  757. CAT_UNDEF nnmm, %%i
  758. %assign %%i %%i+1
  759. %endrep
  760. INIT_CPUFLAGS %1
  761. %endmacro
  762. %macro INIT_XMM 0-1+
  763. %assign avx_enabled 0
  764. %define RESET_MM_PERMUTATION INIT_XMM %1
  765. %define mmsize 16
  766. %define num_mmregs 8
  767. %if ARCH_X86_64
  768. %define num_mmregs 16
  769. %endif
  770. %define mova movdqa
  771. %define movu movdqu
  772. %define movh movq
  773. %define movnta movntdq
  774. %assign %%i 0
  775. %rep num_mmregs
  776. CAT_XDEFINE m, %%i, xmm %+ %%i
  777. CAT_XDEFINE nnxmm, %%i, %%i
  778. %assign %%i %%i+1
  779. %endrep
  780. INIT_CPUFLAGS %1
  781. %endmacro
  782. %macro INIT_YMM 0-1+
  783. %assign avx_enabled 1
  784. %define RESET_MM_PERMUTATION INIT_YMM %1
  785. %define mmsize 32
  786. %define num_mmregs 8
  787. %if ARCH_X86_64
  788. %define num_mmregs 16
  789. %endif
  790. %define mova movdqa
  791. %define movu movdqu
  792. %undef movh
  793. %define movnta movntdq
  794. %assign %%i 0
  795. %rep num_mmregs
  796. CAT_XDEFINE m, %%i, ymm %+ %%i
  797. CAT_XDEFINE nnymm, %%i, %%i
  798. %assign %%i %%i+1
  799. %endrep
  800. INIT_CPUFLAGS %1
  801. %endmacro
  802. INIT_XMM
  803. %macro DECLARE_MMCAST 1
  804. %define mmmm%1 mm%1
  805. %define mmxmm%1 mm%1
  806. %define mmymm%1 mm%1
  807. %define xmmmm%1 mm%1
  808. %define xmmxmm%1 xmm%1
  809. %define xmmymm%1 xmm%1
  810. %define ymmmm%1 mm%1
  811. %define ymmxmm%1 xmm%1
  812. %define ymmymm%1 ymm%1
  813. %define xm%1 xmm %+ m%1
  814. %define ym%1 ymm %+ m%1
  815. %endmacro
  816. %assign i 0
  817. %rep 16
  818. DECLARE_MMCAST i
  819. %assign i i+1
  820. %endrep
  821. ; I often want to use macros that permute their arguments. e.g. there's no
  822. ; efficient way to implement butterfly or transpose or dct without swapping some
  823. ; arguments.
  824. ;
  825. ; I would like to not have to manually keep track of the permutations:
  826. ; If I insert a permutation in the middle of a function, it should automatically
  827. ; change everything that follows. For more complex macros I may also have multiple
  828. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  829. ;
  830. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  831. ; permutes its arguments. It's equivalent to exchanging the contents of the
  832. ; registers, except that this way you exchange the register names instead, so it
  833. ; doesn't cost any cycles.
  834. %macro PERMUTE 2-* ; takes a list of pairs to swap
  835. %rep %0/2
  836. %xdefine %%tmp%2 m%2
  837. %rotate 2
  838. %endrep
  839. %rep %0/2
  840. %xdefine m%1 %%tmp%2
  841. CAT_XDEFINE nn, m%1, %1
  842. %rotate 2
  843. %endrep
  844. %endmacro
  845. %macro SWAP 2+ ; swaps a single chain (sometimes more concise than pairs)
  846. %ifnum %1 ; SWAP 0, 1, ...
  847. SWAP_INTERNAL_NUM %1, %2
  848. %else ; SWAP m0, m1, ...
  849. SWAP_INTERNAL_NAME %1, %2
  850. %endif
  851. %endmacro
  852. %macro SWAP_INTERNAL_NUM 2-*
  853. %rep %0-1
  854. %xdefine %%tmp m%1
  855. %xdefine m%1 m%2
  856. %xdefine m%2 %%tmp
  857. CAT_XDEFINE nn, m%1, %1
  858. CAT_XDEFINE nn, m%2, %2
  859. %rotate 1
  860. %endrep
  861. %endmacro
  862. %macro SWAP_INTERNAL_NAME 2-*
  863. %xdefine %%args nn %+ %1
  864. %rep %0-1
  865. %xdefine %%args %%args, nn %+ %2
  866. %rotate 1
  867. %endrep
  868. SWAP_INTERNAL_NUM %%args
  869. %endmacro
  870. ; If SAVE_MM_PERMUTATION is placed at the end of a function, then any later
  871. ; calls to that function will automatically load the permutation, so values can
  872. ; be returned in mmregs.
  873. %macro SAVE_MM_PERMUTATION 0-1
  874. %if %0
  875. %xdefine %%f %1_m
  876. %else
  877. %xdefine %%f current_function %+ _m
  878. %endif
  879. %assign %%i 0
  880. %rep num_mmregs
  881. CAT_XDEFINE %%f, %%i, m %+ %%i
  882. %assign %%i %%i+1
  883. %endrep
  884. %endmacro
  885. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  886. %ifdef %1_m0
  887. %assign %%i 0
  888. %rep num_mmregs
  889. CAT_XDEFINE m, %%i, %1_m %+ %%i
  890. CAT_XDEFINE nn, m %+ %%i, %%i
  891. %assign %%i %%i+1
  892. %endrep
  893. %endif
  894. %endmacro
  895. ; Append cpuflags to the callee's name iff the appended name is known and the plain name isn't
  896. %macro call 1
  897. call_internal %1 %+ SUFFIX, %1
  898. %endmacro
  899. %macro call_internal 2
  900. %xdefine %%i %2
  901. %ifndef cglobaled_%2
  902. %ifdef cglobaled_%1
  903. %xdefine %%i %1
  904. %endif
  905. %endif
  906. call %%i
  907. LOAD_MM_PERMUTATION %%i
  908. %endmacro
  909. ; Substitutions that reduce instruction size but are functionally equivalent
  910. %macro add 2
  911. %ifnum %2
  912. %if %2==128
  913. sub %1, -128
  914. %else
  915. add %1, %2
  916. %endif
  917. %else
  918. add %1, %2
  919. %endif
  920. %endmacro
  921. %macro sub 2
  922. %ifnum %2
  923. %if %2==128
  924. add %1, -128
  925. %else
  926. sub %1, %2
  927. %endif
  928. %else
  929. sub %1, %2
  930. %endif
  931. %endmacro
  932. ;=============================================================================
  933. ; AVX abstraction layer
  934. ;=============================================================================
  935. %assign i 0
  936. %rep 16
  937. %if i < 8
  938. CAT_XDEFINE sizeofmm, i, 8
  939. %endif
  940. CAT_XDEFINE sizeofxmm, i, 16
  941. CAT_XDEFINE sizeofymm, i, 32
  942. %assign i i+1
  943. %endrep
  944. %undef i
  945. %macro CHECK_AVX_INSTR_EMU 3-*
  946. %xdefine %%opcode %1
  947. %xdefine %%dst %2
  948. %rep %0-2
  949. %ifidn %%dst, %3
  950. %error non-avx emulation of ``%%opcode'' is not supported
  951. %endif
  952. %rotate 1
  953. %endrep
  954. %endmacro
  955. ;%1 == instruction
  956. ;%2 == minimal instruction set
  957. ;%3 == 1 if float, 0 if int
  958. ;%4 == 1 if non-destructive or 4-operand (xmm, xmm, xmm, imm), 0 otherwise
  959. ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
  960. ;%6+: operands
  961. %macro RUN_AVX_INSTR 6-9+
  962. %ifnum sizeof%7
  963. %assign __sizeofreg sizeof%7
  964. %elifnum sizeof%6
  965. %assign __sizeofreg sizeof%6
  966. %else
  967. %assign __sizeofreg mmsize
  968. %endif
  969. %assign __emulate_avx 0
  970. %if avx_enabled && __sizeofreg >= 16
  971. %xdefine __instr v%1
  972. %else
  973. %xdefine __instr %1
  974. %if %0 >= 8+%4
  975. %assign __emulate_avx 1
  976. %endif
  977. %endif
  978. %ifnidn %2, fnord
  979. %ifdef cpuname
  980. %if notcpuflag(%2)
  981. %error use of ``%1'' %2 instruction in cpuname function: current_function
  982. %elif cpuflags_%2 < cpuflags_sse && notcpuflag(sse2) && __sizeofreg > 8
  983. %error use of ``%1'' sse2 instruction in cpuname function: current_function
  984. %endif
  985. %endif
  986. %endif
  987. %if __emulate_avx
  988. %xdefine __src1 %7
  989. %xdefine __src2 %8
  990. %ifnidn %6, %7
  991. %if %0 >= 9
  992. CHECK_AVX_INSTR_EMU {%1 %6, %7, %8, %9}, %6, %8, %9
  993. %else
  994. CHECK_AVX_INSTR_EMU {%1 %6, %7, %8}, %6, %8
  995. %endif
  996. %if %5 && %4 == 0
  997. %ifnid %8
  998. ; 3-operand AVX instructions with a memory arg can only have it in src2,
  999. ; whereas SSE emulation prefers to have it in src1 (i.e. the mov).
  1000. ; So, if the instruction is commutative with a memory arg, swap them.
  1001. %xdefine __src1 %8
  1002. %xdefine __src2 %7
  1003. %endif
  1004. %endif
  1005. %if __sizeofreg == 8
  1006. MOVQ %6, __src1
  1007. %elif %3
  1008. MOVAPS %6, __src1
  1009. %else
  1010. MOVDQA %6, __src1
  1011. %endif
  1012. %endif
  1013. %if %0 >= 9
  1014. %1 %6, __src2, %9
  1015. %else
  1016. %1 %6, __src2
  1017. %endif
  1018. %elif %0 >= 9
  1019. __instr %6, %7, %8, %9
  1020. %elif %0 == 8
  1021. __instr %6, %7, %8
  1022. %elif %0 == 7
  1023. __instr %6, %7
  1024. %else
  1025. __instr %6
  1026. %endif
  1027. %endmacro
  1028. ;%1 == instruction
  1029. ;%2 == minimal instruction set
  1030. ;%3 == 1 if float, 0 if int
  1031. ;%4 == 1 if non-destructive or 4-operand (xmm, xmm, xmm, imm), 0 otherwise
  1032. ;%5 == 1 if commutative (i.e. doesn't matter which src arg is which), 0 if not
  1033. %macro AVX_INSTR 1-5 fnord, 0, 1, 0
  1034. %macro %1 1-10 fnord, fnord, fnord, fnord, %1, %2, %3, %4, %5
  1035. %ifidn %2, fnord
  1036. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1
  1037. %elifidn %3, fnord
  1038. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2
  1039. %elifidn %4, fnord
  1040. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3
  1041. %elifidn %5, fnord
  1042. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4
  1043. %else
  1044. RUN_AVX_INSTR %6, %7, %8, %9, %10, %1, %2, %3, %4, %5
  1045. %endif
  1046. %endmacro
  1047. %endmacro
  1048. ; Instructions with both VEX and non-VEX encodings
  1049. ; Non-destructive instructions are written without parameters
  1050. AVX_INSTR addpd, sse2, 1, 0, 1
  1051. AVX_INSTR addps, sse, 1, 0, 1
  1052. AVX_INSTR addsd, sse2, 1, 0, 1
  1053. AVX_INSTR addss, sse, 1, 0, 1
  1054. AVX_INSTR addsubpd, sse3, 1, 0, 0
  1055. AVX_INSTR addsubps, sse3, 1, 0, 0
  1056. AVX_INSTR aesdec, fnord, 0, 0, 0
  1057. AVX_INSTR aesdeclast, fnord, 0, 0, 0
  1058. AVX_INSTR aesenc, fnord, 0, 0, 0
  1059. AVX_INSTR aesenclast, fnord, 0, 0, 0
  1060. AVX_INSTR aesimc
  1061. AVX_INSTR aeskeygenassist
  1062. AVX_INSTR andnpd, sse2, 1, 0, 0
  1063. AVX_INSTR andnps, sse, 1, 0, 0
  1064. AVX_INSTR andpd, sse2, 1, 0, 1
  1065. AVX_INSTR andps, sse, 1, 0, 1
  1066. AVX_INSTR blendpd, sse4, 1, 0, 0
  1067. AVX_INSTR blendps, sse4, 1, 0, 0
  1068. AVX_INSTR blendvpd, sse4, 1, 0, 0
  1069. AVX_INSTR blendvps, sse4, 1, 0, 0
  1070. AVX_INSTR cmppd, sse2, 1, 1, 0
  1071. AVX_INSTR cmpps, sse, 1, 1, 0
  1072. AVX_INSTR cmpsd, sse2, 1, 1, 0
  1073. AVX_INSTR cmpss, sse, 1, 1, 0
  1074. AVX_INSTR comisd, sse2
  1075. AVX_INSTR comiss, sse
  1076. AVX_INSTR cvtdq2pd, sse2
  1077. AVX_INSTR cvtdq2ps, sse2
  1078. AVX_INSTR cvtpd2dq, sse2
  1079. AVX_INSTR cvtpd2ps, sse2
  1080. AVX_INSTR cvtps2dq, sse2
  1081. AVX_INSTR cvtps2pd, sse2
  1082. AVX_INSTR cvtsd2si, sse2
  1083. AVX_INSTR cvtsd2ss, sse2
  1084. AVX_INSTR cvtsi2sd, sse2
  1085. AVX_INSTR cvtsi2ss, sse
  1086. AVX_INSTR cvtss2sd, sse2
  1087. AVX_INSTR cvtss2si, sse
  1088. AVX_INSTR cvttpd2dq, sse2
  1089. AVX_INSTR cvttps2dq, sse2
  1090. AVX_INSTR cvttsd2si, sse2
  1091. AVX_INSTR cvttss2si, sse
  1092. AVX_INSTR divpd, sse2, 1, 0, 0
  1093. AVX_INSTR divps, sse, 1, 0, 0
  1094. AVX_INSTR divsd, sse2, 1, 0, 0
  1095. AVX_INSTR divss, sse, 1, 0, 0
  1096. AVX_INSTR dppd, sse4, 1, 1, 0
  1097. AVX_INSTR dpps, sse4, 1, 1, 0
  1098. AVX_INSTR extractps, sse4
  1099. AVX_INSTR haddpd, sse3, 1, 0, 0
  1100. AVX_INSTR haddps, sse3, 1, 0, 0
  1101. AVX_INSTR hsubpd, sse3, 1, 0, 0
  1102. AVX_INSTR hsubps, sse3, 1, 0, 0
  1103. AVX_INSTR insertps, sse4, 1, 1, 0
  1104. AVX_INSTR lddqu, sse3
  1105. AVX_INSTR ldmxcsr, sse
  1106. AVX_INSTR maskmovdqu, sse2
  1107. AVX_INSTR maxpd, sse2, 1, 0, 1
  1108. AVX_INSTR maxps, sse, 1, 0, 1
  1109. AVX_INSTR maxsd, sse2, 1, 0, 1
  1110. AVX_INSTR maxss, sse, 1, 0, 1
  1111. AVX_INSTR minpd, sse2, 1, 0, 1
  1112. AVX_INSTR minps, sse, 1, 0, 1
  1113. AVX_INSTR minsd, sse2, 1, 0, 1
  1114. AVX_INSTR minss, sse, 1, 0, 1
  1115. AVX_INSTR movapd, sse2
  1116. AVX_INSTR movaps, sse
  1117. AVX_INSTR movd, mmx
  1118. AVX_INSTR movddup, sse3
  1119. AVX_INSTR movdqa, sse2
  1120. AVX_INSTR movdqu, sse2
  1121. AVX_INSTR movhlps, sse, 1, 0, 0
  1122. AVX_INSTR movhpd, sse2, 1, 0, 0
  1123. AVX_INSTR movhps, sse, 1, 0, 0
  1124. AVX_INSTR movlhps, sse, 1, 0, 0
  1125. AVX_INSTR movlpd, sse2, 1, 0, 0
  1126. AVX_INSTR movlps, sse, 1, 0, 0
  1127. AVX_INSTR movmskpd, sse2
  1128. AVX_INSTR movmskps, sse
  1129. AVX_INSTR movntdq, sse2
  1130. AVX_INSTR movntdqa, sse4
  1131. AVX_INSTR movntpd, sse2
  1132. AVX_INSTR movntps, sse
  1133. AVX_INSTR movq, mmx
  1134. AVX_INSTR movsd, sse2, 1, 0, 0
  1135. AVX_INSTR movshdup, sse3
  1136. AVX_INSTR movsldup, sse3
  1137. AVX_INSTR movss, sse, 1, 0, 0
  1138. AVX_INSTR movupd, sse2
  1139. AVX_INSTR movups, sse
  1140. AVX_INSTR mpsadbw, sse4
  1141. AVX_INSTR mulpd, sse2, 1, 0, 1
  1142. AVX_INSTR mulps, sse, 1, 0, 1
  1143. AVX_INSTR mulsd, sse2, 1, 0, 1
  1144. AVX_INSTR mulss, sse, 1, 0, 1
  1145. AVX_INSTR orpd, sse2, 1, 0, 1
  1146. AVX_INSTR orps, sse, 1, 0, 1
  1147. AVX_INSTR pabsb, ssse3
  1148. AVX_INSTR pabsd, ssse3
  1149. AVX_INSTR pabsw, ssse3
  1150. AVX_INSTR packsswb, mmx, 0, 0, 0
  1151. AVX_INSTR packssdw, mmx, 0, 0, 0
  1152. AVX_INSTR packuswb, mmx, 0, 0, 0
  1153. AVX_INSTR packusdw, sse4, 0, 0, 0
  1154. AVX_INSTR paddb, mmx, 0, 0, 1
  1155. AVX_INSTR paddw, mmx, 0, 0, 1
  1156. AVX_INSTR paddd, mmx, 0, 0, 1
  1157. AVX_INSTR paddq, sse2, 0, 0, 1
  1158. AVX_INSTR paddsb, mmx, 0, 0, 1
  1159. AVX_INSTR paddsw, mmx, 0, 0, 1
  1160. AVX_INSTR paddusb, mmx, 0, 0, 1
  1161. AVX_INSTR paddusw, mmx, 0, 0, 1
  1162. AVX_INSTR palignr, ssse3
  1163. AVX_INSTR pand, mmx, 0, 0, 1
  1164. AVX_INSTR pandn, mmx, 0, 0, 0
  1165. AVX_INSTR pavgb, mmx2, 0, 0, 1
  1166. AVX_INSTR pavgw, mmx2, 0, 0, 1
  1167. AVX_INSTR pblendvb, sse4, 0, 0, 0
  1168. AVX_INSTR pblendw, sse4
  1169. AVX_INSTR pclmulqdq
  1170. AVX_INSTR pcmpestri, sse42
  1171. AVX_INSTR pcmpestrm, sse42
  1172. AVX_INSTR pcmpistri, sse42
  1173. AVX_INSTR pcmpistrm, sse42
  1174. AVX_INSTR pcmpeqb, mmx, 0, 0, 1
  1175. AVX_INSTR pcmpeqw, mmx, 0, 0, 1
  1176. AVX_INSTR pcmpeqd, mmx, 0, 0, 1
  1177. AVX_INSTR pcmpeqq, sse4, 0, 0, 1
  1178. AVX_INSTR pcmpgtb, mmx, 0, 0, 0
  1179. AVX_INSTR pcmpgtw, mmx, 0, 0, 0
  1180. AVX_INSTR pcmpgtd, mmx, 0, 0, 0
  1181. AVX_INSTR pcmpgtq, sse42, 0, 0, 0
  1182. AVX_INSTR pextrb, sse4
  1183. AVX_INSTR pextrd, sse4
  1184. AVX_INSTR pextrq, sse4
  1185. AVX_INSTR pextrw, mmx2
  1186. AVX_INSTR phaddw, ssse3, 0, 0, 0
  1187. AVX_INSTR phaddd, ssse3, 0, 0, 0
  1188. AVX_INSTR phaddsw, ssse3, 0, 0, 0
  1189. AVX_INSTR phminposuw, sse4
  1190. AVX_INSTR phsubw, ssse3, 0, 0, 0
  1191. AVX_INSTR phsubd, ssse3, 0, 0, 0
  1192. AVX_INSTR phsubsw, ssse3, 0, 0, 0
  1193. AVX_INSTR pinsrb, sse4
  1194. AVX_INSTR pinsrd, sse4
  1195. AVX_INSTR pinsrq, sse4
  1196. AVX_INSTR pinsrw, mmx2
  1197. AVX_INSTR pmaddwd, mmx, 0, 0, 1
  1198. AVX_INSTR pmaddubsw, ssse3, 0, 0, 0
  1199. AVX_INSTR pmaxsb, sse4, 0, 0, 1
  1200. AVX_INSTR pmaxsw, mmx2, 0, 0, 1
  1201. AVX_INSTR pmaxsd, sse4, 0, 0, 1
  1202. AVX_INSTR pmaxub, mmx2, 0, 0, 1
  1203. AVX_INSTR pmaxuw, sse4, 0, 0, 1
  1204. AVX_INSTR pmaxud, sse4, 0, 0, 1
  1205. AVX_INSTR pminsb, sse4, 0, 0, 1
  1206. AVX_INSTR pminsw, mmx2, 0, 0, 1
  1207. AVX_INSTR pminsd, sse4, 0, 0, 1
  1208. AVX_INSTR pminub, mmx2, 0, 0, 1
  1209. AVX_INSTR pminuw, sse4, 0, 0, 1
  1210. AVX_INSTR pminud, sse4, 0, 0, 1
  1211. AVX_INSTR pmovmskb, mmx2
  1212. AVX_INSTR pmovsxbw, sse4
  1213. AVX_INSTR pmovsxbd, sse4
  1214. AVX_INSTR pmovsxbq, sse4
  1215. AVX_INSTR pmovsxwd, sse4
  1216. AVX_INSTR pmovsxwq, sse4
  1217. AVX_INSTR pmovsxdq, sse4
  1218. AVX_INSTR pmovzxbw, sse4
  1219. AVX_INSTR pmovzxbd, sse4
  1220. AVX_INSTR pmovzxbq, sse4
  1221. AVX_INSTR pmovzxwd, sse4
  1222. AVX_INSTR pmovzxwq, sse4
  1223. AVX_INSTR pmovzxdq, sse4
  1224. AVX_INSTR pmuldq, sse4, 0, 0, 1
  1225. AVX_INSTR pmulhrsw, ssse3, 0, 0, 1
  1226. AVX_INSTR pmulhuw, mmx2, 0, 0, 1
  1227. AVX_INSTR pmulhw, mmx, 0, 0, 1
  1228. AVX_INSTR pmullw, mmx, 0, 0, 1
  1229. AVX_INSTR pmulld, sse4, 0, 0, 1
  1230. AVX_INSTR pmuludq, sse2, 0, 0, 1
  1231. AVX_INSTR por, mmx, 0, 0, 1
  1232. AVX_INSTR psadbw, mmx2, 0, 0, 1
  1233. AVX_INSTR pshufb, ssse3, 0, 0, 0
  1234. AVX_INSTR pshufd, sse2
  1235. AVX_INSTR pshufhw, sse2
  1236. AVX_INSTR pshuflw, sse2
  1237. AVX_INSTR psignb, ssse3, 0, 0, 0
  1238. AVX_INSTR psignw, ssse3, 0, 0, 0
  1239. AVX_INSTR psignd, ssse3, 0, 0, 0
  1240. AVX_INSTR psllw, mmx, 0, 0, 0
  1241. AVX_INSTR pslld, mmx, 0, 0, 0
  1242. AVX_INSTR psllq, mmx, 0, 0, 0
  1243. AVX_INSTR pslldq, sse2, 0, 0, 0
  1244. AVX_INSTR psraw, mmx, 0, 0, 0
  1245. AVX_INSTR psrad, mmx, 0, 0, 0
  1246. AVX_INSTR psrlw, mmx, 0, 0, 0
  1247. AVX_INSTR psrld, mmx, 0, 0, 0
  1248. AVX_INSTR psrlq, mmx, 0, 0, 0
  1249. AVX_INSTR psrldq, sse2, 0, 0, 0
  1250. AVX_INSTR psubb, mmx, 0, 0, 0
  1251. AVX_INSTR psubw, mmx, 0, 0, 0
  1252. AVX_INSTR psubd, mmx, 0, 0, 0
  1253. AVX_INSTR psubq, sse2, 0, 0, 0
  1254. AVX_INSTR psubsb, mmx, 0, 0, 0
  1255. AVX_INSTR psubsw, mmx, 0, 0, 0
  1256. AVX_INSTR psubusb, mmx, 0, 0, 0
  1257. AVX_INSTR psubusw, mmx, 0, 0, 0
  1258. AVX_INSTR ptest, sse4
  1259. AVX_INSTR punpckhbw, mmx, 0, 0, 0
  1260. AVX_INSTR punpckhwd, mmx, 0, 0, 0
  1261. AVX_INSTR punpckhdq, mmx, 0, 0, 0
  1262. AVX_INSTR punpckhqdq, sse2, 0, 0, 0
  1263. AVX_INSTR punpcklbw, mmx, 0, 0, 0
  1264. AVX_INSTR punpcklwd, mmx, 0, 0, 0
  1265. AVX_INSTR punpckldq, mmx, 0, 0, 0
  1266. AVX_INSTR punpcklqdq, sse2, 0, 0, 0
  1267. AVX_INSTR pxor, mmx, 0, 0, 1
  1268. AVX_INSTR rcpps, sse, 1, 0, 0
  1269. AVX_INSTR rcpss, sse, 1, 0, 0
  1270. AVX_INSTR roundpd, sse4
  1271. AVX_INSTR roundps, sse4
  1272. AVX_INSTR roundsd, sse4
  1273. AVX_INSTR roundss, sse4
  1274. AVX_INSTR rsqrtps, sse, 1, 0, 0
  1275. AVX_INSTR rsqrtss, sse, 1, 0, 0
  1276. AVX_INSTR shufpd, sse2, 1, 1, 0
  1277. AVX_INSTR shufps, sse, 1, 1, 0
  1278. AVX_INSTR sqrtpd, sse2, 1, 0, 0
  1279. AVX_INSTR sqrtps, sse, 1, 0, 0
  1280. AVX_INSTR sqrtsd, sse2, 1, 0, 0
  1281. AVX_INSTR sqrtss, sse, 1, 0, 0
  1282. AVX_INSTR stmxcsr, sse
  1283. AVX_INSTR subpd, sse2, 1, 0, 0
  1284. AVX_INSTR subps, sse, 1, 0, 0
  1285. AVX_INSTR subsd, sse2, 1, 0, 0
  1286. AVX_INSTR subss, sse, 1, 0, 0
  1287. AVX_INSTR ucomisd, sse2
  1288. AVX_INSTR ucomiss, sse
  1289. AVX_INSTR unpckhpd, sse2, 1, 0, 0
  1290. AVX_INSTR unpckhps, sse, 1, 0, 0
  1291. AVX_INSTR unpcklpd, sse2, 1, 0, 0
  1292. AVX_INSTR unpcklps, sse, 1, 0, 0
  1293. AVX_INSTR xorpd, sse2, 1, 0, 1
  1294. AVX_INSTR xorps, sse, 1, 0, 1
  1295. ; 3DNow instructions, for sharing code between AVX, SSE and 3DN
  1296. AVX_INSTR pfadd, 3dnow, 1, 0, 1
  1297. AVX_INSTR pfsub, 3dnow, 1, 0, 0
  1298. AVX_INSTR pfmul, 3dnow, 1, 0, 1
  1299. ; base-4 constants for shuffles
  1300. %assign i 0
  1301. %rep 256
  1302. %assign j ((i>>6)&3)*1000 + ((i>>4)&3)*100 + ((i>>2)&3)*10 + (i&3)
  1303. %if j < 10
  1304. CAT_XDEFINE q000, j, i
  1305. %elif j < 100
  1306. CAT_XDEFINE q00, j, i
  1307. %elif j < 1000
  1308. CAT_XDEFINE q0, j, i
  1309. %else
  1310. CAT_XDEFINE q, j, i
  1311. %endif
  1312. %assign i i+1
  1313. %endrep
  1314. %undef i
  1315. %undef j
  1316. %macro FMA_INSTR 3
  1317. %macro %1 4-7 %1, %2, %3
  1318. %if cpuflag(xop)
  1319. v%5 %1, %2, %3, %4
  1320. %else
  1321. %6 %1, %2, %3
  1322. %7 %1, %4
  1323. %endif
  1324. %endmacro
  1325. %endmacro
  1326. FMA_INSTR pmacsww, pmullw, paddw
  1327. FMA_INSTR pmacsdd, pmulld, paddd ; sse4 emulation
  1328. FMA_INSTR pmacsdql, pmuldq, paddq ; sse4 emulation
  1329. FMA_INSTR pmadcswd, pmaddwd, paddd
  1330. ; tzcnt is equivalent to "rep bsf" and is backwards-compatible with bsf.
  1331. ; This lets us use tzcnt without bumping the yasm version requirement yet.
  1332. %define tzcnt rep bsf
  1333. ; convert FMA4 to FMA3 if possible
  1334. %macro FMA4_INSTR 4
  1335. %macro %1 4-8 %1, %2, %3, %4
  1336. %if cpuflag(fma4)
  1337. v%5 %1, %2, %3, %4
  1338. %elifidn %1, %2
  1339. v%6 %1, %4, %3 ; %1 = %1 * %3 + %4
  1340. %elifidn %1, %3
  1341. v%7 %1, %2, %4 ; %1 = %2 * %1 + %4
  1342. %elifidn %1, %4
  1343. v%8 %1, %2, %3 ; %1 = %2 * %3 + %1
  1344. %else
  1345. %error fma3 emulation of ``%5 %1, %2, %3, %4'' is not supported
  1346. %endif
  1347. %endmacro
  1348. %endmacro
  1349. FMA4_INSTR fmaddpd, fmadd132pd, fmadd213pd, fmadd231pd
  1350. FMA4_INSTR fmaddps, fmadd132ps, fmadd213ps, fmadd231ps
  1351. FMA4_INSTR fmaddsd, fmadd132sd, fmadd213sd, fmadd231sd
  1352. FMA4_INSTR fmaddss, fmadd132ss, fmadd213ss, fmadd231ss
  1353. FMA4_INSTR fmaddsubpd, fmaddsub132pd, fmaddsub213pd, fmaddsub231pd
  1354. FMA4_INSTR fmaddsubps, fmaddsub132ps, fmaddsub213ps, fmaddsub231ps
  1355. FMA4_INSTR fmsubaddpd, fmsubadd132pd, fmsubadd213pd, fmsubadd231pd
  1356. FMA4_INSTR fmsubaddps, fmsubadd132ps, fmsubadd213ps, fmsubadd231ps
  1357. FMA4_INSTR fmsubpd, fmsub132pd, fmsub213pd, fmsub231pd
  1358. FMA4_INSTR fmsubps, fmsub132ps, fmsub213ps, fmsub231ps
  1359. FMA4_INSTR fmsubsd, fmsub132sd, fmsub213sd, fmsub231sd
  1360. FMA4_INSTR fmsubss, fmsub132ss, fmsub213ss, fmsub231ss
  1361. FMA4_INSTR fnmaddpd, fnmadd132pd, fnmadd213pd, fnmadd231pd
  1362. FMA4_INSTR fnmaddps, fnmadd132ps, fnmadd213ps, fnmadd231ps
  1363. FMA4_INSTR fnmaddsd, fnmadd132sd, fnmadd213sd, fnmadd231sd
  1364. FMA4_INSTR fnmaddss, fnmadd132ss, fnmadd213ss, fnmadd231ss
  1365. FMA4_INSTR fnmsubpd, fnmsub132pd, fnmsub213pd, fnmsub231pd
  1366. FMA4_INSTR fnmsubps, fnmsub132ps, fnmsub213ps, fnmsub231ps
  1367. FMA4_INSTR fnmsubsd, fnmsub132sd, fnmsub213sd, fnmsub231sd
  1368. FMA4_INSTR fnmsubss, fnmsub132ss, fnmsub213ss, fnmsub231ss
  1369. ; workaround: vpbroadcastq is broken in x86_32 due to a yasm bug (fixed in 1.3.0)
  1370. %ifdef __YASM_VER__
  1371. %if __YASM_VERSION_ID__ < 0x01030000 && ARCH_X86_64 == 0
  1372. %macro vpbroadcastq 2
  1373. %if sizeof%1 == 16
  1374. movddup %1, %2
  1375. %else
  1376. vbroadcastsd %1, %2
  1377. %endif
  1378. %endmacro
  1379. %endif
  1380. %endif