From cccc3550afc541b96cd227c725e5045af0f0e115 Mon Sep 17 00:00:00 2001 From: Daniel Sabo Date: Tue, 25 Jun 2013 03:25:19 -0700 Subject: [PATCH] SSE2 & SSE4.1 versions of GimpOperationNormalMode * Add configure checks for SSE intrinsic code. * Use SSE helper libraries in app/operations to compile code with different CFLAGS. --- app/operations/.gitignore | 3 + app/operations/Makefile.am | 33 ++++- app/operations/gimpoperationnormalmode-sse2.c | 117 ++++++++++++++++++ app/operations/gimpoperationnormalmode-sse4.c | 116 +++++++++++++++++ app/operations/gimpoperationnormalmode.c | 32 +++-- app/operations/gimpoperationnormalmode.h | 34 +++-- configure.ac | 36 ++++++ 7 files changed, 352 insertions(+), 19 deletions(-) create mode 100644 app/operations/gimpoperationnormalmode-sse2.c create mode 100644 app/operations/gimpoperationnormalmode-sse4.c diff --git a/app/operations/.gitignore b/app/operations/.gitignore index 5d7ce9ea07..321edd73d3 100644 --- a/app/operations/.gitignore +++ b/app/operations/.gitignore @@ -3,3 +3,6 @@ /.deps /.libs /libappoperations.a +/libappoperations-generic.a +/libappoperations-sse2.a +/libappoperations-sse4.a diff --git a/app/operations/Makefile.am b/app/operations/Makefile.am index 2f68d3b8fb..2505bb6d07 100644 --- a/app/operations/Makefile.am +++ b/app/operations/Makefile.am @@ -12,9 +12,12 @@ AM_CPPFLAGS = \ $(GDK_PIXBUF_CFLAGS) \ -I$(includedir) -noinst_LIBRARIES = libappoperations.a +noinst_LIBRARIES = libappoperations-generic.a \ + libappoperations-sse2.a \ + libappoperations-sse4.a \ + libappoperations.a -libappoperations_a_sources = \ +libappoperations_generic_a_sources = \ operations-types.h \ gimp-operations.c \ gimp-operations.h \ @@ -144,4 +147,28 @@ libappoperations_a_sources = \ gimplayermodefunctions.c \ gimplayermodefunctions.h -libappoperations_a_SOURCES = $(libappoperations_a_sources) +libappoperations_sse2_a_sources = \ + gimpoperationnormalmode-sse2.c + +libappoperations_sse4_a_sources = \ + gimpoperationnormalmode-sse4.c + +libappoperations_sse2_a_SOURCES = $(libappoperations_sse2_a_sources) + +libappoperations_sse2_a_CFLAGS = $(SSE2_EXTRA_CFLAGS) + +libappoperations_sse4_a_SOURCES = $(libappoperations_sse4_a_sources) + +libappoperations_sse4_a_CFLAGS = $(SSE4_1_EXTRA_CFLAGS) + +libappoperations_generic_a_SOURCES = $(libappoperations_generic_a_sources) + +libappoperations.a: libappoperations-generic.a \ + libappoperations-sse2.a \ + libappoperations-sse4.a + $(AR) $(ARFLAGS) libappoperations.a \ + $(libappoperations_generic_a_OBJECTS) \ + $(libappoperations_sse2_a_OBJECTS) \ + $(libappoperations_sse4_a_OBJECTS) + $(RANLIB) libappoperations.a + diff --git a/app/operations/gimpoperationnormalmode-sse2.c b/app/operations/gimpoperationnormalmode-sse2.c new file mode 100644 index 0000000000..c746470fc2 --- /dev/null +++ b/app/operations/gimpoperationnormalmode-sse2.c @@ -0,0 +1,117 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpoperationnormalmode-sse2.c + * Copyright (C) 2013 Daniel Sabo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "operations-types.h" + +#include "gimpoperationnormalmode.h" + +#if COMPILE_SSE2_INTRINISICS +/* SSE2 */ +#include + +gboolean +gimp_operation_normal_mode_process_pixels_sse2 (gfloat *in, + gfloat *aux, + gfloat *mask, + gfloat *out, + gfloat opacity, + glong samples, + const GeglRectangle *roi, + gint level) +{ + /* check alignment */ + if ((((uintptr_t)in) | ((uintptr_t)aux) | ((uintptr_t)out)) & 0x0F) + { + return gimp_operation_normal_mode_process_pixels_core (in, aux, mask, out, + opacity, samples, + roi, level); + } + else + { + const __v4sf *v_in = (const __v4sf*) in; + const __v4sf *v_aux = (const __v4sf*) aux; + __v4sf *v_out = ( __v4sf*) out; + + const __v4sf one = _mm_set1_ps (1.0f); + const __v4sf v_opacity = _mm_set1_ps (opacity); + + while (samples--) + { + __v4sf rgba_in, rgba_aux, alpha; + + rgba_in = *v_in++; + rgba_aux = *v_aux++; + + /* expand alpha */ + alpha = (__v4sf)_mm_shuffle_epi32 ((__m128i)rgba_aux, + _MM_SHUFFLE (3, 3, 3, 3)); + + if (mask) + { + __v4sf mask_alpha; + + /* multiply aux's alpha by the mask */ + mask_alpha = _mm_set1_ps (*mask++); + alpha = alpha * mask_alpha; + } + + alpha = alpha * v_opacity; + + if (_mm_ucomigt_ss (alpha, _mm_setzero_ps ())) + { + __v4sf dst_alpha, a_term, out_pixel, out_alpha, out_pixel_rbaa; + + /* expand alpha */ + dst_alpha = (__v4sf)_mm_shuffle_epi32 ((__m128i)rgba_in, + _MM_SHUFFLE (3, 3, 3, 3)); + + /* a_term = dst_a * (1.0 - src_a) */ + a_term = dst_alpha * (one - alpha); + + /* out(color) = src * src_a + dst * a_term */ + out_pixel = rgba_aux * alpha + rgba_in * a_term; + + /* out(alpha) = 1.0 * src_a + 1.0 * a_term */ + out_alpha = alpha + a_term; + + /* un-premultiply */ + out_pixel = out_pixel / out_alpha; + + /* swap in the real alpha */ + out_pixel_rbaa = _mm_shuffle_ps (out_pixel, out_alpha, _MM_SHUFFLE (3, 3, 2, 0)); + out_pixel = _mm_shuffle_ps (out_pixel, out_pixel_rbaa, _MM_SHUFFLE (2, 1, 1, 0)); + + *v_out++ = out_pixel; + } + else + { + *v_out++ = rgba_in; + } + } + } + + return TRUE; +} +#endif /* COMPILE_SSE2_INTRINISICS */ diff --git a/app/operations/gimpoperationnormalmode-sse4.c b/app/operations/gimpoperationnormalmode-sse4.c new file mode 100644 index 0000000000..37109ab907 --- /dev/null +++ b/app/operations/gimpoperationnormalmode-sse4.c @@ -0,0 +1,116 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpoperationnormalmode-sse2.c + * Copyright (C) 2013 Daniel Sabo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "operations-types.h" + +#include "gimpoperationnormalmode.h" + +#if COMPILE_SSE4_1_INTRINISICS +/* SSE4 */ +#include + +gboolean +gimp_operation_normal_mode_process_pixels_sse4 (gfloat *in, + gfloat *aux, + gfloat *mask, + gfloat *out, + gfloat opacity, + glong samples, + const GeglRectangle *roi, + gint level) +{ + /* check alignment */ + if ((((uintptr_t)in) | ((uintptr_t)aux) | ((uintptr_t)out)) & 0x0F) + { + return gimp_operation_normal_mode_process_pixels_core (in, aux, mask, out, + opacity, samples, + roi, level); + } + else + { + const __v4sf *v_in = (const __v4sf*) in; + const __v4sf *v_aux = (const __v4sf*) aux; + __v4sf *v_out = ( __v4sf*) out; + + const __v4sf one = _mm_set1_ps (1.0f); + const __v4sf v_opacity = _mm_set1_ps (opacity); + + while (samples--) + { + __v4sf rgba_in, rgba_aux, alpha; + + rgba_in = *v_in++; + rgba_aux = *v_aux++; + + /* expand alpha */ + alpha = (__v4sf)_mm_shuffle_epi32 ((__m128i)rgba_aux, + _MM_SHUFFLE (3, 3, 3, 3)); + + if (mask) + { + __v4sf mask_alpha; + + /* multiply aux's alpha by the mask */ + mask_alpha = _mm_set1_ps (*mask++); + alpha = alpha * mask_alpha; + } + + alpha = alpha * v_opacity; + + if (_mm_ucomigt_ss (alpha, _mm_setzero_ps ())) + { + __v4sf dst_alpha, a_term, out_pixel, out_alpha; + + /* expand alpha */ + dst_alpha = (__v4sf)_mm_shuffle_epi32 ((__m128i)rgba_in, + _MM_SHUFFLE (3, 3, 3, 3)); + + /* a_term = dst_a * (1.0 - src_a) */ + a_term = dst_alpha * (one - alpha); + + /* out(color) = src * src_a + dst * a_term */ + out_pixel = rgba_aux * alpha + rgba_in * a_term; + + /* out(alpha) = 1.0 * src_a + 1.0 * a_term */ + out_alpha = alpha + a_term; + + /* un-premultiply */ + out_pixel = out_pixel / out_alpha; + + /* swap in the real alpha */ + out_pixel = _mm_blend_ps (out_pixel, out_alpha, 0x08); + + *v_out++ = out_pixel; + } + else + { + *v_out++ = rgba_in; + } + } + } + + return TRUE; +} +#endif /* COMPILE_SSE4_1_INTRINISICS */ diff --git a/app/operations/gimpoperationnormalmode.c b/app/operations/gimpoperationnormalmode.c index 3c0725c061..fb8af23df5 100644 --- a/app/operations/gimpoperationnormalmode.c +++ b/app/operations/gimpoperationnormalmode.c @@ -23,10 +23,14 @@ #include +#include + #include "operations-types.h" #include "gimpoperationnormalmode.h" +GimpLayerModeFunction gimp_operation_normal_mode_process_pixels = NULL; + static gboolean gimp_operation_normal_parent_process (GeglOperation *operation, GeglOperationContext *context, @@ -84,6 +88,18 @@ gimp_operation_normal_mode_class_init (GimpOperationNormalModeClass *klass) operation_class->process = gimp_operation_normal_parent_process; point_class->process = gimp_operation_normal_mode_process; + + gimp_operation_normal_mode_process_pixels = gimp_operation_normal_mode_process_pixels_core; + +#if COMPILE_SSE2_INTRINISICS + if (gimp_cpu_accel_get_support() & GIMP_CPU_ACCEL_X86_SSE2) + gimp_operation_normal_mode_process_pixels = gimp_operation_normal_mode_process_pixels_sse2; +#endif /* COMPILE_SSE2_INTRINISICS */ + +#if COMPILE_SSE4_1_INTRINISICS + if (gimp_cpu_accel_get_support() & GIMP_CPU_ACCEL_X86_SSE4_1) + gimp_operation_normal_mode_process_pixels = gimp_operation_normal_mode_process_pixels_sse4; +#endif /* COMPILE_SSE4_1_INTRINISICS */ } static void @@ -162,14 +178,14 @@ gimp_operation_normal_mode_process (GeglOperation *operation, } gboolean -gimp_operation_normal_mode_process_pixels (gfloat *in, - gfloat *aux, - gfloat *mask, - gfloat *out, - gfloat opacity, - glong samples, - const GeglRectangle *roi, - gint level) +gimp_operation_normal_mode_process_pixels_core (gfloat *in, + gfloat *aux, + gfloat *mask, + gfloat *out, + gfloat opacity, + glong samples, + const GeglRectangle *roi, + gint level) { const gboolean has_mask = mask != NULL; diff --git a/app/operations/gimpoperationnormalmode.h b/app/operations/gimpoperationnormalmode.h index 27174ee3ac..338540e7b3 100644 --- a/app/operations/gimpoperationnormalmode.h +++ b/app/operations/gimpoperationnormalmode.h @@ -50,15 +50,33 @@ struct _GimpOperationNormalModeClass GType gimp_operation_normal_mode_get_type (void) G_GNUC_CONST; +extern GimpLayerModeFunction gimp_operation_normal_mode_process_pixels; -gboolean gimp_operation_normal_mode_process_pixels (gfloat *in, - gfloat *aux, - gfloat *mask, - gfloat *out, - gfloat opacity, - glong samples, - const GeglRectangle *roi, - gint level); +gboolean gimp_operation_normal_mode_process_pixels_core (gfloat *in, + gfloat *aux, + gfloat *mask, + gfloat *out, + gfloat opacity, + glong samples, + const GeglRectangle *roi, + gint level); +gboolean gimp_operation_normal_mode_process_pixels_sse2 (gfloat *in, + gfloat *aux, + gfloat *mask, + gfloat *out, + gfloat opacity, + glong samples, + const GeglRectangle *roi, + gint level); + +gboolean gimp_operation_normal_mode_process_pixels_sse4 (gfloat *in, + gfloat *aux, + gfloat *mask, + gfloat *out, + gfloat opacity, + glong samples, + const GeglRectangle *roi, + gint level); #endif /* __GIMP_OPERATION_NORMAL_MODE_H__ */ diff --git a/configure.ac b/configure.ac index ce64792cac..fe6924e93d 100644 --- a/configure.ac +++ b/configure.ac @@ -686,6 +686,42 @@ LIBS=$gimp_save_LIBS AC_SUBST(SOCKET_LIBS) +################################### +# Check for Intel vector intrinsics +################################### +intrinsics_save_CFLAGS="$CFLAGS" +#FIXME: Check the CFLAGS separatly +GIMP_DETECT_CFLAGS(SSE_MATH_CFLAG, '-mfpmath=sse') +GIMP_DETECT_CFLAGS(SSE2_CFLAG, '-msse2') +SSE2_EXTRA_CFLAGS="$SSE_MATH_CFLAG $SSE2_CFLAG" +CFLAGS="$intrinsics_save_CFLAGS $SSE2_EXTRA_CFLAGS" + +AC_MSG_CHECKING(whether we can compile SSE2 intrinsics) +AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ],[__m128i one = _mm_set1_epi32 (1);])], + AC_DEFINE(COMPILE_SSE2_INTRINISICS, 1, [Define to 1 if SSE2 intrinsics are available.]) + AC_SUBST(SSE2_EXTRA_CFLAGS) + AC_MSG_RESULT(yes) +, + AC_MSG_RESULT(no) + AC_MSG_WARN([SSE2 intrinsics not available.]) +) + + +GIMP_DETECT_CFLAGS(SSE4_1_CFLAG, '-msse4.1') +SSE4_1_EXTRA_CFLAGS="$SSE_MATH_CFLAG $SSE4_1_CFLAG" +CFLAGS="$intrinsics_save_CFLAGS $SSE4_1_EXTRA_CFLAGS" + +AC_MSG_CHECKING(whether we can compile SSE4.1 intrinsics) +AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ],[__v4sf a, b, c; c = _mm_blend_ps(a, b, 0x08);])], + AC_DEFINE(COMPILE_SSE4_1_INTRINISICS, 1, [Define to 1 if SSE4.1 intrinsics are available.]) + AC_SUBST(SSE4_1_EXTRA_CFLAGS) + AC_MSG_RESULT(yes) +, + AC_MSG_RESULT(no) + AC_MSG_WARN([SSE4.1 intrinsics not available.]) +) +CFLAGS="$intrinsics_save_CFLAGS" + ######################## # Check for MMX assembly