double sinc(double x)
{
- // This is bad for very small x, should use power series instead.
- if (x == 0.0)
- return 1.0;
- else
+ static const double eps = 2.22045e-016;
+ static const double cutoff = sqrt(sqrt(eps));
+
+ if (abs(x) < cutoff) {
+ // For small |x|, use Taylor series instead
+ const double x2 = x * x;
+ const double x4 = x2 * x2;
+
+ return 1.0 - x2 / 6.0 + x4 / 120.0;
+ } else {
return sin(x) / x;
+ }
}
double lanczos_tap(double x)
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);
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) {
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;
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)
else
ch = (unsigned char)acc;
*dptr++ = ch;
+#endif
+ *dptr++ = (unsigned char)acc;
}
+ ch = dptr[-1];
for ( ; x < dstride; ++x) {
*dptr++ = ch;
}
"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 */