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.

637 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. %assign xmm_regs_used %3
  239. ASSERT xmm_regs_used <= 16
  240. %if regs_used > 4
  241. push r4
  242. push r5
  243. %assign stack_offset stack_offset+16
  244. %endif
  245. %if xmm_regs_used > 6
  246. sub rsp, (xmm_regs_used-6)*16+16
  247. %assign stack_offset stack_offset+(xmm_regs_used-6)*16+16
  248. %assign %%i xmm_regs_used
  249. %rep (xmm_regs_used-6)
  250. %assign %%i %%i-1
  251. movdqa [rsp + (%%i-6)*16+8], xmm %+ %%i
  252. %endrep
  253. %endif
  254. LOAD_IF_USED 4, %1
  255. LOAD_IF_USED 5, %1
  256. LOAD_IF_USED 6, %1
  257. DEFINE_ARGS %4
  258. %endmacro
  259. %macro RESTORE_XMM_INTERNAL 1
  260. %if xmm_regs_used > 6
  261. %assign %%i xmm_regs_used
  262. %rep (xmm_regs_used-6)
  263. %assign %%i %%i-1
  264. movdqa xmm %+ %%i, [%1 + (%%i-6)*16+8]
  265. %endrep
  266. add %1, (xmm_regs_used-6)*16+16
  267. %endif
  268. %endmacro
  269. %macro RESTORE_XMM 1
  270. RESTORE_XMM_INTERNAL %1
  271. %assign stack_offset stack_offset-(xmm_regs_used-6)*16+16
  272. %assign xmm_regs_used 0
  273. %endmacro
  274. %macro RET 0
  275. RESTORE_XMM_INTERNAL rsp
  276. %if regs_used > 4
  277. pop r5
  278. pop r4
  279. %endif
  280. ret
  281. %endmacro
  282. %macro REP_RET 0
  283. %if regs_used > 4 || xmm_regs_used > 6
  284. RET
  285. %else
  286. rep ret
  287. %endif
  288. %endmacro
  289. %elifdef ARCH_X86_64 ; *nix x64 ;=============================================
  290. DECLARE_REG 0, rdi, edi, di, dil, edi
  291. DECLARE_REG 1, rsi, esi, si, sil, esi
  292. DECLARE_REG 2, rdx, edx, dx, dl, edx
  293. DECLARE_REG 3, rcx, ecx, cx, cl, ecx
  294. DECLARE_REG 4, r8, r8d, r8w, r8b, r8d
  295. DECLARE_REG 5, r9, r9d, r9w, r9b, r9d
  296. DECLARE_REG 6, rax, eax, ax, al, [rsp + stack_offset + 8]
  297. %define r7m [rsp + stack_offset + 16]
  298. %define r8m [rsp + stack_offset + 24]
  299. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  300. %if %1 < %2
  301. mov r%1, [rsp - 40 + %1*8]
  302. %endif
  303. %endmacro
  304. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  305. ASSERT %2 >= %1
  306. ASSERT %2 <= 7
  307. LOAD_IF_USED 6, %1
  308. DEFINE_ARGS %4
  309. %endmacro
  310. %macro RET 0
  311. ret
  312. %endmacro
  313. %macro REP_RET 0
  314. rep ret
  315. %endmacro
  316. %else ; X86_32 ;==============================================================
  317. DECLARE_REG 0, eax, eax, ax, al, [esp + stack_offset + 4]
  318. DECLARE_REG 1, ecx, ecx, cx, cl, [esp + stack_offset + 8]
  319. DECLARE_REG 2, edx, edx, dx, dl, [esp + stack_offset + 12]
  320. DECLARE_REG 3, ebx, ebx, bx, bl, [esp + stack_offset + 16]
  321. DECLARE_REG 4, esi, esi, si, null, [esp + stack_offset + 20]
  322. DECLARE_REG 5, edi, edi, di, null, [esp + stack_offset + 24]
  323. DECLARE_REG 6, ebp, ebp, bp, null, [esp + stack_offset + 28]
  324. %define r7m [esp + stack_offset + 32]
  325. %define r8m [esp + stack_offset + 36]
  326. %define rsp esp
  327. %macro PUSH_IF_USED 1 ; reg_id
  328. %if %1 < regs_used
  329. push r%1
  330. %assign stack_offset stack_offset+4
  331. %endif
  332. %endmacro
  333. %macro POP_IF_USED 1 ; reg_id
  334. %if %1 < regs_used
  335. pop r%1
  336. %endif
  337. %endmacro
  338. %macro LOAD_IF_USED 2 ; reg_id, number_of_args
  339. %if %1 < %2
  340. mov r%1, [esp + stack_offset + 4 + %1*4]
  341. %endif
  342. %endmacro
  343. %macro PROLOGUE 2-4+ ; #args, #regs, #xmm_regs, arg_names...
  344. ASSERT %2 >= %1
  345. %assign regs_used %2
  346. ASSERT regs_used <= 7
  347. PUSH_IF_USED 3
  348. PUSH_IF_USED 4
  349. PUSH_IF_USED 5
  350. PUSH_IF_USED 6
  351. LOAD_IF_USED 0, %1
  352. LOAD_IF_USED 1, %1
  353. LOAD_IF_USED 2, %1
  354. LOAD_IF_USED 3, %1
  355. LOAD_IF_USED 4, %1
  356. LOAD_IF_USED 5, %1
  357. LOAD_IF_USED 6, %1
  358. DEFINE_ARGS %4
  359. %endmacro
  360. %macro RET 0
  361. POP_IF_USED 6
  362. POP_IF_USED 5
  363. POP_IF_USED 4
  364. POP_IF_USED 3
  365. ret
  366. %endmacro
  367. %macro REP_RET 0
  368. %if regs_used > 3
  369. RET
  370. %else
  371. rep ret
  372. %endif
  373. %endmacro
  374. %endif ;======================================================================
  375. ;=============================================================================
  376. ; arch-independent part
  377. ;=============================================================================
  378. %assign function_align 16
  379. ; Symbol prefix for C linkage
  380. %macro cglobal 1-2+
  381. %xdefine %1 mangle(program_name %+ _ %+ %1)
  382. %xdefine %1.skip_prologue %1 %+ .skip_prologue
  383. %ifidn __OUTPUT_FORMAT__,elf
  384. global %1:function hidden
  385. %else
  386. global %1
  387. %endif
  388. align function_align
  389. %1:
  390. RESET_MM_PERMUTATION ; not really needed, but makes disassembly somewhat nicer
  391. %assign stack_offset 0
  392. %if %0 > 1
  393. PROLOGUE %2
  394. %endif
  395. %endmacro
  396. %macro cextern 1
  397. %xdefine %1 mangle(program_name %+ _ %+ %1)
  398. extern %1
  399. %endmacro
  400. ;like cextern, but without the prefix
  401. %macro cextern_naked 1
  402. %xdefine %1 mangle(%1)
  403. extern %1
  404. %endmacro
  405. %macro const 2+
  406. %xdefine %1 mangle(program_name %+ _ %+ %1)
  407. global %1
  408. %1: %2
  409. %endmacro
  410. ; This is needed for ELF, otherwise the GNU linker assumes the stack is
  411. ; executable by default.
  412. %ifidn __OUTPUT_FORMAT__,elf
  413. SECTION .note.GNU-stack noalloc noexec nowrite progbits
  414. %endif
  415. ; merge mmx and sse*
  416. %macro CAT_XDEFINE 3
  417. %xdefine %1%2 %3
  418. %endmacro
  419. %macro CAT_UNDEF 2
  420. %undef %1%2
  421. %endmacro
  422. %macro INIT_MMX 0
  423. %define RESET_MM_PERMUTATION INIT_MMX
  424. %define mmsize 8
  425. %define num_mmregs 8
  426. %define mova movq
  427. %define movu movq
  428. %define movh movd
  429. %define movnt movntq
  430. %assign %%i 0
  431. %rep 8
  432. CAT_XDEFINE m, %%i, mm %+ %%i
  433. CAT_XDEFINE nmm, %%i, %%i
  434. %assign %%i %%i+1
  435. %endrep
  436. %rep 8
  437. CAT_UNDEF m, %%i
  438. CAT_UNDEF nmm, %%i
  439. %assign %%i %%i+1
  440. %endrep
  441. %endmacro
  442. %macro INIT_XMM 0
  443. %define RESET_MM_PERMUTATION INIT_XMM
  444. %define mmsize 16
  445. %define num_mmregs 8
  446. %ifdef ARCH_X86_64
  447. %define num_mmregs 16
  448. %endif
  449. %define mova movdqa
  450. %define movu movdqu
  451. %define movh movq
  452. %define movnt movntdq
  453. %assign %%i 0
  454. %rep num_mmregs
  455. CAT_XDEFINE m, %%i, xmm %+ %%i
  456. CAT_XDEFINE nxmm, %%i, %%i
  457. %assign %%i %%i+1
  458. %endrep
  459. %endmacro
  460. INIT_MMX
  461. ; I often want to use macros that permute their arguments. e.g. there's no
  462. ; efficient way to implement butterfly or transpose or dct without swapping some
  463. ; arguments.
  464. ;
  465. ; I would like to not have to manually keep track of the permutations:
  466. ; If I insert a permutation in the middle of a function, it should automatically
  467. ; change everything that follows. For more complex macros I may also have multiple
  468. ; implementations, e.g. the SSE2 and SSSE3 versions may have different permutations.
  469. ;
  470. ; Hence these macros. Insert a PERMUTE or some SWAPs at the end of a macro that
  471. ; permutes its arguments. It's equivalent to exchanging the contents of the
  472. ; registers, except that this way you exchange the register names instead, so it
  473. ; doesn't cost any cycles.
  474. %macro PERMUTE 2-* ; takes a list of pairs to swap
  475. %rep %0/2
  476. %xdefine tmp%2 m%2
  477. %xdefine ntmp%2 nm%2
  478. %rotate 2
  479. %endrep
  480. %rep %0/2
  481. %xdefine m%1 tmp%2
  482. %xdefine nm%1 ntmp%2
  483. %undef tmp%2
  484. %undef ntmp%2
  485. %rotate 2
  486. %endrep
  487. %endmacro
  488. %macro SWAP 2-* ; swaps a single chain (sometimes more concise than pairs)
  489. %rep %0-1
  490. %ifdef m%1
  491. %xdefine tmp m%1
  492. %xdefine m%1 m%2
  493. %xdefine m%2 tmp
  494. CAT_XDEFINE n, m%1, %1
  495. CAT_XDEFINE n, m%2, %2
  496. %else
  497. ; If we were called as "SWAP m0,m1" rather than "SWAP 0,1" infer the original numbers here.
  498. ; Be careful using this mode in nested macros though, as in some cases there may be
  499. ; other copies of m# that have already been dereferenced and don't get updated correctly.
  500. %xdefine %%n1 n %+ %1
  501. %xdefine %%n2 n %+ %2
  502. %xdefine tmp m %+ %%n1
  503. CAT_XDEFINE m, %%n1, m %+ %%n2
  504. CAT_XDEFINE m, %%n2, tmp
  505. CAT_XDEFINE n, m %+ %%n1, %%n1
  506. CAT_XDEFINE n, m %+ %%n2, %%n2
  507. %endif
  508. %undef tmp
  509. %rotate 1
  510. %endrep
  511. %endmacro
  512. ; If SAVE_MM_PERMUTATION is placed at the end of a function and given the
  513. ; function name, then any later calls to that function will automatically
  514. ; load the permutation, so values can be returned in mmregs.
  515. %macro SAVE_MM_PERMUTATION 1 ; name to save as
  516. %assign %%i 0
  517. %rep num_mmregs
  518. CAT_XDEFINE %1_m, %%i, m %+ %%i
  519. %assign %%i %%i+1
  520. %endrep
  521. %endmacro
  522. %macro LOAD_MM_PERMUTATION 1 ; name to load from
  523. %assign %%i 0
  524. %rep num_mmregs
  525. CAT_XDEFINE m, %%i, %1_m %+ %%i
  526. CAT_XDEFINE n, m %+ %%i, %%i
  527. %assign %%i %%i+1
  528. %endrep
  529. %endmacro
  530. %macro call 1
  531. call %1
  532. %ifdef %1_m0
  533. LOAD_MM_PERMUTATION %1
  534. %endif
  535. %endmacro
  536. ; Substitutions that reduce instruction size but are functionally equivalent
  537. %macro add 2
  538. %ifnum %2
  539. %if %2==128
  540. sub %1, -128
  541. %else
  542. add %1, %2
  543. %endif
  544. %else
  545. add %1, %2
  546. %endif
  547. %endmacro
  548. %macro sub 2
  549. %ifnum %2
  550. %if %2==128
  551. add %1, -128
  552. %else
  553. sub %1, %2
  554. %endif
  555. %else
  556. sub %1, %2
  557. %endif
  558. %endmacro