Blame thirdparty/libjpeg-turbo/libjpeg-turbo-2.0.6/simd/x86_64/jcsample-sse2.asm

shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; jcsample.asm - downsampling (64-bit 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) 2009, 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
shun-iwasawa 82a8f5
; --------------------------------------------------------------------------
shun-iwasawa 82a8f5
    SECTION     SEG_TEXT
shun-iwasawa 82a8f5
    BITS        64
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; Downsample pixel values of a single component.
shun-iwasawa 82a8f5
; This version handles the common case of 2:1 horizontal and 1:1 vertical,
shun-iwasawa 82a8f5
; without smoothing.
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; GLOBAL(void)
shun-iwasawa 82a8f5
; jsimd_h2v1_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
shun-iwasawa 82a8f5
;                            JDIMENSION v_samp_factor,
shun-iwasawa 82a8f5
;                            JDIMENSION width_in_blocks, JSAMPARRAY input_data,
shun-iwasawa 82a8f5
;                            JSAMPARRAY output_data);
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
; r10d = JDIMENSION image_width
shun-iwasawa 82a8f5
; r11 = int max_v_samp_factor
shun-iwasawa 82a8f5
; r12d = JDIMENSION v_samp_factor
shun-iwasawa 82a8f5
; r13d = JDIMENSION width_in_blocks
shun-iwasawa 82a8f5
; r14 = JSAMPARRAY input_data
shun-iwasawa 82a8f5
; r15 = JSAMPARRAY output_data
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    align       32
shun-iwasawa 82a8f5
    GLOBAL_FUNCTION(jsimd_h2v1_downsample_sse2)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_h2v1_downsample_sse2):
shun-iwasawa 82a8f5
    push        rbp
shun-iwasawa 82a8f5
    mov         rax, rsp
shun-iwasawa 82a8f5
    mov         rbp, rsp
shun-iwasawa 82a8f5
    collect_args 6
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         ecx, r13d
shun-iwasawa 82a8f5
    shl         rcx, 3                  ; imul rcx,DCTSIZE (rcx = output_cols)
shun-iwasawa 82a8f5
    jz          near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edx, r10d
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- expand_right_edge
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    push        rcx
shun-iwasawa 82a8f5
    shl         rcx, 1                  ; output_cols * 2
shun-iwasawa 82a8f5
    sub         rcx, rdx
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rax, r11
shun-iwasawa 82a8f5
    test        rax, rax
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cld
shun-iwasawa 82a8f5
    mov         rsi, r14                ; input_data
shun-iwasawa 82a8f5
.expandloop:
shun-iwasawa 82a8f5
    push        rax
shun-iwasawa 82a8f5
    push        rcx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rdi, JSAMPROW [rsi]
shun-iwasawa 82a8f5
    add         rdi, rdx
shun-iwasawa 82a8f5
    mov         al, JSAMPLE [rdi-1]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    rep stosb
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         rcx
shun-iwasawa 82a8f5
    pop         rax
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         rsi, byte SIZEOF_JSAMPROW
shun-iwasawa 82a8f5
    dec         rax
shun-iwasawa 82a8f5
    jg          short .expandloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.expand_end:
shun-iwasawa 82a8f5
    pop         rcx                     ; output_cols
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- h2v1_downsample
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         eax, r12d               ; rowctr
shun-iwasawa 82a8f5
    test        eax, eax
shun-iwasawa 82a8f5
    jle         near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rdx, 0x00010000         ; bias pattern
shun-iwasawa 82a8f5
    movd        xmm7, edx
shun-iwasawa 82a8f5
    pcmpeqw     xmm6, xmm6
shun-iwasawa 82a8f5
    pshufd      xmm7, xmm7, 0x00        ; xmm7={0, 1, 0, 1, 0, 1, 0, 1}
shun-iwasawa 82a8f5
    psrlw       xmm6, BYTE_BIT          ; xmm6={0xFF 0x00 0xFF 0x00 ..}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rsi, r14                ; input_data
