shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; jquanti.asm - sample data conversion and quantization (AVX2)
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB</ossman@cendio.se>
shun-iwasawa 82a8f5
; Copyright (C) 2016, 2018, D. R. Commander.
shun-iwasawa 82a8f5
; Copyright (C) 2016, Matthieu Darbois.
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; Based on the x86 SIMD extension for IJG JPEG library
shun-iwasawa 82a8f5
; Copyright (C) 1999-2006, MIYASAKA Masaru.
shun-iwasawa 82a8f5
; For conditions of distribution and use, see copyright notice in jsimdext.inc
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; This file should be assembled with NASM (Netwide Assembler),
shun-iwasawa 82a8f5
; can *not* be assembled with Microsoft's MASM or any compatible
shun-iwasawa 82a8f5
; assembler (including Borland's Turbo Assembler).
shun-iwasawa 82a8f5
; NASM is available from http://nasm.sourceforge.net/ or
shun-iwasawa 82a8f5
; http://sourceforge.net/project/showfiles.php?group_id=6208
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
%include "jsimdext.inc"
shun-iwasawa 82a8f5
%include "jdct.inc"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
; --------------------------------------------------------------------------
shun-iwasawa 82a8f5
    SECTION     SEG_TEXT
shun-iwasawa 82a8f5
    BITS        32
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; Load data into workspace, applying unsigned->signed conversion
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; GLOBAL(void)
shun-iwasawa 82a8f5
; jsimd_convsamp_avx2(JSAMPARRAY sample_data, JDIMENSION start_col,
shun-iwasawa 82a8f5
;                     DCTELEM *workspace);
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
%define sample_data  ebp + 8            ; JSAMPARRAY sample_data
shun-iwasawa 82a8f5
%define start_col    ebp + 12           ; JDIMENSION start_col
shun-iwasawa 82a8f5
%define workspace    ebp + 16           ; DCTELEM *workspace
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    align       32
shun-iwasawa 82a8f5
    GLOBAL_FUNCTION(jsimd_convsamp_avx2)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_convsamp_avx2):
shun-iwasawa 82a8f5
    push        ebp
shun-iwasawa 82a8f5
    mov         ebp, esp
shun-iwasawa 82a8f5
    push        ebx
shun-iwasawa 82a8f5
;   push        ecx                     ; need not be preserved
shun-iwasawa 82a8f5
;   push        edx                     ; need not be preserved
shun-iwasawa 82a8f5
    push        esi
shun-iwasawa 82a8f5
    push        edi
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         esi, JSAMPARRAY [sample_data]  ; (JSAMPROW *)
shun-iwasawa 82a8f5
    mov         eax, JDIMENSION [start_col]
