Bug 817153 - AltiVec acceleration for qcms. r=jmuizelaar
authorTobias Netzel <tobias.netzel@googlemail.com>
Tue, 15 Jan 2013 19:44:43 -0500
changeset 118972 fc851f15ebd92f33a3b33fa832e352cad13983f1
parent 118971 f8d45d0d0304ff4618ad8d92b1185fd60bfe6ef8
child 118973 1f67a1c30736cb2dc55c18c3965836d750421f65
push id21387
push userryanvm@gmail.com
push dateWed, 16 Jan 2013 00:44:49 +0000
treeherdermozilla-inbound@2e0383dd35a3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmuizelaar
bugs817153
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 817153 - AltiVec acceleration for qcms. r=jmuizelaar
gfx/qcms/Makefile.in
gfx/qcms/qcmsint.h
gfx/qcms/transform-altivec.c
gfx/qcms/transform.c
--- a/gfx/qcms/Makefile.in
+++ b/gfx/qcms/Makefile.in
@@ -47,16 +47,23 @@ else
 	SSE2_FLAGS= -xO4
 endif
 else
 	SSE1_FLAGS=
 	SSE2_FLAGS=
 endif
 endif
 endif
+else
+ifeq (ppc,$(CPU_ARCH))
+ifdef GNU_CC
+	CSRCS += transform-altivec.c
+	ALTIVEC_FLAGS=-maltivec
+endif
+endif
 endif
 
 # -pedantic causes warnings that we don't care about
 # so turn it off
 ifdef GNU_CC
 OS_CFLAGS := $(filter-out -pedantic,$(OS_CFLAGS))
 endif
 
@@ -70,8 +77,10 @@ CFLAGS          += -DMOZ_QCMS
 ifdef GNU_CC
 CFLAGS += -Wno-missing-field-initializers
 endif # GNU_CC
 
 # special rules for transform-sse*.c to get the right cflags. (taken from pixman/src/Makefile.in)
 transform-sse1.$(OBJ_SUFFIX): COMPILE_CFLAGS += $(SSE1_FLAGS)
 
 transform-sse2.$(OBJ_SUFFIX): COMPILE_CFLAGS += $(SSE2_FLAGS)
+
+transform-altivec.$(OBJ_SUFFIX): COMPILE_CFLAGS += $(ALTIVEC_FLAGS)
--- a/gfx/qcms/qcmsint.h
+++ b/gfx/qcms/qcmsint.h
@@ -268,16 +268,25 @@ void qcms_transform_data_rgb_out_lut_sse
                                           unsigned char *src,
                                           unsigned char *dest,
                                           size_t length);
 void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
                                           unsigned char *src,
                                           unsigned char *dest,
                                           size_t length);
 
+void qcms_transform_data_rgb_out_lut_altivec(qcms_transform *transform,
+                                             unsigned char *src,
+                                             unsigned char *dest,
+                                             size_t length);
+void qcms_transform_data_rgba_out_lut_altivec(qcms_transform *transform,
+                                              unsigned char *src,
+                                              unsigned char *dest,
+                                              size_t length);
+
 extern qcms_bool qcms_supports_iccv4;
 
 #ifdef NATIVE_OUTPUT
 # define RGB_OUTPUT_COMPONENTS 4
 # define RGBA_OUTPUT_COMPONENTS 4
 # ifdef IS_LITTLE_ENDIAN
 #  define OUTPUT_A_INDEX 3
 #  define OUTPUT_R_INDEX 2
