gdkcairo: faster pixel conversion without SIMD

Read and write each pixel as one word and multiply red and blue
together.
This commit is contained in:
Daemonratte 2026-10-16 13:47:28 +02:00
commit 6b3e8b172f

View file

@ -17,6 +17,8 @@
* Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "gdkcairo.h"
#include "gdkrgbsimd.h"
#include "gdkdrawable.h"
@ -240,51 +242,50 @@ gdk_cairo_set_source_pixbuf (cairo_t *cr,
p += done * n_channels;
q += done * 4;
/* One word out for each pixel, so that machines without vector
* units, the PowerPC G3 or ARMv5, store once, not three or four
* times; and the unused byte of RGB24 gets a value */
if (n_channels == 3)
{
guchar *end = gdk_pixels + 3 * width;
guint32 *q32 = (guint32 *) q;
while (p < end)
{
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
q[0] = p[2];
q[1] = p[1];
q[2] = p[0];
#else
q[1] = p[0];
q[2] = p[1];
q[3] = p[2];
#endif
p += 3;
q += 4;
}
for (; p < end; p += 3)
*q32++ = 0xff000000 | ((guint32) p[0] << 16) | (p[1] << 8) | p[2];
}
else
{
guchar *end = gdk_pixels + 4 * width;
guint t1,t2,t3;
guint32 *q32 = (guint32 *) q;
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END
while (p < end)
/* Red and blue 16 bits apart in one register, multiplied by
* alpha at once: c * a / 255 to the nearest, as
* ((t >> 8) + t) >> 8 with t = c * a + 0x80, which does not
* carry from one into the other. The pixel is read as one word
* as well. */
for (; p < end; p += 4)
{
guint32 w, a, rb, g;
memcpy (&w, p, 4);
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
MULT(q[0], p[2], p[3], t1);
MULT(q[1], p[1], p[3], t2);
MULT(q[2], p[0], p[3], t3);
q[3] = p[3];
a = w >> 24;
rb = w & 0x00ff00ff; /* B << 16 | R */
g = (w >> 8) & 0xff;
#else
q[0] = p[3];
MULT(q[1], p[0], p[3], t1);
MULT(q[2], p[1], p[3], t2);
MULT(q[3], p[2], p[3], t3);
a = w & 0xff;
rb = (w >> 8) & 0x00ff00ff; /* R << 16 | B */
g = (w >> 16) & 0xff;
#endif
p += 4;
q += 4;
rb = rb * a + 0x00800080;
rb = ((((rb >> 8) & 0x00ff00ff) + rb) >> 8) & 0x00ff00ff;
g = g * a + 0x80;
g = ((g >> 8) + g) >> 8;
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
rb = (rb << 16) | (rb >> 16); /* R << 16 | B */
#endif
*q32++ = (a << 24) | rb | (g << 8);
}
#undef MULT
}
gdk_pixels += gdk_rowstride;