Blame thirdparty/libjpeg-turbo/libjpeg-turbo-2.0.6/simd/i386/jquanti-sse2.asm

shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; jquanti.asm - sample data conversion and quantization (SSE2)
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, D. R. Commander.
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_sse2(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_sse2)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_convsamp_sse2):
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
    pxor        xmm6, xmm6              ; xmm6=(all 0's)
shun-iwasawa 82a8f5
    pcmpeqw     xmm7, xmm7
shun-iwasawa 82a8f5
    psllw       xmm7, 7                 ; xmm7={0xFF80 0xFF80 0xFF80 0xFF80 ..}
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
    mov         ecx, DCTSIZE/4
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.convloop:
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
shun-iwasawa 82a8f5
    movq        xmm0, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]  ; xmm0=(01234567)
shun-iwasawa 82a8f5
    movq        xmm1, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]  ; xmm1=(89ABCDEF)
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
shun-iwasawa 82a8f5
    movq        xmm2, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]  ; xmm2=(GHIJKLMN)
shun-iwasawa 82a8f5
    movq        xmm3, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]  ; xmm3=(OPQRSTUV)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    punpcklbw   xmm0, xmm6              ; xmm0=(01234567)
shun-iwasawa 82a8f5
    punpcklbw   xmm1, xmm6              ; xmm1=(89ABCDEF)
shun-iwasawa 82a8f5
    paddw       xmm0, xmm7
shun-iwasawa 82a8f5
    paddw       xmm1, xmm7
shun-iwasawa 82a8f5
    punpcklbw   xmm2, xmm6              ; xmm2=(GHIJKLMN)
shun-iwasawa 82a8f5
    punpcklbw   xmm3, xmm6              ; xmm3=(OPQRSTUV)
shun-iwasawa 82a8f5
    paddw       xmm2, xmm7
shun-iwasawa 82a8f5
    paddw       xmm3, xmm7
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(0,0,edi,SIZEOF_DCTELEM)], xmm0
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(1,0,edi,SIZEOF_DCTELEM)], xmm1
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(2,0,edi,SIZEOF_DCTELEM)], xmm2
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(3,0,edi,SIZEOF_DCTELEM)], xmm3
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         esi, byte 4*SIZEOF_JSAMPROW
shun-iwasawa 82a8f5
    add         edi, byte 4*DCTSIZE*SIZEOF_DCTELEM
shun-iwasawa 82a8f5
    dec         ecx
shun-iwasawa 82a8f5
    jnz         short .convloop