new file mode 100644
--- /dev/null
+++ b/gfx/qcms/transform-altivec.c
@@ -0,0 +1,269 @@
+/* vim: set ts=8 sw=8 noexpandtab: */
+//  qcms
+//  Copyright (C) 2009 Mozilla Corporation
+//  Copyright (C) 1998-2007 Marti Maria
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the Software
+// is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
+// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+#include <altivec.h>
+
+#include "qcmsint.h"
+
+#define FLOATSCALE (float)(PRECACHE_OUTPUT_SIZE)
+#define CLAMPMAXVAL (((float) (PRECACHE_OUTPUT_SIZE - 1)) / PRECACHE_OUTPUT_SIZE)
+static const ALIGN float floatScaleX4 = FLOATSCALE;
+static const ALIGN float clampMaxValueX4 = CLAMPMAXVAL;
+
+inline vector float load_aligned_float(float *dataPtr)
+{
+	vector float data = vec_lde(0, dataPtr);
+	vector unsigned char moveToStart = vec_lvsl(0, dataPtr);
+	return vec_perm(data, data, moveToStart);
+}
+
+void qcms_transform_data_rgb_out_lut_altivec(qcms_transform *transform,
+                                             unsigned char *src,
+                                             unsigned char *dest,
+                                             size_t length)
+{
+	unsigned int i;
+	float (*mat)[4] = transform->matrix;
+	char input_back[32];
+	/* Ensure we have a buffer that's 16 byte aligned regardless of the original
+	 * stack alignment. We can't use __attribute__((aligned(16))) or __declspec(align(32))
+	 * because they don't work on stack variables. gcc 4.4 does do the right thing
+	 * on x86 but that's too new for us right now. For more info: gcc bug #16660 */
+	float const *input = (float*)(((uintptr_t)&input_back[16]) & ~0xf);
+	/* share input and output locations to save having to keep the
+ 	 * locations in separate registers */
+	uint32_t const *output = (uint32_t*)input;
+
+	/* deref *transform now to avoid it in loop */
+	const float *igtbl_r = transform->input_gamma_table_r;
+	const float *igtbl_g = transform->input_gamma_table_g;
+	const float *igtbl_b = transform->input_gamma_table_b;
+
+	/* deref *transform now to avoid it in loop */
+	const uint8_t *otdata_r = &transform->output_table_r->data[0];
+	const uint8_t *otdata_g = &transform->output_table_g->data[0];
+	const uint8_t *otdata_b = &transform->output_table_b->data[0];
+
+	/* input matrix values never change */
+	const vector float mat0 = vec_ldl(0, (vector float*)mat[0]);
+	const vector float mat1 = vec_ldl(0, (vector float*)mat[1]);
+	const vector float mat2 = vec_ldl(0, (vector float*)mat[2]);
+
+	/* these values don't change, either */
+	const vector float max = vec_splat(vec_lde(0, (float*)&clampMaxValueX4), 0);
+	const vector float min = (vector float)vec_splat_u32(0);
+	const vector float scale = vec_splat(vec_lde(0, (float*)&floatScaleX4), 0);
+
+	/* working variables */
+	vector float vec_r, vec_g, vec_b, result;
+
+	/* CYA */
+	if (!length)
+		return;
+
+	/* one pixel is handled outside of the loop */
+	length--;
+
+	/* setup for transforming 1st pixel */
+	vec_r = load_aligned_float((float*)&igtbl_r[src[0]]);
+	vec_g = load_aligned_float((float*)&igtbl_r[src[1]]);
+	vec_b = load_aligned_float((float*)&igtbl_r[src[2]]);
+	src += 3;
+
+	/* transform all but final pixel */
+
+	for (i=0; i<length; i++)
+	{
+		/* position values from gamma tables */
+		vec_r = vec_splat(vec_r, 0);
+		vec_g = vec_splat(vec_g, 0);
+		vec_b = vec_splat(vec_b, 0);
+
+		/* gamma * matrix */
+		vec_r = vec_madd(vec_r, mat0, min);
+		vec_g = vec_madd(vec_g, mat1, min);
+		vec_b = vec_madd(vec_b, mat2, min);
+
+		/* crunch, crunch, crunch */
+		vec_r = vec_add(vec_r, vec_add(vec_g, vec_b));
+		vec_r = vec_max(min, vec_r);
+		vec_r = vec_min(max, vec_r);
+		result = vec_madd(vec_r, scale, min);
+
+		/* store calc'd output tables indices */
+		vec_st(vec_ctu(vec_round(result), 0), 0, (vector unsigned int*)output);
+
+		/* load for next loop while store completes */
+		vec_r = load_aligned_float((float*)&igtbl_r[src[0]]);
+		vec_g = load_aligned_float((float*)&igtbl_r[src[1]]);
+		vec_b = load_aligned_float((float*)&igtbl_r[src[2]]);
+		src += 3;
+
+		/* use calc'd indices to output RGB values */
+		dest[0] = otdata_r[output[0]];
+		dest[1] = otdata_g[output[1]];
+		dest[2] = otdata_b[output[2]];
+		dest += 3;
+	}
+
+	/* handle final (maybe only) pixel */
+
+	vec_r = vec_splat(vec_r, 0);
+	vec_g = vec_splat(vec_g, 0);
+	vec_b = vec_splat(vec_b, 0);
+
+	vec_r = vec_madd(vec_r, mat0, min);
+	vec_g = vec_madd(vec_g, mat1, min);
+	vec_b = vec_madd(vec_b, mat2, min);
+
+	vec_r = vec_add(vec_r, vec_add(vec_g, vec_b));
+	vec_r = vec_max(min, vec_r);
+	vec_r = vec_min(max, vec_r);
+	result = vec_madd(vec_r, scale, min);
+
+	vec_st(vec_ctu(vec_round(result),0),0,(vector unsigned int*)output);
+
+	dest[0] = otdata_r[output[0]];
+	dest[1] = otdata_g[output[1]];
+	dest[2] = otdata_b[output[2]];
+}
+
+void qcms_transform_data_rgba_out_lut_altivec(qcms_transform *transform,
+                                              unsigned char *src,
+                                              unsigned char *dest,
+                                              size_t length)
+{
+	unsigned int i;
+	float (*mat)[4] = transform->matrix;
+	char input_back[32];
+	/* Ensure we have a buffer that's 16 byte aligned regardless of the original
+	 * stack alignment. We can't use __attribute__((aligned(16))) or __declspec(align(32))
+	 * because they don't work on stack variables. gcc 4.4 does do the right thing
+	 * on x86 but that's too new for us right now. For more info: gcc bug #16660 */
+	float const *input = (float*)(((uintptr_t)&input_back[16]) & ~0xf);
+	/* share input and output locations to save having to keep the
+	 * locations in separate registers */
+	uint32_t const *output = (uint32_t*)input;
+
+	/* deref *transform now to avoid it in loop */
+	const float *igtbl_r = transform->input_gamma_table_r;
+	const float *igtbl_g = transform->input_gamma_table_g;
+	const float *igtbl_b = transform->input_gamma_table_b;
+
+	/* deref *transform now to avoid it in loop */
+	const uint8_t *otdata_r = &transform->output_table_r->data[0];
+	const uint8_t *otdata_g = &transform->output_table_g->data[0];
+	const uint8_t *otdata_b = &transform->output_table_b->data[0];
+
+	/* input matrix values never change */
+	const vector float mat0 = vec_ldl(0, (vector float*)mat[0]);
+	const vector float mat1 = vec_ldl(0, (vector float*)mat[1]);
+	const vector float mat2 = vec_ldl(0, (vector float*)mat[2]);
+
+	/* these values don't change, either */
+	const vector float max = vec_splat(vec_lde(0, (float*)&clampMaxValueX4), 0);
+	const vector float min = (vector float)vec_splat_u32(0);
+	const vector float scale = vec_splat(vec_lde(0, (float*)&floatScaleX4), 0);
+
+	/* working variables */
+	vector float vec_r, vec_g, vec_b, result;
+	unsigned char alpha;
+
+	/* CYA */
+	if (!length)
+		return;
+
+	/* one pixel is handled outside of the loop */
+	length--;
+
+	/* setup for transforming 1st pixel */
+	vec_r = load_aligned_float((float*)&igtbl_r[src[0]]);
+	vec_g = load_aligned_float((float*)&igtbl_r[src[1]]);
+	vec_b = load_aligned_float((float*)&igtbl_r[src[2]]);
+	alpha = src[3];
+	src += 4;
+
+	/* transform all but final pixel */
+
+	for (i=0; i<length; i++)
+	{
+		/* position values from gamma tables */
+		vec_r = vec_splat(vec_r, 0);
+		vec_g = vec_splat(vec_g, 0);
+		vec_b = vec_splat(vec_b, 0);
+
+		/* gamma * matrix */
+		vec_r = vec_madd(vec_r, mat0, min);
+		vec_g = vec_madd(vec_g, mat1, min);
+		vec_b = vec_madd(vec_b, mat2, min);
+
+		/* store alpha for this pixel; load alpha for next */
+		dest[3] = alpha;
+		alpha = src[3];
+
+		/* crunch, crunch, crunch */
+		vec_r = vec_add(vec_r, vec_add(vec_g, vec_b));
+		vec_r = vec_max(min, vec_r);
+		vec_r = vec_min(max, vec_r);
+		result = vec_madd(vec_r, scale, min);
+
+		/* store calc'd output tables indices */
+		vec_st(vec_ctu(vec_round(result), 0), 0, (vector unsigned int*)output);
+
+		/* load gamma values for next loop while store completes */
+		vec_r = load_aligned_float((float*)&igtbl_r[src[0]]);
+		vec_g = load_aligned_float((float*)&igtbl_r[src[1]]);
+		vec_b = load_aligned_float((float*)&igtbl_r[src[2]]);
+		src += 4;
+
+		/* use calc'd indices to output RGB values */
+		dest[0] = otdata_r[output[0]];
+		dest[1] = otdata_g[output[1]];
+		dest[2] = otdata_b[output[2]];
+		dest += 4;
+	}
+
+	/* handle final (maybe only) pixel */
+
+	vec_r = vec_splat(vec_r, 0);
+	vec_g = vec_splat(vec_g, 0);
+	vec_b = vec_splat(vec_b, 0);
+
+	vec_r = vec_madd(vec_r, mat0, min);
+	vec_g = vec_madd(vec_g, mat1, min);
+	vec_b = vec_madd(vec_b, mat2, min);
+
+	dest[3] = alpha;
+
+	vec_r = vec_add(vec_r, vec_add(vec_g, vec_b));
+	vec_r = vec_max(min, vec_r);
+	vec_r = vec_min(max, vec_r);
+	result = vec_madd(vec_r, scale, min);
+
+	vec_st(vec_ctu(vec_round(result), 0), 0, (vector unsigned int*)output);
+
+	dest[0] = otdata_r[output[0]];
+	dest[1] = otdata_g[output[1]];
+	dest[2] = otdata_b[output[2]];
+}
+
--- a/gfx/qcms/transform.c
+++ b/gfx/qcms/transform.c
@@ -30,16 +30,92 @@
 #include "matrix.h"
 #include "transform_util.h"
 
 /* for MSVC, GCC, Intel, and Sun compilers */
 #if defined(_M_IX86) || defined(__i386__) || defined(__i386) || defined(_M_AMD64) || defined(__x86_64__) || defined(__x86_64)
 #define X86
 #endif /* _M_IX86 || __i386__ || __i386 || _M_AMD64 || __x86_64__ || __x86_64 */
 
