x86inc.asm 34 KB

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