shun-iwasawa 82a8f5
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_sse2(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
  XMMBLOCK(DCTSIZE * 0 + (m), (n), (b), SIZEOF_DCTELEM)
shun-iwasawa 82a8f5
%define CORRECTION(m, n, b) \
shun-iwasawa 82a8f5
  XMMBLOCK(DCTSIZE * 1 + (m), (n), (b), SIZEOF_DCTELEM)
shun-iwasawa 82a8f5
%define SCALE(m, n, b) \
shun-iwasawa 82a8f5
  XMMBLOCK(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_sse2)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_quantize_sse2):
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
    mov         eax, DCTSIZE2/32
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.quantloop:
shun-iwasawa 82a8f5
    movdqa      xmm4, XMMWORD [XMMBLOCK(0,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    movdqa      xmm5, XMMWORD [XMMBLOCK(1,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    movdqa      xmm6, XMMWORD [XMMBLOCK(2,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    movdqa      xmm7, XMMWORD [XMMBLOCK(3,0,esi,SIZEOF_DCTELEM)]
shun-iwasawa 82a8f5
    movdqa      xmm0, xmm4
shun-iwasawa 82a8f5
    movdqa      xmm1, xmm5
shun-iwasawa 82a8f5
    movdqa      xmm2, xmm6
shun-iwasawa 82a8f5
    movdqa      xmm3, xmm7
shun-iwasawa 82a8f5
    psraw       xmm4, (WORD_BIT-1)
shun-iwasawa 82a8f5
    psraw       xmm5, (WORD_BIT-1)
shun-iwasawa 82a8f5
    psraw       xmm6, (WORD_BIT-1)
shun-iwasawa 82a8f5
    psraw       xmm7, (WORD_BIT-1)
shun-iwasawa 82a8f5
    pxor        xmm0, xmm4
shun-iwasawa 82a8f5
    pxor        xmm1, xmm5
shun-iwasawa 82a8f5
    pxor        xmm2, xmm6
shun-iwasawa 82a8f5
    pxor        xmm3, xmm7
shun-iwasawa 82a8f5
    psubw       xmm0, xmm4              ; if (xmm0 < 0) xmm0 = -xmm0;
shun-iwasawa 82a8f5
    psubw       xmm1, xmm5              ; if (xmm1 < 0) xmm1 = -xmm1;
shun-iwasawa 82a8f5
    psubw       xmm2, xmm6              ; if (xmm2 < 0) xmm2 = -xmm2;
shun-iwasawa 82a8f5
    psubw       xmm3, xmm7              ; if (xmm3 < 0) xmm3 = -xmm3;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    paddw       xmm0, XMMWORD [CORRECTION(0,0,edx)]  ; correction + roundfactor
shun-iwasawa 82a8f5
    paddw       xmm1, XMMWORD [CORRECTION(1,0,edx)]
shun-iwasawa 82a8f5
    paddw       xmm2, XMMWORD [CORRECTION(2,0,edx)]
shun-iwasawa 82a8f5
    paddw       xmm3, XMMWORD [CORRECTION(3,0,edx)]
shun-iwasawa 82a8f5
    pmulhuw     xmm0, XMMWORD [RECIPROCAL(0,0,edx)]  ; reciprocal
shun-iwasawa 82a8f5
    pmulhuw     xmm1, XMMWORD [RECIPROCAL(1,0,edx)]
shun-iwasawa 82a8f5
    pmulhuw     xmm2, XMMWORD [RECIPROCAL(2,0,edx)]
shun-iwasawa 82a8f5
    pmulhuw     xmm3, XMMWORD [RECIPROCAL(3,0,edx)]
shun-iwasawa 82a8f5
    pmulhuw     xmm0, XMMWORD [SCALE(0,0,edx)]       ; scale
shun-iwasawa 82a8f5
    pmulhuw     xmm1, XMMWORD [SCALE(1,0,edx)]
shun-iwasawa 82a8f5
    pmulhuw     xmm2, XMMWORD [SCALE(2,0,edx)]
shun-iwasawa 82a8f5
    pmulhuw     xmm3, XMMWORD [SCALE(3,0,edx)]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pxor        xmm0, xmm4
shun-iwasawa 82a8f5
    pxor        xmm1, xmm5
shun-iwasawa 82a8f5
    pxor        xmm2, xmm6
shun-iwasawa 82a8f5
    pxor        xmm3, xmm7
shun-iwasawa 82a8f5
    psubw       xmm0, xmm4
shun-iwasawa 82a8f5
    psubw       xmm1, xmm5
shun-iwasawa 82a8f5
    psubw       xmm2, xmm6
shun-iwasawa 82a8f5
    psubw       xmm3, xmm7
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(0,0,edi,SIZEOF_DCTELEM)], xmm0
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(1,0,edi,SIZEOF_DCTELEM)], xmm1
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(2,0,edi,SIZEOF_DCTELEM)], xmm2
shun-iwasawa 82a8f5
    movdqa      XMMWORD [XMMBLOCK(3,0,edi,SIZEOF_DCTELEM)], xmm3
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         esi, byte 32*SIZEOF_DCTELEM
shun-iwasawa 82a8f5
    add         edx, byte 32*SIZEOF_DCTELEM
shun-iwasawa 82a8f5
    add         edi, byte 32*SIZEOF_JCOEF
shun-iwasawa 82a8f5
    dec         eax
shun-iwasawa 82a8f5
    jnz         near .quantloop
shun-iwasawa 82a8f5
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