9front - general discussion about 9front
 help / color / mirror / Atom feed
From: "José Miguel Sánchez García" <soy.jmi2k@gmail.com>
To: 9front@9front.org
Subject: [9front] resample: improve performance
Date: Fri, 23 Apr 2021 15:34:56 +0200	[thread overview]
Message-ID: <CAA85C84S2_BficRn+6c3nzevZKg7SGFS7Znb02+kh0VucYtLrA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 651 bytes --]

Resample is well known for taking a long time to resize an image. This
patch brings an important performance boost (in my test image, time
was reduced from ~2850ms to ~500ms). It does that by extracting FP
multiplication and division out of the innermost loop of
resamplex/resampley.

The results differ slightly from the current implementation: in my
test: ~0.3% of the bytes had a ±2 difference in their value, which I
attribute to rounding errors. I'm personally not concerned with that
deviation, given the performance gains. However, I recommend testing
it just to be sure I didn't overlook anything.

José Miguel Sánchez García

[-- Attachment #2: resample-optimize-fpmath.diff --]
[-- Type: application/octet-stream, Size: 1261 bytes --]

--- a/sys/src/cmd/resample.c
+++ b/sys/src/cmd/resample.c
@@ -67,10 +67,10 @@
 resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
 {
 	int i, x, k;
-	double X, xx, v, rat;
+	double X, xx, v, rat, rato10;
 
-
 	rat = (double)inx/(double)outx;
+	rato10 = rat/10.;
 	for(x=0; x<outx; x++){
 		if(inx == outx){
 			/* don't resample if size unchanged */
@@ -79,8 +79,8 @@
 		}
 		v = 0.0;
 		X = x*rat;
+		xx = X + rato10*(-K2);
 		for(k=-K2; k<=K2; k++){
-			xx = X + rat*k/10.;
 			i = xx;
 			if(i < 0)
 				i = 0;
@@ -87,6 +87,7 @@
 			if(i >= inx)
 				i = inx-1;
 			v += in[off+i*d] * K[K2+k];
+			xx += rato10;
 		}
 		out[off+x*d] = v;
 	}
@@ -96,9 +97,10 @@
 resampley(uchar **in, int off, int iny, uchar **out, int outy)
 {
 	int y, i, k;
-	double Y, yy, v, rat;
+	double Y, yy, v, rat, rato10;
 
 	rat = (double)iny/(double)outy;
+	rato10 = rat/10.;
 	for(y=0; y<outy; y++){
 		if(iny == outy){
 			/* don't resample if size unchanged */
@@ -107,8 +109,8 @@
 		}
 		v = 0.0;
 		Y = y*rat;
+		yy = Y + rato10*(-K2);
 		for(k=-K2; k<=K2; k++){
-			yy = Y + rat*k/10.;
 			i = yy;
 			if(i < 0)
 				i = 0;
@@ -115,6 +117,7 @@
 			if(i >= iny)
 				i = iny-1;
 			v += in[i][off] * K[K2+k];
+			yy += rato10;
 		}
 		out[y][off] = v;
 	}


             reply	other threads:[~2021-04-23 13:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-23 13:34 José Miguel Sánchez García [this message]
2021-04-25 10:13 ` cinap_lenrek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAA85C84S2_BficRn+6c3nzevZKg7SGFS7Znb02+kh0VucYtLrA@mail.gmail.com \
    --to=soy.jmi2k@gmail.com \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).