shun-iwasawa 82a8f5
    mov         edi, POINTER [workspace]       ; (DCTELEM *)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         ebx, JSAMPROW [esi+0*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    mov         edx, JSAMPROW [esi+1*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    movq        xmm0, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
    movq        xmm1, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         ebx, JSAMPROW [esi+2*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    mov         edx, JSAMPROW [esi+3*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    movq        xmm2, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
    movq        xmm3, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         ebx, JSAMPROW [esi+4*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    mov         edx, JSAMPROW [esi+5*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    movq        xmm4, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
    movq        xmm5, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         ebx, JSAMPROW [esi+6*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    mov         edx, JSAMPROW [esi+7*SIZEOF_JSAMPROW]  ; (JSAMPLE *)
shun-iwasawa 82a8f5
    movq        xmm6, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
    movq        xmm7, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vinserti128 ymm0, ymm0, xmm1, 1
shun-iwasawa 82a8f5
    vinserti128 ymm2, ymm2, xmm3, 1
shun-iwasawa 82a8f5
    vinserti128 ymm4, ymm4, xmm5, 1
shun-iwasawa 82a8f5
    vinserti128 ymm6, ymm6, xmm7, 1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vpxor       ymm1, ymm1, ymm1        ; ymm1=(all 0's)
shun-iwasawa 82a8f5
    vpunpcklbw  ymm0, ymm0, ymm1
shun-iwasawa 82a8f5
    vpunpcklbw  ymm2, ymm2, ymm1
shun-iwasawa 82a8f5
    vpunpcklbw  ymm4, ymm4, ymm1
shun-iwasawa 82a8f5
    vpunpcklbw  ymm6, ymm6, ymm1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vpcmpeqw    ymm7, ymm7, ymm7
shun-iwasawa 82a8f5
    vpsllw      ymm7, ymm7, 7           ; ymm7={0xFF80 0xFF80 0xFF80 0xFF80 ..}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vpaddw      ymm0, ymm0, ymm7
shun-iwasawa 82a8f5
    vpaddw      ymm2, ymm2, ymm7
shun-iwasawa 82a8f5
    vpaddw      ymm4, ymm4, ymm7
shun-iwasawa 82a8f5
    vpaddw      ymm6, ymm6, ymm7
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vmovdqu     YMMWORD [YMMBLOCK(0,0,edi,SIZEOF_DCTELEM)], ymm0
shun-iwasawa 82a8f5
    vmovdqu     YMMWORD [YMMBLOCK(2,0,edi,SIZEOF_DCTELEM)], ymm2
shun-iwasawa 82a8f5
    vmovdqu     YMMWORD [YMMBLOCK(4,0,edi,SIZEOF_DCTELEM)], ymm4
shun-iwasawa 82a8f5
    vmovdqu     YMMWORD [YMMBLOCK(6,0,edi,SIZEOF_DCTELEM)], ymm6
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vzeroupper
shun-iwasawa 82a8f5
    pop         edi
shun-iwasawa 82a8f5
    pop         esi
shun-iwasawa 82a8f5
;   pop         edx                     ; need not be preserved
shun-iwasawa 82a8f5
;   pop         ecx                     ; need not be preserved
shun-iwasawa 82a8f5
    pop         ebx
shun-iwasawa 82a8f5
    pop         ebp
shun-iwasawa 82a8f5
    ret
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
; --------------------------------------------------------------------------
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; Quantize/descale the coefficients, and store into coef_block
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; This implementation is based on an algorithm described in
shun-iwasawa 82a8f5
;   "How to optimize for the Pentium family of microprocessors"
shun-iwasawa 82a8f5
;   (http://www.agner.org/assem/).
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; GLOBAL(void)
shun-iwasawa 82a8f5
; jsimd_quantize_avx2(JCOEFPTR coef_block, DCTELEM *divisors,
shun-iwasawa 82a8f5
;                     DCTELEM *workspace);
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
%define RECIPROCAL(m, n, b) \
shun-iwasawa 82a8f5
  YMMBLOCK(DCTSIZE * 0 + (m), (n), (b), SIZEOF_DCTELEM)
shun-iwasawa 82a8f5
%define CORRECTION(m, n, b) \
shun-iwasawa 82a8f5
  YMMBLOCK(DCTSIZE * 1 + (m), (n), (b), SIZEOF_DCTELEM)
shun-iwasawa 82a8f5
%define SCALE(m, n, b) \
shun-iwasawa 82a8f5
  YMMBLOCK(DCTSIZE * 2 + (m), (n), (b), SIZEOF_DCTELEM)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
%define coef_block  ebp + 8             ; JCOEFPTR coef_block
shun-iwasawa 82a8f5
%define divisors    ebp + 12            ; DCTELEM *divisors
shun-iwasawa 82a8f5
%define workspace   ebp + 16            ; DCTELEM *workspace
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    align       32
shun-iwasawa 82a8f5
    GLOBAL_FUNCTION(jsimd_quantize_avx2)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_quantize_avx2):
shun-iwasawa 82a8f5
    push        ebp
shun-iwasawa 82a8f5
    mov         ebp, esp
shun-iwasawa 82a8f5
;   push        ebx                     ; unused
shun-iwasawa 82a8f5
;   push        ecx                     ; unused
shun-iwasawa 82a8f5
;   push        edx                     ; need not be preserved
shun-iwasawa 82a8f5
    push        esi
shun-iwasawa 82a8f5
    push        edi
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         esi, POINTER [workspace]
shun-iwasawa 82a8f5
    mov         edx, POINTER [divisors]
shun-iwasawa 82a8f5
    mov         edi, JCOEFPTR [coef_block]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vmovdqu     ymm4, [YMMBLOCK(0,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    vmovdqu     ymm5, [YMMBLOCK(2,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    vmovdqu     ymm6, [YMMBLOCK(4,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    vmovdqu     ymm7, [YMMBLOCK(6,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    vpabsw      ymm0, ymm4
shun-iwasawa 82a8f5
    vpabsw      ymm1, ymm5
shun-iwasawa 82a8f5
    vpabsw      ymm2, ymm6
shun-iwasawa 82a8f5
    vpabsw      ymm3, ymm7
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vpaddw      ymm0, YMMWORD [CORRECTION(0,0,edx)]  ; correction + roundfactor
shun-iwasawa 82a8f5
    vpaddw      ymm1, YMMWORD [CORRECTION(2,0,edx)]
shun-iwasawa 82a8f5
    vpaddw      ymm2, YMMWORD [CORRECTION(4,0,edx)]
shun-iwasawa 82a8f5
    vpaddw      ymm3, YMMWORD [CORRECTION(6,0,edx)]
shun-iwasawa 82a8f5
    vpmulhuw    ymm0, YMMWORD [RECIPROCAL(0,0,edx)]  ; reciprocal
shun-iwasawa 82a8f5
    vpmulhuw    ymm1, YMMWORD [RECIPROCAL(2,0,edx)]
shun-iwasawa 82a8f5
    vpmulhuw    ymm2, YMMWORD [RECIPROCAL(4,0,edx)]
shun-iwasawa 82a8f5
    vpmulhuw    ymm3, YMMWORD [RECIPROCAL(6,0,edx)]
shun-iwasawa 82a8f5
    vpmulhuw    ymm0, YMMWORD [SCALE(0,0,edx)]       ; scale
shun-iwasawa 82a8f5
    vpmulhuw    ymm1, YMMWORD [SCALE(2,0,edx)]
shun-iwasawa 82a8f5
    vpmulhuw    ymm2, YMMWORD [SCALE(4,0,edx)]
shun-iwasawa 82a8f5
    vpmulhuw    ymm3, YMMWORD [SCALE(6,0,edx)]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vpsignw     ymm0, ymm0, ymm4
shun-iwasawa 82a8f5
    vpsignw     ymm1, ymm1, ymm5
shun-iwasawa 82a8f5
    vpsignw     ymm2, ymm2, ymm6
shun-iwasawa 82a8f5
    vpsignw     ymm3, ymm3, ymm7
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vmovdqu     [YMMBLOCK(0,0,edi,SIZEOF_DCTELEM)], ymm0
shun-iwasawa 82a8f5
    vmovdqu     [YMMBLOCK(2,0,edi,SIZEOF_DCTELEM)], ymm1
shun-iwasawa 82a8f5
    vmovdqu     [YMMBLOCK(4,0,edi,SIZEOF_DCTELEM)], ymm2
shun-iwasawa 82a8f5
    vmovdqu     [YMMBLOCK(6,0,edi,SIZEOF_DCTELEM)], ymm3
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    vzeroupper
shun-iwasawa 82a8f5
    pop         edi
shun-iwasawa 82a8f5
    pop         esi
shun-iwasawa 82a8f5
;   pop         edx                     ; need not be preserved
shun-iwasawa 82a8f5
;   pop         ecx                     ; unused
shun-iwasawa 82a8f5
;   pop         ebx                     ; unused
shun-iwasawa 82a8f5
    pop         ebp
shun-iwasawa 82a8f5
    ret
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
; For some reason, the OS X linker does not honor the request to align the
shun-iwasawa 82a8f5
; segment unless we do this.
shun-iwasawa 82a8f5
    align       32