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