shun-iwasawa 82a8f5
;
shun-iwasawa 82a8f5
; jcsample.asm - downsampling (MMX)
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
shun-iwasawa 82a8f5
; --------------------------------------------------------------------------
shun-iwasawa 82a8f5
    SECTION     SEG_TEXT
shun-iwasawa 82a8f5
    BITS        32
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_mmx(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
%define img_width(b)    (b) + 8         ; JDIMENSION image_width
shun-iwasawa 82a8f5
%define max_v_samp(b)   (b) + 12        ; int max_v_samp_factor
shun-iwasawa 82a8f5
%define v_samp(b)       (b) + 16        ; JDIMENSION v_samp_factor
shun-iwasawa 82a8f5
%define width_blks(b)   (b) + 20        ; JDIMENSION width_in_blocks
shun-iwasawa 82a8f5
%define input_data(b)   (b) + 24        ; JSAMPARRAY input_data
shun-iwasawa 82a8f5
%define output_data(b)  (b) + 28        ; JSAMPARRAY output_data
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    align       32
shun-iwasawa 82a8f5
    GLOBAL_FUNCTION(jsimd_h2v1_downsample_mmx)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_h2v1_downsample_mmx):
shun-iwasawa 82a8f5
    push        ebp
shun-iwasawa 82a8f5
    mov         ebp, esp
shun-iwasawa 82a8f5
;   push        ebx                     ; unused
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         ecx, JDIMENSION [width_blks(ebp)]
shun-iwasawa 82a8f5
    shl         ecx, 3                  ; imul ecx,DCTSIZE (ecx = output_cols)
shun-iwasawa 82a8f5
    jz          near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edx, JDIMENSION [img_width(ebp)]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- expand_right_edge
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    push        ecx
shun-iwasawa 82a8f5
    shl         ecx, 1                  ; output_cols * 2
shun-iwasawa 82a8f5
    sub         ecx, edx
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         eax, INT [max_v_samp(ebp)]
shun-iwasawa 82a8f5
    test        eax, eax
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cld
shun-iwasawa 82a8f5
    mov         esi, JSAMPARRAY [input_data(ebp)]  ; input_data
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.expandloop:
shun-iwasawa 82a8f5
    push        eax
shun-iwasawa 82a8f5
    push        ecx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edi, JSAMPROW [esi]
shun-iwasawa 82a8f5
    add         edi, edx
shun-iwasawa 82a8f5
    mov         al, JSAMPLE [edi-1]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    rep stosb
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         ecx
shun-iwasawa 82a8f5
    pop         eax
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         esi, byte SIZEOF_JSAMPROW
shun-iwasawa 82a8f5
    dec         eax
shun-iwasawa 82a8f5
    jg          short .expandloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.expand_end:
shun-iwasawa 82a8f5
    pop         ecx                     ; output_cols
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- h2v1_downsample
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         eax, JDIMENSION [v_samp(ebp)]  ; rowctr
shun-iwasawa 82a8f5
    test        eax, eax
shun-iwasawa 82a8f5
    jle         near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edx, 0x00010000         ; bias pattern
shun-iwasawa 82a8f5
    movd        mm7, edx
shun-iwasawa 82a8f5
    pcmpeqw     mm6, mm6
shun-iwasawa 82a8f5
    punpckldq   mm7, mm7                ; mm7={0, 1, 0, 1}
shun-iwasawa 82a8f5
    psrlw       mm6, BYTE_BIT           ; mm6={0xFF 0x00 0xFF 0x00 ..}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         esi, JSAMPARRAY [input_data(ebp)]   ; input_data
shun-iwasawa 82a8f5
    mov         edi, JSAMPARRAY [output_data(ebp)]  ; output_data
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.rowloop:
shun-iwasawa 82a8f5
    push        ecx
shun-iwasawa 82a8f5
    push        edi
shun-iwasawa 82a8f5
    push        esi
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         esi, JSAMPROW [esi]     ; inptr
shun-iwasawa 82a8f5
    mov         edi, JSAMPROW [edi]     ; outptr
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.columnloop:
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movq        mm0, MMWORD [esi+0*SIZEOF_MMWORD]
shun-iwasawa 82a8f5
    movq        mm1, MMWORD [esi+1*SIZEOF_MMWORD]
shun-iwasawa 82a8f5
    movq        mm2, mm0
shun-iwasawa 82a8f5
    movq        mm3, mm1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pand        mm0, mm6
