x86inc.asm 46 KB

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