summaryrefslogtreecommitdiff
path: root/gfx/qcms/transform-altivec.c
blob: 230efbba2054f4aa51bde6dc98d9123293c60ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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]];
}