+/**
+ * AltiVec detection for PowerPC CPUs
+ * In case we have a method of detecting do the runtime detection.
+ * Otherwise statically choose the AltiVec path in case the compiler
+ * was told to build with AltiVec support.
+ */
+#if (defined(__POWERPC__) || defined(__powerpc__))
+#if defined(__linux__)
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <elf.h>
+#include <linux/auxvec.h>
+#include <asm/cputable.h>
+
+static inline qcms_bool have_altivec() {
+	static int available = -1;
+	int new_avail = 0;
+        ElfW(auxv_t) auxv;
+	ssize_t count;
+	int fd, i;
+
+	if (available != -1)
+		return (available != 0 ? true : false);
+
+	fd = open("/proc/self/auxv", O_RDONLY);
+	if (fd < 0)
+		goto out;
+	do {
+		count = read(fd, &auxv, sizeof(auxv));
+		if (count < 0)
+			goto out_close;
+
+		if (auxv.a_type == AT_HWCAP) {
+			new_avail = !!(auxv.a_un.a_val & PPC_FEATURE_HAS_ALTIVEC);
+			goto out_close;
+		}
+	} while (auxv.a_type != AT_NULL);
+
+out_close:
+	close(fd);
+out:
+	available = new_avail;
+	return (available != 0 ? true : false);
+}
+#elif defined(__APPLE__) && defined(__MACH__)
+#include <sys/sysctl.h>
+
+/**
+ * rip-off from ffmpeg AltiVec detection code.
+ * this code also appears on Apple's AltiVec pages.
+ */
+static inline qcms_bool have_altivec() {
+	int sels[2] = {CTL_HW, HW_VECTORUNIT};
+	static int available = -1;
+	size_t len = sizeof(available);
+	int err;
+
+	if (available != -1)
+		return (available != 0 ? true : false);
+
+	err = sysctl(sels, 2, &available, &len, NULL, 0);
+
+	if (err == 0)
+		if (available != 0)
+			return true;
+
+	return false;
+}
+#elif defined(__ALTIVEC__) || defined(__APPLE_ALTIVEC__)
+#define have_altivec() true
+#else
+#define have_altivec() false
+#endif
+#endif // (defined(__POWERPC__) || defined(__powerpc__))
+
 // Build a White point, primary chromas transfer matrix from RGB to CIE XYZ
 // This is just an approximation, I am not handling all the non-linear
 // aspects of the RGB to XYZ process, and assumming that the gamma correction
 // has transitive property in the tranformation chain.
 //
 // the alghoritm:
 //
 //            - First I build the absolute conversion matrix using
@@ -1189,16 +1265,24 @@ qcms_transform* qcms_transform_create(
 		    if (sse_version_available() >= 1) {
 			    if (in_type == QCMS_DATA_RGB_8)
 				    transform->transform_fn = qcms_transform_data_rgb_out_lut_sse1;
 			    else
 				    transform->transform_fn = qcms_transform_data_rgba_out_lut_sse1;
 #endif
 		    } else
 #endif
+#if (defined(__POWERPC__) || defined(__powerpc__))
+		    if (have_altivec()) {
+			    if (in_type == QCMS_DATA_RGB_8)
+				    transform->transform_fn = qcms_transform_data_rgb_out_lut_altivec;
+			    else
+				    transform->transform_fn = qcms_transform_data_rgba_out_lut_altivec;
+		    } else
+#endif
 			{
 				if (in_type == QCMS_DATA_RGB_8)
 					transform->transform_fn = qcms_transform_data_rgb_out_lut_precache;
 				else
 					transform->transform_fn = qcms_transform_data_rgba_out_lut_precache;
 			}
 		} else {
 			if (in_type == QCMS_DATA_RGB_8)