diff --git a/configure.ac b/configure.ac index 64a68bf9a..b26dcea9e 100644 --- a/configure.ac +++ b/configure.ac @@ -482,8 +482,9 @@ 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. +# at run time (getauxval() or elf_aux_info() where cpuid doesn't). # +AC_CHECK_FUNCS(getauxval elf_aux_info) dnl GTK_CHECK_SIMD_FLAGS(NAME, FLAGS|OTHER FLAGS, PROLOGUE, BODY): dnl sets NAME_CFLAGS to the first flags the test compiles with @@ -508,6 +509,7 @@ AC_SUBST($1_CFLAGS)]) MMX_CFLAGS= SSE2_CFLAGS= +ALTIVEC_CFLAGS= case "$host_cpu" in i?86|x86_64|amd64) GTK_CHECK_SIMD_FLAGS([MMX], [-mmmx], @@ -523,6 +525,14 @@ case "$host_cpu" in #endif], [__m128i v = _mm_setzero_si128 (); v = _mm_mullo_epi16 (v, v); (void) v;]) ;; + powerpc*|ppc*) + GTK_CHECK_SIMD_FLAGS([ALTIVEC], [-maltivec], + [#include + #if !defined(__ALTIVEC__) || !defined(__BIG_ENDIAN__) + #error no big endian AltiVec + #endif], + [vector unsigned short v = vec_splats ((unsigned short) 1); v = vec_mladd (v, v, v); (void) v;]) + ;; esac # _NL_TIME_FIRST_WEEKDAY is an enum and not a define diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 0fb460a1f..a9311ffea 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -171,12 +171,15 @@ common_sources = \ # 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-sse2.la \ + libgdk-rgb-altivec.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) +libgdk_rgb_altivec_la_SOURCES = gdkrgb-altivec.c +libgdk_rgb_altivec_la_CFLAGS = $(ALTIVEC_CFLAGS) gdk_rgb_simd_libs = $(noinst_LTLIBRARIES) diff --git a/gdk/gdkrgb-altivec.c b/gdk/gdkrgb-altivec.c new file mode 100644 index 000000000..2a294f5e4 --- /dev/null +++ b/gdk/gdkrgb-altivec.c @@ -0,0 +1,326 @@ +/* GDK - The GIMP Drawing Kit + * GdkRGB loops for AltiVec (PowerPC G4 and G5) + * + * 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(__ALTIVEC__) && defined(__BIG_ENDIAN__) + +#include + +/* An x888 pixel is the bytes 0xff, R, G, B; a 565 pixel is a big + * endian 16-bit word. Vector loads and stores need 16-byte aligned + * addresses: loads are put together from two, and each loop starts + * with single pixels until the output is aligned. */ + +typedef vector unsigned char vu8; +typedef vector unsigned short vu16; +typedef vector unsigned int vu32; + +#define VU8(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \ + ((vu8) { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p }) + +/* Reads 16 bytes after 16 bytes from any address, one aligned load + * each; never reads a block that holds none of the bytes asked for. + * Set up only when at least 16 bytes will be read. */ +typedef struct +{ + const guchar *p; + vu8 perm, last; + gboolean aligned; +} Reader; + +static inline void +reader_init (Reader *r, const guchar *p) +{ + r->p = p; + r->aligned = ((gsize) p & 15) == 0; + r->perm = vec_lvsl (0, p); + r->last = vec_ld (0, p); +} + +static inline vu8 +reader_next (Reader *r) +{ + vu8 v; + + if (r->aligned) + v = vec_ld (0, r->p); + else + { + /* the block of the 16th byte follows the block of the first */ + vu8 next = vec_ld (15, r->p); + + v = vec_perm (r->last, next, r->perm); + r->last = next; + } + r->p += 16; + + return v; +} + +static inline gboolean +aligned (const void *p) +{ + return ((gsize) p & 15) == 0; +} + +static inline guint32 +x888 (const guchar *p) +{ + return 0xff000000 | (p[0] << 16) | (p[1] << 8) | p[2]; +} + +static inline guint16 +rgb565 (const guchar *p) +{ + return ((p[0] & 0xf8) << 8) | ((p[1] & 0xfc) << 3) | (p[2] >> 3); +} + +static inline guint +blend8 (guint a, guint s, guint d) +{ + guint t = a * s + (255 - a) * d + 0x80; + + return (t + (t >> 8)) >> 8; +} + +static inline void +composite1_x888 (guint32 *pixel, const guchar *s) +{ + guint32 d = *pixel; + + if (s[3] != 0) + *pixel = 0xff000000 | + (blend8 (s[3], s[0], (d >> 16) & 0xff) << 16) | + (blend8 (s[3], s[1], (d >> 8) & 0xff) << 8) | + blend8 (s[3], s[2], d & 0xff); +} + +static inline void +composite1_565 (guint16 *pixel, const guchar *s) +{ + guint d = *pixel, r = d >> 11, g = (d >> 5) & 0x3f, b = d & 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)); + *pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3); +} + +/* 0xff in the first byte of each pixel */ +static inline vu8 +fill_x888 (void) +{ + /* -8 shifts by 24 */ + return (vu8) vec_sl (vec_splat_u32 (-1), vec_splat_u32 (-8)); +} + +static gint +rgb_to_x888 (guint32 *out, const guchar *rgb, gint n) +{ + const vu8 fill = fill_x888 (); + const vu8 p0 = VU8 (0, 0, 1, 2, 0, 3, 4, 5, 0, 6, 7, 8, 0, 9, 10, 11); + const vu8 p1 = VU8 (0, 12, 13, 14, 0, 15, 16, 17, 0, 18, 19, 20, 0, 21, 22, 23); + const vu8 p2 = VU8 (0, 8, 9, 10, 0, 11, 12, 13, 0, 14, 15, 16, 0, 17, 18, 19); + const vu8 p3 = VU8 (0, 4, 5, 6, 0, 7, 8, 9, 0, 10, 11, 12, 0, 13, 14, 15); + Reader r; + gint i; + + for (i = 0; i < n && !aligned (out + i); i++, rgb += 3) + out[i] = x888 (rgb); + + if (i + 16 <= n) + reader_init (&r, rgb); + for (; i + 16 <= n; i += 16) + { + vu8 a = reader_next (&r), b = reader_next (&r), c = reader_next (&r); + + vec_st (vec_or (vec_perm (a, a, p0), fill), 0, (guchar *) (out + i)); + vec_st (vec_or (vec_perm (a, b, p1), fill), 16, (guchar *) (out + i)); + vec_st (vec_or (vec_perm (b, c, p2), fill), 32, (guchar *) (out + i)); + vec_st (vec_or (vec_perm (c, c, p3), fill), 48, (guchar *) (out + i)); + } + + return i; +} + +static gint +rgbx_to_x888 (guint32 *out, const guchar *rgbx, gint n) +{ + const vu8 fill = fill_x888 (); + const vu8 p = VU8 (0, 0, 1, 2, 0, 4, 5, 6, 0, 8, 9, 10, 0, 12, 13, 14); + Reader r; + gint i; + + for (i = 0; i < n && !aligned (out + i); i++, rgbx += 4) + out[i] = x888 (rgbx); + + if (i + 4 <= n) + reader_init (&r, rgbx); + for (; i + 4 <= n; i += 4) + { + vu8 a = reader_next (&r); + + vec_st (vec_or (vec_perm (a, a, p), fill), 0, (guchar *) (out + i)); + } + + return i; +} + +/* Bytes of eight RGB pixels as 16-bit lanes, for one channel */ +static inline vu16 +channel (vu8 a, vu8 b, vu8 p) +{ + return vec_and ((vu16) vec_perm (a, b, p), vec_splats ((unsigned short) 0xff)); +} + +static inline vu16 +pack565 (vu16 r, vu16 g, vu16 b) +{ + return vec_or (vec_or (vec_sl (vec_and (r, vec_splats ((unsigned short) 0xf8)), vec_splats ((unsigned short) 8)), + vec_sl (vec_and (g, vec_splats ((unsigned short) 0xfc)), vec_splats ((unsigned short) 3))), + vec_sr (b, vec_splats ((unsigned short) 3))); +} + +static gint +rgb_to_565 (guint16 *out, const guchar *rgb, gint n) +{ + /* the channel byte of pixel k in both bytes of lane k */ + const vu8 r0 = VU8 (0, 0, 3, 3, 6, 6, 9, 9, 12, 12, 15, 15, 18, 18, 21, 21); + const vu8 g0 = VU8 (1, 1, 4, 4, 7, 7, 10, 10, 13, 13, 16, 16, 19, 19, 22, 22); + const vu8 b0 = VU8 (2, 2, 5, 5, 8, 8, 11, 11, 14, 14, 17, 17, 20, 20, 23, 23); + const vu8 r1 = VU8 (8, 8, 11, 11, 14, 14, 17, 17, 20, 20, 23, 23, 26, 26, 29, 29); + const vu8 g1 = VU8 (9, 9, 12, 12, 15, 15, 18, 18, 21, 21, 24, 24, 27, 27, 30, 30); + const vu8 b1 = VU8 (10, 10, 13, 13, 16, 16, 19, 19, 22, 22, 25, 25, 28, 28, 31, 31); + Reader r; + gint i; + + for (i = 0; i < n && !aligned (out + i); i++, rgb += 3) + out[i] = rgb565 (rgb); + + if (i + 16 <= n) + reader_init (&r, rgb); + for (; i + 16 <= n; i += 16) + { + vu8 a = reader_next (&r), b = reader_next (&r), c = reader_next (&r); + + vec_st (pack565 (channel (a, b, r0), channel (a, b, g0), channel (a, b, b0)), 0, out + i); + vec_st (pack565 (channel (b, c, r1), channel (b, c, g1), channel (b, c, b1)), 0, out + i + 8); + } + + return i; +} + +/* (a * s + (255 - a) * d) / 255, rounded, in 16-bit lanes */ +static inline vu16 +blend (vu16 t) +{ + t = vec_add (t, vec_splats ((unsigned short) 0x80)); + + return vec_sr (vec_add (t, vec_sr (t, vec_splats ((unsigned short) 8))), + vec_splats ((unsigned short) 8)); +} + +static gint +composite_x888 (guint32 *pixels, const guchar *rgba, gint n) +{ + const vu8 fill = fill_x888 (); + const vu8 argb = VU8 (3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14); + const vu8 alpha = VU8 (3, 3, 3, 3, 7, 7, 7, 7, 11, 11, 11, 11, 15, 15, 15, 15); + const vu8 merge = VU8 (1, 17, 3, 19, 5, 21, 7, 23, 9, 25, 11, 27, 13, 29, 15, 31); + Reader r; + gint i; + + for (i = 0; i < n && !aligned (pixels + i); i++, rgba += 4) + composite1_x888 (pixels + i, rgba); + + if (i + 4 <= n) + reader_init (&r, rgba); + for (; i + 4 <= n; i += 4) + { + vu8 s = reader_next (&r); + vu8 d = vec_ld (0, (guchar *) (pixels + i)); + vu8 a = vec_perm (s, s, alpha); + vu8 inv = vec_nor (a, a); + vu16 even, odd; + vu8 v; + + s = vec_perm (s, s, argb); + even = blend (vec_add (vec_mule (s, a), vec_mule (d, inv))); + odd = blend (vec_add (vec_mulo (s, a), vec_mulo (d, inv))); + v = vec_or (vec_perm ((vu8) even, (vu8) odd, merge), fill); + + /* fully transparent pixels stay as they are */ + vec_st (vec_sel (v, d, vec_cmpeq (a, vec_splat_u8 (0))), 0, (guchar *) (pixels + i)); + } + + return i; +} + +static gint +composite_565 (guint16 *pixels, const guchar *rgba, gint n) +{ + const vu8 pr = VU8 (0, 0, 4, 4, 8, 8, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28); + const vu8 pg = VU8 (1, 1, 5, 5, 9, 9, 13, 13, 17, 17, 21, 21, 25, 25, 29, 29); + const vu8 pb = VU8 (2, 2, 6, 6, 10, 10, 14, 14, 18, 18, 22, 22, 26, 26, 30, 30); + const vu8 pa = VU8 (3, 3, 7, 7, 11, 11, 15, 15, 19, 19, 23, 23, 27, 27, 31, 31); + const vu16 c255 = vec_splats ((unsigned short) 255); + Reader r; + gint i; + + for (i = 0; i < n && !aligned (pixels + i); i++, rgba += 4) + if (rgba[3] != 0) + composite1_565 (pixels + i, rgba); + + if (i + 8 <= n) + reader_init (&r, rgba); + for (; i + 8 <= n; i += 8) + { + vu8 s0 = reader_next (&r), s1 = reader_next (&r); + vu16 d = vec_ld (0, pixels + i); + vu16 a = channel (s0, s1, pa), inv = vec_sub (c255, a); + vu16 dr = vec_sr (d, vec_splats ((unsigned short) 11)); + vu16 dg = vec_and (vec_sr (d, vec_splats ((unsigned short) 5)), vec_splats ((unsigned short) 0x3f)); + vu16 db = vec_and (d, vec_splats ((unsigned short) 0x1f)); + + /* the bits of the pixel repeated up to 8 */ + dr = vec_or (vec_sl (dr, vec_splats ((unsigned short) 3)), vec_sr (dr, vec_splats ((unsigned short) 2))); + dg = vec_or (vec_sl (dg, vec_splats ((unsigned short) 2)), vec_sr (dg, vec_splats ((unsigned short) 4))); + db = vec_or (vec_sl (db, vec_splats ((unsigned short) 3)), vec_sr (db, vec_splats ((unsigned short) 2))); + + vec_st (pack565 (blend (vec_mladd (channel (s0, s1, pr), a, vec_mladd (dr, inv, vec_splats ((unsigned short) 0)))), + blend (vec_mladd (channel (s0, s1, pg), a, vec_mladd (dg, inv, vec_splats ((unsigned short) 0)))), + blend (vec_mladd (channel (s0, s1, pb), a, vec_mladd (db, inv, vec_splats ((unsigned short) 0))))), + 0, pixels + i); + } + + return i; +} + +void +_gdk_rgb_simd_init_altivec (GdkRgbSimd *simd) +{ + simd->name = "altivec"; + 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_altivec (GdkRgbSimd *simd) +{ +} + +#endif diff --git a/gdk/gdkrgbsimd.c b/gdk/gdkrgbsimd.c index cb7055a74..99f201d0f 100644 --- a/gdk/gdkrgbsimd.c +++ b/gdk/gdkrgbsimd.c @@ -17,6 +17,31 @@ #if defined(__GNUC__) #include #endif +#elif defined(__powerpc__) || defined(__ppc__) +#if defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO) +#include +#elif defined(__APPLE__) +#include +#include +#endif +#endif + +/* The CPU's hardware capabilities word, as the kernel tells them */ +#if defined(__powerpc__) && \ + (defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO)) +static unsigned long +hwcap (void) +{ +#if defined(HAVE_GETAUXVAL) + return getauxval (AT_HWCAP); +#else + unsigned long caps = 0; + + if (elf_aux_info (AT_HWCAP, &caps, sizeof (caps)) != 0) + return 0; + return caps; +#endif +} #endif static void @@ -37,6 +62,16 @@ detect (GdkRgbSimd *simd) _gdk_rgb_simd_init_sse2 (simd); } +#elif defined(__powerpc__) && (defined(HAVE_GETAUXVAL) || defined(HAVE_ELF_AUX_INFO)) + if (hwcap () & 0x10000000) /* PPC_FEATURE_HAS_ALTIVEC */ + _gdk_rgb_simd_init_altivec (simd); + +#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); #endif } diff --git a/gdk/gdkrgbsimd.h b/gdk/gdkrgbsimd.h index 159a002dd..abe9978a9 100644 --- a/gdk/gdkrgbsimd.h +++ b/gdk/gdkrgbsimd.h @@ -45,6 +45,7 @@ struct _GdkRgbSimd * only called when the CPU has it. */ void _gdk_rgb_simd_init_mmx (GdkRgbSimd *simd); void _gdk_rgb_simd_init_sse2 (GdkRgbSimd *simd); +void _gdk_rgb_simd_init_altivec (GdkRgbSimd *simd); /* The kernels for this CPU, or none with GDK_DISABLE_SIMD set */ const GdkRgbSimd *_gdk_rgb_simd_get (void); diff --git a/gdk/makefile.msc b/gdk/makefile.msc index 93c51369f..f255c9958 100644 --- a/gdk/makefile.msc +++ b/gdk/makefile.msc @@ -95,6 +95,7 @@ gdk_OBJECTS = \ gdkrgbsimd.obj \ gdkrgb-mmx.obj \ gdkrgb-sse2.obj \ + gdkrgb-altivec.obj \ gdkscreen.obj \ gdkselection.obj \ gdkvisual.obj \