shun-iwasawa 82a8f5
    psrlw       mm2, BYTE_BIT
shun-iwasawa 82a8f5
    pand        mm1, mm6
shun-iwasawa 82a8f5
    psrlw       mm3, BYTE_BIT
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    paddw       mm0, mm2
shun-iwasawa 82a8f5
    paddw       mm1, mm3
shun-iwasawa 82a8f5
    paddw       mm0, mm7
shun-iwasawa 82a8f5
    paddw       mm1, mm7
shun-iwasawa 82a8f5
    psrlw       mm0, 1
shun-iwasawa 82a8f5
    psrlw       mm1, 1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    packuswb    mm0, mm1
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movq        MMWORD [edi+0*SIZEOF_MMWORD], mm0
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         esi, byte 2*SIZEOF_MMWORD  ; inptr
shun-iwasawa 82a8f5
    add         edi, byte 1*SIZEOF_MMWORD  ; outptr
shun-iwasawa 82a8f5
    sub         ecx, byte SIZEOF_MMWORD    ; outcol
shun-iwasawa 82a8f5
    jnz         short .columnloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         esi
shun-iwasawa 82a8f5
    pop         edi
shun-iwasawa 82a8f5
    pop         ecx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         esi, byte SIZEOF_JSAMPROW  ; input_data
shun-iwasawa 82a8f5
    add         edi, byte SIZEOF_JSAMPROW  ; output_data
shun-iwasawa 82a8f5
    dec         eax                        ; rowctr
