3 * Written by José Fonseca <j_r_fonseca@yahoo.co.uk>
10 /* integer multiplication - alpha plus one
12 * makes the following approximation to the division (Sree)
14 * rgb*a/255 ~= (rgb*(a+1)) >> 256
16 * which is the fastest method that satisfies the following OpenGL criteria
18 * 0*0 = 0 and 255*255 = 255
20 * note that MX1 is a register with 0xffffffffffffffff constant which can be easily obtained making
22 * PCMPEQW ( MX1, MX1 )
24 #define GMB_MULT_AP1( MP1, MA1, MP2, MA2, MX1 ) \
25 PSUBW ( MX1, MA1 ) /* a1 + 1 | a1 + 1 | a1 + 1 | a1 + 1 */ ;\
26 PMULLW ( MP1, MA1 ) /* t1 = p1*a1 */ ;\
28 TWO(PSUBW ( MX1, MA2 )) /* a2 + 1 | a2 + 1 | a2 + 1 | a2 + 1 */ ;\
29 TWO(PMULLW ( MP2, MA2 )) /* t2 = p2*a2 */ ;\
31 PSRLW ( CONST(8), MA1 ) /* t1 >> 8 ~= t1/255 */ ;\
32 TWO(PSRLW ( CONST(8), MA2 )) /* t2 >> 8 ~= t2/255 */
35 /* integer multiplication - geometric series
37 * takes the geometric series approximation to the division
39 * t/255 = (t >> 8) + (t >> 16) + (t >> 24) ..
41 * in this case just the first two terms to fit in 16bit arithmetic
43 * t/255 ~= (t + (t >> 8)) >> 8
45 * note that just by itself it doesn't satisfies the OpenGL criteria, as 255*255 = 254,
46 * so the special case a = 255 must be accounted or roundoff must be used
48 #define GMB_MULT_GS( MP1, MA1, MP2, MA2 ) \
49 PMULLW ( MP1, MA1 ) /* t1 = p1*a1 */ ;\
50 TWO(PMULLW ( MP2, MA2 )) /* t2 = p2*a2 */ ;\
53 PSRLW ( CONST(8), MA1 ) /* t1 >> 8 */ ;\
55 TWO(MOVQ ( MA2, MP2 )) ;\
56 TWO(PSRLW ( CONST(8), MA2 )) /* t2 >> 8 */ ;\
58 PADDW ( MP1, MA1 ) /* t1 + (t1 >> 8) ~= (t1/255) << 8 */ ;\
59 PSRLW ( CONST(8), MA1 ) /* sa1 | sb1 | sg1 | sr1 */ ;\
61 TWO(PADDW ( MP2, MA2 )) /* t2 + (t2 >> 8) ~= (t2/255) << 8 */ ;\
62 TWO(PSRLW ( CONST(8), MA2 )) /* sa2 | sb2 | sg2 | sr2 */
65 /* integer multiplication - geometric series plus rounding
67 * when using a geometric series division instead of truncating the result
68 * use roundoff in the approximation (Jim Blinn)
72 * achieving the exact results
74 * note that M80 is register with the 0x0080008000800080 constant
76 #define GMB_MULT_GSR( MP1, MA1, MP2, MA2, M80 ) \
77 PMULLW ( MP1, MA1 ) /* t1 = p1*a1 */ ;\
78 PADDW ( M80, MA1 ) /* t1 += 0x80 */ ;\
80 TWO(PMULLW ( MP2, MA2 )) /* t2 = p2*a2 */ ;\
81 TWO(PADDW ( M80, MA2 )) /* t2 += 0x80 */ ;\
84 PSRLW ( CONST(8), MA1 ) /* t1 >> 8 */ ;\
86 TWO(MOVQ ( MA2, MP2 )) ;\
87 TWO(PSRLW ( CONST(8), MA2 )) /* t2 >> 8 */ ;\
89 PADDW ( MP1, MA1 ) /* t1 + (t1 >> 8) ~= (t1/255) << 8 */ ;\
90 PSRLW ( CONST(8), MA1 ) /* sa1 | sb1 | sg1 | sr1 */ ;\
92 TWO(PADDW ( MP2, MA2 )) /* t2 + (t2 >> 8) ~= (t2/255) << 8 */ ;\
93 TWO(PSRLW ( CONST(8), MA2 )) /* sa2 | sb2 | sg2 | sr2 */
96 /* linear interpolation - geometric series
98 #define GMB_LERP_GS( MP1, MQ1, MA1, MP2, MQ2, MA2) \
99 PSUBW ( MQ1, MP1 ) /* pa1 - qa1 | pb1 - qb1 | pg1 - qg1 | pr1 - qr1 */ ;\
100 PSLLW ( CONST(8), MQ1 ) /* q1 << 8 */ ;\
101 PMULLW ( MP1, MA1 ) /* t1 = (q1 - p1)*pa1 */ ;\
103 TWO(PSUBW ( MQ2, MP2 )) /* pa2 - qa2 | pb2 - qb2 | pg2 - qg2 | pr2 - qr2 */ ;\
104 TWO(PSLLW ( CONST(8), MQ2 )) /* q2 << 8 */ ;\
105 TWO(PMULLW ( MP2, MA2 )) /* t2 = (q2 - p2)*pa2 */ ;\
108 PSRLW ( CONST(8), MA1 ) /* t1 >> 8 */ ;\
110 TWO(MOVQ ( MA2, MP2 )) ;\
111 TWO(PSRLW ( CONST(8), MA2 )) /* t2 >> 8 */ ;\
113 PADDW ( MP1, MA1 ) /* t1 + (t1 >> 8) ~= (t1/255) << 8 */ ;\
114 TWO(PADDW ( MP2, MA2 )) /* t2 + (t2 >> 8) ~= (t2/255) << 8 */ ;\
116 PADDW ( MQ1, MA1 ) /* (t1/255 + q1) << 8 */ ;\
117 TWO(PADDW ( MQ2, MA2 )) /* (t2/255 + q2) << 8 */ ;\
119 PSRLW ( CONST(8), MA1 ) /* sa1 | sb1 | sg1 | sr1 */ ;\
120 TWO(PSRLW ( CONST(8), MA2 )) /* sa2 | sb2 | sg2 | sr2 */
123 /* linear interpolation - geometric series with roundoff
125 * this is a generalization of Blinn's formula to signed arithmetic
127 * note that M80 is a register with the 0x0080008000800080 constant
129 #define GMB_LERP_GSR( MP1, MQ1, MA1, MP2, MQ2, MA2, M80) \
130 PSUBW ( MQ1, MP1 ) /* pa1 - qa1 | pb1 - qb1 | pg1 - qg1 | pr1 - qr1 */ ;\
131 PSLLW ( CONST(8), MQ1 ) /* q1 << 8 */ ;\
132 PMULLW ( MP1, MA1 ) /* t1 = (q1 - p1)*pa1 */ ;\
134 TWO(PSUBW ( MQ2, MP2 )) /* pa2 - qa2 | pb2 - qb2 | pg2 - qg2 | pr2 - qr2 */ ;\
135 TWO(PSLLW ( CONST(8), MQ2 )) /* q2 << 8 */ ;\
136 TWO(PMULLW ( MP2, MA2 )) /* t2 = (q2 - p2)*pa2 */ ;\
138 PSRLW ( CONST(15), MP1 ) /* q1 > p1 ? 1 : 0 */ ;\
139 TWO(PSRLW ( CONST(15), MP2 )) /* q2 > q2 ? 1 : 0 */ ;\
141 PSLLW ( CONST(8), MP1 ) /* q1 > p1 ? 0x100 : 0 */ ;\
142 TWO(PSLLW ( CONST(8), MP2 )) /* q2 > q2 ? 0x100 : 0 */ ;\
144 PSUBW ( MP1, MA1 ) /* t1 -=? 0x100 */ ;\
145 TWO(PSUBW ( MP2, MA2 )) /* t2 -=? 0x100 */ ;\
147 PADDW ( M80, MA1 ) /* t1 += 0x80 */ ;\
148 TWO(PADDW ( M80, MA2 )) /* t2 += 0x80 */ ;\
151 PSRLW ( CONST(8), MA1 ) /* t1 >> 8 */ ;\
153 TWO(MOVQ ( MA2, MP2 )) ;\
154 TWO(PSRLW ( CONST(8), MA2 )) /* t2 >> 8 */ ;\
156 PADDW ( MP1, MA1 ) /* t1 + (t1 >> 8) ~= (t1/255) << 8 */ ;\
157 TWO(PADDW ( MP2, MA2 )) /* t2 + (t2 >> 8) ~= (t2/255) << 8 */ ;\
159 PADDW ( MQ1, MA1 ) /* (t1/255 + q1) << 8 */ ;\
160 TWO(PADDW ( MQ2, MA2 )) /* (t2/255 + q2) << 8 */ ;\
162 PSRLW ( CONST(8), MA1 ) /* sa1 | sb1 | sg1 | sr1 */ ;\
163 TWO(PSRLW ( CONST(8), MA2 )) /* sa2 | sb2 | sg2 | sr2 */
166 /* linear interpolation - geometric series with correction
168 * instead of the roundoff this adds a small correction to satisfy the OpenGL criteria
170 * t/255 ~= (t + (t >> 8) + (t >> 15)) >> 8
172 * note that although is faster than rounding off it doesn't give always the exact results
174 #define GMB_LERP_GSC( MP1, MQ1, MA1, MP2, MQ2, MA2) \
175 PSUBW ( MQ1, MP1 ) /* pa1 - qa1 | pb1 - qb1 | pg1 - qg1 | pr1 - qr1 */ ;\
176 PSLLW ( CONST(8), MQ1 ) /* q1 << 8 */ ;\
177 PMULLW ( MP1, MA1 ) /* t1 = (q1 - p1)*pa1 */ ;\
179 TWO(PSUBW ( MQ2, MP2 )) /* pa2 - qa2 | pb2 - qb2 | pg2 - qg2 | pr2 - qr2 */ ;\
180 TWO(PSLLW ( CONST(8), MQ2 )) /* q2 << 8 */ ;\
181 TWO(PMULLW ( MP2, MA2 )) /* t2 = (q2 - p2)*pa2 */ ;\
184 PSRLW ( CONST(8), MA1 ) /* t1 >> 8 */ ;\
186 TWO(MOVQ ( MA2, MP2 )) ;\
187 TWO(PSRLW ( CONST(8), MA2 )) /* t2 >> 8 */ ;\
189 PADDW ( MA1, MP1 ) /* t1 + (t1 >> 8) ~= (t1/255) << 8 */ ;\
190 PSRLW ( CONST(7), MA1 ) /* t1 >> 15 */ ;\
192 TWO(PADDW ( MA2, MP2 )) /* t2 + (t2 >> 8) ~= (t2/255) << 8 */ ;\
193 TWO(PSRLW ( CONST(7), MA2 )) /* t2 >> 15 */ ;\
195 PADDW ( MP1, MA1 ) /* t1 + (t1 >> 8) + (t1 >>15) ~= (t1/255) << 8 */ ;\
196 TWO(PADDW ( MP2, MA2 )) /* t2 + (t2 >> 8) + (t2 >>15) ~= (t2/255) << 8 */ ;\
198 PADDW ( MQ1, MA1 ) /* (t1/255 + q1) << 8 */ ;\
199 TWO(PADDW ( MQ2, MA2 )) /* (t2/255 + q2) << 8 */ ;\
201 PSRLW ( CONST(8), MA1 ) /* sa1 | sb1 | sg1 | sr1 */ ;\
202 TWO(PSRLW ( CONST(8), MA2 )) /* sa2 | sb2 | sg2 | sr2 */
205 /* common blending setup code
207 * note that M00 is a register with 0x0000000000000000 constant which can be easily obtained making
211 #define GMB_LOAD(rgba, dest, MPP, MQQ) \
212 ONE(MOVD ( REGIND(rgba), MPP )) /* | | | | qa1 | qb1 | qg1 | qr1 */ ;\
213 ONE(MOVD ( REGIND(dest), MQQ )) /* | | | | pa1 | pb1 | pg1 | pr1 */ ;\
215 TWO(MOVQ ( REGIND(rgba), MPP )) /* qa2 | qb2 | qg2 | qr2 | qa1 | qb1 | qg1 | qr1 */ ;\
216 TWO(MOVQ ( REGIND(dest), MQQ )) /* pa2 | pb2 | pg2 | pr2 | pa1 | pb1 | pg1 | pr1 */
218 #define GMB_UNPACK(MP1, MQ1, MP2, MQ2, M00) \
219 TWO(MOVQ ( MP1, MP2 )) ;\
220 TWO(MOVQ ( MQ1, MQ2 )) ;\
222 PUNPCKLBW ( M00, MQ1 ) /* qa1 | qb1 | qg1 | qr1 */ ;\
223 TWO(PUNPCKHBW ( M00, MQ2 )) /* qa2 | qb2 | qg2 | qr2 */ ;\
224 PUNPCKLBW ( M00, MP1 ) /* pa1 | pb1 | pg1 | pr1 */ ;\
225 TWO(PUNPCKHBW ( M00, MP2 )) /* pa2 | pb2 | pg2 | pr2 */
227 #define GMB_ALPHA(MP1, MA1, MP2, MA2) \
229 TWO(MOVQ ( MP2, MA2 )) ;\
231 PUNPCKHWD ( MA1, MA1 ) /* pa1 | pa1 | | */ ;\
232 TWO(PUNPCKHWD ( MA2, MA2 )) /* pa2 | pa2 | | */ ;\
233 PUNPCKHDQ ( MA1, MA1 ) /* pa1 | pa1 | pa1 | pa1 */ ;\
234 TWO(PUNPCKHDQ ( MA2, MA2 )) /* pa2 | pa2 | pa2 | pa2 */
236 #define GMB_PACK( MS1, MS2 ) \
237 PACKUSWB ( MS2, MS1 ) /* sa2 | sb2 | sg2 | sr2 | sa1 | sb1 | sg1 | sr1 */ ;\
239 #define GMB_STORE(rgba, MSS ) \
240 ONE(MOVD ( MSS, REGIND(rgba) )) /* | | | | sa1 | sb1 | sg1 | sr1 */ ;\
241 TWO(MOVQ ( MSS, REGIND(rgba) )) /* sa2 | sb2 | sg2 | sr2 | sa1 | sb1 | sg1 | sr1 */
243 /* Kevin F. Quinn <kevquinn@gentoo.org> 2 July 2006
244 * Replace data segment constants with text-segment
245 * constants (via pushl/movq)
250 D_LONG 0x00800080, 0x00800080
253 D_LONG 0x80808080, 0x80808080
255 #define const_0080_l 0x00800080
256 #define const_0080_h 0x00800080
257 #define const_80_l 0x80808080
258 #define const_80_h 0x80808080
263 /* Blend transparency function
266 #define TAG(x) CONCAT(x,_transparency)
267 #define LLTAG(x) LLBL2(x,_transparency)
270 PXOR ( MM0, MM0 ) /* 0x0000 | 0x0000 | 0x0000 | 0x0000 */
272 #define MAIN( rgba, dest ) \
273 GMB_LOAD( rgba, dest, MM1, MM2 ) ;\
274 GMB_UNPACK( MM1, MM2, MM4, MM5, MM0 ) ;\
275 GMB_ALPHA( MM1, MM3, MM4, MM6 ) ;\
276 GMB_LERP_GSC( MM1, MM2, MM3, MM4, MM5, MM6 ) ;\
277 GMB_PACK( MM3, MM6 ) ;\
278 GMB_STORE( rgba, MM3 )
280 #include "mmx_blendtmp.h"
283 /* Blend add function
285 * FIXME: Add some loop unrolling here...
288 #define TAG(x) CONCAT(x,_add)
289 #define LLTAG(x) LLBL2(x,_add)
293 #define MAIN( rgba, dest ) \
294 ONE(MOVD ( REGIND(rgba), MM1 )) /* | | | | qa1 | qb1 | qg1 | qr1 */ ;\
295 ONE(MOVD ( REGIND(dest), MM2 )) /* | | | | pa1 | pb1 | pg1 | pr1 */ ;\
296 ONE(PADDUSB ( MM2, MM1 )) ;\
297 ONE(MOVD ( MM1, REGIND(rgba) )) /* | | | | sa1 | sb1 | sg1 | sr1 */ ;\
299 TWO(MOVQ ( REGIND(rgba), MM1 )) /* qa2 | qb2 | qg2 | qr2 | qa1 | qb1 | qg1 | qr1 */ ;\
300 TWO(PADDUSB ( REGIND(dest), MM1 )) /* sa2 | sb2 | sg2 | sr2 | sa1 | sb1 | sg1 | sr1 */ ;\
301 TWO(MOVQ ( MM1, REGIND(rgba) ))
303 #include "mmx_blendtmp.h"
306 /* Blend min function
309 #define TAG(x) CONCAT(x,_min)
310 #define LLTAG(x) LLBL2(x,_min)
312 /* Kevin F. Quinn 2nd July 2006
313 * Replace data segment constants with text-segment instructions
315 MOVQ ( CONTENT(const_80), MM7 )
318 PUSH_L ( CONST(const_80_h) ) /* 0x80| 0x80| 0x80| 0x80| 0x80| 0x80| 0x80| 0x80*/ ;\
319 PUSH_L ( CONST(const_80_l) ) ;\
320 MOVQ ( REGIND(ESP), MM7 ) ;\
321 ADD_L ( CONST(8), ESP)
323 #define MAIN( rgba, dest ) \
324 GMB_LOAD( rgba, dest, MM1, MM2 ) ;\
327 PXOR ( MM7, MM3 ) /* unsigned -> signed */ ;\
328 PXOR ( MM7, MM4 ) /* unsigned -> signed */ ;\
329 PCMPGTB ( MM3, MM4 ) /* q > p ? 0xff : 0x00 */ ;\
330 PAND ( MM4, MM1 ) /* q > p ? p : 0 */ ;\
331 PANDN ( MM2, MM4 ) /* q > p ? 0 : q */ ;\
332 POR ( MM1, MM4 ) /* q > p ? p : q */ ;\
333 GMB_STORE( rgba, MM4 )
335 #include "mmx_blendtmp.h"
338 /* Blend max function
341 #define TAG(x) CONCAT(x,_max)
342 #define LLTAG(x) LLBL2(x,_max)
344 /* Kevin F. Quinn 2nd July 2006
345 * Replace data segment constants with text-segment instructions
347 MOVQ ( CONTENT(const_80), MM7 )
350 PUSH_L ( CONST(const_80_l) ) /* 0x80| 0x80| 0x80| 0x80| 0x80| 0x80| 0x80| 0x80*/ ;\
351 PUSH_L ( CONST(const_80_h) ) ;\
352 MOVQ ( REGIND(ESP), MM7 ) ;\
353 ADD_L ( CONST(8), ESP)
355 #define MAIN( rgba, dest ) \
356 GMB_LOAD( rgba, dest, MM1, MM2 ) ;\
359 PXOR ( MM7, MM3 ) /* unsigned -> signed */ ;\
360 PXOR ( MM7, MM4 ) /* unsigned -> signed */ ;\
361 PCMPGTB ( MM3, MM4 ) /* q > p ? 0xff : 0x00 */ ;\
362 PAND ( MM4, MM2 ) /* q > p ? q : 0 */ ;\
363 PANDN ( MM1, MM4 ) /* q > p ? 0 : p */ ;\
364 POR ( MM2, MM4 ) /* q > p ? p : q */ ;\
365 GMB_STORE( rgba, MM4 )
367 #include "mmx_blendtmp.h"
370 /* Blend modulate function
373 #define TAG(x) CONCAT(x,_modulate)
374 #define LLTAG(x) LLBL2(x,_modulate)
376 /* Kevin F. Quinn 2nd July 2006
377 * Replace data segment constants with text-segment instructions
379 MOVQ ( CONTENT(const_0080), MM7 )
382 PXOR ( MM0, MM0 ) /* 0x0000 | 0x0000 | 0x0000 | 0x0000 */ ;\
383 PUSH_L ( CONST(const_0080_l) ) /* 0x0080 | 0x0080 | 0x0080 | 0x0080 */ ;\
384 PUSH_L ( CONST(const_0080_h) ) ;\
385 MOVQ ( REGIND(ESP), MM7 ) ;\
386 ADD_L ( CONST(8), ESP)
388 #define MAIN( rgba, dest ) \
389 GMB_LOAD( rgba, dest, MM1, MM2 ) ;\
390 GMB_UNPACK( MM1, MM2, MM4, MM5, MM0 ) ;\
391 GMB_MULT_GSR( MM1, MM2, MM4, MM5, MM7 ) ;\
392 GMB_PACK( MM2, MM5 ) ;\
393 GMB_STORE( rgba, MM2 )
395 #include "mmx_blendtmp.h"
399 #if defined (__ELF__) && defined (__linux__)
400 .section .note.GNU-stack,"",%progbits