gdkrgb: add SSE2 paths, picked at runtime

This commit is contained in:
Daemonratte 2026-10-16 13:46:11 +02:00
commit 8e7fc8d68c
7 changed files with 411 additions and 9 deletions

View file

@ -479,6 +479,45 @@ AC_SUBST(REBUILD)
AC_CHECK_FUNCS(lstat mkstemp flockfile getc_unlocked _getc_nolock)
AC_CHECK_FUNCS(localtime_r)
#
# Vector instructions for GdkRGB. Each instruction set has a file of its
# own, compiled with the flags found here; CPUs without it are told apart
# at run time.
#
dnl GTK_CHECK_SIMD_FLAGS(NAME, FLAGS|OTHER FLAGS, PROLOGUE, BODY):
dnl sets NAME_CFLAGS to the first flags the test compiles with
AC_DEFUN([GTK_CHECK_SIMD_FLAGS],
[AC_MSG_CHECKING([for flags to compile $1 code])
$1_CFLAGS=
gtk_save_CFLAGS="$CFLAGS"
gtk_simd_list='$2'
gtk_save_IFS="$IFS"
IFS='|'
for gtk_simd_flags in $gtk_simd_list; do
IFS="$gtk_save_IFS"
CFLAGS="$gtk_save_CFLAGS $gtk_simd_flags"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[$3]], [[$4]])],
[$1_CFLAGS="$gtk_simd_flags"
break])
done
IFS="$gtk_save_IFS"
CFLAGS="$gtk_save_CFLAGS"
AC_MSG_RESULT([${$1_CFLAGS:-none}])
AC_SUBST($1_CFLAGS)])
SSE2_CFLAGS=
case "$host_cpu" in
i?86|x86_64|amd64)
GTK_CHECK_SIMD_FLAGS([SSE2], [-msse2],
[#include <emmintrin.h>
#ifndef __SSE2__
#error no SSE2
#endif],
[__m128i v = _mm_setzero_si128 (); v = _mm_mullo_epi16 (v, v); (void) v;])
;;
esac
# _NL_TIME_FIRST_WEEKDAY is an enum and not a define
AC_MSG_CHECKING([for _NL_TIME_FIRST_WEEKDAY])
AC_LINK_IFELSE(

View file

@ -107,6 +107,7 @@ gdk_built_private_headers = \
gdk_private_headers = \
gdkinternals.h \
gdkintl.h \
gdkrgbsimd.h \
gdkpoly-generic.h \
gdkregion-generic.h
@ -136,6 +137,7 @@ gdk_c_sources = \
gdkrectangle.c \
gdkregion-generic.c \
gdkrgb.c \
gdkrgbsimd.c \
gdkscreen.c \
gdkselection.c \
gdkvisual.c \
@ -164,21 +166,32 @@ common_sources = \
gdkmarshalers.c \
gdkmarshalers.h
# Vector versions of GdkRGB loops, each built with the flags of its
# 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-sse2.la
libgdk_rgb_sse2_la_SOURCES = gdkrgb-sse2.c
libgdk_rgb_sse2_la_CFLAGS = $(SSE2_CFLAGS)
gdk_rgb_simd_libs = $(noinst_LTLIBRARIES)
libgdk_directfb_2_0_la_SOURCES = $(common_sources)
libgdk_directfb_2_0_la_LIBADD = directfb/libgdk-directfb.la $(GDK_DEP_LIBS)
libgdk_directfb_2_0_la_LIBADD = directfb/libgdk-directfb.la $(gdk_rgb_simd_libs) $(GDK_DEP_LIBS)
libgdk_directfb_2_0_la_LDFLAGS = $(LDADD)
libgdk_x11_2_0_la_SOURCES = $(common_sources)
libgdk_x11_2_0_la_LIBADD = x11/libgdk-x11.la $(GDK_DEP_LIBS)
libgdk_x11_2_0_la_LIBADD = x11/libgdk-x11.la $(gdk_rgb_simd_libs) $(GDK_DEP_LIBS)
libgdk_x11_2_0_la_LDFLAGS = $(LDADD)
libgdk_quartz_2_0_la_SOURCES = $(common_sources) gdkkeynames.c
libgdk_quartz_2_0_la_LIBADD = quartz/libgdk-quartz.la $(GDK_DEP_LIBS)
libgdk_quartz_2_0_la_LIBADD = quartz/libgdk-quartz.la $(gdk_rgb_simd_libs) $(GDK_DEP_LIBS)
libgdk_quartz_2_0_la_LDFLAGS = $(LDADD)
libgdk_win32_2_0_la_SOURCES = $(common_sources) gdkkeynames.c
libgdk_win32_2_0_la_LIBADD = win32/libgdk-win32.la $(GDK_DEP_LIBS)
libgdk_win32_2_0_la_DEPENDENCIES = win32/libgdk-win32.la win32/rc/gdk-win32-res.o gdk.def
libgdk_win32_2_0_la_LIBADD = win32/libgdk-win32.la $(gdk_rgb_simd_libs) $(GDK_DEP_LIBS)
libgdk_win32_2_0_la_DEPENDENCIES = win32/libgdk-win32.la $(gdk_rgb_simd_libs) win32/rc/gdk-win32-res.o gdk.def
libgdk_win32_2_0_la_LDFLAGS = -Wl,win32/rc/gdk-win32-res.o -export-symbols $(srcdir)/gdk.def $(LDADD)
if HAVE_INTROSPECTION

208
gdk/gdkrgb-sse2.c Normal file
View file

@ -0,0 +1,208 @@
/* GDK - The GIMP Drawing Kit
* GdkRGB loops for SSE2
*
* 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 "gdkrgbsimd.h"
#if defined(__SSE2__)
#include <emmintrin.h>
/* x86 is little endian: an x888 pixel is the bytes B, G, R, 0xff */
#define SPLAT(v) _mm_set1_epi32 ((gint) (v))
/* The 12 bytes of four RGB pixels as lanes R | G << 8 | B << 16 */
static inline __m128i
rgb_lanes (__m128i v)
{
__m128i lo = _mm_unpacklo_epi32 (v, _mm_srli_si128 (v, 3));
__m128i hi = _mm_unpacklo_epi32 (_mm_srli_si128 (v, 6), _mm_srli_si128 (v, 9));
return _mm_unpacklo_epi64 (lo, hi);
}
/* Lanes R | G << 8 | B << 16 to x888 */
static inline __m128i
x888_of (__m128i v)
{
return _mm_or_si128 (_mm_or_si128 (_mm_slli_epi32 (_mm_and_si128 (v, SPLAT (0xff)), 16),
_mm_and_si128 (v, SPLAT (0xff00))),
_mm_or_si128 (_mm_and_si128 (_mm_srli_epi32 (v, 16), SPLAT (0xff)),
SPLAT (0xff000000)));
}
/* Lanes R | G << 8 | B << 16 to 565, sign extended for packing */
static inline __m128i
rgb565_of (__m128i v)
{
v = _mm_or_si128 (_mm_or_si128 (_mm_slli_epi32 (_mm_and_si128 (v, SPLAT (0xf8)), 8),
_mm_srli_epi32 (_mm_and_si128 (v, SPLAT (0xfc00)), 5)),
_mm_and_si128 (_mm_srli_epi32 (v, 19), SPLAT (0x1f)));
return _mm_srai_epi32 (_mm_slli_epi32 (v, 16), 16);
}
static gint
rgb_to_x888 (guint32 *out, const guchar *rgb, gint n)
{
gint i;
for (i = 0; i + 8 <= n; i += 8, rgb += 24)
{
__m128i a = _mm_loadu_si128 ((const __m128i *) rgb);
__m128i b = _mm_loadl_epi64 ((const __m128i *) (rgb + 16));
__m128i c = _mm_or_si128 (_mm_srli_si128 (a, 12), _mm_slli_si128 (b, 4));
_mm_storeu_si128 ((__m128i *) (out + i), x888_of (rgb_lanes (a)));
_mm_storeu_si128 ((__m128i *) (out + i + 4), x888_of (rgb_lanes (c)));
}
return i;
}
static gint
rgbx_to_x888 (guint32 *out, const guchar *rgbx, gint n)
{
gint i;
for (i = 0; i + 4 <= n; i += 4, rgbx += 16)
_mm_storeu_si128 ((__m128i *) (out + i),
x888_of (_mm_loadu_si128 ((const __m128i *) rgbx)));
return i;
}
static gint
rgb_to_565 (guint16 *out, const guchar *rgb, gint n)
{
gint i;
for (i = 0; i + 8 <= n; i += 8, rgb += 24)
{
__m128i a = _mm_loadu_si128 ((const __m128i *) rgb);
__m128i b = _mm_loadl_epi64 ((const __m128i *) (rgb + 16));
__m128i c = _mm_or_si128 (_mm_srli_si128 (a, 12), _mm_slli_si128 (b, 4));
_mm_storeu_si128 ((__m128i *) (out + i),
_mm_packs_epi32 (rgb565_of (rgb_lanes (a)),
rgb565_of (rgb_lanes (c))));
}
return i;
}
/* (a * s + (255 - a) * d) / 255, rounded, in 16-bit lanes */
static inline __m128i
blend (__m128i s, __m128i d, __m128i a)
{
__m128i t = _mm_add_epi16 (_mm_add_epi16 (_mm_mullo_epi16 (s, a),
_mm_mullo_epi16 (d, _mm_sub_epi16 (_mm_set1_epi16 (255), a))),
_mm_set1_epi16 (0x80));
return _mm_srli_epi16 (_mm_add_epi16 (t, _mm_srli_epi16 (t, 8)), 8);
}
/* Alpha of each pixel in all four 16-bit lanes of the pixel */
static inline __m128i
alpha_of (__m128i v)
{
return _mm_shufflehi_epi16 (_mm_shufflelo_epi16 (v, 0xff), 0xff);
}
static gint
composite_x888 (guint32 *pixels, const guchar *rgba, gint n)
{
const __m128i zero = _mm_setzero_si128 ();
gint i;
for (i = 0; i + 4 <= n; i += 4, rgba += 16)
{
__m128i s = _mm_loadu_si128 ((const __m128i *) rgba);
__m128i d = _mm_loadu_si128 ((const __m128i *) (pixels + i));
__m128i transparent = _mm_cmpeq_epi32 (_mm_srli_epi32 (s, 24), zero);
__m128i lo, hi, v;
/* RGBA to BGRA, the order of the pixels */
s = _mm_or_si128 (_mm_and_si128 (s, SPLAT (0xff00ff00)),
_mm_or_si128 (_mm_slli_epi32 (_mm_and_si128 (s, SPLAT (0xff)), 16),
_mm_and_si128 (_mm_srli_epi32 (s, 16), SPLAT (0xff))));
lo = _mm_unpacklo_epi8 (s, zero);
hi = _mm_unpackhi_epi8 (s, zero);
lo = blend (lo, _mm_unpacklo_epi8 (d, zero), alpha_of (lo));
hi = blend (hi, _mm_unpackhi_epi8 (d, zero), alpha_of (hi));
v = _mm_or_si128 (_mm_packus_epi16 (lo, hi), SPLAT (0xff000000));
/* fully transparent pixels stay as they are */
v = _mm_or_si128 (_mm_and_si128 (transparent, d), _mm_andnot_si128 (transparent, v));
_mm_storeu_si128 ((__m128i *) (pixels + i), v);
}
return i;
}
static gint
composite_565 (guint16 *pixels, const guchar *rgba, gint n)
{
gint i;
for (i = 0; i + 8 <= n; i += 8, rgba += 32)
{
__m128i s0 = _mm_loadu_si128 ((const __m128i *) rgba);
__m128i s1 = _mm_loadu_si128 ((const __m128i *) (rgba + 16));
__m128i d = _mm_loadu_si128 ((const __m128i *) (pixels + i));
__m128i r = _mm_packs_epi32 (_mm_and_si128 (s0, SPLAT (0xff)),
_mm_and_si128 (s1, SPLAT (0xff)));
__m128i g = _mm_packs_epi32 (_mm_and_si128 (_mm_srli_epi32 (s0, 8), SPLAT (0xff)),
_mm_and_si128 (_mm_srli_epi32 (s1, 8), SPLAT (0xff)));
__m128i b = _mm_packs_epi32 (_mm_and_si128 (_mm_srli_epi32 (s0, 16), SPLAT (0xff)),
_mm_and_si128 (_mm_srli_epi32 (s1, 16), SPLAT (0xff)));
__m128i a = _mm_packs_epi32 (_mm_srli_epi32 (s0, 24), _mm_srli_epi32 (s1, 24));
__m128i dr = _mm_srli_epi16 (d, 11);
__m128i dg = _mm_and_si128 (_mm_srli_epi16 (d, 5), _mm_set1_epi16 (0x3f));
__m128i db = _mm_and_si128 (d, _mm_set1_epi16 (0x1f));
/* the bits of the pixel repeated up to 8 */
dr = _mm_or_si128 (_mm_slli_epi16 (dr, 3), _mm_srli_epi16 (dr, 2));
dg = _mm_or_si128 (_mm_slli_epi16 (dg, 2), _mm_srli_epi16 (dg, 4));
db = _mm_or_si128 (_mm_slli_epi16 (db, 3), _mm_srli_epi16 (db, 2));
r = blend (r, dr, a);
g = blend (g, dg, a);
b = blend (b, db, a);
d = _mm_or_si128 (_mm_or_si128 (_mm_slli_epi16 (_mm_and_si128 (r, _mm_set1_epi16 (0xf8)), 8),
_mm_slli_epi16 (_mm_and_si128 (g, _mm_set1_epi16 (0xfc)), 3)),
_mm_srli_epi16 (b, 3));
_mm_storeu_si128 ((__m128i *) (pixels + i), d);
}
return i;
}
void
_gdk_rgb_simd_init_sse2 (GdkRgbSimd *simd)
{
simd->name = "sse2";
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_sse2 (GdkRgbSimd *simd)
{
}
#endif

View file

@ -39,6 +39,7 @@
#include "gdkinternals.h" /* _gdk_windowing_get_bits_for_depth() */
#include "gdkrgb.h"
#include "gdkrgbsimd.h"
#include "gdkscreen.h"
#include "gdkalias.h"
#include <glib/gprintf.h>
@ -1476,6 +1477,25 @@ gdk_rgb_truecolor_row (GdkRgbInfo *info,
return;
}
/* vector code where there is some, C for the rest of the row */
if (native && (input == GDK_RGB_INPUT_RGB || input == GDK_RGB_INPUT_RGBX))
{
const GdkRgbSimd *simd = _gdk_rgb_simd_get ();
gint done = 0;
if (bits == 32 && info->layout == GDK_RGB_LAYOUT_X888 && input == GDK_RGB_INPUT_RGB)
done = simd->rgb_to_x888 ? simd->rgb_to_x888 (out32, in, width) : 0;
else if (bits == 32 && info->layout == GDK_RGB_LAYOUT_X888)
done = simd->rgbx_to_x888 ? simd->rgbx_to_x888 (out32, in, width) : 0;
else if (bits == 16 && info->layout == GDK_RGB_LAYOUT_565 && input == GDK_RGB_INPUT_RGB)
done = simd->rgb_to_565 ? simd->rgb_to_565 (out16, in, width) : 0;
out16 += done;
out32 += done;
in += done * gdk_rgb_input_bytes[input];
width -= done;
}
if (native && bits == 16)
switch (info->layout)
{
@ -1672,8 +1692,9 @@ gdk_rgb_select_conv (GdkRgbInfo *info)
gboolean gray = type == GDK_VISUAL_STATIC_GRAY || type == GDK_VISUAL_GRAYSCALE;
if (gdk_rgb_verbose)
g_print ("Chose visual type=%d depth=%d, image bpp=%d, %s first\n",
type, depth, bits, visual->byte_order == GDK_LSB_FIRST ? "lsb" : "msb");
g_print ("Chose visual type=%d depth=%d, image bpp=%d, %s first, vector code: %s\n",
type, depth, bits, visual->byte_order == GDK_LSB_FIRST ? "lsb" : "msb",
_gdk_rgb_simd_get ()->name);
info->dith_default = FALSE;
info->index_row = info->index_row_dither = NULL;
@ -1941,6 +1962,7 @@ _gdk_rgb_composite (GdkColormap *colormap,
gboolean msb_first = image->byte_order == GDK_MSB_FIRST;
gboolean native = msb_first == (G_BYTE_ORDER == G_BIG_ENDIAN);
gboolean deep = visual->red_prec > 8 || visual->green_prec > 8 || visual->blue_prec > 8;
const GdkRgbSimd *simd = _gdk_rgb_simd_get ();
guchar *out = (guchar *) image->mem + y0 * image->bpl + x0 * (bits / 8);
guint32 row[ROW_MAX];
gint y;
@ -1951,10 +1973,18 @@ _gdk_rgb_composite (GdkColormap *colormap,
for (y = 0; y < height; y++, rgba += rowstride, out += image->bpl)
{
gint done = 0;
/* vector code where there is some, C for the rest of the row */
if (native && bits == 16 && info->layout == GDK_RGB_LAYOUT_565 && simd->composite_565)
done = simd->composite_565 ((guint16 *) out, rgba, width);
else if (native && bits == 32 && info->layout == GDK_RGB_LAYOUT_X888 && simd->composite_x888)
done = simd->composite_x888 ((guint32 *) out, rgba, width);
if (native && bits == 16 && !deep)
gdk_rgb_composite_row16 (info, (guint16 *) out, rgba, width);
gdk_rgb_composite_row16 (info, (guint16 *) out + done, rgba + done * 4, width - done);
else if (native && bits == 32 && !deep)
gdk_rgb_composite_row32 (info, (guint32 *) out, rgba, width);
gdk_rgb_composite_row32 (info, (guint32 *) out + done, rgba + done * 4, width - done);
else if (native && bits == 32)
gdk_rgb_composite_row_deep (info, (guint32 *) out, rgba, width);
else

57
gdk/gdkrgbsimd.c Normal file
View file

@ -0,0 +1,57 @@
/* GDK - The GIMP Drawing Kit
* Choosing the vector versions of GdkRGB loops for this CPU
*
* 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 <stdlib.h>
#include "gdkrgbsimd.h"
#if defined(__i386__) || defined(__x86_64__)
#if defined(__GNUC__)
#include <cpuid.h>
#endif
#endif
static void
detect (GdkRgbSimd *simd)
{
#if defined(__x86_64__)
/* every x86-64 CPU has SSE2 */
_gdk_rgb_simd_init_sse2 (simd);
#elif defined(__i386__) && defined(__GNUC__)
unsigned int eax, ebx, ecx, edx;
if (__get_cpuid (1, &eax, &ebx, &ecx, &edx))
{
if (edx & (1 << 26))
_gdk_rgb_simd_init_sse2 (simd);
}
#endif
}
const GdkRgbSimd *
_gdk_rgb_simd_get (void)
{
static GdkRgbSimd simd = { "none", };
static gboolean done = FALSE;
if (!done)
{
const gchar *disable = g_getenv ("GDK_DISABLE_SIMD");
if (!disable || !*disable || *disable == '0')
detect (&simd);
done = TRUE;
}
return &simd;
}

53
gdk/gdkrgbsimd.h Normal file
View file

@ -0,0 +1,53 @@
/* GDK - The GIMP Drawing Kit
* Vector versions of GdkRGB loops
*
* 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.
*/
/* Vector versions of the busiest GdkRGB loops.
*
* Each converts pixels from the start of a row, as many as it likes, and
* returns how many; the C code does the rest. Pixels are words in the
* byte order of the machine:
*
* x888 0xffRRGGBB, 32 bits
* 565 RRRRRGGG GGGBBBBB, 16 bits
*
* RGB and RGBA are bytes in that order; RGBA is not premultiplied.
* Blending rounds as GdkRGB's C code does, so the results are the same.
* No alignment is required of any pointer.
*/
#ifndef __GDK_RGB_SIMD_H__
#define __GDK_RGB_SIMD_H__
#include <glib.h>
G_BEGIN_DECLS
typedef struct _GdkRgbSimd GdkRgbSimd;
struct _GdkRgbSimd
{
const gchar *name;
gint (*rgb_to_x888) (guint32 *out, const guchar *rgb, gint n);
gint (*rgbx_to_x888) (guint32 *out, const guchar *rgbx, gint n);
gint (*rgb_to_565) (guint16 *out, const guchar *rgb, gint n);
gint (*composite_x888) (guint32 *pixels, const guchar *rgba, gint n);
gint (*composite_565) (guint16 *pixels, const guchar *rgba, gint n);
};
/* 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_sse2 (GdkRgbSimd *simd);
/* The kernels for this CPU, or none with GDK_DISABLE_SIMD set */
const GdkRgbSimd *_gdk_rgb_simd_get (void);
G_END_DECLS
#endif /* __GDK_RGB_SIMD_H__ */

View file

@ -92,6 +92,8 @@ gdk_OBJECTS = \
gdkrectangle.obj \
gdkregion-generic.obj \
gdkrgb.obj \
gdkrgbsimd.obj \
gdkrgb-sse2.obj \
gdkscreen.obj \
gdkselection.obj \
gdkvisual.obj \