gdkrgb: MMX for CPUs without SSE2

This commit is contained in:
Daemonratte 2026-10-15 14:08:02 +02:00
commit 74b8339ced
No known key found for this signature in database
6 changed files with 245 additions and 0 deletions

View file

@ -504,9 +504,16 @@ CFLAGS="$gtk_save_CFLAGS"
AC_MSG_RESULT([${$1_CFLAGS:-none}])
AC_SUBST($1_CFLAGS)])
MMX_CFLAGS=
SSE2_CFLAGS=
case "$host_cpu" in
i?86|x86_64|amd64)
GTK_CHECK_SIMD_FLAGS([MMX], [-mmmx],
[#include <mmintrin.h>
#ifndef __MMX__
#error no MMX
#endif],
[__m64 v = _mm_setzero_si64 (); v = _mm_mullo_pi16 (v, v); _mm_empty ();])
GTK_CHECK_SIMD_FLAGS([SSE2], [-msse2],
[#include <emmintrin.h>
#ifndef __SSE2__

View file

@ -170,8 +170,11 @@ common_sources = \
# instruction set. configure leaves the flags empty where the compiler
# can't do them, and the file then has nothing but an empty function.
noinst_LTLIBRARIES = \
libgdk-rgb-mmx.la \
libgdk-rgb-sse2.la
libgdk_rgb_mmx_la_SOURCES = gdkrgb-mmx.c
libgdk_rgb_mmx_la_CFLAGS = $(MMX_CFLAGS)
libgdk_rgb_sse2_la_SOURCES = gdkrgb-sse2.c
libgdk_rgb_sse2_la_CFLAGS = $(SSE2_CFLAGS)

231
gdk/gdkrgb-mmx.c Normal file
View file

@ -0,0 +1,231 @@
/* GDK - The GIMP Drawing Kit
* GdkRGB loops for MMX (Pentium MMX, Pentium II, K6)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*/
#include "config.h"
#include <string.h>
#include "gdkrgbsimd.h"
#if defined(__MMX__)
#include <mmintrin.h>
/* Only the original MMX instructions are used, so no pshufw. */
#define SPLAT(v) _mm_set1_pi32 ((gint) (v))
static inline __m64
load8 (const void *p)
{
__m64 v;
memcpy (&v, p, 8);
return v;
}
static inline void
store8 (void *p, __m64 v)
{
memcpy (p, &v, 8);
}
static inline __m64
load4 (const void *p)
{
gint32 v;
memcpy (&v, p, 4);
return _mm_cvtsi32_si64 (v);
}
/* The 6 bytes of two RGB pixels as lanes R | G << 8 | B << 16 */
static inline __m64
rgb_lanes (__m64 v)
{
return _mm_unpacklo_pi32 (v, _mm_srli_si64 (v, 24));
}
static inline __m64
x888_of (__m64 v)
{
return _mm_or_si64 (_mm_or_si64 (_mm_slli_pi32 (_mm_and_si64 (v, SPLAT (0xff)), 16),
_mm_and_si64 (v, SPLAT (0xff00))),
_mm_or_si64 (_mm_and_si64 (_mm_srli_pi32 (v, 16), SPLAT (0xff)),
SPLAT (0xff000000)));
}
static inline __m64
rgb565_of (__m64 v)
{
v = _mm_or_si64 (_mm_or_si64 (_mm_slli_pi32 (_mm_and_si64 (v, SPLAT (0xf8)), 8),
_mm_srli_pi32 (_mm_and_si64 (v, SPLAT (0xfc00)), 5)),
_mm_and_si64 (_mm_srli_pi32 (v, 19), SPLAT (0x1f)));
return _mm_srai_pi32 (_mm_slli_pi32 (v, 16), 16);
}
static gint
rgb_to_x888 (guint32 *out, const guchar *rgb, gint n)
{
gint i;
for (i = 0; i + 4 <= n; i += 4, rgb += 12)
{
__m64 a = load8 (rgb);
__m64 b = _mm_or_si64 (_mm_srli_si64 (a, 48), _mm_slli_si64 (load4 (rgb + 8), 16));
store8 (out + i, x888_of (rgb_lanes (a)));
store8 (out + i + 2, x888_of (rgb_lanes (b)));
}
_mm_empty ();
return i;
}
static gint
rgbx_to_x888 (guint32 *out, const guchar *rgbx, gint n)
{
gint i;
for (i = 0; i + 2 <= n; i += 2, rgbx += 8)
store8 (out + i, x888_of (load8 (rgbx)));
_mm_empty ();
return i;
}
static gint
rgb_to_565 (guint16 *out, const guchar *rgb, gint n)
{
gint i;
for (i = 0; i + 4 <= n; i += 4, rgb += 12)
{
__m64 a = load8 (rgb);
__m64 b = _mm_or_si64 (_mm_srli_si64 (a, 48), _mm_slli_si64 (load4 (rgb + 8), 16));
store8 (out + i, _mm_packs_pi32 (rgb565_of (rgb_lanes (a)), rgb565_of (rgb_lanes (b))));
}
_mm_empty ();
return i;
}
/* (a * s + (255 - a) * d) / 255, rounded, in 16-bit lanes */
static inline __m64
blend (__m64 s, __m64 d, __m64 a)
{
__m64 t = _mm_add_pi16 (_mm_add_pi16 (_mm_mullo_pi16 (s, a),
_mm_mullo_pi16 (d, _mm_sub_pi16 (_mm_set1_pi16 (255), a))),
_mm_set1_pi16 (0x80));
return _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
}
/* The alpha of a pixel in 16-bit lanes B, G, R, A in all four lanes */
static inline __m64
alpha_of (__m64 v)
{
v = _mm_srli_si64 (v, 48);
v = _mm_unpacklo_pi16 (v, v);
return _mm_unpacklo_pi32 (v, v);
}
static gint
composite_x888 (guint32 *pixels, const guchar *rgba, gint n)
{
const __m64 zero = _mm_setzero_si64 ();
gint i;
for (i = 0; i + 2 <= n; i += 2, rgba += 8)
{
__m64 s = load8 (rgba);
__m64 d = load8 (pixels + i);
__m64 transparent = _mm_cmpeq_pi32 (_mm_srli_pi32 (s, 24), zero);
__m64 lo, hi, v;
/* RGBA to BGRA, the order of the pixels */
s = _mm_or_si64 (_mm_and_si64 (s, SPLAT (0xff00ff00)),
_mm_or_si64 (_mm_slli_pi32 (_mm_and_si64 (s, SPLAT (0xff)), 16),
_mm_and_si64 (_mm_srli_pi32 (s, 16), SPLAT (0xff))));
lo = _mm_unpacklo_pi8 (s, zero);
hi = _mm_unpackhi_pi8 (s, zero);
lo = blend (lo, _mm_unpacklo_pi8 (d, zero), alpha_of (lo));
hi = blend (hi, _mm_unpackhi_pi8 (d, zero), alpha_of (hi));
v = _mm_or_si64 (_mm_packs_pu16 (lo, hi), SPLAT (0xff000000));
/* fully transparent pixels stay as they are */
store8 (pixels + i, _mm_or_si64 (_mm_and_si64 (transparent, d),
_mm_andnot_si64 (transparent, v)));
}
_mm_empty ();
return i;
}
static gint
composite_565 (guint16 *pixels, const guchar *rgba, gint n)
{
gint i;
for (i = 0; i + 4 <= n; i += 4, rgba += 16)
{
__m64 s0 = load8 (rgba);
__m64 s1 = load8 (rgba + 8);
__m64 d = load8 (pixels + i);
__m64 r = _mm_packs_pi32 (_mm_and_si64 (s0, SPLAT (0xff)), _mm_and_si64 (s1, SPLAT (0xff)));
__m64 g = _mm_packs_pi32 (_mm_and_si64 (_mm_srli_pi32 (s0, 8), SPLAT (0xff)),
_mm_and_si64 (_mm_srli_pi32 (s1, 8), SPLAT (0xff)));
__m64 b = _mm_packs_pi32 (_mm_and_si64 (_mm_srli_pi32 (s0, 16), SPLAT (0xff)),
_mm_and_si64 (_mm_srli_pi32 (s1, 16), SPLAT (0xff)));
__m64 a = _mm_packs_pi32 (_mm_srli_pi32 (s0, 24), _mm_srli_pi32 (s1, 24));
__m64 dr = _mm_srli_pi16 (d, 11);
__m64 dg = _mm_and_si64 (_mm_srli_pi16 (d, 5), _mm_set1_pi16 (0x3f));
__m64 db = _mm_and_si64 (d, _mm_set1_pi16 (0x1f));
/* the bits of the pixel repeated up to 8 */
dr = _mm_or_si64 (_mm_slli_pi16 (dr, 3), _mm_srli_pi16 (dr, 2));
dg = _mm_or_si64 (_mm_slli_pi16 (dg, 2), _mm_srli_pi16 (dg, 4));
db = _mm_or_si64 (_mm_slli_pi16 (db, 3), _mm_srli_pi16 (db, 2));
r = blend (r, dr, a);
g = blend (g, dg, a);
b = blend (b, db, a);
store8 (pixels + i,
_mm_or_si64 (_mm_or_si64 (_mm_slli_pi16 (_mm_and_si64 (r, _mm_set1_pi16 (0xf8)), 8),
_mm_slli_pi16 (_mm_and_si64 (g, _mm_set1_pi16 (0xfc)), 3)),
_mm_srli_pi16 (b, 3)));
}
_mm_empty ();
return i;
}
void
_gdk_rgb_simd_init_mmx (GdkRgbSimd *simd)
{
simd->name = "mmx";
simd->rgb_to_x888 = rgb_to_x888;
simd->rgbx_to_x888 = rgbx_to_x888;
simd->rgb_to_565 = rgb_to_565;
simd->composite_x888 = composite_x888;
simd->composite_565 = composite_565;
}
#else
void
_gdk_rgb_simd_init_mmx (GdkRgbSimd *simd)
{
}
#endif

View file

@ -31,6 +31,8 @@ detect (GdkRgbSimd *simd)
if (__get_cpuid (1, &eax, &ebx, &ecx, &edx))
{
if (edx & (1 << 23))
_gdk_rgb_simd_init_mmx (simd);
if (edx & (1 << 26))
_gdk_rgb_simd_init_sse2 (simd);
}

View file

@ -43,6 +43,7 @@ struct _GdkRgbSimd
/* Each fills in what its instruction set does better than C; they are
* only called when the CPU has it. */
void _gdk_rgb_simd_init_mmx (GdkRgbSimd *simd);
void _gdk_rgb_simd_init_sse2 (GdkRgbSimd *simd);
/* The kernels for this CPU, or none with GDK_DISABLE_SIMD set */

View file

@ -93,6 +93,7 @@ gdk_OBJECTS = \
gdkregion-generic.obj \
gdkrgb.obj \
gdkrgbsimd.obj \
gdkrgb-mmx.obj \
gdkrgb-sse2.obj \
gdkscreen.obj \
gdkselection.obj \