diff --git a/docs/reference/gtk/running.sgml b/docs/reference/gtk/running.sgml index 1721ba239..a27c86581 100644 --- a/docs/reference/gtk/running.sgml +++ b/docs/reference/gtk/running.sgml @@ -407,6 +407,24 @@ nevertheless. + + <envar>GDK_DISABLE_SIMD</envar>, <envar>GDK_RGB_SIMD</envar> + + + GDK converts and blends pixels with the vector instructions of the + CPU where it has them (MMX, the integer part of SSE, 3DNow!, SSE2, + AltiVec, NEON), with the same results as its C code. If + GDK_DISABLE_SIMD is set to anything but 0, only the + C code is used. GDK_RGB_SIMD chooses by hand, to + compare: c for none, the name of an instruction + set for that one, or kernel=set for a single + kernel, several separated by commas. The program + rgbsimdbench in the gdk + directory of the sources (make rgbsimdbench) + lists the kernels and sets and times them on the machine. + + + <envar>XDG_DATA_HOME</envar>, <envar>XDG_DATA_DIRS</envar> diff --git a/gdk/Makefile.am b/gdk/Makefile.am index b74627918..042caf505 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -50,7 +50,7 @@ endif # libtool stuff: set version and export symbols for resolving # since automake doesn't support conditionalized libsomething_la_LDFLAGS # we use the general approach here -LDADD = \ +gdk_ldflags = \ $(GTK_LINK_FLAGS) \ -version-info $(LT_VERSION_INFO) \ -export-dynamic \ @@ -186,22 +186,30 @@ libgdk_rgb_neon_la_CFLAGS = $(NEON_CFLAGS) gdk_rgb_simd_libs = $(noinst_LTLIBRARIES) +# Checks and times the kernels on this machine against the C code: +# make rgbsimdbench && ./rgbsimdbench +EXTRA_PROGRAMS = rgbsimdbench +rgbsimdbench_SOURCES = rgbsimdbench.c gdkrgbsimd.c +rgbsimdbench_CFLAGS = $(AM_CFLAGS) +rgbsimdbench_LDADD = $(gdk_rgb_simd_libs) $(GDK_DEP_LIBS) +CLEANFILES += rgbsimdbench + libgdk_directfb_2_0_la_SOURCES = $(common_sources) 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_directfb_2_0_la_LDFLAGS = $(gdk_ldflags) libgdk_x11_2_0_la_SOURCES = $(common_sources) 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_x11_2_0_la_LDFLAGS = $(gdk_ldflags) libgdk_quartz_2_0_la_SOURCES = $(common_sources) gdkkeynames.c 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_quartz_2_0_la_LDFLAGS = $(gdk_ldflags) libgdk_win32_2_0_la_SOURCES = $(common_sources) gdkkeynames.c 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) +libgdk_win32_2_0_la_LDFLAGS = -Wl,win32/rc/gdk-win32-res.o -export-symbols $(srcdir)/gdk.def $(gdk_ldflags) if HAVE_INTROSPECTION diff --git a/gdk/rgbsimdbench.c b/gdk/rgbsimdbench.c new file mode 100644 index 000000000..3a4402d2f --- /dev/null +++ b/gdk/rgbsimdbench.c @@ -0,0 +1,310 @@ +/* rgbsimdbench: checks and times GdkRGB's vector kernels on this machine + * + * Every kernel of every instruction set the CPU has is run against the + * C code it stands in for: first on random data, which must come out + * the same, then for speed. The table at the end shows millions of + * pixels per second; the fastest of each row is marked. + * + * make -C gdk rgbsimdbench && gdk/rgbsimdbench + * + * GDK_RGB_SIMD="op=set,..." tries a choice in a running program. + * + * 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 +#include + +#include + +#include "gdkrgbsimd.h" + +#define WIDTH 256 +#define ROWS 64 + +typedef gint (*Kernel) (gpointer out, gconstpointer in, gint n); + +static guint32 seed = 1; + +static guint +rnd (void) +{ + seed = seed * 1103515245 + 12345; + return seed >> 8; +} + +static inline guint +blend8 (guint a, guint s, guint d) +{ + guint t = a * s + (255 - a) * d + 0x80; + + return (t + (t >> 8)) >> 8; +} + +/* The C code of GdkRGB and gdk_cairo_set_source_pixbuf(), as plain as + * there, for one pixel at position i */ +static void +c_pixel (gint op, gpointer out, const guchar *s, gint i) +{ + guint32 *o32 = (guint32 *) out + i; + guint16 *o16 = (guint16 *) out + i; + guint r, g, b; + + switch (op) + { + case 0: /* rgb_to_x888 */ + case 1: /* rgbx_to_x888 */ + *o32 = 0xff000000 | (s[0] << 16) | (s[1] << 8) | s[2]; + break; + case 2: /* rgb_to_565 */ + *o16 = ((s[0] & 0xf8) << 8) | ((s[1] & 0xfc) << 3) | (s[2] >> 3); + break; + case 3: /* rgb_to_555 */ + *o16 = 0x8000 | ((s[0] & 0xf8) << 7) | ((s[1] & 0xf8) << 2) | (s[2] >> 3); + break; + case 4: /* composite_x888 */ + if (s[3] == 0) + break; + if (s[3] == 255) + *o32 = 0xff000000 | (s[0] << 16) | (s[1] << 8) | s[2]; + else + *o32 = 0xff000000 | (blend8 (s[3], s[0], (*o32 >> 16) & 0xff) << 16) | + (blend8 (s[3], s[1], (*o32 >> 8) & 0xff) << 8) | blend8 (s[3], s[2], *o32 & 0xff); + break; + case 5: /* composite_565 */ + if (s[3] == 0) + break; + r = *o16 >> 11, g = (*o16 >> 5) & 0x3f, b = *o16 & 0x1f; + r = blend8 (s[3], s[0], (r << 3) | (r >> 2)); + g = blend8 (s[3], s[1], (g << 2) | (g >> 4)); + b = blend8 (s[3], s[2], (b << 3) | (b >> 2)); + *o16 = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3); + break; + case 6: /* composite_555 */ + if (s[3] == 0) + break; + r = (*o16 >> 10) & 0x1f, g = (*o16 >> 5) & 0x1f, b = *o16 & 0x1f; + r = blend8 (s[3], s[0], (r << 3) | (r >> 2)); + g = blend8 (s[3], s[1], (g << 3) | (g >> 2)); + b = blend8 (s[3], s[2], (b << 3) | (b >> 2)); + *o16 = 0x8000 | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3); + break; + case 7: /* rgba_to_argb */ + *o32 = ((guint32) s[3] << 24) | (blend8 (s[3], s[0], 0) << 16) | + (blend8 (s[3], s[1], 0) << 8) | blend8 (s[3], s[2], 0); + break; + } +} + +static gint +in_bytes (gint op) +{ + return op == 0 || op == 2 || op == 3 ? 3 : 4; +} + +static gint +out_bytes (gint op) +{ + return op == 2 || op == 3 || op == 5 || op == 6 ? 2 : 4; +} + +static void +run_c (gint op, gpointer out, const guchar *in, gint n) +{ + gint i, step = in_bytes (op); + + for (i = 0; i < n; i++) + c_pixel (op, out, in + i * step, i); +} + +static void +run (gint op, Kernel kernel, gpointer out, const guchar *in, gint n) +{ + gint done = kernel ? kernel (out, in, n) : 0, i, step = in_bytes (op); + + /* the C code does the rest of the row */ + for (i = done; i < n; i++) + c_pixel (op, out, in + i * step, i); +} + +/* Source rows: for the alpha either like icons, mostly nothing or + * everything, or like soft shadows, everything in between */ +static void +fill_source (guchar *in, gint op, gboolean icons) +{ + gint i, n = ROWS * WIDTH * in_bytes (op); + + for (i = 0; i < n; i++) + in[i] = rnd (); + if (in_bytes (op) == 4) + for (i = 3; i < n; i += 4) + { + guint k = rnd () % 10; + + if (icons && k < 4) + in[i] = 0; + else if (icons && k < 8) + in[i] = 255; + } +} + +static gboolean +check (gint op, Kernel kernel) +{ + static guchar in[WIDTH * 4 + 16], a[WIDTH * 4 + 16], b[WIDTH * 4 + 16]; + gint t, i; + + for (t = 0; t < 2000; t++) + { + gint n = rnd () % WIDTH, ia = rnd () % 16, oa = (rnd () % 8) * 2; + + for (i = 0; i < (gint) sizeof in; i++) + in[i] = rnd (); + for (i = 3; i < (gint) sizeof in; i += 4) + if (rnd () % 3 == 0) + in[i] = rnd () % 2 ? 0 : 255; + for (i = 0; i < (gint) sizeof a; i++) + a[i] = b[i] = rnd (); + run_c (op, a + oa, in + ia, n); + run (op, kernel, b + oa, in + ia, n); + if (memcmp (a, b, sizeof a) != 0) + return FALSE; + } + + return TRUE; +} + +/* Millions of pixels per second */ +static gdouble +measure (gint op, Kernel kernel, gboolean c_only, gboolean icons) +{ + static guchar in[ROWS * WIDTH * 4], out[ROWS * WIDTH * 4]; + gint64 start, elapsed; + gint rounds = 0, y; + + seed = 7; + fill_source (in, op, icons); + for (y = 0; y < (gint) sizeof out; y++) + out[y] = rnd (); + + start = g_get_monotonic_time (); + do + { + for (y = 0; y < ROWS; y++) + { + const guchar *src = in + y * WIDTH * in_bytes (op); + guchar *dst = out + y * WIDTH * out_bytes (op); + + if (c_only) + run_c (op, dst, src, WIDTH); + else + run (op, kernel, dst, src, WIDTH); + } + rounds++; + elapsed = g_get_monotonic_time () - start; + } + while (elapsed < 300000); + + return (gdouble) rounds * ROWS * WIDTH / elapsed; +} + +static void +print_cpu (void) +{ + gchar *info = NULL, **lines; + gint i, shown = 0; + + if (!g_file_get_contents ("/proc/cpuinfo", &info, NULL, NULL)) + return; + lines = g_strsplit (info, "\n", -1); + for (i = 0; lines[i] && shown < 4; i++) + if (g_str_has_prefix (lines[i], "model name") || g_str_has_prefix (lines[i], "cpu\t") || + g_str_has_prefix (lines[i], "vendor_id") || g_str_has_prefix (lines[i], "flags") || + g_str_has_prefix (lines[i], "Features") || g_str_has_prefix (lines[i], "CPU part") || + g_str_has_prefix (lines[i], "Hardware")) + { + printf ("%.150s\n", lines[i]); + shown++; + } + g_strfreev (lines); + g_free (info); +} + +int +main (int argc, char **argv) +{ + const GdkRgbSimdSet *sets; + const GdkRgbSimdOp *ops; + const GdkRgbSimd *chosen; + gint n_sets, n_ops, s, k, pass; + gboolean all_good = TRUE; + + print_cpu (); + sets = _gdk_rgb_simd_sets (&n_sets); + ops = _gdk_rgb_simd_ops (&n_ops); + chosen = _gdk_rgb_simd_get (); + + printf ("\nMillions of pixels per second, * the fastest, < what GDK takes now\n\n"); + printf ("%-22s %8s", "", "c"); + for (s = 0; s < n_sets; s++) + printf (" %9s", sets[s].name); + printf ("\n"); + + for (k = 0; k < n_ops; k++) + for (pass = 0; pass < (in_bytes (k) == 4 && k != 1 ? 2 : 1); pass++) + { + gdouble speed[16], best = 0; + const gchar *mark[16]; + gchar label[64]; + Kernel now = G_STRUCT_MEMBER (Kernel, chosen, ops[k].offset); + + g_snprintf (label, sizeof label, "%s%s", ops[k].name, + in_bytes (k) == 4 && k != 1 ? (pass ? " soft" : " icons") : ""); + speed[0] = measure (k, NULL, TRUE, !pass); + best = speed[0]; + for (s = 0; s < n_sets; s++) + { + GdkRgbSimd set; + Kernel kernel; + + memset (&set, 0, sizeof set); + sets[s].init (&set); + kernel = G_STRUCT_MEMBER (Kernel, &set, ops[k].offset); + speed[s + 1] = -1; + mark[s + 1] = " "; + if (!kernel) + continue; + if (!check (k, kernel)) + { + printf ("%s of %s does not give what the C code does!\n", ops[k].name, sets[s].name); + all_good = FALSE; + continue; + } + speed[s + 1] = measure (k, kernel, FALSE, !pass); + if (speed[s + 1] > best) + best = speed[s + 1]; + if (kernel == now) + mark[s + 1] = "<"; + } + + printf ("%-22s %7.1f%s", label, speed[0], speed[0] == best ? "*" : (now ? " " : "<")); + for (s = 0; s < n_sets; s++) + { + if (speed[s + 1] < 0) + printf (" %9s", "-"); + else + printf (" %8.1f%s", speed[s + 1], speed[s + 1] == best ? "*" : mark[s + 1]); + } + printf ("\n"); + } + + if (!all_good) + printf ("\nSome kernels are wrong on this machine; please send this output.\n"); + + return all_good ? 0 : 1; +}