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.

208 lines
5.3KB

  1. ;*****************************************************************************
  2. ;* Assembly testing and benchmarking tool
  3. ;* Copyright (c) 2008 Loren Merritt
  4. ;* Copyright (c) 2012 Henrik Gramner
  5. ;*
  6. ;* This file is part of FFmpeg.
  7. ;*
  8. ;* FFmpeg is free software; you can redistribute it and/or modify
  9. ;* it under the terms of the GNU General Public License as published by
  10. ;* the Free Software Foundation; either version 2 of the License, or
  11. ;* (at your option) any later version.
  12. ;*
  13. ;* FFmpeg is distributed in the hope that it will be useful,
  14. ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;* GNU General Public License for more details.
  17. ;*
  18. ;* You should have received a copy of the GNU General Public License
  19. ;* along with this program; if not, write to the Free Software
  20. ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  21. ;*****************************************************************************
  22. %define private_prefix checkasm
  23. %include "libavutil/x86/x86inc.asm"
  24. SECTION_RODATA
  25. error_message: db "failed to preserve register", 0
  26. %if ARCH_X86_64
  27. ; just random numbers to reduce the chance of incidental match
  28. ALIGN 16
  29. x6: dq 0x1a1b2550a612b48c,0x79445c159ce79064
  30. x7: dq 0x2eed899d5a28ddcd,0x86b2536fcd8cf636
  31. x8: dq 0xb0856806085e7943,0x3f2bf84fc0fcca4e
  32. x9: dq 0xacbd382dcf5b8de2,0xd229e1f5b281303f
  33. x10: dq 0x71aeaff20b095fd9,0xab63e2e11fa38ed9
  34. x11: dq 0x89b0c0765892729a,0x77d410d5c42c882d
  35. x12: dq 0xc45ea11a955d8dd5,0x24b3c1d2a024048b
  36. x13: dq 0x2e8ec680de14b47c,0xdd7b8919edd42786
  37. x14: dq 0x135ce6888fa02cbf,0x11e53e2b2ac655ef
  38. x15: dq 0x011ff554472a7a10,0x6de8f4c914c334d5
  39. n7: dq 0x21f86d66c8ca00ce
  40. n8: dq 0x75b6ba21077c48ad
  41. n9: dq 0xed56bb2dcb3c7736
  42. n10: dq 0x8bda43d3fd1a7e06
  43. n11: dq 0xb64a9c9e5d318408
  44. n12: dq 0xdf9a54b303f1d3a3
  45. n13: dq 0x4a75479abd64e097
  46. n14: dq 0x249214109d5d1c88
  47. %endif
  48. SECTION .text
  49. cextern fail_func
  50. ; max number of args used by any asm function.
  51. ; (max_args % 4) must equal 3 for stack alignment
  52. %define max_args 15
  53. %if ARCH_X86_64
  54. ;-----------------------------------------------------------------------------
  55. ; int checkasm_stack_clobber(uint64_t clobber, ...)
  56. ;-----------------------------------------------------------------------------
  57. cglobal stack_clobber, 1,2
  58. ; Clobber the stack with junk below the stack pointer
  59. %define size (max_args+6)*8
  60. SUB rsp, size
  61. mov r1, size-8
  62. .loop:
  63. mov [rsp+r1], r0
  64. sub r1, 8
  65. jge .loop
  66. ADD rsp, size
  67. RET
  68. %if WIN64
  69. %assign free_regs 7
  70. DECLARE_REG_TMP 4
  71. %else
  72. %assign free_regs 9
  73. DECLARE_REG_TMP 7
  74. %endif
  75. ;-----------------------------------------------------------------------------
  76. ; void checkasm_checked_call(void *func, ...)
  77. ;-----------------------------------------------------------------------------
  78. INIT_XMM
  79. cglobal checked_call, 2,15,16,max_args*8+8
  80. mov t0, r0
  81. ; All arguments have been pushed on the stack instead of registers in order to
  82. ; test for incorrect assumptions that 32-bit ints are zero-extended to 64-bit.
  83. mov r0, r6mp
  84. mov r1, r7mp
  85. mov r2, r8mp
  86. mov r3, r9mp
  87. %if UNIX64
  88. mov r4, r10mp
  89. mov r5, r11mp
  90. %assign i 6
  91. %rep max_args-6
  92. mov r9, [rsp+stack_offset+(i+1)*8]
  93. mov [rsp+(i-6)*8], r9
  94. %assign i i+1
  95. %endrep
  96. %else ; WIN64
  97. %assign i 4
  98. %rep max_args-4
  99. mov r9, [rsp+stack_offset+(i+7)*8]
  100. mov [rsp+i*8], r9
  101. %assign i i+1
  102. %endrep
  103. ; Move possible floating-point arguments to the correct registers
  104. movq m0, r0
  105. movq m1, r1
  106. movq m2, r2
  107. movq m3, r3
  108. %assign i 6
  109. %rep 16-6
  110. mova m %+ i, [x %+ i]
  111. %assign i i+1
  112. %endrep
  113. %endif
  114. %assign i 14
  115. %rep 15-free_regs
  116. mov r %+ i, [n %+ i]
  117. %assign i i-1
  118. %endrep
  119. call t0
  120. %assign i 14
  121. %rep 15-free_regs
  122. xor r %+ i, [n %+ i]
  123. or r14, r %+ i
  124. %assign i i-1
  125. %endrep
  126. %if WIN64
  127. %assign i 6
  128. %rep 16-6
  129. pxor m %+ i, [x %+ i]
  130. por m6, m %+ i
  131. %assign i i+1
  132. %endrep
  133. packsswb m6, m6
  134. movq r5, m6
  135. or r14, r5
  136. %endif
  137. ; Call fail_func() with a descriptive message to mark it as a failure
  138. ; if the called function didn't preserve all callee-saved registers.
  139. ; Save the return value located in rdx:rax first to prevent clobbering.
  140. jz .ok
  141. mov r9, rax
  142. mov r10, rdx
  143. lea r0, [error_message]
  144. xor eax, eax
  145. call fail_func
  146. mov rdx, r10
  147. mov rax, r9
  148. .ok:
  149. RET
  150. %else
  151. ; just random numbers to reduce the chance of incidental match
  152. %define n3 dword 0x6549315c
  153. %define n4 dword 0xe02f3e23
  154. %define n5 dword 0xb78d0d1d
  155. %define n6 dword 0x33627ba7
  156. ;-----------------------------------------------------------------------------
  157. ; void checkasm_checked_call(void *func, ...)
  158. ;-----------------------------------------------------------------------------
  159. cglobal checked_call, 1,7
  160. mov r3, n3
  161. mov r4, n4
  162. mov r5, n5
  163. mov r6, n6
  164. %rep max_args
  165. PUSH dword [esp+20+max_args*4]
  166. %endrep
  167. call r0
  168. xor r3, n3
  169. xor r4, n4
  170. xor r5, n5
  171. xor r6, n6
  172. or r3, r4
  173. or r5, r6
  174. or r3, r5
  175. jz .ok
  176. mov r3, eax
  177. mov r4, edx
  178. lea r0, [error_message]
  179. mov [esp], r0
  180. call fail_func
  181. mov edx, r4
  182. mov eax, r3
  183. .ok:
  184. add esp, max_args*4
  185. REP_RET
  186. %endif ; ARCH_X86_64