gdkrgb: add pshufw, pmulhuw and 3DNow! kernels
Each kernel is picked on its own depending on what the CPU can do, so Pentium IIIs and older Athlons get the parts they support.
This commit is contained in:
parent
488ec64633
commit
7c91040408
3 changed files with 466 additions and 80 deletions
341
gdk/gdkrgb-mmx.c
341
gdk/gdkrgb-mmx.c
|
|
@ -1,5 +1,7 @@
|
|||
/* GDK - The GIMP Drawing Kit
|
||||
* GdkRGB loops for MMX (Pentium MMX, Pentium II, K6)
|
||||
* GdkRGB loops for MMX, and for what came later on the same registers:
|
||||
* the integer SSE instructions of the Pentium III and Athlon (pshufw,
|
||||
* pmulhuw), and 3DNow! of the K6-2, Athlon and Geode LX (pmulhrw)
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
|
|
@ -13,11 +15,29 @@
|
|||
|
||||
#include "gdkrgbsimd.h"
|
||||
|
||||
#if defined(__MMX__)
|
||||
/* x86-64 always has SSE2, which does all of this better */
|
||||
#if defined(__MMX__) && !defined(__x86_64__)
|
||||
|
||||
#include <mmintrin.h>
|
||||
|
||||
/* Only the original MMX instructions are used, so no pshufw. */
|
||||
/* The file is compiled for plain MMX. The later instructions are
|
||||
* written as assembler, so the compiler uses them nowhere else, and
|
||||
* the kernels with them are only called on CPUs that have them. */
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define KERNEL static inline __attribute__ ((always_inline))
|
||||
#else
|
||||
#define KERNEL static inline
|
||||
#endif
|
||||
|
||||
/* How a kernel does what MMX has no instruction for */
|
||||
enum
|
||||
{
|
||||
PLAIN, /* MMX only */
|
||||
EXT, /* pshufw and pmulhuw */
|
||||
AMD3D, /* pmulhrw */
|
||||
AMD3D_EXT /* all three */
|
||||
};
|
||||
|
||||
#define SPLAT(v) _mm_set1_pi32 ((gint) (v))
|
||||
|
||||
|
|
@ -45,6 +65,37 @@ load4 (const void *p)
|
|||
return _mm_cvtsi32_si64 (v);
|
||||
}
|
||||
|
||||
#if defined(__i386__) && defined(__GNUC__)
|
||||
|
||||
#define HAVE_LATER_MMX 1
|
||||
|
||||
#define PSHUFW(v, imm) G_STMT_START { \
|
||||
__asm__ ("pshufw $" #imm ", %0, %0" : "+y" (v)); \
|
||||
} G_STMT_END
|
||||
|
||||
static inline __m64
|
||||
pmulhuw (__m64 a, __m64 b)
|
||||
{
|
||||
__asm__ ("pmulhuw %1, %0" : "+y" (a) : "y" (b));
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline __m64
|
||||
pmulhrw (__m64 a, __m64 b)
|
||||
{
|
||||
__asm__ ("pmulhrw %1, %0" : "+y" (a) : "y" (b));
|
||||
return a;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/* never used: only plain kernels are made here */
|
||||
#define PSHUFW(v, imm) G_STMT_START { } G_STMT_END
|
||||
#define pmulhuw(a, b) ((void) (b), (a))
|
||||
#define pmulhrw(a, b) ((void) (b), (a))
|
||||
|
||||
#endif
|
||||
|
||||
/* The 6 bytes of two RGB pixels as lanes R | G << 8 | B << 16 */
|
||||
static inline __m64
|
||||
rgb_lanes (__m64 v)
|
||||
|
|
@ -61,6 +112,8 @@ x888_of (__m64 v)
|
|||
SPLAT (0xff000000)));
|
||||
}
|
||||
|
||||
/* Lanes R | G << 8 | B << 16 to 565 or 555, sign extended for packing.
|
||||
* The bit 555 has no use for is set, as in all of GdkRGB's pixels. */
|
||||
static inline __m64
|
||||
rgb565_of (__m64 v)
|
||||
{
|
||||
|
|
@ -71,6 +124,17 @@ rgb565_of (__m64 v)
|
|||
return _mm_srai_pi32 (_mm_slli_pi32 (v, 16), 16);
|
||||
}
|
||||
|
||||
static inline __m64
|
||||
rgb555_of (__m64 v)
|
||||
{
|
||||
v = _mm_or_si64 (_mm_or_si64 (_mm_slli_pi32 (_mm_and_si64 (v, SPLAT (0xf8)), 7),
|
||||
_mm_srli_pi32 (_mm_and_si64 (v, SPLAT (0xf800)), 6)),
|
||||
_mm_or_si64 (_mm_and_si64 (_mm_srli_pi32 (v, 19), SPLAT (0x1f)),
|
||||
SPLAT (0x8000)));
|
||||
|
||||
return _mm_srai_pi32 (_mm_slli_pi32 (v, 16), 16);
|
||||
}
|
||||
|
||||
static gint
|
||||
rgb_to_x888 (guint32 *out, const guchar *rgb, gint n)
|
||||
{
|
||||
|
|
@ -118,29 +182,113 @@ rgb_to_565 (guint16 *out, const guchar *rgb, gint n)
|
|||
return i;
|
||||
}
|
||||
|
||||
/* (a * s + (255 - a) * d) / 255, rounded, in 16-bit lanes */
|
||||
static inline __m64
|
||||
blend (__m64 s, __m64 d, __m64 a)
|
||||
static gint
|
||||
rgb_to_555 (guint16 *out, const guchar *rgb, gint n)
|
||||
{
|
||||
__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));
|
||||
gint i;
|
||||
|
||||
return _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
|
||||
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 (rgb555_of (rgb_lanes (a)), rgb555_of (rgb_lanes (b))));
|
||||
}
|
||||
_mm_empty ();
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/* The alpha of a pixel in 16-bit lanes B, G, R, A in all four lanes */
|
||||
static inline __m64
|
||||
alpha_of (__m64 v)
|
||||
/* (a * s + (255 - a) * d) / 255, rounded as GdkRGB's C code does, in
|
||||
* 16-bit lanes. 3DNow! does it as d + (s - d) * a / 255: pmulhrw
|
||||
* rounds the signed difference times a * 257, a correction makes up
|
||||
* for a * 257 not fitting a signed lane from a = 128 on. That is the
|
||||
* same in all 16777216 cases. */
|
||||
KERNEL __m64
|
||||
blend (__m64 s, __m64 d, __m64 a, const gint how)
|
||||
{
|
||||
if (how == AMD3D || how == AMD3D_EXT)
|
||||
{
|
||||
__m64 scaled = _mm_slli_pi16 (a, 8);
|
||||
__m64 mask = _mm_srai_pi16 (scaled, 15);
|
||||
__m64 coeff = _mm_sub_pi16 (_mm_add_pi16 (a, scaled), mask);
|
||||
__m64 delta = _mm_sub_pi16 (s, d);
|
||||
|
||||
return _mm_add_pi16 (_mm_add_pi16 (pmulhrw (delta, coeff), _mm_and_si64 (delta, mask)), d);
|
||||
}
|
||||
else
|
||||
{
|
||||
__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));
|
||||
|
||||
/* t * 257 >> 16 is (t + (t >> 8)) >> 8 */
|
||||
if (how == EXT)
|
||||
return pmulhuw (t, _mm_set1_pi16 (257));
|
||||
return _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
|
||||
}
|
||||
}
|
||||
|
||||
/* c * a / 255, rounded the same way */
|
||||
KERNEL __m64
|
||||
premultiply (__m64 c, __m64 a, const gint how)
|
||||
{
|
||||
if (how == AMD3D || how == AMD3D_EXT)
|
||||
return blend (c, _mm_setzero_si64 (), a, how);
|
||||
else
|
||||
{
|
||||
__m64 t = _mm_add_pi16 (_mm_mullo_pi16 (c, a), _mm_set1_pi16 (0x80));
|
||||
|
||||
if (how == EXT)
|
||||
return pmulhuw (t, _mm_set1_pi16 (257));
|
||||
return _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
|
||||
}
|
||||
}
|
||||
|
||||
/* The alpha of a pixel in 16-bit lanes in all four lanes */
|
||||
KERNEL __m64
|
||||
alpha_of (__m64 v, const gint how)
|
||||
{
|
||||
if (how == EXT || how == AMD3D_EXT)
|
||||
{
|
||||
PSHUFW (v, 0xff);
|
||||
return 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)
|
||||
/* Two RGBA pixels as 16-bit lanes B, G, R, A each: the order of the
|
||||
* bytes of 0xAARRGGBB */
|
||||
KERNEL void
|
||||
unpack_bgra (__m64 s, __m64 *lo, __m64 *hi, const gint how)
|
||||
{
|
||||
const __m64 zero = _mm_setzero_si64 ();
|
||||
|
||||
if (how == EXT || how == AMD3D_EXT)
|
||||
{
|
||||
__m64 l = _mm_unpacklo_pi8 (s, zero);
|
||||
__m64 h = _mm_unpackhi_pi8 (s, zero);
|
||||
|
||||
PSHUFW (l, 0xc6);
|
||||
PSHUFW (h, 0xc6);
|
||||
*lo = l;
|
||||
*hi = h;
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
KERNEL gint
|
||||
composite_x888_k (guint32 *pixels, const guchar *rgba, gint n, const gint how)
|
||||
{
|
||||
const __m64 zero = _mm_setzero_si64 ();
|
||||
gint i;
|
||||
|
|
@ -152,15 +300,9 @@ composite_x888 (guint32 *pixels, const guchar *rgba, gint n)
|
|||
__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));
|
||||
unpack_bgra (s, &lo, &hi, how);
|
||||
lo = blend (lo, _mm_unpacklo_pi8 (d, zero), alpha_of (lo, how), how);
|
||||
hi = blend (hi, _mm_unpackhi_pi8 (d, zero), alpha_of (hi, how), how);
|
||||
v = _mm_or_si64 (_mm_packs_pu16 (lo, hi), SPLAT (0xff000000));
|
||||
|
||||
/* fully transparent pixels stay as they are */
|
||||
|
|
@ -172,9 +314,12 @@ composite_x888 (guint32 *pixels, const guchar *rgba, gint n)
|
|||
return i;
|
||||
}
|
||||
|
||||
static gint
|
||||
composite_565 (guint16 *pixels, const guchar *rgba, gint n)
|
||||
/* Four 565 or 555 pixels at a time, each channel in a register */
|
||||
KERNEL gint
|
||||
composite_16_k (guint16 *pixels, const guchar *rgba, gint n, const gint green_bits, const gint how)
|
||||
{
|
||||
const gint red_shift = 5 + green_bits;
|
||||
const __m64 zero = _mm_setzero_si64 ();
|
||||
gint i;
|
||||
|
||||
for (i = 0; i + 4 <= n; i += 4, rgba += 16)
|
||||
|
|
@ -188,51 +333,54 @@ composite_565 (guint16 *pixels, const guchar *rgba, gint n)
|
|||
__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 dr = _mm_and_si64 (_mm_srli_pi16 (d, red_shift), _mm_set1_pi16 (0x1f));
|
||||
__m64 dg = _mm_and_si64 (_mm_srli_pi16 (d, 5), _mm_set1_pi16 ((1 << green_bits) - 1));
|
||||
__m64 db = _mm_and_si64 (d, _mm_set1_pi16 (0x1f));
|
||||
__m64 v;
|
||||
|
||||
/* 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));
|
||||
if (green_bits == 6)
|
||||
dg = _mm_or_si64 (_mm_slli_pi16 (dg, 2), _mm_srli_pi16 (dg, 4));
|
||||
else
|
||||
dg = _mm_or_si64 (_mm_slli_pi16 (dg, 3), _mm_srli_pi16 (dg, 2));
|
||||
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)));
|
||||
r = blend (r, dr, a, how);
|
||||
g = blend (g, dg, a, how);
|
||||
b = blend (b, db, a, how);
|
||||
v = _mm_or_si64 (_mm_or_si64 (_mm_slli_pi16 (_mm_srli_pi16 (r, 3), red_shift),
|
||||
_mm_slli_pi16 (_mm_srli_pi16 (g, 8 - green_bits), 5)),
|
||||
_mm_srli_pi16 (b, 3));
|
||||
|
||||
if (green_bits == 5)
|
||||
{
|
||||
/* the unused bit is set, except where nothing is drawn */
|
||||
__m64 transparent = _mm_cmpeq_pi16 (a, zero);
|
||||
|
||||
v = _mm_or_si64 (_mm_and_si64 (transparent, d),
|
||||
_mm_andnot_si64 (transparent, _mm_or_si64 (v, _mm_set1_pi16 ((gshort) 0x8000))));
|
||||
}
|
||||
store8 (pixels + i, v);
|
||||
}
|
||||
_mm_empty ();
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/* c * a / 255 to the nearest */
|
||||
static gint
|
||||
rgba_to_argb (guint32 *out, const guchar *rgba, gint n)
|
||||
KERNEL gint
|
||||
rgba_to_argb_k (guint32 *out, const guchar *rgba, gint n, const gint how)
|
||||
{
|
||||
const __m64 zero = _mm_setzero_si64 ();
|
||||
gint i;
|
||||
|
||||
for (i = 0; i + 2 <= n; i += 2, rgba += 8)
|
||||
{
|
||||
__m64 s = load8 (rgba);
|
||||
__m64 bgra, lo, hi, t;
|
||||
__m64 lo, hi;
|
||||
|
||||
/* RGBA to BGRA, the bytes of 0xAARRGGBB */
|
||||
bgra = _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 (bgra, zero);
|
||||
hi = _mm_unpackhi_pi8 (bgra, zero);
|
||||
|
||||
t = _mm_add_pi16 (_mm_mullo_pi16 (lo, alpha_of (lo)), _mm_set1_pi16 (0x80));
|
||||
lo = _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
|
||||
t = _mm_add_pi16 (_mm_mullo_pi16 (hi, alpha_of (hi)), _mm_set1_pi16 (0x80));
|
||||
hi = _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
|
||||
unpack_bgra (s, &lo, &hi, how);
|
||||
lo = premultiply (lo, alpha_of (lo, how), how);
|
||||
hi = premultiply (hi, alpha_of (hi, how), how);
|
||||
|
||||
/* the alpha byte as it was */
|
||||
store8 (out + i, _mm_or_si64 (_mm_and_si64 (_mm_packs_pu16 (lo, hi), SPLAT (0x00ffffff)),
|
||||
|
|
@ -243,23 +391,90 @@ rgba_to_argb (guint32 *out, const guchar *rgba, gint n)
|
|||
return i;
|
||||
}
|
||||
|
||||
#define DEFINE_KERNELS(suffix, how) \
|
||||
static gint \
|
||||
composite_x888_##suffix (guint32 *pixels, const guchar *rgba, gint n) \
|
||||
{ \
|
||||
return composite_x888_k (pixels, rgba, n, how); \
|
||||
} \
|
||||
\
|
||||
static gint \
|
||||
composite_565_##suffix (guint16 *pixels, const guchar *rgba, gint n) \
|
||||
{ \
|
||||
return composite_16_k (pixels, rgba, n, 6, how); \
|
||||
} \
|
||||
\
|
||||
static gint \
|
||||
composite_555_##suffix (guint16 *pixels, const guchar *rgba, gint n) \
|
||||
{ \
|
||||
return composite_16_k (pixels, rgba, n, 5, how); \
|
||||
} \
|
||||
\
|
||||
static gint \
|
||||
rgba_to_argb_##suffix (guint32 *out, const guchar *rgba, gint n) \
|
||||
{ \
|
||||
return rgba_to_argb_k (out, rgba, n, how); \
|
||||
} \
|
||||
\
|
||||
static void \
|
||||
init_##suffix (GdkRgbSimd *simd, const gchar *name) \
|
||||
{ \
|
||||
simd->name = name; \
|
||||
simd->rgb_to_x888 = rgb_to_x888; \
|
||||
simd->rgbx_to_x888 = rgbx_to_x888; \
|
||||
simd->rgb_to_565 = rgb_to_565; \
|
||||
simd->rgb_to_555 = rgb_to_555; \
|
||||
simd->composite_x888 = composite_x888_##suffix; \
|
||||
simd->composite_565 = composite_565_##suffix; \
|
||||
simd->composite_555 = composite_555_##suffix; \
|
||||
simd->rgba_to_argb = rgba_to_argb_##suffix; \
|
||||
}
|
||||
|
||||
DEFINE_KERNELS (mmx, PLAIN)
|
||||
|
||||
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;
|
||||
simd->rgba_to_argb = rgba_to_argb;
|
||||
init_mmx (simd, "mmx");
|
||||
}
|
||||
|
||||
#if defined(HAVE_LATER_MMX)
|
||||
|
||||
DEFINE_KERNELS (ext, EXT)
|
||||
DEFINE_KERNELS (3dnow, AMD3D)
|
||||
DEFINE_KERNELS (3dnowext, AMD3D_EXT)
|
||||
|
||||
void
|
||||
_gdk_rgb_simd_init_mmxext (GdkRgbSimd *simd)
|
||||
{
|
||||
init_ext (simd, "mmxext");
|
||||
}
|
||||
|
||||
void
|
||||
_gdk_rgb_simd_init_3dnow (GdkRgbSimd *simd)
|
||||
{
|
||||
init_3dnow (simd, "3dnow");
|
||||
}
|
||||
|
||||
void
|
||||
_gdk_rgb_simd_init_3dnowext (GdkRgbSimd *simd)
|
||||
{
|
||||
init_3dnowext (simd, "3dnowext");
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void
|
||||
_gdk_rgb_simd_init_mmx (GdkRgbSimd *simd)
|
||||
{
|
||||
}
|
||||
void _gdk_rgb_simd_init_mmxext (GdkRgbSimd *simd) { }
|
||||
void _gdk_rgb_simd_init_3dnow (GdkRgbSimd *simd) { }
|
||||
void _gdk_rgb_simd_init_3dnowext (GdkRgbSimd *simd) { }
|
||||
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
void _gdk_rgb_simd_init_mmx (GdkRgbSimd *simd) { }
|
||||
void _gdk_rgb_simd_init_mmxext (GdkRgbSimd *simd) { }
|
||||
void _gdk_rgb_simd_init_3dnow (GdkRgbSimd *simd) { }
|
||||
void _gdk_rgb_simd_init_3dnowext (GdkRgbSimd *simd) { }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
190
gdk/gdkrgbsimd.c
190
gdk/gdkrgbsimd.c
|
|
@ -10,6 +10,7 @@
|
|||
#include "config.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "gdkrgbsimd.h"
|
||||
|
||||
|
|
@ -26,6 +27,33 @@
|
|||
#endif
|
||||
#endif
|
||||
|
||||
/* All instruction sets, from the least to the most preferred: each
|
||||
* kernel comes from the last set that has it and the CPU can run */
|
||||
static const GdkRgbSimdSet all_sets[] =
|
||||
{
|
||||
{ "mmx", _gdk_rgb_simd_init_mmx },
|
||||
{ "mmxext", _gdk_rgb_simd_init_mmxext },
|
||||
{ "3dnow", _gdk_rgb_simd_init_3dnow },
|
||||
{ "3dnowext", _gdk_rgb_simd_init_3dnowext },
|
||||
{ "sse2", _gdk_rgb_simd_init_sse2 },
|
||||
{ "altivec", _gdk_rgb_simd_init_altivec },
|
||||
{ "neon", _gdk_rgb_simd_init_neon },
|
||||
};
|
||||
|
||||
static const GdkRgbSimdOp all_ops[] =
|
||||
{
|
||||
{ "rgb_to_x888", G_STRUCT_OFFSET (GdkRgbSimd, rgb_to_x888) },
|
||||
{ "rgbx_to_x888", G_STRUCT_OFFSET (GdkRgbSimd, rgbx_to_x888) },
|
||||
{ "rgb_to_565", G_STRUCT_OFFSET (GdkRgbSimd, rgb_to_565) },
|
||||
{ "rgb_to_555", G_STRUCT_OFFSET (GdkRgbSimd, rgb_to_555) },
|
||||
{ "composite_x888", G_STRUCT_OFFSET (GdkRgbSimd, composite_x888) },
|
||||
{ "composite_565", G_STRUCT_OFFSET (GdkRgbSimd, composite_565) },
|
||||
{ "composite_555", G_STRUCT_OFFSET (GdkRgbSimd, composite_555) },
|
||||
{ "rgba_to_argb", G_STRUCT_OFFSET (GdkRgbSimd, rgba_to_argb) },
|
||||
};
|
||||
|
||||
#define OP(simd, i) G_STRUCT_MEMBER (gpointer, (simd), all_ops[i].offset)
|
||||
|
||||
/* The CPU's hardware capabilities word, as the kernel tells them */
|
||||
#if (defined(__powerpc__) || defined(__arm__)) && \
|
||||
(defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO))
|
||||
|
|
@ -44,44 +72,150 @@ hwcap (void)
|
|||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
detect (GdkRgbSimd *simd)
|
||||
/* Whether this CPU can run the set */
|
||||
static gboolean
|
||||
cpu_has (const gchar *set)
|
||||
{
|
||||
#if defined(__x86_64__)
|
||||
/* every x86-64 CPU has SSE2 */
|
||||
_gdk_rgb_simd_init_sse2 (simd);
|
||||
/* every x86-64 CPU has SSE2, which beats the older sets */
|
||||
return strcmp (set, "sse2") == 0;
|
||||
|
||||
#elif defined(__i386__) && defined(__GNUC__)
|
||||
unsigned int eax, ebx, ecx, edx;
|
||||
unsigned int eax, ebx, ecx, edx, ext = 0, max_ext;
|
||||
|
||||
if (__get_cpuid (1, &eax, &ebx, &ecx, &edx))
|
||||
if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
|
||||
return FALSE;
|
||||
/* CPUs without the extended leaves answer with something else */
|
||||
max_ext = __get_cpuid_max (0x80000000, NULL);
|
||||
if ((max_ext & 0xffff0000) == 0x80000000 && max_ext >= 0x80000001)
|
||||
{
|
||||
if (edx & (1 << 23))
|
||||
_gdk_rgb_simd_init_mmx (simd);
|
||||
if (edx & (1 << 26))
|
||||
_gdk_rgb_simd_init_sse2 (simd);
|
||||
unsigned int a, b, c;
|
||||
|
||||
__cpuid (0x80000001, a, b, c, ext);
|
||||
}
|
||||
|
||||
if (strcmp (set, "mmx") == 0)
|
||||
return (edx & (1 << 23)) != 0;
|
||||
/* the integer part of SSE, or AMD's extensions to MMX */
|
||||
if (strcmp (set, "mmxext") == 0)
|
||||
return (edx & (1 << 25)) != 0 || (ext & (1 << 22)) != 0;
|
||||
if (strcmp (set, "3dnow") == 0)
|
||||
return (ext & (1u << 31)) != 0;
|
||||
if (strcmp (set, "3dnowext") == 0)
|
||||
return (ext & (1 << 30)) != 0 && (ext & (1u << 31)) != 0 && (ext & (1 << 22)) != 0;
|
||||
if (strcmp (set, "sse2") == 0)
|
||||
return (edx & (1 << 26)) != 0;
|
||||
return FALSE;
|
||||
|
||||
#elif defined(__aarch64__)
|
||||
_gdk_rgb_simd_init_neon (simd);
|
||||
return strcmp (set, "neon") == 0;
|
||||
|
||||
#elif defined(__arm__) && (defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO))
|
||||
if (hwcap () & (1 << 12)) /* HWCAP_NEON */
|
||||
_gdk_rgb_simd_init_neon (simd);
|
||||
return strcmp (set, "neon") == 0 && (hwcap () & (1 << 12)) != 0; /* HWCAP_NEON */
|
||||
|
||||
#elif defined(__powerpc__) && (defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO))
|
||||
if (hwcap () & 0x10000000) /* PPC_FEATURE_HAS_ALTIVEC */
|
||||
_gdk_rgb_simd_init_altivec (simd);
|
||||
return strcmp (set, "altivec") == 0 && (hwcap () & 0x10000000) != 0; /* PPC_FEATURE_HAS_ALTIVEC */
|
||||
|
||||
#elif defined(__ppc__) && defined(__APPLE__)
|
||||
int vector_unit = 0;
|
||||
size_t size = sizeof (vector_unit);
|
||||
|
||||
if (sysctlbyname ("hw.vectorunit", &vector_unit, &size, NULL, 0) == 0 && vector_unit)
|
||||
_gdk_rgb_simd_init_altivec (simd);
|
||||
return strcmp (set, "altivec") == 0 &&
|
||||
sysctlbyname ("hw.vectorunit", &vector_unit, &size, NULL, 0) == 0 && vector_unit;
|
||||
#else
|
||||
return FALSE;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* _gdk_rgb_simd_sets:
|
||||
* @n_sets: the number of sets
|
||||
*
|
||||
* The instruction sets this CPU can run, least preferred first. The
|
||||
* init function of a set fills in only what is in it.
|
||||
*/
|
||||
const GdkRgbSimdSet *
|
||||
_gdk_rgb_simd_sets (gint *n_sets)
|
||||
{
|
||||
static GdkRgbSimdSet sets[G_N_ELEMENTS (all_sets)];
|
||||
static gint n = -1;
|
||||
guint i;
|
||||
|
||||
if (n < 0)
|
||||
{
|
||||
n = 0;
|
||||
for (i = 0; i < G_N_ELEMENTS (all_sets); i++)
|
||||
if (cpu_has (all_sets[i].name))
|
||||
{
|
||||
GdkRgbSimd probe = { NULL, };
|
||||
|
||||
/* a set the compiler couldn't build fills in nothing */
|
||||
all_sets[i].init (&probe);
|
||||
if (probe.name)
|
||||
sets[n++] = all_sets[i];
|
||||
}
|
||||
}
|
||||
|
||||
*n_sets = n;
|
||||
return sets;
|
||||
}
|
||||
|
||||
/**
|
||||
* _gdk_rgb_simd_ops:
|
||||
* @n_ops: the number of operations
|
||||
*
|
||||
* The names of the kernels and where they are in a #GdkRgbSimd.
|
||||
*/
|
||||
const GdkRgbSimdOp *
|
||||
_gdk_rgb_simd_ops (gint *n_ops)
|
||||
{
|
||||
*n_ops = G_N_ELEMENTS (all_ops);
|
||||
return all_ops;
|
||||
}
|
||||
|
||||
/* GDK_RGB_SIMD picks sets by hand, to try them: "c" for none, a set
|
||||
* for that one wherever it has a kernel, "op=set" or "op=c" for one
|
||||
* kernel; several of these separated by commas, applied in order. */
|
||||
static void
|
||||
choose_by_hand (GdkRgbSimd *simd,
|
||||
const gchar *spec,
|
||||
const GdkRgbSimdSet *sets,
|
||||
gint n_sets)
|
||||
{
|
||||
gchar **items = g_strsplit (spec, ",", -1);
|
||||
gint i, k, s;
|
||||
|
||||
for (i = 0; items[i]; i++)
|
||||
{
|
||||
gchar *item = g_strstrip (items[i]);
|
||||
gchar *eq = strchr (item, '=');
|
||||
const gchar *set = eq ? eq + 1 : item;
|
||||
GdkRgbSimd from = { NULL, };
|
||||
|
||||
if (eq)
|
||||
*eq = '\0';
|
||||
|
||||
for (s = 0; s < n_sets; s++)
|
||||
if (strcmp (sets[s].name, set) == 0)
|
||||
{
|
||||
sets[s].init (&from);
|
||||
break;
|
||||
}
|
||||
if (s == n_sets && strcmp (set, "c") != 0)
|
||||
{
|
||||
g_warning ("GDK_RGB_SIMD: %s is not an instruction set this CPU has", set);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (k = 0; k < (gint) G_N_ELEMENTS (all_ops); k++)
|
||||
if (!eq || strcmp (item, all_ops[k].name) == 0)
|
||||
OP (simd, k) = OP (&from, k);
|
||||
}
|
||||
|
||||
simd->name = "by hand";
|
||||
g_strfreev (items);
|
||||
}
|
||||
|
||||
const GdkRgbSimd *
|
||||
_gdk_rgb_simd_get (void)
|
||||
{
|
||||
|
|
@ -91,10 +225,28 @@ _gdk_rgb_simd_get (void)
|
|||
if (!done)
|
||||
{
|
||||
const gchar *disable = g_getenv ("GDK_DISABLE_SIMD");
|
||||
const gchar *spec = g_getenv ("GDK_RGB_SIMD");
|
||||
const GdkRgbSimdSet *sets;
|
||||
gint n_sets, s, k;
|
||||
|
||||
if (!disable || !*disable || *disable == '0')
|
||||
detect (&simd);
|
||||
done = TRUE;
|
||||
if (disable && *disable && *disable != '0')
|
||||
return &simd;
|
||||
|
||||
sets = _gdk_rgb_simd_sets (&n_sets);
|
||||
for (s = 0; s < n_sets; s++)
|
||||
{
|
||||
GdkRgbSimd set = { NULL, };
|
||||
|
||||
sets[s].init (&set);
|
||||
for (k = 0; k < (gint) G_N_ELEMENTS (all_ops); k++)
|
||||
if (OP (&set, k))
|
||||
OP (&simd, k) = OP (&set, k);
|
||||
simd.name = set.name;
|
||||
}
|
||||
|
||||
if (spec && *spec)
|
||||
choose_by_hand (&simd, spec, sets, n_sets);
|
||||
}
|
||||
|
||||
return &simd;
|
||||
|
|
|
|||
|
|
@ -47,9 +47,24 @@ struct _GdkRgbSimd
|
|||
gint (*rgba_to_argb) (guint32 *out, const guchar *rgba, gint n);
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const gchar *name;
|
||||
void (*init) (GdkRgbSimd *simd);
|
||||
} GdkRgbSimdSet;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const gchar *name;
|
||||
glong offset;
|
||||
} GdkRgbSimdOp;
|
||||
|
||||
/* 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_mmxext (GdkRgbSimd *simd);
|
||||
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_altivec (GdkRgbSimd *simd);
|
||||
void _gdk_rgb_simd_init_neon (GdkRgbSimd *simd);
|
||||
|
|
@ -57,6 +72,10 @@ void _gdk_rgb_simd_init_neon (GdkRgbSimd *simd);
|
|||
/* The kernels for this CPU, or none with GDK_DISABLE_SIMD set */
|
||||
const GdkRgbSimd *_gdk_rgb_simd_get (void);
|
||||
|
||||
/* For trying them: the sets this CPU has and the kernels by name */
|
||||
const GdkRgbSimdSet *_gdk_rgb_simd_sets (gint *n_sets);
|
||||
const GdkRgbSimdOp *_gdk_rgb_simd_ops (gint *n_ops);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GDK_RGB_SIMD_H__ */
|
||||
|
|
|
|||
Loading…
Reference in a new issue