jquanti-avx2.asm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ;
  2. ; jquanti.asm - sample data conversion and quantization (64-bit AVX2)
  3. ;
  4. ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  5. ; Copyright (C) 2009, 2016, 2018, D. R. Commander.
  6. ; Copyright (C) 2016, Matthieu Darbois.
  7. ; Copyright (C) 2018, Matthias Räncker.
  8. ;
  9. ; Based on the x86 SIMD extension for IJG JPEG library
  10. ; Copyright (C) 1999-2006, MIYASAKA Masaru.
  11. ; For conditions of distribution and use, see copyright notice in jsimdext.inc
  12. ;
  13. ; This file should be assembled with NASM (Netwide Assembler),
  14. ; can *not* be assembled with Microsoft's MASM or any compatible
  15. ; assembler (including Borland's Turbo Assembler).
  16. ; NASM is available from http://nasm.sourceforge.net/ or
  17. ; http://sourceforge.net/project/showfiles.php?group_id=6208
  18. %include "jsimdext.inc"
  19. %include "jdct.inc"
  20. ; --------------------------------------------------------------------------
  21. SECTION SEG_TEXT
  22. BITS 64
  23. ;
  24. ; Load data into workspace, applying unsigned->signed conversion
  25. ;
  26. ; GLOBAL(void)
  27. ; jsimd_convsamp_avx2(JSAMPARRAY sample_data, JDIMENSION start_col,
  28. ; DCTELEM *workspace);
  29. ;
  30. ; r10 = JSAMPARRAY sample_data
  31. ; r11d = JDIMENSION start_col
  32. ; r12 = DCTELEM *workspace
  33. align 32
  34. GLOBAL_FUNCTION(jsimd_convsamp_avx2)
  35. EXTN(jsimd_convsamp_avx2):
  36. push rbp
  37. mov rax, rsp
  38. mov rbp, rsp
  39. collect_args 3
  40. mov eax, r11d
  41. mov rsip, JSAMPROW [r10+0*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  42. mov rdip, JSAMPROW [r10+1*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  43. movq xmm0, XMM_MMWORD [rsi+rax*SIZEOF_JSAMPLE]
  44. pinsrq xmm0, XMM_MMWORD [rdi+rax*SIZEOF_JSAMPLE], 1
  45. mov rsip, JSAMPROW [r10+2*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  46. mov rdip, JSAMPROW [r10+3*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  47. movq xmm1, XMM_MMWORD [rsi+rax*SIZEOF_JSAMPLE]
  48. pinsrq xmm1, XMM_MMWORD [rdi+rax*SIZEOF_JSAMPLE], 1
  49. mov rsip, JSAMPROW [r10+4*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  50. mov rdip, JSAMPROW [r10+5*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  51. movq xmm2, XMM_MMWORD [rsi+rax*SIZEOF_JSAMPLE]
  52. pinsrq xmm2, XMM_MMWORD [rdi+rax*SIZEOF_JSAMPLE], 1
  53. mov rsip, JSAMPROW [r10+6*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  54. mov rdip, JSAMPROW [r10+7*SIZEOF_JSAMPROW] ; (JSAMPLE *)
  55. movq xmm3, XMM_MMWORD [rsi+rax*SIZEOF_JSAMPLE]
  56. pinsrq xmm3, XMM_MMWORD [rdi+rax*SIZEOF_JSAMPLE], 1
  57. vpmovzxbw ymm0, xmm0 ; ymm0=(00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17)
  58. vpmovzxbw ymm1, xmm1 ; ymm1=(20 21 22 23 24 25 26 27 30 31 32 33 34 35 36 37)
  59. vpmovzxbw ymm2, xmm2 ; ymm2=(40 41 42 43 44 45 46 47 50 51 52 53 54 55 56 57)
  60. vpmovzxbw ymm3, xmm3 ; ymm3=(60 61 62 63 64 65 66 67 70 71 72 73 74 75 76 77)
  61. vpcmpeqw ymm7, ymm7, ymm7
  62. vpsllw ymm7, ymm7, 7 ; ymm7={0xFF80 0xFF80 0xFF80 0xFF80 ..}
  63. vpaddw ymm0, ymm0, ymm7
  64. vpaddw ymm1, ymm1, ymm7
  65. vpaddw ymm2, ymm2, ymm7
  66. vpaddw ymm3, ymm3, ymm7
  67. vmovdqu YMMWORD [YMMBLOCK(0,0,r12,SIZEOF_DCTELEM)], ymm0
  68. vmovdqu YMMWORD [YMMBLOCK(2,0,r12,SIZEOF_DCTELEM)], ymm1
  69. vmovdqu YMMWORD [YMMBLOCK(4,0,r12,SIZEOF_DCTELEM)], ymm2
  70. vmovdqu YMMWORD [YMMBLOCK(6,0,r12,SIZEOF_DCTELEM)], ymm3
  71. vzeroupper
  72. uncollect_args 3
  73. pop rbp
  74. ret
  75. ; --------------------------------------------------------------------------
  76. ;
  77. ; Quantize/descale the coefficients, and store into coef_block
  78. ;
  79. ; This implementation is based on an algorithm described in
  80. ; "How to optimize for the Pentium family of microprocessors"
  81. ; (http://www.agner.org/assem/).
  82. ;
  83. ; GLOBAL(void)
  84. ; jsimd_quantize_avx2(JCOEFPTR coef_block, DCTELEM *divisors,
  85. ; DCTELEM *workspace);
  86. ;
  87. %define RECIPROCAL(m, n, b) \
  88. YMMBLOCK(DCTSIZE * 0 + (m), (n), (b), SIZEOF_DCTELEM)
  89. %define CORRECTION(m, n, b) \
  90. YMMBLOCK(DCTSIZE * 1 + (m), (n), (b), SIZEOF_DCTELEM)
  91. %define SCALE(m, n, b) \
  92. YMMBLOCK(DCTSIZE * 2 + (m), (n), (b), SIZEOF_DCTELEM)
  93. ; r10 = JCOEFPTR coef_block
  94. ; r11 = DCTELEM *divisors
  95. ; r12 = DCTELEM *workspace
  96. align 32
  97. GLOBAL_FUNCTION(jsimd_quantize_avx2)
  98. EXTN(jsimd_quantize_avx2):
  99. push rbp
  100. mov rax, rsp
  101. mov rbp, rsp
  102. collect_args 3
  103. vmovdqu ymm4, [YMMBLOCK(0,0,r12,SIZEOF_DCTELEM)]
  104. vmovdqu ymm5, [YMMBLOCK(2,0,r12,SIZEOF_DCTELEM)]
  105. vmovdqu ymm6, [YMMBLOCK(4,0,r12,SIZEOF_DCTELEM)]
  106. vmovdqu ymm7, [YMMBLOCK(6,0,r12,SIZEOF_DCTELEM)]
  107. vpabsw ymm0, ymm4
  108. vpabsw ymm1, ymm5
  109. vpabsw ymm2, ymm6
  110. vpabsw ymm3, ymm7
  111. vpaddw ymm0, YMMWORD [CORRECTION(0,0,r11)] ; correction + roundfactor
  112. vpaddw ymm1, YMMWORD [CORRECTION(2,0,r11)]
  113. vpaddw ymm2, YMMWORD [CORRECTION(4,0,r11)]
  114. vpaddw ymm3, YMMWORD [CORRECTION(6,0,r11)]
  115. vpmulhuw ymm0, YMMWORD [RECIPROCAL(0,0,r11)] ; reciprocal
  116. vpmulhuw ymm1, YMMWORD [RECIPROCAL(2,0,r11)]
  117. vpmulhuw ymm2, YMMWORD [RECIPROCAL(4,0,r11)]
  118. vpmulhuw ymm3, YMMWORD [RECIPROCAL(6,0,r11)]
  119. vpmulhuw ymm0, YMMWORD [SCALE(0,0,r11)] ; scale
  120. vpmulhuw ymm1, YMMWORD [SCALE(2,0,r11)]
  121. vpmulhuw ymm2, YMMWORD [SCALE(4,0,r11)]
  122. vpmulhuw ymm3, YMMWORD [SCALE(6,0,r11)]
  123. vpsignw ymm0, ymm0, ymm4
  124. vpsignw ymm1, ymm1, ymm5
  125. vpsignw ymm2, ymm2, ymm6
  126. vpsignw ymm3, ymm3, ymm7
  127. vmovdqu [YMMBLOCK(0,0,r10,SIZEOF_DCTELEM)], ymm0
  128. vmovdqu [YMMBLOCK(2,0,r10,SIZEOF_DCTELEM)], ymm1
  129. vmovdqu [YMMBLOCK(4,0,r10,SIZEOF_DCTELEM)], ymm2
  130. vmovdqu [YMMBLOCK(6,0,r10,SIZEOF_DCTELEM)], ymm3
  131. vzeroupper
  132. uncollect_args 3
  133. pop rbp
  134. ret
  135. ; For some reason, the OS X linker does not honor the request to align the
  136. ; segment unless we do this.
  137. align 32