From 85edeac1ae18442aa7dc43074a2e4ddc31b6a731 Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Sun, 3 Feb 2008 17:14:40 +0100 Subject: [PATCH] Add SSE the other way as well, requiring SSE3. --- qscale.c | 60 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/qscale.c b/qscale.c index d5536bc..6d529bc 100644 --- a/qscale.c +++ b/qscale.c @@ -38,7 +38,7 @@ void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw int size_coeffs = 8; float *coeffs = (float *)malloc(size_coeffs * sizeof(float)); int num_coeffs = 0; - int x, y, sx; + int x, y; double sf = (double)w / (double)nw; double support = (w > nw) ? (3.0 * sf) : (3.0 / sf); @@ -46,6 +46,7 @@ void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw for (x = 0; x < nw; ++x) { int start = ceil(x * sf - support); int end = floor(x * sf + support); + int sx; double sum = 0.0; if (start < 0) { @@ -55,6 +56,19 @@ void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw end = w - 1; } + // round up so we get a multiple of four for the SSE code + int num = (end - start + 1); + if (num % 4 != 0) { + // prefer aligning it if possible + if (start % 4 != 0 && start % 4 <= num % 4) { + num += start % 4; + start -= start % 4; + } + if (num % 4 != 0) { + end += 4 - (num % 4); + } + } + pd[x].start = start; pd[x].end = end; pd[x].startcoeff = num_coeffs; @@ -79,16 +93,35 @@ void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw for (y = 0; y < h; ++y) { float *sptr = pix + y*w; unsigned char *dptr = npix + y*dstride; - unsigned char ch = 0; + unsigned char ch; for (x = 0; x < nw; ++x) { - float acc = 0.0; - float *cf = &coeffs[pd[x].startcoeff]; - unsigned sx; - - for (sx = pd[x].start; sx <= pd[x].end; ++sx) { - acc += sptr[sx] * *cf++; - } + float acc; + static const float low = 0.0, high = 255.0; + asm ( + "pxor %0, %0 \n" + "xor %%eax, %%eax \n" + ".lbl2: \n" + "movups (%2,%%eax),%%xmm1 \n" + "movups (%1,%%eax),%%xmm2 \n" + "mulps %%xmm2,%%xmm1 \n" + "addps %%xmm1,%0 \n" + "addl $16,%%eax \n" + "dec %3 \n" + "jnz .lbl2 \n" + "haddps %0,%0 \n" + "haddps %0,%0 \n" + "maxss %4,%0 \n" + "minss %5,%0 \n" + : "=x" (acc) + : "r" (&coeffs[pd[x].startcoeff]), + "r" (&sptr[pd[x].start]), + "r" ((pd[x].end - pd[x].start + 1)/4), + "m" (low), + "m" (high) + : "eax", "xmm1", "xmm2" + ); +#if 0 if (acc < 0.0) ch = 0; else if (acc > 255.0) @@ -96,7 +129,10 @@ void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw else ch = (unsigned char)acc; *dptr++ = ch; +#endif + *dptr++ = (unsigned char)acc; } + ch = dptr[-1]; for ( ; x < dstride; ++x) { *dptr++ = ch; } @@ -240,9 +276,9 @@ void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh "addps %%xmm6, %%xmm3 \n" /* move along, and loop */ - "addl $4, %2 \n" - "addl %3, %0 \n" - "decl %1 \n" + "add $4, %2 \n" + "add %3, %0 \n" + "dec %1 \n" "jnz .lbl \n" /* store the values */ -- 2.39.2