Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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