You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

648 lines
16KB

  1. ;*****************************************************************************
  2. ;* x86inc.asm
  3. ;*****************************************************************************
  4. ;* Copyright (C) 2005-2008 x264 project
  5. ;*
  6. ;* Authors: Loren Merritt <lorenm@u.washington.edu>
  7. ;* Anton Mitrofanov <BugMaster@narod.ru>
  8. ;*
  9. ;* Permission to use, copy, modify, and/or distribute this software for any
  10. ;* purpose with or without fee is hereby granted, provided that the above
  11. ;* copyright notice and this permission notice appear in all copies.
  12. ;*
  13. ;* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  14. ;* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  16. ;* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. ;* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18. ;* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  19. ;* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20. ;*****************************************************************************
  21. ; This is a header file for the x264ASM assembly language, which uses
  22. ; NASM/YASM syntax combined with a large number of macros to provide easy
  23. ; abstraction between different calling conventions (x86_32, win64, linux64).
  24. ; It also has various other useful features to simplify writing the kind of
  25. ; DSP functions that are most often used in x264.
  26. ; Unlike the rest of x264, this file is available under an ISC license, as it
  27. ; has significant usefulness outside of x264 and we want it to be available
  28. ; to the largest audience possible. Of course, if you modify it for your own
  29. ; purposes to add a new feature, we strongly encourage contributing a patch
  30. ; as this feature might be useful for others as well. Send patches or ideas
  31. ; to x264-devel@videolan.org .
  32. %define program_name ff
  33. %ifdef ARCH_X86_64
  34. %ifidn __OUTPUT_FORMAT__,win32
  35. %define WIN64
  36. %else
  37. %define UNIX64
  38. %endif
  39. %endif
  40. %ifdef PREFIX
  41. %define mangle(x) _ %+ x
  42. %else
  43. %define mangle(x) x
  44. %endif
  45. ; FIXME: All of the 64bit asm functions that take a stride as an argument
  46. ; via register, assume that the high dword of that register is filled with 0.
  47. ; This is true in practice (since we never do any 64bit arithmetic on strides,
  48. ; and x264's strides are all positive), but is not guaranteed by the ABI.
  49. ; Name of the .rodata section.
  50. ; Kludge: Something on OS X fails to align .rodata even given an align attribute,
  51. ; so use a different read-only section.
  52. %macro SECTION_RODATA 0-1 16
  53. %ifidn __OUTPUT_FORMAT__,macho64
  54. SECTION .text align=%1
  55. %elifidn __OUTPUT_FORMAT__,macho
  56. SECTION .text align=%1
  57. fakegot:
  58. %else
  59. SECTION .rodata align=%1
  60. %endif
  61. %endmacro
  62. %ifdef WIN64
  63. %define PIC
  64. %elifndef ARCH_X86_64
  65. ; x86_32 doesn't require PIC.
  66. ; Some distros prefer shared objects to be PIC, but nothing breaks if
  67. ; the code contains a few textrels, so we'll skip that complexity.
  68. %undef PIC
  69. %endif
  70. %ifdef PIC
  71. default rel
  72. %endif
  73. ; Macros to eliminate most code duplication between x86_32 and x86_64:
  74. ; Currently this works only for leaf functions which load all their arguments
  75. ; into registers at the start, and make no other use of the stack. Luckily that
  76. ; covers most of x264's asm.
  77. ; PROLOGUE:
  78. ; %1 = number of arguments. loads them from stack if needed.
  79. ; %2 = number of registers used. pushes callee-saved regs if needed.
  80. ; %3 = number of xmm registers used. pushes callee-saved xmm regs if needed.
  81. ; %4 = list of names to define to registers
  82. ; PROLOGUE can also be invoked by adding the same options to cglobal
  83. ; e.g.
  84. ; cglobal foo, 2,3,0, dst, src, tmp
  85. ; declares a function (foo), taking two args (dst and src) and one local variable (tmp)
  86. ; TODO Some functions can use some args directly from the stack. If they're the
  87. ; last args then you can just not declare them, but if they're in the middle
  88. ; we need more flexible macro.
  89. ; RET:
  90. ; Pops anything that was pushed by PROLOGUE
  91. ; REP_RET:
  92. ; Same, but if it doesn't pop anything it becomes a 2-byte ret, for athlons
  93. ; which are slow when a normal ret follows a branch.
  94. ; registers:
  95. ; rN and rNq are the native-size register holding function argument N
  96. ; rNd, rNw, rNb are dword, word, and byte size
  97. ; rNm is the original location of arg N (a register or on the stack), dword
  98. ; rNmp is native size
  99. %macro DECLARE_REG 6
  100. %define r%1q %2
  101. %define r%1d %3
  102. %define r%1w %4
  103. %define r%1b %5
  104. %define r%1m %6
  105. %ifid %6 ; i.e. it's a register
  106. %define r%1mp %2
  107. %elifdef ARCH_X86_64 ; memory
  108. %define r%1mp qword %6
  109. %else
  110. %define r%1mp dword %6
  111. %endif
  112. %define r%1 %2
  113. %endmacro
  114. %macro DECLARE_REG_SIZE 2
  115. %define r%1q r%1
  116. %define e%1q r%1
  117. %define r%1d e%1
  118. %define e%1d e%1
  119. %define r%1w %1
  120. %define e%1w %1
  121. %define r%1b %2
  122. %define e%1b %2
  123. %ifndef ARCH_X86_64
  124. %define r%1 e%1
  125. %endif
  126. %endmacro
  127. DECLARE_REG_SIZE ax, al
  128. DECLARE_REG_SIZE bx, bl
  129. DECLARE_REG_SIZE cx, cl
  130. DECLARE_REG_SIZE dx, dl
  131. DECLARE_REG_SIZE si, sil
  132. DECLARE_REG_SIZE di, dil
  133. DECLARE_REG_SIZE bp, bpl
  134. ; t# defines for when per-arch register allocation is more complex than just function arguments
  135. %macro DECLARE_REG_TMP 1-*
  136. %assign %%i 0
  137. %rep %0
  138. CAT_XDEFINE t, %%i, r%1
  139. %assign %%i %%i+1
  140. %rotate 1
  141. %endrep
  142. %endmacro
  143. %macro DECLARE_REG_TMP_SIZE 0-*
  144. %rep %0
  145. %define t%1q t%1 %+ q
  146. %define t%1d t%1 %+ d
  147. %define t%1w t%1 %+ w
  148. %define t%1b t%1 %+ b
  149. %rotate 1
  150. %endrep
  151. %endmacro
  152. DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9
  153. %ifdef ARCH_X86_64
  154. %define gprsize 8
  155. %else
  156. %define gprsize 4
  157. %endif
  158. %macro PUSH 1
  159. push %1
  160. %assign stack_offset stack_offset+gprsize
  161. %endmacro
  162. %macro POP 1
  163. pop %1
  164. %assign stack_offset stack_offset-gprsize
  165. %endmacro
  166. %macro SUB 2
  167. sub %1, %2
  168. %ifidn %1, rsp
  169. %assign stack_offset stack_offset+(%2)
  170. %endif
  171. %endmacro
  172. %macro ADD 2
  173. add %1, %2
  174. %ifidn %1, rsp
  175. %assign stack_offset stack_offset-(%2)
  176. %endif
  177. %endmacro
  178. %macro movifnidn 2
  179. %ifnidn %1, %2
  180. mov %1, %2
  181. %endif
  182. %endmacro
  183. %macro movsxdifnidn 2
  184. %ifnidn %1, %2
  185. movsxd %1, %2
  186. %endif
  187. %endmacro
  188. %macro ASSERT 1
  189. %if (%1) == 0
  190. %error assert failed
  191. %endif
  192. %endmacro
  193. %macro DEFINE_ARGS 0-*
  194. %ifdef n_arg_names
  195. %assign %%i 0
  196. %rep n_arg_names
  197. CAT_UNDEF arg_name %+ %%i, q
  198. CAT_UNDEF arg_name %+ %%i, d
  199. CAT_UNDEF arg_name %+ %%i, w
  200. CAT_UNDEF arg_name %+ %%i, b
  201. CAT_UNDEF arg_name %+ %%i, m
  202. CAT_UNDEF arg_name, %%i
  203. %assign %%i %%i+1
  204. %endrep
  205. %endif
  206. %assign %%i 0
  207. %rep %0
  208. %xdefine %1q r %+ %%i %+ q
  209. %xdefine %1d r %+ %%i %+ d
  210. %xdefine %1w r %+ %%i %+ w
  211. %xdefine %1b r %+ %%i %+ b
  212. %xdefine %1m r %+ %%i %+ m
  213. CAT_XDEFINE arg_name, %%i, %1
  214. %assign %%i %%i+1
  215. %rotate 1
  216. %endrep
  217. %assign n_arg_names %%i
  218. %endmacro
  219. %ifdef WIN64 ; Windows x64 ;=================================================
  220. DECLARE_REG 0, rcx, ecx, cx, cl, ecx
  221. DECLARE_REG 1, rdx, edx, dx, dl, edx
  222. DECLARE_REG 2, r8, r8d, r8w, r8b, r8d
  223. DECLARE_REG 3, r9, r9d, r9w, r9b, r9d
  224. DECLARE_REG 4, rdi, edi, di, dil, [rsp + stack_offset + 40]
  225. DECLARE_REG 5, rsi, esi, si, sil, [rsp + stack_offset + 48]
  226. DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 56]
  227. %define r7m [rsp + stack_offset + 64]
  228. %define r8m [rsp + stack_offset + 72]
  229. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  230. %if %1 < %2
  231. mov r%1, [rsp + stack_offset + 8 + %1*8]
  232. %endif
  233. %endmacro
  234. %macro PROLOGUE 2-4+ 0 ; #args, #regs, #xmm_regs, arg_names...
  235. ASSERT %2 >= %1
  236. %assign regs_used %2
  237. ASSERT regs_used <= 7
  238. %if regs_used > 4
  239. push r4
  240. push r5
  241. %assign stack_offset stack_offset+16
  242. %endif
  243. WIN64_SPILL_XMM %3
  244. LOAD_IF_USED 4, %1
  245. LOAD_IF_USED 5, %1
  246. LOAD_IF_USED 6, %1
  247. DEFINE_ARGS %4
  248. %endmacro
  249. %macro WIN64_SPILL_XMM 1
  250. %assign xmm_regs_used %1
  251. ASSERT xmm_regs_used <= 16
  252. %if xmm_regs_used > 6
  253. sub rsp, (xmm_regs_used-6)*16+16
  254. %assign stack_offset stack_offset+(xmm_regs_used-6)*16+16
  255. %assign %%i xmm_regs_used
  256. %rep (xmm_regs_used-6)
  257. %assign %%i %%i-1
  258. movdqa [rsp + (%%i-6)*16+8], xmm %+ %%i
  259. %endrep
  260. %endif
  261. %endmacro
  262. %macro WIN64_RESTORE_XMM_INTERNAL 1
  263. %if xmm_regs_used > 6
  264. %assign %%i xmm_regs_used
  265. %rep (xmm_regs_used-6)
  266. %assign %%i %%i-1
  267. movdqa xmm %+ %%i, [%1 + (%%i-6)*16+8]
  268. %endrep
  269. add %1, (xmm_regs_used-6)*16+16
  270. %endif
  271. %endmacro
  272. %macro WIN64_RESTORE_XMM 1
  273. WIN64_RESTORE_XMM_INTERNAL %1
  274. %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
  275. %assign xmm_regs_used 0
  276. %endmacro
  277. %macro RET 0
  278. WIN64_RESTORE_XMM_INTERNAL rsp
  279. %if regs_used > 4
  280. pop r5
  281. pop r4
  282. %endif
  283. ret
  284. %endmacro
  285. %macro REP_RET 0
  286. %if regs_used > 4 || xmm_regs_used > 6
  287. RET
  288. %else
  289. rep ret
  290. %endif
  291. %endmacro
  292. %elifdef ARCH_X86_64 ; *nix x64 ;=============================================
  293. DECLARE_REG 0, rdi, edi, di, dil, edi
  294. DECLARE_REG 1, rsi, esi, si, sil, esi
  295. DECLARE_REG 2, rdx, edx, dx, dl, edx
  296. DECLARE_REG 3, rcx, ecx, cx, cl, ecx
  297. DECLARE_REG 4, r8, r8d, r8w, r8b, r8d
  298. DECLARE_REG 5, r9, r9d, r9w, r9b, r9d
  299. DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 8]
  300. %define r7m [rsp + stack_offset + 16]
  301. %define r8m [rsp + stack_offset + 24]
  302. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  303. %if %1 < %2
  304. mov r%1, [rsp - 40 + %1*8]
  305. %endif
  306. %endmacro
  307. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  308. ASSERT %2 >= %1
  309. ASSERT %2 <= 7
  310. LOAD_IF_USED 6, %1
  311. DEFINE_ARGS %4
  312. %endmacro
  313. %macro RET 0
  314. ret
  315. %endmacro
  316. %macro REP_RET 0
  317. rep ret
  318. %endmacro
  319. %else ; X86_32 ;==============================================================
  320. DECLARE_REG 0, eax, eax, ax, al, [esp + stack_offset + 4]
  321. DECLARE_REG 1, ecx, ecx, cx, cl, [esp + stack_offset + 8]
  322. DECLARE_REG 2, edx, edx, dx, dl, [esp + stack_offset + 12]
  323. DECLARE_REG 3, ebx, ebx, bx, bl, [esp + stack_offset + 16]
  324. DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
  325. DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
  326. DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
  327. %define r7m [esp + stack_offset + 32]
  328. %define r8m [esp + stack_offset + 36]
  329. %define rsp esp
  330. %macro PUSH_IF_USED 1 ; reg_id
  331. %if %1 < regs_used
  332. push r%1
  333. %assign stack_offset stack_offset+4
  334. %endif
  335. %endmacro
  336. %macro POP_IF_USED 1 ; reg_id
  337. %if %1 < regs_used
  338. pop r%1
  339. %endif
  340. %endmacro
  341. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  342. %if %1 < %2
  343. mov r%1, [esp + stack_offset + 4 + %1*4]
  344. %endif
  345. %endmacro
  346. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  347. ASSERT %2 >= %1
  348. %assign regs_used %2
  349. ASSERT regs_used <= 7
  350. PUSH_IF_USED 3
  351. PUSH_IF_USED 4
  352. PUSH_IF_USED 5
  353. PUSH_IF_USED 6
  354. LOAD_IF_USED 0, %1
  355. LOAD_IF_USED 1, %1
  356. LOAD_IF_USED 2, %1
  357. LOAD_IF_USED 3, %1
  358. LOAD_IF_USED 4, %1
  359. LOAD_IF_USED 5, %1
  360. LOAD_IF_USED 6, %1
  361. DEFINE_ARGS %4
  362. %endmacro
  363. %macro RET 0
  364. POP_IF_USED 6
  365. POP_IF_USED 5
  366. POP_IF_USED 4
  367. POP_IF_USED 3
  368. ret
  369. %endmacro
  370. %macro REP_RET 0
  371. %if regs_used > 3
  372. RET
  373. %else
  374. rep ret
  375. %endif
  376. %endmacro
  377. %endif ;======================================================================
  378. %ifndef WIN64
  379. %macro WIN64_SPILL_XMM 1
  380. %endmacro
  381. %macro WIN64_RESTORE_XMM 1
  382. %endmacro
  383. %endif
  384. ;=============================================================================
  385. ; arch-independent part
  386. ;=============================================================================
  387. %assign function_align 16
  388. ; Symbol prefix for C linkage
  389. %macro cglobal 1-2+
  390. %xdefine %1 mangle(program_name %+ _ %+ %1)
  391. %xdefine %1.skip_prologue %1 %+ .skip_prologue
  392. %ifidn __OUTPUT_FORMAT__,elf
  393. global %1:function hidden
  394. %else
  395. global %1
  396. %endif
  397. align function_align
  398. %1:
  399. RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
  400. %assign stack_offset 0
  401. %if %0 > 1
  402. PROLOGUE %2
  403. %endif
  404. %endmacro
  405. %macro cextern 1
  406. %xdefine %1 mangle(program_name %+ _ %+ %1)
  407. extern %1
  408. %endmacro
  409. ;like cextern, but without the prefix
  410. %macro cextern_naked 1
  411. %xdefine %1 mangle(%1)
  412. extern %1
  413. %endmacro
  414. %macro const 2+
  415. %xdefine %1 mangle(program_name %+ _ %+ %1)
  416. global %1
  417. %1: %2
  418. %endmacro
  419. ; This is needed for ELF, otherwise the GNU linker assumes the stack is
  420. ; executable by default.
  421. %ifidn __OUTPUT_FORMAT__,elf
  422. SECTION .note.GNU-stack noalloc noexec nowrite progbits
  423. %endif
  424. ; merge mmx and sse*
  425. %macro CAT_XDEFINE 3
  426. %xdefine %1%2 %3
  427. %endmacro
  428. %macro CAT_UNDEF 2
  429. %undef %1%2
  430. %endmacro
  431. %macro INIT_MMX 0
  432. %define RESET_MM_PERMUTATION INIT_MMX
  433. %define mmsize 8
  434. %define num_mmregs 8
  435. %define mova movq
  436. %define movu movq
  437. %define movh movd
  438. %define movnta movntq
  439. %assign %%i 0
  440. %rep 8
  441. CAT_XDEFINE m, %%i, mm %+ %%i
  442. CAT_XDEFINE nmm, %%i, %%i
  443. %assign %%i %%i+1
  444. %endrep
  445. %rep 8
  446. CAT_UNDEF m, %%i
  447. CAT_UNDEF nmm, %%i
  448. %assign %%i %%i+1
  449. %endrep
  450. %endmacro
  451. %macro INIT_XMM 0
  452. %define RESET_MM_PERMUTATION INIT_XMM
  453. %define mmsize 16
  454. %define num_mmregs 8
  455. %ifdef ARCH_X86_64
  456. %define num_mmregs 16
  457. %endif
  458. %define mova movdqa
  459. %define movu movdqu
  460. %define movh movq
  461. %define movnta movntdq
  462. %assign %%i 0
  463. %rep num_mmregs
  464. CAT_XDEFINE m, %%i, xmm %+ %%i
  465. CAT_XDEFINE nxmm, %%i, %%i
  466. %assign %%i %%i+1
  467. %endrep
  468. %endmacro
  469. INIT_MMX
  470. ; I often want to use macros that permute their arguments. e.g. there's no
  471. ; efficient way to implement butterfly or transpose or dct without swapping some
  472. ; arguments.
  473. ;
  474. ; I would like to not have to manually keep track of the permutations:
  475. ; If I insert a permutation in the middle of a function, it should automatically
  476. ; change everything that follows. For more complex macros I may also have multiple
  477. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  478. ;
  479. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  480. ; permutes its arguments. It's equivalent to exchanging the contents of the
  481. ; registers, except that this way you exchange the register names instead, so it
  482. ; doesn't cost any cycles.
  483. %macro PERMUTE 2-* ; takes a list of pairs to swap
  484. %rep %0/2
  485. %xdefine tmp%2 m%2
  486. %xdefine ntmp%2 nm%2
  487. %rotate 2
  488. %endrep
  489. %rep %0/2
  490. %xdefine m%1 tmp%2
  491. %xdefine nm%1 ntmp%2
  492. %undef tmp%2
  493. %undef ntmp%2
  494. %rotate 2
  495. %endrep
  496. %endmacro
  497. %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
  498. %rep %0-1
  499. %ifdef m%1
  500. %xdefine tmp m%1
  501. %xdefine m%1 m%2
  502. %xdefine m%2 tmp
  503. CAT_XDEFINE n, m%1, %1
  504. CAT_XDEFINE n, m%2, %2
  505. %else
  506. ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
  507. ; Be careful using this mode in nested macros though, as in some cases there may be
  508. ; other copies of m# that have already been dereferenced and don't get updated correctly.
  509. %xdefine %%n1 n %+ %1
  510. %xdefine %%n2 n %+ %2
  511. %xdefine tmp m %+ %%n1
  512. CAT_XDEFINE m, %%n1, m %+ %%n2
  513. CAT_XDEFINE m, %%n2, tmp
  514. CAT_XDEFINE n, m %+ %%n1, %%n1
  515. CAT_XDEFINE n, m %+ %%n2, %%n2
  516. %endif
  517. %undef tmp
  518. %rotate 1
  519. %endrep
  520. %endmacro
  521. ; If SAVE_MM_PERMUTATION is placed at the end of a function and given the
  522. ; function name, then any later calls to that function will automatically
  523. ; load the permutation, so values can be returned in mmregs.
  524. %macro SAVE_MM_PERMUTATION 1 ; name to save as
  525. %assign %%i 0
  526. %rep num_mmregs
  527. CAT_XDEFINE %1_m, %%i, m %+ %%i
  528. %assign %%i %%i+1
  529. %endrep
  530. %endmacro
  531. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  532. %assign %%i 0
  533. %rep num_mmregs
  534. CAT_XDEFINE m, %%i, %1_m %+ %%i
  535. CAT_XDEFINE n, m %+ %%i, %%i
  536. %assign %%i %%i+1
  537. %endrep
  538. %endmacro
  539. %macro call 1
  540. call %1
  541. %ifdef %1_m0
  542. LOAD_MM_PERMUTATION %1
  543. %endif
  544. %endmacro
  545. ; Substitutions that reduce instruction size but are functionally equivalent
  546. %macro add 2
  547. %ifnum %2
  548. %if %2==128
  549. sub %1, -128
  550. %else
  551. add %1, %2
  552. %endif
  553. %else
  554. add %1, %2
  555. %endif
  556. %endmacro
  557. %macro sub 2
  558. %ifnum %2
  559. %if %2==128
  560. add %1, -128
  561. %else
  562. sub %1, %2
  563. %endif
  564. %else
  565. sub %1, %2
  566. %endif
  567. %endmacro