michael@0: ; michael@0: ; jcqntmmx.asm - sample data conversion and quantization (MMX) michael@0: ; michael@0: ; Copyright 2009 Pierre Ossman for Cendio AB michael@0: ; michael@0: ; Based on michael@0: ; x86 SIMD extension for IJG JPEG library michael@0: ; Copyright (C) 1999-2006, MIYASAKA Masaru. michael@0: ; For conditions of distribution and use, see copyright notice in jsimdext.inc michael@0: ; michael@0: ; This file should be assembled with NASM (Netwide Assembler), michael@0: ; can *not* be assembled with Microsoft's MASM or any compatible michael@0: ; assembler (including Borland's Turbo Assembler). michael@0: ; NASM is available from http://nasm.sourceforge.net/ or michael@0: ; http://sourceforge.net/project/showfiles.php?group_id=6208 michael@0: ; michael@0: ; [TAB8] michael@0: michael@0: %include "jsimdext.inc" michael@0: %include "jdct.inc" michael@0: michael@0: ; -------------------------------------------------------------------------- michael@0: SECTION SEG_TEXT michael@0: BITS 32 michael@0: ; michael@0: ; Load data into workspace, applying unsigned->signed conversion michael@0: ; michael@0: ; GLOBAL(void) michael@0: ; jsimd_convsamp_mmx (JSAMPARRAY sample_data, JDIMENSION start_col, michael@0: ; DCTELEM * workspace); michael@0: ; michael@0: michael@0: %define sample_data ebp+8 ; JSAMPARRAY sample_data michael@0: %define start_col ebp+12 ; JDIMENSION start_col michael@0: %define workspace ebp+16 ; DCTELEM * workspace michael@0: michael@0: align 16 michael@0: global EXTN(jsimd_convsamp_mmx) michael@0: michael@0: EXTN(jsimd_convsamp_mmx): michael@0: push ebp michael@0: mov ebp,esp michael@0: push ebx michael@0: ; push ecx ; need not be preserved michael@0: ; push edx ; need not be preserved michael@0: push esi michael@0: push edi michael@0: michael@0: pxor mm6,mm6 ; mm6=(all 0's) michael@0: pcmpeqw mm7,mm7 michael@0: psllw mm7,7 ; mm7={0xFF80 0xFF80 0xFF80 0xFF80} michael@0: michael@0: mov esi, JSAMPARRAY [sample_data] ; (JSAMPROW *) michael@0: mov eax, JDIMENSION [start_col] michael@0: mov edi, POINTER [workspace] ; (DCTELEM *) michael@0: mov ecx, DCTSIZE/4 michael@0: alignx 16,7 michael@0: .convloop: michael@0: mov ebx, JSAMPROW [esi+0*SIZEOF_JSAMPROW] ; (JSAMPLE *) michael@0: mov edx, JSAMPROW [esi+1*SIZEOF_JSAMPROW] ; (JSAMPLE *) michael@0: michael@0: movq mm0, MMWORD [ebx+eax*SIZEOF_JSAMPLE] ; mm0=(01234567) michael@0: movq mm1, MMWORD [edx+eax*SIZEOF_JSAMPLE] ; mm1=(89ABCDEF) michael@0: michael@0: mov ebx, JSAMPROW [esi+2*SIZEOF_JSAMPROW] ; (JSAMPLE *) michael@0: mov edx, JSAMPROW [esi+3*SIZEOF_JSAMPROW] ; (JSAMPLE *) michael@0: michael@0: movq mm2, MMWORD [ebx+eax*SIZEOF_JSAMPLE] ; mm2=(GHIJKLMN) michael@0: movq mm3, MMWORD [edx+eax*SIZEOF_JSAMPLE] ; mm3=(OPQRSTUV) michael@0: michael@0: movq mm4,mm0 michael@0: punpcklbw mm0,mm6 ; mm0=(0123) michael@0: punpckhbw mm4,mm6 ; mm4=(4567) michael@0: movq mm5,mm1 michael@0: punpcklbw mm1,mm6 ; mm1=(89AB) michael@0: punpckhbw mm5,mm6 ; mm5=(CDEF) michael@0: michael@0: paddw mm0,mm7 michael@0: paddw mm4,mm7 michael@0: paddw mm1,mm7 michael@0: paddw mm5,mm7 michael@0: michael@0: movq MMWORD [MMBLOCK(0,0,edi,SIZEOF_DCTELEM)], mm0 michael@0: movq MMWORD [MMBLOCK(0,1,edi,SIZEOF_DCTELEM)], mm4 michael@0: movq MMWORD [MMBLOCK(1,0,edi,SIZEOF_DCTELEM)], mm1 michael@0: movq MMWORD [MMBLOCK(1,1,edi,SIZEOF_DCTELEM)], mm5 michael@0: michael@0: movq mm0,mm2 michael@0: punpcklbw mm2,mm6 ; mm2=(GHIJ) michael@0: punpckhbw mm0,mm6 ; mm0=(KLMN) michael@0: movq mm4,mm3 michael@0: punpcklbw mm3,mm6 ; mm3=(OPQR) michael@0: punpckhbw mm4,mm6 ; mm4=(STUV) michael@0: michael@0: paddw mm2,mm7 michael@0: paddw mm0,mm7 michael@0: paddw mm3,mm7 michael@0: paddw mm4,mm7 michael@0: michael@0: movq MMWORD [MMBLOCK(2,0,edi,SIZEOF_DCTELEM)], mm2 michael@0: movq MMWORD [MMBLOCK(2,1,edi,SIZEOF_DCTELEM)], mm0 michael@0: movq MMWORD [MMBLOCK(3,0,edi,SIZEOF_DCTELEM)], mm3 michael@0: movq MMWORD [MMBLOCK(3,1,edi,SIZEOF_DCTELEM)], mm4 michael@0: michael@0: add esi, byte 4*SIZEOF_JSAMPROW michael@0: add edi, byte 4*DCTSIZE*SIZEOF_DCTELEM michael@0: dec ecx michael@0: jnz short .convloop michael@0: michael@0: emms ; empty MMX state michael@0: michael@0: pop edi michael@0: pop esi michael@0: ; pop edx ; need not be preserved michael@0: ; pop ecx ; need not be preserved michael@0: pop ebx michael@0: pop ebp michael@0: ret michael@0: michael@0: ; -------------------------------------------------------------------------- michael@0: ; michael@0: ; Quantize/descale the coefficients, and store into coef_block michael@0: ; michael@0: ; This implementation is based on an algorithm described in michael@0: ; "How to optimize for the Pentium family of microprocessors" michael@0: ; (http://www.agner.org/assem/). michael@0: ; michael@0: ; GLOBAL(void) michael@0: ; jsimd_quantize_mmx (JCOEFPTR coef_block, DCTELEM * divisors, michael@0: ; DCTELEM * workspace); michael@0: ; michael@0: michael@0: %define RECIPROCAL(m,n,b) MMBLOCK(DCTSIZE*0+(m),(n),(b),SIZEOF_DCTELEM) michael@0: %define CORRECTION(m,n,b) MMBLOCK(DCTSIZE*1+(m),(n),(b),SIZEOF_DCTELEM) michael@0: %define SCALE(m,n,b) MMBLOCK(DCTSIZE*2+(m),(n),(b),SIZEOF_DCTELEM) michael@0: %define SHIFT(m,n,b) MMBLOCK(DCTSIZE*3+(m),(n),(b),SIZEOF_DCTELEM) michael@0: michael@0: %define coef_block ebp+8 ; JCOEFPTR coef_block michael@0: %define divisors ebp+12 ; DCTELEM * divisors michael@0: %define workspace ebp+16 ; DCTELEM * workspace michael@0: michael@0: align 16 michael@0: global EXTN(jsimd_quantize_mmx) michael@0: michael@0: EXTN(jsimd_quantize_mmx): michael@0: push ebp michael@0: mov ebp,esp michael@0: ; push ebx ; unused michael@0: ; push ecx ; unused michael@0: ; push edx ; need not be preserved michael@0: push esi michael@0: push edi michael@0: michael@0: mov esi, POINTER [workspace] michael@0: mov edx, POINTER [divisors] michael@0: mov edi, JCOEFPTR [coef_block] michael@0: mov ah, 2 michael@0: alignx 16,7 michael@0: .quantloop1: michael@0: mov al, DCTSIZE2/8/2 michael@0: alignx 16,7 michael@0: .quantloop2: michael@0: movq mm2, MMWORD [MMBLOCK(0,0,esi,SIZEOF_DCTELEM)] michael@0: movq mm3, MMWORD [MMBLOCK(0,1,esi,SIZEOF_DCTELEM)] michael@0: michael@0: movq mm0,mm2 michael@0: movq mm1,mm3 michael@0: michael@0: psraw mm2,(WORD_BIT-1) ; -1 if value < 0, 0 otherwise michael@0: psraw mm3,(WORD_BIT-1) michael@0: michael@0: pxor mm0,mm2 ; val = -val michael@0: pxor mm1,mm3 michael@0: psubw mm0,mm2 michael@0: psubw mm1,mm3 michael@0: michael@0: ; michael@0: ; MMX is an annoyingly crappy instruction set. It has two michael@0: ; misfeatures that are causing problems here: michael@0: ; michael@0: ; - All multiplications are signed. michael@0: ; michael@0: ; - The second operand for the shifts is not treated as packed. michael@0: ; michael@0: ; michael@0: ; We work around the first problem by implementing this algorithm: michael@0: ; michael@0: ; unsigned long unsigned_multiply(unsigned short x, unsigned short y) michael@0: ; { michael@0: ; enum { SHORT_BIT = 16 }; michael@0: ; signed short sx = (signed short) x; michael@0: ; signed short sy = (signed short) y; michael@0: ; signed long sz; michael@0: ; michael@0: ; sz = (long) sx * (long) sy; /* signed multiply */ michael@0: ; michael@0: ; if (sx < 0) sz += (long) sy << SHORT_BIT; michael@0: ; if (sy < 0) sz += (long) sx << SHORT_BIT; michael@0: ; michael@0: ; return (unsigned long) sz; michael@0: ; } michael@0: ; michael@0: ; (note that a negative sx adds _sy_ and vice versa) michael@0: ; michael@0: ; For the second problem, we replace the shift by a multiplication. michael@0: ; Unfortunately that means we have to deal with the signed issue again. michael@0: ; michael@0: michael@0: paddw mm0, MMWORD [CORRECTION(0,0,edx)] ; correction + roundfactor michael@0: paddw mm1, MMWORD [CORRECTION(0,1,edx)] michael@0: michael@0: movq mm4,mm0 ; store current value for later michael@0: movq mm5,mm1 michael@0: pmulhw mm0, MMWORD [RECIPROCAL(0,0,edx)] ; reciprocal michael@0: pmulhw mm1, MMWORD [RECIPROCAL(0,1,edx)] michael@0: paddw mm0,mm4 ; reciprocal is always negative (MSB=1), michael@0: paddw mm1,mm5 ; so we always need to add the initial value michael@0: ; (input value is never negative as we michael@0: ; inverted it at the start of this routine) michael@0: michael@0: ; here it gets a bit tricky as both scale michael@0: ; and mm0/mm1 can be negative michael@0: movq mm6, MMWORD [SCALE(0,0,edx)] ; scale michael@0: movq mm7, MMWORD [SCALE(0,1,edx)] michael@0: movq mm4,mm0 michael@0: movq mm5,mm1 michael@0: pmulhw mm0,mm6 michael@0: pmulhw mm1,mm7 michael@0: michael@0: psraw mm6,(WORD_BIT-1) ; determine if scale is negative michael@0: psraw mm7,(WORD_BIT-1) michael@0: michael@0: pand mm6,mm4 ; and add input if it is michael@0: pand mm7,mm5 michael@0: paddw mm0,mm6 michael@0: paddw mm1,mm7 michael@0: michael@0: psraw mm4,(WORD_BIT-1) ; then check if negative input michael@0: psraw mm5,(WORD_BIT-1) michael@0: michael@0: pand mm4, MMWORD [SCALE(0,0,edx)] ; and add scale if it is michael@0: pand mm5, MMWORD [SCALE(0,1,edx)] michael@0: paddw mm0,mm4 michael@0: paddw mm1,mm5 michael@0: michael@0: pxor mm0,mm2 ; val = -val michael@0: pxor mm1,mm3 michael@0: psubw mm0,mm2 michael@0: psubw mm1,mm3 michael@0: michael@0: movq MMWORD [MMBLOCK(0,0,edi,SIZEOF_DCTELEM)], mm0 michael@0: movq MMWORD [MMBLOCK(0,1,edi,SIZEOF_DCTELEM)], mm1 michael@0: michael@0: add esi, byte 8*SIZEOF_DCTELEM michael@0: add edx, byte 8*SIZEOF_DCTELEM michael@0: add edi, byte 8*SIZEOF_JCOEF michael@0: dec al michael@0: jnz near .quantloop2 michael@0: dec ah michael@0: jnz near .quantloop1 ; to avoid branch misprediction michael@0: michael@0: emms ; empty MMX state michael@0: michael@0: pop edi michael@0: pop esi michael@0: ; pop edx ; need not be preserved michael@0: ; pop ecx ; unused michael@0: ; pop ebx ; unused michael@0: pop ebp michael@0: ret michael@0: michael@0: ; For some reason, the OS X linker does not honor the request to align the michael@0: ; segment unless we do this. michael@0: align 16