gdkrgb: add AVX2 paths for 32-bit pixels

This commit is contained in:
Daemonratte 2026-10-16 13:47:28 +02:00
commit a8774c33db
6 changed files with 184 additions and 1 deletions

View file

@ -510,6 +510,7 @@ AC_SUBST($1_CFLAGS)])
MMX_CFLAGS=
SSE2_CFLAGS=
SSSE3_CFLAGS=
AVX2_CFLAGS=
ALTIVEC_CFLAGS=
NEON_CFLAGS=
case "$host_cpu" in
@ -532,6 +533,19 @@ case "$host_cpu" in
#error no SSSE3
#endif],
[__m128i v = _mm_setzero_si128 (); v = _mm_shuffle_epi8 (v, v); (void) v;])
dnl not with MinGW: GCC does not align its stack for AVX there (GCC
dnl bug 54412), and unoptimized builds would crash on spills
case "$host_os" in
mingw*|cygwin*) ;;
*)
GTK_CHECK_SIMD_FLAGS([AVX2], [-mavx2],
[#include <immintrin.h>
#ifndef __AVX2__
#error no AVX2
#endif],
[__m256i v = _mm256_setzero_si256 (); v = _mm256_shuffle_epi8 (v, v); (void) v;])
;;
esac
;;
powerpc*|ppc*)
GTK_CHECK_SIMD_FLAGS([ALTIVEC], [-maltivec],

View file

@ -173,6 +173,7 @@ noinst_LTLIBRARIES = \
libgdk-rgb-mmx.la \
libgdk-rgb-sse2.la \
libgdk-rgb-ssse3.la \
libgdk-rgb-avx2.la \
libgdk-rgb-altivec.la \
libgdk-rgb-neon.la
@ -182,6 +183,8 @@ libgdk_rgb_sse2_la_SOURCES = gdkrgb-sse2.c
libgdk_rgb_sse2_la_CFLAGS = $(SSE2_CFLAGS)
libgdk_rgb_ssse3_la_SOURCES = gdkrgb-ssse3.c
libgdk_rgb_ssse3_la_CFLAGS = $(SSSE3_CFLAGS)
libgdk_rgb_avx2_la_SOURCES = gdkrgb-avx2.c
libgdk_rgb_avx2_la_CFLAGS = $(AVX2_CFLAGS)
libgdk_rgb_altivec_la_SOURCES = gdkrgb-altivec.c
libgdk_rgb_altivec_la_CFLAGS = $(ALTIVEC_CFLAGS)
libgdk_rgb_neon_la_SOURCES = gdkrgb-neon.c

136
gdk/gdkrgb-avx2.c Normal file
View file

@ -0,0 +1,136 @@
/* GDK - The GIMP Drawing Kit
* GdkRGB loops for AVX2 (Haswell, Zen and later): the SSSE3 loops eight
* pixels at a time, for the pixels of four bytes, where the lanes of 128
* bits do not mix. Pixels of three bytes would cross the two halves of
* a register and need permutes; those stay with SSSE3.
*
* 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(__AVX2__)
#include <immintrin.h>
/* x86 is little endian: an x888 pixel is the bytes B, G, R, 0xff */
#define SPLAT(v) _mm256_set1_epi32 ((gint) (v))
/* the same order of bytes in both halves */
#define BYTES(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
_mm256_setr_epi8 (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, \
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
static gint
rgbx_to_x888 (guint32 *out, const guchar *rgbx, gint n)
{
const __m256i order = BYTES (2, 1, 0, -1, 6, 5, 4, -1, 10, 9, 8, -1, 14, 13, 12, -1);
gint i;
for (i = 0; i + 8 <= n; i += 8, rgbx += 32)
_mm256_storeu_si256 ((__m256i *) (out + i),
_mm256_or_si256 (_mm256_shuffle_epi8 (_mm256_loadu_si256 ((const __m256i *) rgbx), order),
SPLAT (0xff000000)));
return i;
}
/* The alpha of each pixel in its four 16-bit lanes */
static inline __m256i
alpha_of (__m256i v)
{
return _mm256_shuffle_epi8 (v, BYTES (6, 7, 6, 7, 6, 7, 6, 7, 14, 15, 14, 15, 14, 15, 14, 15));
}
/* (a * s + (255 - a) * d) / 255, rounded as GdkRGB's C code does */
static inline __m256i
blend (__m256i s, __m256i d, __m256i a)
{
__m256i t = _mm256_add_epi16 (_mm256_add_epi16 (_mm256_mullo_epi16 (s, a),
_mm256_mullo_epi16 (d, _mm256_sub_epi16 (_mm256_set1_epi16 (255), a))),
_mm256_set1_epi16 (0x80));
return _mm256_mulhi_epu16 (t, _mm256_set1_epi16 (257));
}
static gint
composite_x888 (guint32 *pixels, const guchar *rgba, gint n)
{
const __m256i zero = _mm256_setzero_si256 ();
const __m256i order = BYTES (2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15);
gint i;
for (i = 0; i + 8 <= n; i += 8, rgba += 32)
{
__m256i s = _mm256_loadu_si256 ((const __m256i *) rgba);
__m256i d = _mm256_loadu_si256 ((const __m256i *) (pixels + i));
__m256i transparent = _mm256_cmpeq_epi32 (_mm256_srli_epi32 (s, 24), zero);
__m256i lo, hi, v;
/* RGBA to BGRA, the order of the pixels */
s = _mm256_shuffle_epi8 (s, order);
lo = _mm256_unpacklo_epi8 (s, zero);
hi = _mm256_unpackhi_epi8 (s, zero);
lo = blend (lo, _mm256_unpacklo_epi8 (d, zero), alpha_of (lo));
hi = blend (hi, _mm256_unpackhi_epi8 (d, zero), alpha_of (hi));
v = _mm256_or_si256 (_mm256_packus_epi16 (lo, hi), SPLAT (0xff000000));
/* fully transparent pixels stay as they are */
v = _mm256_blendv_epi8 (v, d, transparent);
_mm256_storeu_si256 ((__m256i *) (pixels + i), v);
}
return i;
}
/* c * a / 255 to the nearest, which (c * a + 0x80) * 257 >> 16 is */
static gint
rgba_to_argb (guint32 *out, const guchar *rgba, gint n)
{
const __m256i zero = _mm256_setzero_si256 ();
const __m256i order = BYTES (2, 1, 0, -1, 6, 5, 4, -1, 10, 9, 8, -1, 14, 13, 12, -1);
gint i;
for (i = 0; i + 8 <= n; i += 8, rgba += 32)
{
__m256i s = _mm256_loadu_si256 ((const __m256i *) rgba);
__m256i lo = _mm256_unpacklo_epi8 (s, zero);
__m256i hi = _mm256_unpackhi_epi8 (s, zero);
lo = _mm256_mulhi_epu16 (_mm256_add_epi16 (_mm256_mullo_epi16 (lo, alpha_of (lo)), _mm256_set1_epi16 (0x80)),
_mm256_set1_epi16 (257));
hi = _mm256_mulhi_epu16 (_mm256_add_epi16 (_mm256_mullo_epi16 (hi, alpha_of (hi)), _mm256_set1_epi16 (0x80)),
_mm256_set1_epi16 (257));
/* R, G, B to B, G, R, and the alpha byte as it was */
_mm256_storeu_si256 ((__m256i *) (out + i),
_mm256_or_si256 (_mm256_shuffle_epi8 (_mm256_packus_epi16 (lo, hi), order),
_mm256_and_si256 (s, SPLAT (0xff000000))));
}
return i;
}
void
_gdk_rgb_simd_init_avx2 (GdkRgbSimd *simd)
{
simd->name = "avx2";
simd->rgbx_to_x888 = rgbx_to_x888;
simd->composite_x888 = composite_x888;
simd->rgba_to_argb = rgba_to_argb;
}
#else
void
_gdk_rgb_simd_init_avx2 (GdkRgbSimd *simd)
{
}
#endif

View file

@ -37,6 +37,7 @@ static const GdkRgbSimdSet all_sets[] =
{ "3dnowext", _gdk_rgb_simd_init_3dnowext },
{ "sse2", _gdk_rgb_simd_init_sse2 },
{ "ssse3", _gdk_rgb_simd_init_ssse3 },
{ "avx2", _gdk_rgb_simd_init_avx2 },
{ "altivec", _gdk_rgb_simd_init_altivec },
{ "neon", _gdk_rgb_simd_init_neon },
};
@ -73,6 +74,27 @@ hwcap (void)
}
#endif
#if (defined(__x86_64__) || defined(__i386__)) && defined(__GNUC__)
/* AVX2: the CPU has it, and the system saves the wide registers
* (OSXSAVE, then XCR0 says SSE and AVX state) */
static gboolean
cpu_has_avx2 (unsigned int ecx)
{
unsigned int eax, ebx, ecx7, edx, xcr0_low, xcr0_high;
if ((ecx & (1 << 27)) == 0 || (ecx & (1 << 28)) == 0 ||
__get_cpuid_max (0, NULL) < 7)
return FALSE;
__cpuid_count (7, 0, eax, ebx, ecx7, edx);
if ((ebx & (1 << 5)) == 0)
return FALSE;
__asm__ (".byte 0x0f, 0x01, 0xd0" /* xgetbv, which old assemblers do not know */
: "=a" (xcr0_low), "=d" (xcr0_high) : "c" (0));
return (xcr0_low & 6) == 6;
}
#endif
/* Whether this CPU can run the set */
static gboolean
cpu_has (const gchar *set)
@ -83,8 +105,12 @@ cpu_has (const gchar *set)
/* every x86-64 CPU has SSE2, which beats the older sets */
if (strcmp (set, "sse2") == 0)
return TRUE;
if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
return FALSE;
if (strcmp (set, "ssse3") == 0)
return __get_cpuid (1, &eax, &ebx, &ecx, &edx) && (ecx & (1 << 9)) != 0;
return (ecx & (1 << 9)) != 0;
if (strcmp (set, "avx2") == 0)
return cpu_has_avx2 (ecx);
return FALSE;
#elif defined(__i386__) && defined(__GNUC__)
@ -114,6 +140,8 @@ cpu_has (const gchar *set)
return (edx & (1 << 26)) != 0;
if (strcmp (set, "ssse3") == 0)
return (ecx & (1 << 9)) != 0;
if (strcmp (set, "avx2") == 0)
return cpu_has_avx2 (ecx);
return FALSE;
#elif defined(__aarch64__)

View file

@ -67,6 +67,7 @@ void _gdk_rgb_simd_init_3dnow (GdkRgbSimd *simd);
void _gdk_rgb_simd_init_3dnowext (GdkRgbSimd *simd);
void _gdk_rgb_simd_init_sse2 (GdkRgbSimd *simd);
void _gdk_rgb_simd_init_ssse3 (GdkRgbSimd *simd);
void _gdk_rgb_simd_init_avx2 (GdkRgbSimd *simd);
void _gdk_rgb_simd_init_altivec (GdkRgbSimd *simd);
void _gdk_rgb_simd_init_neon (GdkRgbSimd *simd);

View file

@ -96,6 +96,7 @@ gdk_OBJECTS = \
gdkrgb-mmx.obj \
gdkrgb-sse2.obj \
gdkrgb-ssse3.obj \
gdkrgb-avx2.obj \
gdkrgb-altivec.obj \
gdkrgb-neon.obj \
gdkscreen.obj \