checkasm.asm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. error_message_emms: db "failed to issue emms", 0
  27. %if ARCH_X86_64
  28. ; just random numbers to reduce the chance of incidental match
  29. ALIGN 16
  30. x6: dq 0x1a1b2550a612b48c,0x79445c159ce79064
  31. x7: dq 0x2eed899d5a28ddcd,0x86b2536fcd8cf636
  32. x8: dq 0xb0856806085e7943,0x3f2bf84fc0fcca4e
  33. x9: dq 0xacbd382dcf5b8de2,0xd229e1f5b281303f
  34. x10: dq 0x71aeaff20b095fd9,0xab63e2e11fa38ed9
  35. x11: dq 0x89b0c0765892729a,0x77d410d5c42c882d
  36. x12: dq 0xc45ea11a955d8dd5,0x24b3c1d2a024048b
  37. x13: dq 0x2e8ec680de14b47c,0xdd7b8919edd42786
  38. x14: dq 0x135ce6888fa02cbf,0x11e53e2b2ac655ef
  39. x15: dq 0x011ff554472a7a10,0x6de8f4c914c334d5
  40. n7: dq 0x21f86d66c8ca00ce
  41. n8: dq 0x75b6ba21077c48ad
  42. n9: dq 0xed56bb2dcb3c7736
  43. n10: dq 0x8bda43d3fd1a7e06
  44. n11: dq 0xb64a9c9e5d318408
  45. n12: dq 0xdf9a54b303f1d3a3
  46. n13: dq 0x4a75479abd64e097
  47. n14: dq 0x249214109d5d1c88
  48. %endif
  49. SECTION .text
  50. cextern fail_func
  51. ; max number of args used by any asm function.
  52. ; (max_args % 4) must equal 3 for stack alignment
  53. %define max_args 15
  54. %if ARCH_X86_64
  55. ;-----------------------------------------------------------------------------
  56. ; int checkasm_stack_clobber(uint64_t clobber, ...)
  57. ;-----------------------------------------------------------------------------
  58. cglobal stack_clobber, 1,2
  59. ; Clobber the stack with junk below the stack pointer
  60. %define argsize (max_args+6)*8
  61. SUB rsp, argsize
  62. mov r1, argsize-8
  63. .loop:
  64. mov [rsp+r1], r0
  65. sub r1, 8
  66. jge .loop
  67. ADD rsp, argsize
  68. RET
  69. %if WIN64
  70. %assign free_regs 7
  71. DECLARE_REG_TMP 4
  72. %else
  73. %assign free_regs 9
  74. DECLARE_REG_TMP 7
  75. %endif
  76. %macro report_fail 1
  77. mov r9, rax
  78. mov r10, rdx
  79. lea r0, [%1]
  80. xor eax, eax
  81. call fail_func
  82. mov rdx, r10
  83. mov rax, r9
  84. %endmacro
  85. ;-----------------------------------------------------------------------------
  86. ; void checkasm_checked_call(void *func, ...)
  87. ;-----------------------------------------------------------------------------
  88. INIT_XMM
  89. %macro CHECKED_CALL 0-1
  90. cglobal checked_call%1, 2,15,16,max_args*8+8
  91. mov t0, r0
  92. ; All arguments have been pushed on the stack instead of registers in order to
  93. ; test for incorrect assumptions that 32-bit ints are zero-extended to 64-bit.
  94. mov r0, r6mp
  95. mov r1, r7mp
  96. mov r2, r8mp
  97. mov r3, r9mp
  98. %if UNIX64
  99. mov r4, r10mp
  100. mov r5, r11mp
  101. %assign i 6
  102. %rep max_args-6
  103. mov r9, [rsp+stack_offset+(i+1)*8]
  104. mov [rsp+(i-6)*8], r9
  105. %assign i i+1
  106. %endrep
  107. %else ; WIN64
  108. %assign i 4
  109. %rep max_args-4
  110. mov r9, [rsp+stack_offset+(i+7)*8]
  111. mov [rsp+i*8], r9
  112. %assign i i+1
  113. %endrep
  114. ; Move possible floating-point arguments to the correct registers
  115. movq m0, r0
  116. movq m1, r1
  117. movq m2, r2
  118. movq m3, r3
  119. %assign i 6
  120. %rep 16-6
  121. mova m %+ i, [x %+ i]
  122. %assign i i+1
  123. %endrep
  124. %endif
  125. %assign i 14
  126. %rep 15-free_regs
  127. mov r %+ i, [n %+ i]
  128. %assign i i-1
  129. %endrep
  130. call t0
  131. %assign i 14
  132. %rep 15-free_regs
  133. xor r %+ i, [n %+ i]
  134. or r14, r %+ i
  135. %assign i i-1
  136. %endrep
  137. %if WIN64
  138. %assign i 6
  139. %rep 16-6
  140. pxor m %+ i, [x %+ i]
  141. por m6, m %+ i
  142. %assign i i+1
  143. %endrep
  144. packsswb m6, m6
  145. movq r5, m6
  146. or r14, r5
  147. %endif
  148. ; Call fail_func() with a descriptive message to mark it as a failure
  149. ; if the called function didn't preserve all callee-saved registers.
  150. ; Save the return value located in rdx:rax first to prevent clobbering.
  151. jz .clobber_ok
  152. report_fail error_message
  153. .clobber_ok:
  154. %ifidn %1, _emms
  155. emms
  156. %elifnidn %1, _float
  157. fstenv [rsp]
  158. cmp word [rsp + 8], 0xffff
  159. je .emms_ok
  160. report_fail error_message_emms
  161. emms
  162. .emms_ok:
  163. %endif
  164. RET
  165. %endmacro
  166. %else
  167. ; just random numbers to reduce the chance of incidental match
  168. %define n3 dword 0x6549315c
  169. %define n4 dword 0xe02f3e23
  170. %define n5 dword 0xb78d0d1d
  171. %define n6 dword 0x33627ba7
  172. %macro report_fail 1
  173. mov r3, eax
  174. mov r4, edx
  175. lea r0, [%1]
  176. mov [esp], r0
  177. call fail_func
  178. mov edx, r4
  179. mov eax, r3
  180. %endmacro
  181. %macro CHECKED_CALL 0-1
  182. ;-----------------------------------------------------------------------------
  183. ; void checkasm_checked_call(void *func, ...)
  184. ;-----------------------------------------------------------------------------
  185. cglobal checked_call%1, 1,7
  186. mov r3, n3
  187. mov r4, n4
  188. mov r5, n5
  189. mov r6, n6
  190. %rep max_args
  191. PUSH dword [esp+20+max_args*4]
  192. %endrep
  193. call r0
  194. xor r3, n3
  195. xor r4, n4
  196. xor r5, n5
  197. xor r6, n6
  198. or r3, r4
  199. or r5, r6
  200. or r3, r5
  201. jz .clobber_ok
  202. report_fail error_message
  203. .clobber_ok:
  204. %ifidn %1, _emms
  205. emms
  206. %elifnidn %1, _float
  207. fstenv [esp]
  208. cmp word [esp + 8], 0xffff
  209. je .emms_ok
  210. report_fail error_message_emms
  211. emms
  212. .emms_ok:
  213. %endif
  214. add esp, max_args*4
  215. RET
  216. %endmacro
  217. %endif ; ARCH_X86_64
  218. CHECKED_CALL
  219. CHECKED_CALL _emms
  220. CHECKED_CALL _float