shun-iwasawa 82a8f5
    mov         rdi, r15                ; output_data
shun-iwasawa 82a8f5
.rowloop:
shun-iwasawa 82a8f5
    push        rcx
shun-iwasawa 82a8f5
    push        rdi
shun-iwasawa 82a8f5
    push        rsi
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rsi, JSAMPROW [rsi]     ; inptr
shun-iwasawa 82a8f5
    mov         rdi, JSAMPROW [rdi]     ; outptr
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cmp         rcx, byte SIZEOF_XMMWORD
shun-iwasawa 82a8f5
    jae         short .columnloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.columnloop_r8:
shun-iwasawa 82a8f5
    movdqa      xmm0, XMMWORD [rsi+0*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
    pxor        xmm1, xmm1
shun-iwasawa 82a8f5
    mov         rcx, SIZEOF_XMMWORD
shun-iwasawa 82a8f5
    jmp         short .downsample
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.columnloop:
shun-iwasawa 82a8f5
    movdqa      xmm0, XMMWORD [rsi+0*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
    movdqa      xmm1, XMMWORD [rsi+1*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.downsample:
shun-iwasawa 82a8f5
    movdqa      xmm2, xmm0
shun-iwasawa 82a8f5
    movdqa      xmm3, xmm1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pand        xmm0, xmm6
shun-iwasawa 82a8f5
    psrlw       xmm2, BYTE_BIT
shun-iwasawa 82a8f5
    pand        xmm1, xmm6
shun-iwasawa 82a8f5
    psrlw       xmm3, BYTE_BIT
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    paddw       xmm0, xmm2
shun-iwasawa 82a8f5
    paddw       xmm1, xmm3
shun-iwasawa 82a8f5
    paddw       xmm0, xmm7
shun-iwasawa 82a8f5
    paddw       xmm1, xmm7
shun-iwasawa 82a8f5
    psrlw       xmm0, 1
shun-iwasawa 82a8f5
    psrlw       xmm1, 1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    packuswb    xmm0, xmm1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movdqa      XMMWORD [rdi+0*SIZEOF_XMMWORD], xmm0
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    sub         rcx, byte SIZEOF_XMMWORD    ; outcol
shun-iwasawa 82a8f5
    add         rsi, byte 2*SIZEOF_XMMWORD  ; inptr
shun-iwasawa 82a8f5
    add         rdi, byte 1*SIZEOF_XMMWORD  ; outptr
shun-iwasawa 82a8f5
    cmp         rcx, byte SIZEOF_XMMWORD
shun-iwasawa 82a8f5
    jae         short .columnloop
shun-iwasawa 82a8f5
    test        rcx, rcx
shun-iwasawa 82a8f5
    jnz         short .columnloop_r8
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         rsi
shun-iwasawa 82a8f5
    pop         rdi
shun-iwasawa 82a8f5
    pop         rcx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         rsi, byte SIZEOF_JSAMPROW  ; input_data
shun-iwasawa 82a8f5
    add         rdi, byte SIZEOF_JSAMPROW  ; output_data
shun-iwasawa 82a8f5
    dec         rax                        ; rowctr
shun-iwasawa 82a8f5
    jg          near .rowloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.return:
shun-iwasawa 82a8f5
    uncollect_args 6
shun-iwasawa 82a8f5
    pop         rbp
shun-iwasawa 82a8f5
    ret
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
; --------------------------------------------------------------------------
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; Downsample pixel values of a single component.
shun-iwasawa 82a8f5
; This version handles the standard case of 2:1 horizontal and 2:1 vertical,
shun-iwasawa 82a8f5
; without smoothing.
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; GLOBAL(void)
shun-iwasawa 82a8f5
; jsimd_h2v2_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
shun-iwasawa 82a8f5
;                            JDIMENSION v_samp_factor,
shun-iwasawa 82a8f5
;                            JDIMENSION width_in_blocks, JSAMPARRAY input_data,
shun-iwasawa 82a8f5
;                            JSAMPARRAY output_data);
shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
; r10d = JDIMENSION image_width
shun-iwasawa 82a8f5
; r11 = int max_v_samp_factor
shun-iwasawa 82a8f5
; r12d = JDIMENSION v_samp_factor
shun-iwasawa 82a8f5
; r13d = JDIMENSION width_in_blocks
shun-iwasawa 82a8f5
; r14 = JSAMPARRAY input_data
shun-iwasawa 82a8f5
; r15 = JSAMPARRAY output_data
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    align       32
shun-iwasawa 82a8f5
    GLOBAL_FUNCTION(jsimd_h2v2_downsample_sse2)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_h2v2_downsample_sse2):
shun-iwasawa 82a8f5
    push        rbp
shun-iwasawa 82a8f5
    mov         rax, rsp
shun-iwasawa 82a8f5
    mov         rbp, rsp
shun-iwasawa 82a8f5
    collect_args 6
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         ecx, r13d
shun-iwasawa 82a8f5
    shl         rcx, 3                  ; imul rcx,DCTSIZE (rcx = output_cols)
shun-iwasawa 82a8f5
    jz          near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edx, r10d
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- expand_right_edge
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    push        rcx
shun-iwasawa 82a8f5
    shl         rcx, 1                  ; output_cols * 2
shun-iwasawa 82a8f5
    sub         rcx, rdx
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rax, r11
shun-iwasawa 82a8f5
    test        rax, rax
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cld
shun-iwasawa 82a8f5
    mov         rsi, r14                ; input_data
shun-iwasawa 82a8f5
.expandloop:
shun-iwasawa 82a8f5
    push        rax
shun-iwasawa 82a8f5
    push        rcx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rdi, JSAMPROW [rsi]
shun-iwasawa 82a8f5
    add         rdi, rdx
shun-iwasawa 82a8f5
    mov         al, JSAMPLE [rdi-1]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    rep stosb
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         rcx
shun-iwasawa 82a8f5
    pop         rax
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         rsi, byte SIZEOF_JSAMPROW
shun-iwasawa 82a8f5
    dec         rax
shun-iwasawa 82a8f5
    jg          short .expandloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.expand_end:
shun-iwasawa 82a8f5
    pop         rcx                     ; output_cols
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- h2v2_downsample
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         eax, r12d               ; rowctr
shun-iwasawa 82a8f5
    test        rax, rax
shun-iwasawa 82a8f5
    jle         near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rdx, 0x00020001         ; bias pattern
shun-iwasawa 82a8f5
    movd        xmm7, edx
shun-iwasawa 82a8f5
    pcmpeqw     xmm6, xmm6
shun-iwasawa 82a8f5
    pshufd      xmm7, xmm7, 0x00        ; xmm7={1, 2, 1, 2, 1, 2, 1, 2}
shun-iwasawa 82a8f5
    psrlw       xmm6, BYTE_BIT          ; xmm6={0xFF 0x00 0xFF 0x00 ..}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rsi, r14                ; input_data
shun-iwasawa 82a8f5
    mov         rdi, r15                ; output_data
shun-iwasawa 82a8f5
.rowloop:
shun-iwasawa 82a8f5
    push        rcx
shun-iwasawa 82a8f5
    push        rdi
shun-iwasawa 82a8f5
    push        rsi
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         rdx, JSAMPROW [rsi+0*SIZEOF_JSAMPROW]  ; inptr0
shun-iwasawa 82a8f5
    mov         rsi, JSAMPROW [rsi+1*SIZEOF_JSAMPROW]  ; inptr1
shun-iwasawa 82a8f5
    mov         rdi, JSAMPROW [rdi]                    ; outptr
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cmp         rcx, byte SIZEOF_XMMWORD
shun-iwasawa 82a8f5
    jae         short .columnloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.columnloop_r8:
shun-iwasawa 82a8f5
    movdqa      xmm0, XMMWORD [rdx+0*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
    movdqa      xmm1, XMMWORD [rsi+0*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
    pxor        xmm2, xmm2
shun-iwasawa 82a8f5
    pxor        xmm3, xmm3
shun-iwasawa 82a8f5
    mov         rcx, SIZEOF_XMMWORD
shun-iwasawa 82a8f5
    jmp         short .downsample
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.columnloop:
shun-iwasawa 82a8f5
    movdqa      xmm0, XMMWORD [rdx+0*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
    movdqa      xmm1, XMMWORD [rsi+0*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
    movdqa      xmm2, XMMWORD [rdx+1*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
    movdqa      xmm3, XMMWORD [rsi+1*SIZEOF_XMMWORD]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.downsample:
shun-iwasawa 82a8f5
    movdqa      xmm4, xmm0
shun-iwasawa 82a8f5
    movdqa      xmm5, xmm1
shun-iwasawa 82a8f5
    pand        xmm0, xmm6
shun-iwasawa 82a8f5
    psrlw       xmm4, BYTE_BIT
shun-iwasawa 82a8f5
    pand        xmm1, xmm6
shun-iwasawa 82a8f5
    psrlw       xmm5, BYTE_BIT
shun-iwasawa 82a8f5
    paddw       xmm0, xmm4
shun-iwasawa 82a8f5
    paddw       xmm1, xmm5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movdqa      xmm4, xmm2
shun-iwasawa 82a8f5
    movdqa      xmm5, xmm3
shun-iwasawa 82a8f5
    pand        xmm2, xmm6
shun-iwasawa 82a8f5
    psrlw       xmm4, BYTE_BIT
shun-iwasawa 82a8f5
    pand        xmm3, xmm6
shun-iwasawa 82a8f5
    psrlw       xmm5, BYTE_BIT
shun-iwasawa 82a8f5
    paddw       xmm2, xmm4
shun-iwasawa 82a8f5
    paddw       xmm3, xmm5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    paddw       xmm0, xmm1
shun-iwasawa 82a8f5
    paddw       xmm2, xmm3
shun-iwasawa 82a8f5
    paddw       xmm0, xmm7
shun-iwasawa 82a8f5
    paddw       xmm2, xmm7
shun-iwasawa 82a8f5
    psrlw       xmm0, 2
shun-iwasawa 82a8f5
    psrlw       xmm2, 2
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    packuswb    xmm0, xmm2
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movdqa      XMMWORD [rdi+0*SIZEOF_XMMWORD], xmm0
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    sub         rcx, byte SIZEOF_XMMWORD    ; outcol
shun-iwasawa 82a8f5
    add         rdx, byte 2*SIZEOF_XMMWORD  ; inptr0
shun-iwasawa 82a8f5
    add         rsi, byte 2*SIZEOF_XMMWORD  ; inptr1
shun-iwasawa 82a8f5
    add         rdi, byte 1*SIZEOF_XMMWORD  ; outptr
shun-iwasawa 82a8f5
    cmp         rcx, byte SIZEOF_XMMWORD
shun-iwasawa 82a8f5
    jae         near .columnloop
shun-iwasawa 82a8f5
    test        rcx, rcx
shun-iwasawa 82a8f5
    jnz         near .columnloop_r8
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         rsi
shun-iwasawa 82a8f5
    pop         rdi
shun-iwasawa 82a8f5
    pop         rcx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         rsi, byte 2*SIZEOF_JSAMPROW  ; input_data
shun-iwasawa 82a8f5
    add         rdi, byte 1*SIZEOF_JSAMPROW  ; output_data
shun-iwasawa 82a8f5
    dec         rax                          ; rowctr
shun-iwasawa 82a8f5
    jg          near .rowloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.return:
shun-iwasawa 82a8f5
    uncollect_args 6
shun-iwasawa 82a8f5
    pop         rbp
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