shun-iwasawa 82a8f5
    jg          short .rowloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    emms                                ; empty MMX state
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.return:
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                     ; unused
shun-iwasawa 82a8f5
    pop         ebp
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_mmx(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
%define img_width(b)    (b) + 8         ; JDIMENSION image_width
shun-iwasawa 82a8f5
%define max_v_samp(b)   (b) + 12        ; int max_v_samp_factor
shun-iwasawa 82a8f5
%define v_samp(b)       (b) + 16        ; JDIMENSION v_samp_factor
shun-iwasawa 82a8f5
%define width_blks(b)   (b) + 20        ; JDIMENSION width_in_blocks
shun-iwasawa 82a8f5
%define input_data(b)   (b) + 24        ; JSAMPARRAY input_data
shun-iwasawa 82a8f5
%define output_data(b)  (b) + 28        ; JSAMPARRAY output_data
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    align       32
shun-iwasawa 82a8f5
    GLOBAL_FUNCTION(jsimd_h2v2_downsample_mmx)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
EXTN(jsimd_h2v2_downsample_mmx):
shun-iwasawa 82a8f5
    push        ebp
shun-iwasawa 82a8f5
    mov         ebp, esp
shun-iwasawa 82a8f5
;   push        ebx                     ; unused
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         ecx, JDIMENSION [width_blks(ebp)]
shun-iwasawa 82a8f5
    shl         ecx, 3                  ; imul ecx,DCTSIZE (ecx = output_cols)
shun-iwasawa 82a8f5
    jz          near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edx, JDIMENSION [img_width(ebp)]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- expand_right_edge
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    push        ecx
shun-iwasawa 82a8f5
    shl         ecx, 1                  ; output_cols * 2
shun-iwasawa 82a8f5
    sub         ecx, edx
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         eax, INT [max_v_samp(ebp)]
shun-iwasawa 82a8f5
    test        eax, eax
shun-iwasawa 82a8f5
    jle         short .expand_end
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cld
shun-iwasawa 82a8f5
    mov         esi, JSAMPARRAY [input_data(ebp)]  ; input_data
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.expandloop:
shun-iwasawa 82a8f5
    push        eax
shun-iwasawa 82a8f5
    push        ecx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edi, JSAMPROW [esi]
shun-iwasawa 82a8f5
    add         edi, edx
shun-iwasawa 82a8f5
    mov         al, JSAMPLE [edi-1]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    rep stosb
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         ecx
shun-iwasawa 82a8f5
    pop         eax
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         esi, byte SIZEOF_JSAMPROW
shun-iwasawa 82a8f5
    dec         eax
shun-iwasawa 82a8f5
    jg          short .expandloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.expand_end:
shun-iwasawa 82a8f5
    pop         ecx                     ; output_cols
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    ; -- h2v2_downsample
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         eax, JDIMENSION [v_samp(ebp)]  ; rowctr
shun-iwasawa 82a8f5
    test        eax, eax
shun-iwasawa 82a8f5
    jle         near .return
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edx, 0x00020001         ; bias pattern
shun-iwasawa 82a8f5
    movd        mm7, edx
shun-iwasawa 82a8f5
    pcmpeqw     mm6, mm6
shun-iwasawa 82a8f5
    punpckldq   mm7, mm7                ; mm7={1, 2, 1, 2}
shun-iwasawa 82a8f5
    psrlw       mm6, BYTE_BIT           ; mm6={0xFF 0x00 0xFF 0x00 ..}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         esi, JSAMPARRAY [input_data(ebp)]   ; input_data
shun-iwasawa 82a8f5
    mov         edi, JSAMPARRAY [output_data(ebp)]  ; output_data
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.rowloop:
shun-iwasawa 82a8f5
    push        ecx
shun-iwasawa 82a8f5
    push        edi
shun-iwasawa 82a8f5
    push        esi
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mov         edx, JSAMPROW [esi+0*SIZEOF_JSAMPROW]  ; inptr0
shun-iwasawa 82a8f5
    mov         esi, JSAMPROW [esi+1*SIZEOF_JSAMPROW]  ; inptr1
shun-iwasawa 82a8f5
    mov         edi, JSAMPROW [edi]                    ; outptr
shun-iwasawa 82a8f5
    alignx      16, 7
shun-iwasawa 82a8f5
.columnloop:
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movq        mm0, MMWORD [edx+0*SIZEOF_MMWORD]
shun-iwasawa 82a8f5
    movq        mm1, MMWORD [esi+0*SIZEOF_MMWORD]
shun-iwasawa 82a8f5
    movq        mm2, MMWORD [edx+1*SIZEOF_MMWORD]
shun-iwasawa 82a8f5
    movq        mm3, MMWORD [esi+1*SIZEOF_MMWORD]
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movq        mm4, mm0
shun-iwasawa 82a8f5
    movq        mm5, mm1
shun-iwasawa 82a8f5
    pand        mm0, mm6
shun-iwasawa 82a8f5
    psrlw       mm4, BYTE_BIT
shun-iwasawa 82a8f5
    pand        mm1, mm6
shun-iwasawa 82a8f5
    psrlw       mm5, BYTE_BIT
shun-iwasawa 82a8f5
    paddw       mm0, mm4
shun-iwasawa 82a8f5
    paddw       mm1, mm5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movq        mm4, mm2
shun-iwasawa 82a8f5
    movq        mm5, mm3
shun-iwasawa 82a8f5
    pand        mm2, mm6
shun-iwasawa 82a8f5
    psrlw       mm4, BYTE_BIT
shun-iwasawa 82a8f5
    pand        mm3, mm6
shun-iwasawa 82a8f5
    psrlw       mm5, BYTE_BIT
shun-iwasawa 82a8f5
    paddw       mm2, mm4
shun-iwasawa 82a8f5
    paddw       mm3, mm5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    paddw       mm0, mm1
shun-iwasawa 82a8f5
    paddw       mm2, mm3
shun-iwasawa 82a8f5
    paddw       mm0, mm7
shun-iwasawa 82a8f5
    paddw       mm2, mm7
shun-iwasawa 82a8f5
    psrlw       mm0, 2
shun-iwasawa 82a8f5
    psrlw       mm2, 2
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    packuswb    mm0, mm2
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    movq        MMWORD [edi+0*SIZEOF_MMWORD], mm0
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         edx, byte 2*SIZEOF_MMWORD  ; inptr0
shun-iwasawa 82a8f5
    add         esi, byte 2*SIZEOF_MMWORD  ; inptr1
shun-iwasawa 82a8f5
    add         edi, byte 1*SIZEOF_MMWORD  ; outptr
shun-iwasawa 82a8f5
    sub         ecx, byte SIZEOF_MMWORD    ; outcol
shun-iwasawa 82a8f5
    jnz         near .columnloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    pop         esi
shun-iwasawa 82a8f5
    pop         edi
shun-iwasawa 82a8f5
    pop         ecx
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    add         esi, byte 2*SIZEOF_JSAMPROW  ; input_data
shun-iwasawa 82a8f5
    add         edi, byte 1*SIZEOF_JSAMPROW  ; output_data
shun-iwasawa 82a8f5
    dec         eax                          ; rowctr
shun-iwasawa 82a8f5
    jg          near .rowloop
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    emms                                ; empty MMX state
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
.return:
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                     ; 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