1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "ShaderCore.hpp"
16
17 #include "Device/Renderer.hpp"
18 #include "System/Debug.hpp"
19
20 #include <limits.h>
21
22 namespace sw {
23
Vector4s()24 Vector4s::Vector4s()
25 {
26 }
27
Vector4s(unsigned short x,unsigned short y,unsigned short z,unsigned short w)28 Vector4s::Vector4s(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
29 {
30 this->x = Short4(x);
31 this->y = Short4(y);
32 this->z = Short4(z);
33 this->w = Short4(w);
34 }
35
Vector4s(const Vector4s & rhs)36 Vector4s::Vector4s(const Vector4s &rhs)
37 {
38 x = rhs.x;
39 y = rhs.y;
40 z = rhs.z;
41 w = rhs.w;
42 }
43
operator =(const Vector4s & rhs)44 Vector4s &Vector4s::operator=(const Vector4s &rhs)
45 {
46 x = rhs.x;
47 y = rhs.y;
48 z = rhs.z;
49 w = rhs.w;
50
51 return *this;
52 }
53
operator [](int i)54 Short4 &Vector4s::operator[](int i)
55 {
56 switch(i)
57 {
58 case 0: return x;
59 case 1: return y;
60 case 2: return z;
61 case 3: return w;
62 }
63
64 return x;
65 }
66
Vector4f()67 Vector4f::Vector4f()
68 {
69 }
70
Vector4f(float x,float y,float z,float w)71 Vector4f::Vector4f(float x, float y, float z, float w)
72 {
73 this->x = Float4(x);
74 this->y = Float4(y);
75 this->z = Float4(z);
76 this->w = Float4(w);
77 }
78
Vector4f(const Vector4f & rhs)79 Vector4f::Vector4f(const Vector4f &rhs)
80 {
81 x = rhs.x;
82 y = rhs.y;
83 z = rhs.z;
84 w = rhs.w;
85 }
86
operator =(const Vector4f & rhs)87 Vector4f &Vector4f::operator=(const Vector4f &rhs)
88 {
89 x = rhs.x;
90 y = rhs.y;
91 z = rhs.z;
92 w = rhs.w;
93
94 return *this;
95 }
96
operator [](int i)97 Float4 &Vector4f::operator[](int i)
98 {
99 switch(i)
100 {
101 case 0: return x;
102 case 1: return y;
103 case 2: return z;
104 case 3: return w;
105 }
106
107 return x;
108 }
109
Vector4i()110 Vector4i::Vector4i()
111 {
112 }
113
Vector4i(int x,int y,int z,int w)114 Vector4i::Vector4i(int x, int y, int z, int w)
115 {
116 this->x = Int4(x);
117 this->y = Int4(y);
118 this->z = Int4(z);
119 this->w = Int4(w);
120 }
121
Vector4i(const Vector4i & rhs)122 Vector4i::Vector4i(const Vector4i &rhs)
123 {
124 x = rhs.x;
125 y = rhs.y;
126 z = rhs.z;
127 w = rhs.w;
128 }
129
operator =(const Vector4i & rhs)130 Vector4i &Vector4i::operator=(const Vector4i &rhs)
131 {
132 x = rhs.x;
133 y = rhs.y;
134 z = rhs.z;
135 w = rhs.w;
136
137 return *this;
138 }
139
operator [](int i)140 Int4 &Vector4i::operator[](int i)
141 {
142 switch(i)
143 {
144 case 0: return x;
145 case 1: return y;
146 case 2: return z;
147 case 3: return w;
148 }
149
150 return x;
151 }
152
exponential2(RValue<Float4> x,bool pp)153 Float4 exponential2(RValue<Float4> x, bool pp)
154 {
155 // This implementation is based on 2^(i + f) = 2^i * 2^f,
156 // where i is the integer part of x and f is the fraction.
157
158 // For 2^i we can put the integer part directly in the exponent of
159 // the IEEE-754 floating-point number. Clamp to prevent overflow
160 // past the representation of infinity.
161 Float4 x0 = x;
162 x0 = Min(x0, As<Float4>(Int4(0x43010000))); // 129.00000e+0f
163 x0 = Max(x0, As<Float4>(Int4(0xC2FDFFFF))); // -126.99999e+0f
164
165 Int4 i = RoundInt(x0 - Float4(0.5f));
166 Float4 ii = As<Float4>((i + Int4(127)) << 23); // Add single-precision bias, and shift into exponent.
167
168 // For the fractional part use a polynomial
169 // which approximates 2^f in the 0 to 1 range.
170 Float4 f = x0 - Float4(i);
171 Float4 ff = As<Float4>(Int4(0x3AF61905)); // 1.8775767e-3f
172 ff = ff * f + As<Float4>(Int4(0x3C134806)); // 8.9893397e-3f
173 ff = ff * f + As<Float4>(Int4(0x3D64AA23)); // 5.5826318e-2f
174 ff = ff * f + As<Float4>(Int4(0x3E75EAD4)); // 2.4015361e-1f
175 ff = ff * f + As<Float4>(Int4(0x3F31727B)); // 6.9315308e-1f
176 ff = ff * f + Float4(1.0f);
177
178 return ii * ff;
179 }
180
logarithm2(RValue<Float4> x,bool pp)181 Float4 logarithm2(RValue<Float4> x, bool pp)
182 {
183 Float4 x0;
184 Float4 x1;
185 Float4 x2;
186 Float4 x3;
187
188 x0 = x;
189
190 x1 = As<Float4>(As<Int4>(x0) & Int4(0x7F800000));
191 x1 = As<Float4>(As<UInt4>(x1) >> 8);
192 x1 = As<Float4>(As<Int4>(x1) | As<Int4>(Float4(1.0f)));
193 x1 = (x1 - Float4(1.4960938f)) * Float4(256.0f); // FIXME: (x1 - 1.4960938f) * 256.0f;
194 x0 = As<Float4>((As<Int4>(x0) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
195
196 x2 = (Float4(9.5428179e-2f) * x0 + Float4(4.7779095e-1f)) * x0 + Float4(1.9782813e-1f);
197 x3 = ((Float4(1.6618466e-2f) * x0 + Float4(2.0350508e-1f)) * x0 + Float4(2.7382900e-1f)) * x0 + Float4(4.0496687e-2f);
198 x2 /= x3;
199
200 x1 += (x0 - Float4(1.0f)) * x2;
201
202 Int4 pos_inf_x = CmpEQ(As<Int4>(x), Int4(0x7F800000));
203 return As<Float4>((pos_inf_x & As<Int4>(x)) | (~pos_inf_x & As<Int4>(x1)));
204 }
205
exponential(RValue<Float4> x,bool pp)206 Float4 exponential(RValue<Float4> x, bool pp)
207 {
208 // TODO: Propagate the constant
209 return exponential2(Float4(1.44269504f) * x, pp); // 1/ln(2)
210 }
211
logarithm(RValue<Float4> x,bool pp)212 Float4 logarithm(RValue<Float4> x, bool pp)
213 {
214 // TODO: Propagate the constant
215 return Float4(6.93147181e-1f) * logarithm2(x, pp); // ln(2)
216 }
217
power(RValue<Float4> x,RValue<Float4> y,bool pp)218 Float4 power(RValue<Float4> x, RValue<Float4> y, bool pp)
219 {
220 Float4 log = logarithm2(x, pp);
221 log *= y;
222 return exponential2(log, pp);
223 }
224
reciprocal(RValue<Float4> x,bool pp,bool finite,bool exactAtPow2)225 Float4 reciprocal(RValue<Float4> x, bool pp, bool finite, bool exactAtPow2)
226 {
227 return Rcp(x, pp ? Precision::Relaxed : Precision::Full, finite, exactAtPow2);
228 }
229
reciprocalSquareRoot(RValue<Float4> x,bool absolute,bool pp)230 Float4 reciprocalSquareRoot(RValue<Float4> x, bool absolute, bool pp)
231 {
232 Float4 abs = x;
233
234 if(absolute)
235 {
236 abs = Abs(abs);
237 }
238
239 return Rcp(abs, pp ? Precision::Relaxed : Precision::Full);
240 }
241
modulo(RValue<Float4> x,RValue<Float4> y)242 Float4 modulo(RValue<Float4> x, RValue<Float4> y)
243 {
244 return x - y * Floor(x / y);
245 }
246
sine_pi(RValue<Float4> x,bool pp)247 Float4 sine_pi(RValue<Float4> x, bool pp)
248 {
249 const Float4 A = Float4(-4.05284734e-1f); // -4/pi^2
250 const Float4 B = Float4(1.27323954e+0f); // 4/pi
251 const Float4 C = Float4(7.75160950e-1f);
252 const Float4 D = Float4(2.24839049e-1f);
253
254 // Parabola approximating sine
255 Float4 sin = x * (Abs(x) * A + B);
256
257 // Improve precision from 0.06 to 0.001
258 if(true)
259 {
260 sin = sin * (Abs(sin) * D + C);
261 }
262
263 return sin;
264 }
265
cosine_pi(RValue<Float4> x,bool pp)266 Float4 cosine_pi(RValue<Float4> x, bool pp)
267 {
268 // cos(x) = sin(x + pi/2)
269 Float4 y = x + Float4(1.57079632e+0f);
270
271 // Wrap around
272 y -= As<Float4>(CmpNLT(y, Float4(3.14159265e+0f)) & As<Int4>(Float4(6.28318530e+0f)));
273
274 return sine_pi(y, pp);
275 }
276
sine(RValue<Float4> x,bool pp)277 Float4 sine(RValue<Float4> x, bool pp)
278 {
279 // Reduce to [-0.5, 0.5] range
280 Float4 y = x * Float4(1.59154943e-1f); // 1/2pi
281 y = y - Round(y);
282
283 if(!pp)
284 {
285 // From the paper: "A Fast, Vectorizable Algorithm for Producing Single-Precision Sine-Cosine Pairs"
286 // This implementation passes OpenGL ES 3.0 precision requirements, at the cost of more operations:
287 // !pp : 17 mul, 7 add, 1 sub, 1 reciprocal
288 // pp : 4 mul, 2 add, 2 abs
289
290 Float4 y2 = y * y;
291 Float4 c1 = y2 * (y2 * (y2 * Float4(-0.0204391631f) + Float4(0.2536086171f)) + Float4(-1.2336977925f)) + Float4(1.0f);
292 Float4 s1 = y * (y2 * (y2 * (y2 * Float4(-0.0046075748f) + Float4(0.0796819754f)) + Float4(-0.645963615f)) + Float4(1.5707963235f));
293 Float4 c2 = (c1 * c1) - (s1 * s1);
294 Float4 s2 = Float4(2.0f) * s1 * c1;
295 return Float4(2.0f) * s2 * c2 * reciprocal(s2 * s2 + c2 * c2, pp, true);
296 }
297
298 const Float4 A = Float4(-16.0f);
299 const Float4 B = Float4(8.0f);
300 const Float4 C = Float4(7.75160950e-1f);
301 const Float4 D = Float4(2.24839049e-1f);
302
303 // Parabola approximating sine
304 Float4 sin = y * (Abs(y) * A + B);
305
306 // Improve precision from 0.06 to 0.001
307 if(true)
308 {
309 sin = sin * (Abs(sin) * D + C);
310 }
311
312 return sin;
313 }
314
cosine(RValue<Float4> x,bool pp)315 Float4 cosine(RValue<Float4> x, bool pp)
316 {
317 // cos(x) = sin(x + pi/2)
318 Float4 y = x + Float4(1.57079632e+0f);
319 return sine(y, pp);
320 }
321
tangent(RValue<Float4> x,bool pp)322 Float4 tangent(RValue<Float4> x, bool pp)
323 {
324 return sine(x, pp) / cosine(x, pp);
325 }
326
arccos(RValue<Float4> x,bool pp)327 Float4 arccos(RValue<Float4> x, bool pp)
328 {
329 // pi/2 - arcsin(x)
330 return Float4(1.57079632e+0f) - arcsin(x);
331 }
332
arcsin(RValue<Float4> x,bool pp)333 Float4 arcsin(RValue<Float4> x, bool pp)
334 {
335 if(false) // Simpler implementation fails even lowp precision tests
336 {
337 // x*(pi/2-sqrt(1-x*x)*pi/5)
338 return x * (Float4(1.57079632e+0f) - Sqrt(Float4(1.0f) - x * x) * Float4(6.28318531e-1f));
339 }
340 else
341 {
342 // From 4.4.45, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
343 const Float4 half_pi(1.57079632f);
344 const Float4 a0(1.5707288f);
345 const Float4 a1(-0.2121144f);
346 const Float4 a2(0.0742610f);
347 const Float4 a3(-0.0187293f);
348 Float4 absx = Abs(x);
349 return As<Float4>(As<Int4>(half_pi - Sqrt(Float4(1.0f) - absx) * (a0 + absx * (a1 + absx * (a2 + absx * a3)))) ^
350 (As<Int4>(x) & Int4(0x80000000)));
351 }
352 }
353
354 // Approximation of atan in [0..1]
arctan_01(Float4 x,bool pp)355 Float4 arctan_01(Float4 x, bool pp)
356 {
357 if(pp)
358 {
359 return x * (Float4(-0.27f) * x + Float4(1.05539816f));
360 }
361 else
362 {
363 // From 4.4.49, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
364 const Float4 a2(-0.3333314528f);
365 const Float4 a4(0.1999355085f);
366 const Float4 a6(-0.1420889944f);
367 const Float4 a8(0.1065626393f);
368 const Float4 a10(-0.0752896400f);
369 const Float4 a12(0.0429096138f);
370 const Float4 a14(-0.0161657367f);
371 const Float4 a16(0.0028662257f);
372 Float4 x2 = x * x;
373 return (x + x * (x2 * (a2 + x2 * (a4 + x2 * (a6 + x2 * (a8 + x2 * (a10 + x2 * (a12 + x2 * (a14 + x2 * a16)))))))));
374 }
375 }
376
arctan(RValue<Float4> x,bool pp)377 Float4 arctan(RValue<Float4> x, bool pp)
378 {
379 Float4 absx = Abs(x);
380 Int4 O = CmpNLT(absx, Float4(1.0f));
381 Float4 y = As<Float4>((O & As<Int4>(Float4(1.0f) / absx)) | (~O & As<Int4>(absx))); // FIXME: Vector select
382
383 const Float4 half_pi(1.57079632f);
384 Float4 theta = arctan_01(y, pp);
385 return As<Float4>(((O & As<Int4>(half_pi - theta)) | (~O & As<Int4>(theta))) ^ // FIXME: Vector select
386 (As<Int4>(x) & Int4(0x80000000)));
387 }
388
arctan(RValue<Float4> y,RValue<Float4> x,bool pp)389 Float4 arctan(RValue<Float4> y, RValue<Float4> x, bool pp)
390 {
391 const Float4 pi(3.14159265f); // pi
392 const Float4 minus_pi(-3.14159265f); // -pi
393 const Float4 half_pi(1.57079632f); // pi/2
394 const Float4 quarter_pi(7.85398163e-1f); // pi/4
395
396 // Rotate to upper semicircle when in lower semicircle
397 Int4 S = CmpLT(y, Float4(0.0f));
398 Float4 theta = As<Float4>(S & As<Int4>(minus_pi));
399 Float4 x0 = As<Float4>((As<Int4>(y) & Int4(0x80000000)) ^ As<Int4>(x));
400 Float4 y0 = Abs(y);
401
402 // Rotate to right quadrant when in left quadrant
403 Int4 Q = CmpLT(x0, Float4(0.0f));
404 theta += As<Float4>(Q & As<Int4>(half_pi));
405 Float4 x1 = As<Float4>((Q & As<Int4>(y0)) | (~Q & As<Int4>(x0))); // FIXME: Vector select
406 Float4 y1 = As<Float4>((Q & As<Int4>(-x0)) | (~Q & As<Int4>(y0))); // FIXME: Vector select
407
408 // Mirror to first octant when in second octant
409 Int4 O = CmpNLT(y1, x1);
410 Float4 x2 = As<Float4>((O & As<Int4>(y1)) | (~O & As<Int4>(x1))); // FIXME: Vector select
411 Float4 y2 = As<Float4>((O & As<Int4>(x1)) | (~O & As<Int4>(y1))); // FIXME: Vector select
412
413 // Approximation of atan in [0..1]
414 Int4 zero_x = CmpEQ(x2, Float4(0.0f));
415 Int4 inf_y = IsInf(y2); // Since x2 >= y2, this means x2 == y2 == inf, so we use 45 degrees or pi/4
416 Float4 atan2_theta = arctan_01(y2 / x2, pp);
417 theta += As<Float4>((~zero_x & ~inf_y & ((O & As<Int4>(half_pi - atan2_theta)) | (~O & (As<Int4>(atan2_theta))))) | // FIXME: Vector select
418 (inf_y & As<Int4>(quarter_pi)));
419
420 // Recover loss of precision for tiny theta angles
421 Int4 precision_loss = S & Q & O & ~inf_y; // This combination results in (-pi + half_pi + half_pi - atan2_theta) which is equivalent to -atan2_theta
422 return As<Float4>((precision_loss & As<Int4>(-atan2_theta)) | (~precision_loss & As<Int4>(theta))); // FIXME: Vector select
423 }
424
sineh(RValue<Float4> x,bool pp)425 Float4 sineh(RValue<Float4> x, bool pp)
426 {
427 return (exponential(x, pp) - exponential(-x, pp)) * Float4(0.5f);
428 }
429
cosineh(RValue<Float4> x,bool pp)430 Float4 cosineh(RValue<Float4> x, bool pp)
431 {
432 return (exponential(x, pp) + exponential(-x, pp)) * Float4(0.5f);
433 }
434
tangenth(RValue<Float4> x,bool pp)435 Float4 tangenth(RValue<Float4> x, bool pp)
436 {
437 Float4 e_x = exponential(x, pp);
438 Float4 e_minus_x = exponential(-x, pp);
439 return (e_x - e_minus_x) / (e_x + e_minus_x);
440 }
441
arccosh(RValue<Float4> x,bool pp)442 Float4 arccosh(RValue<Float4> x, bool pp)
443 {
444 return logarithm(x + Sqrt(x + Float4(1.0f)) * Sqrt(x - Float4(1.0f)), pp);
445 }
446
arcsinh(RValue<Float4> x,bool pp)447 Float4 arcsinh(RValue<Float4> x, bool pp)
448 {
449 return logarithm(x + Sqrt(x * x + Float4(1.0f)), pp);
450 }
451
arctanh(RValue<Float4> x,bool pp)452 Float4 arctanh(RValue<Float4> x, bool pp)
453 {
454 return logarithm((Float4(1.0f) + x) / (Float4(1.0f) - x), pp) * Float4(0.5f);
455 }
456
dot2(const Vector4f & v0,const Vector4f & v1)457 Float4 dot2(const Vector4f &v0, const Vector4f &v1)
458 {
459 return v0.x * v1.x + v0.y * v1.y;
460 }
461
dot3(const Vector4f & v0,const Vector4f & v1)462 Float4 dot3(const Vector4f &v0, const Vector4f &v1)
463 {
464 return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
465 }
466
dot4(const Vector4f & v0,const Vector4f & v1)467 Float4 dot4(const Vector4f &v0, const Vector4f &v1)
468 {
469 return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z + v0.w * v1.w;
470 }
471
transpose4x4(Short4 & row0,Short4 & row1,Short4 & row2,Short4 & row3)472 void transpose4x4(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
473 {
474 Int2 tmp0 = UnpackHigh(row0, row1);
475 Int2 tmp1 = UnpackHigh(row2, row3);
476 Int2 tmp2 = UnpackLow(row0, row1);
477 Int2 tmp3 = UnpackLow(row2, row3);
478
479 row0 = UnpackLow(tmp2, tmp3);
480 row1 = UnpackHigh(tmp2, tmp3);
481 row2 = UnpackLow(tmp0, tmp1);
482 row3 = UnpackHigh(tmp0, tmp1);
483 }
484
transpose4x3(Short4 & row0,Short4 & row1,Short4 & row2,Short4 & row3)485 void transpose4x3(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
486 {
487 Int2 tmp0 = UnpackHigh(row0, row1);
488 Int2 tmp1 = UnpackHigh(row2, row3);
489 Int2 tmp2 = UnpackLow(row0, row1);
490 Int2 tmp3 = UnpackLow(row2, row3);
491
492 row0 = UnpackLow(tmp2, tmp3);
493 row1 = UnpackHigh(tmp2, tmp3);
494 row2 = UnpackLow(tmp0, tmp1);
495 }
496
transpose4x4(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)497 void transpose4x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
498 {
499 Float4 tmp0 = UnpackLow(row0, row1);
500 Float4 tmp1 = UnpackLow(row2, row3);
501 Float4 tmp2 = UnpackHigh(row0, row1);
502 Float4 tmp3 = UnpackHigh(row2, row3);
503
504 row0 = Float4(tmp0.xy, tmp1.xy);
505 row1 = Float4(tmp0.zw, tmp1.zw);
506 row2 = Float4(tmp2.xy, tmp3.xy);
507 row3 = Float4(tmp2.zw, tmp3.zw);
508 }
509
transpose4x3(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)510 void transpose4x3(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
511 {
512 Float4 tmp0 = UnpackLow(row0, row1);
513 Float4 tmp1 = UnpackLow(row2, row3);
514 Float4 tmp2 = UnpackHigh(row0, row1);
515 Float4 tmp3 = UnpackHigh(row2, row3);
516
517 row0 = Float4(tmp0.xy, tmp1.xy);
518 row1 = Float4(tmp0.zw, tmp1.zw);
519 row2 = Float4(tmp2.xy, tmp3.xy);
520 }
521
transpose4x2(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)522 void transpose4x2(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
523 {
524 Float4 tmp0 = UnpackLow(row0, row1);
525 Float4 tmp1 = UnpackLow(row2, row3);
526
527 row0 = Float4(tmp0.xy, tmp1.xy);
528 row1 = Float4(tmp0.zw, tmp1.zw);
529 }
530
transpose4x1(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)531 void transpose4x1(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
532 {
533 Float4 tmp0 = UnpackLow(row0, row1);
534 Float4 tmp1 = UnpackLow(row2, row3);
535
536 row0 = Float4(tmp0.xy, tmp1.xy);
537 }
538
transpose2x4(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)539 void transpose2x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
540 {
541 Float4 tmp01 = UnpackLow(row0, row1);
542 Float4 tmp23 = UnpackHigh(row0, row1);
543
544 row0 = tmp01;
545 row1 = Float4(tmp01.zw, row1.zw);
546 row2 = tmp23;
547 row3 = Float4(tmp23.zw, row3.zw);
548 }
549
transpose4xN(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3,int N)550 void transpose4xN(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3, int N)
551 {
552 switch(N)
553 {
554 case 1: transpose4x1(row0, row1, row2, row3); break;
555 case 2: transpose4x2(row0, row1, row2, row3); break;
556 case 3: transpose4x3(row0, row1, row2, row3); break;
557 case 4: transpose4x4(row0, row1, row2, row3); break;
558 }
559 }
560
halfToFloatBits(SIMD::UInt halfBits)561 SIMD::UInt halfToFloatBits(SIMD::UInt halfBits)
562 {
563 auto magic = SIMD::UInt(126 << 23);
564
565 auto sign16 = halfBits & SIMD::UInt(0x8000);
566 auto man16 = halfBits & SIMD::UInt(0x03FF);
567 auto exp16 = halfBits & SIMD::UInt(0x7C00);
568
569 auto isDnormOrZero = CmpEQ(exp16, SIMD::UInt(0));
570 auto isInfOrNaN = CmpEQ(exp16, SIMD::UInt(0x7C00));
571
572 auto sign32 = sign16 << 16;
573 auto man32 = man16 << 13;
574 auto exp32 = (exp16 + SIMD::UInt(0x1C000)) << 13;
575 auto norm32 = (man32 | exp32) | (isInfOrNaN & SIMD::UInt(0x7F800000));
576
577 auto denorm32 = As<SIMD::UInt>(As<SIMD::Float>(magic + man16) - As<SIMD::Float>(magic));
578
579 return sign32 | (norm32 & ~isDnormOrZero) | (denorm32 & isDnormOrZero);
580 }
581
floatToHalfBits(SIMD::UInt floatBits,bool storeInUpperBits)582 SIMD::UInt floatToHalfBits(SIMD::UInt floatBits, bool storeInUpperBits)
583 {
584 SIMD::UInt sign = floatBits & SIMD::UInt(0x80000000);
585 SIMD::UInt abs = floatBits & SIMD::UInt(0x7FFFFFFF);
586
587 SIMD::UInt normal = CmpNLE(abs, SIMD::UInt(0x38800000));
588
589 SIMD::UInt mantissa = (abs & SIMD::UInt(0x007FFFFF)) | SIMD::UInt(0x00800000);
590 SIMD::UInt e = SIMD::UInt(113) - (abs >> 23);
591 SIMD::UInt denormal = CmpLT(e, SIMD::UInt(24)) & (mantissa >> e);
592
593 SIMD::UInt base = (normal & abs) | (~normal & denormal); // TODO: IfThenElse()
594
595 // float exponent bias is 127, half bias is 15, so adjust by -112
596 SIMD::UInt bias = normal & SIMD::UInt(0xC8000000);
597
598 SIMD::UInt rounded = base + bias + SIMD::UInt(0x00000FFF) + ((base >> 13) & SIMD::UInt(1));
599 SIMD::UInt fp16u = rounded >> 13;
600
601 // Infinity
602 fp16u |= CmpNLE(abs, SIMD::UInt(0x47FFEFFF)) & SIMD::UInt(0x7FFF);
603
604 return storeInUpperBits ? (sign | (fp16u << 16)) : ((sign >> 16) | fp16u);
605 }
606
r11g11b10Unpack(UInt r11g11b10bits)607 Float4 r11g11b10Unpack(UInt r11g11b10bits)
608 {
609 // 10 (or 11) bit float formats are unsigned formats with a 5 bit exponent and a 5 (or 6) bit mantissa.
610 // Since the Half float format also has a 5 bit exponent, we can convert these formats to half by
611 // copy/pasting the bits so the the exponent bits and top mantissa bits are aligned to the half format.
612 // In this case, we have:
613 // MSB | B B B B B B B B B B G G G G G G G G G G G R R R R R R R R R R R | LSB
614 UInt4 halfBits;
615 halfBits = Insert(halfBits, (r11g11b10bits & UInt(0x000007FFu)) << 4, 0);
616 halfBits = Insert(halfBits, (r11g11b10bits & UInt(0x003FF800u)) >> 7, 1);
617 halfBits = Insert(halfBits, (r11g11b10bits & UInt(0xFFC00000u)) >> 17, 2);
618 halfBits = Insert(halfBits, UInt(0x00003C00u), 3);
619 return As<Float4>(halfToFloatBits(halfBits));
620 }
621
r11g11b10Pack(const Float4 & value)622 UInt r11g11b10Pack(const Float4 &value)
623 {
624 // 10 and 11 bit floats are unsigned, so their minimal value is 0
625 auto halfBits = floatToHalfBits(As<UInt4>(Max(value, Float4(0.0f))), true);
626 // Truncates instead of rounding. See b/147900455
627 UInt4 truncBits = halfBits & UInt4(0x7FF00000, 0x7FF00000, 0x7FE00000, 0);
628 return (UInt(truncBits.x) >> 20) | (UInt(truncBits.y) >> 9) | (UInt(truncBits.z) << 1);
629 }
630
a2b10g10r10Unpack(const Int4 & value)631 Vector4s a2b10g10r10Unpack(const Int4 &value)
632 {
633 Vector4s result;
634
635 result.x = Short4(value << 6) & Short4(0xFFC0u);
636 result.y = Short4(value >> 4) & Short4(0xFFC0u);
637 result.z = Short4(value >> 14) & Short4(0xFFC0u);
638 result.w = Short4(value >> 16) & Short4(0xC000u);
639
640 // Expand to 16 bit range
641 result.x |= As<Short4>(As<UShort4>(result.x) >> 10);
642 result.y |= As<Short4>(As<UShort4>(result.y) >> 10);
643 result.z |= As<Short4>(As<UShort4>(result.z) >> 10);
644 result.w |= As<Short4>(As<UShort4>(result.w) >> 2);
645 result.w |= As<Short4>(As<UShort4>(result.w) >> 4);
646 result.w |= As<Short4>(As<UShort4>(result.w) >> 8);
647
648 return result;
649 }
650
a2r10g10b10Unpack(const Int4 & value)651 Vector4s a2r10g10b10Unpack(const Int4 &value)
652 {
653 Vector4s result;
654
655 result.x = Short4(value >> 14) & Short4(0xFFC0u);
656 result.y = Short4(value >> 4) & Short4(0xFFC0u);
657 result.z = Short4(value << 6) & Short4(0xFFC0u);
658 result.w = Short4(value >> 16) & Short4(0xC000u);
659
660 // Expand to 16 bit range
661 result.x |= As<Short4>(As<UShort4>(result.x) >> 10);
662 result.y |= As<Short4>(As<UShort4>(result.y) >> 10);
663 result.z |= As<Short4>(As<UShort4>(result.z) >> 10);
664 result.w |= As<Short4>(As<UShort4>(result.w) >> 2);
665 result.w |= As<Short4>(As<UShort4>(result.w) >> 4);
666 result.w |= As<Short4>(As<UShort4>(result.w) >> 8);
667
668 return result;
669 }
670
AnyTrue(rr::RValue<sw::SIMD::Int> const & ints)671 rr::RValue<rr::Bool> AnyTrue(rr::RValue<sw::SIMD::Int> const &ints)
672 {
673 return rr::SignMask(ints) != 0;
674 }
675
AnyFalse(rr::RValue<sw::SIMD::Int> const & ints)676 rr::RValue<rr::Bool> AnyFalse(rr::RValue<sw::SIMD::Int> const &ints)
677 {
678 return rr::SignMask(~ints) != 0;
679 }
680
Sign(rr::RValue<sw::SIMD::Float> const & val)681 rr::RValue<sw::SIMD::Float> Sign(rr::RValue<sw::SIMD::Float> const &val)
682 {
683 return rr::As<sw::SIMD::Float>((rr::As<sw::SIMD::UInt>(val) & sw::SIMD::UInt(0x80000000)) | sw::SIMD::UInt(0x3f800000));
684 }
685
686 // Returns the <whole, frac> of val.
687 // Both whole and frac will have the same sign as val.
688 std::pair<rr::RValue<sw::SIMD::Float>, rr::RValue<sw::SIMD::Float>>
Modf(rr::RValue<sw::SIMD::Float> const & val)689 Modf(rr::RValue<sw::SIMD::Float> const &val)
690 {
691 auto abs = Abs(val);
692 auto sign = Sign(val);
693 auto whole = Floor(abs) * sign;
694 auto frac = Frac(abs) * sign;
695 return std::make_pair(whole, frac);
696 }
697
698 // Returns the number of 1s in bits, per lane.
CountBits(rr::RValue<sw::SIMD::UInt> const & bits)699 sw::SIMD::UInt CountBits(rr::RValue<sw::SIMD::UInt> const &bits)
700 {
701 // TODO: Add an intrinsic to reactor. Even if there isn't a
702 // single vector instruction, there may be target-dependent
703 // ways to make this faster.
704 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
705 sw::SIMD::UInt c = bits - ((bits >> 1) & sw::SIMD::UInt(0x55555555));
706 c = ((c >> 2) & sw::SIMD::UInt(0x33333333)) + (c & sw::SIMD::UInt(0x33333333));
707 c = ((c >> 4) + c) & sw::SIMD::UInt(0x0F0F0F0F);
708 c = ((c >> 8) + c) & sw::SIMD::UInt(0x00FF00FF);
709 c = ((c >> 16) + c) & sw::SIMD::UInt(0x0000FFFF);
710 return c;
711 }
712
713 // Returns 1 << bits.
714 // If the resulting bit overflows a 32 bit integer, 0 is returned.
NthBit32(rr::RValue<sw::SIMD::UInt> const & bits)715 rr::RValue<sw::SIMD::UInt> NthBit32(rr::RValue<sw::SIMD::UInt> const &bits)
716 {
717 return ((sw::SIMD::UInt(1) << bits) & rr::CmpLT(bits, sw::SIMD::UInt(32)));
718 }
719
720 // Returns bitCount number of of 1's starting from the LSB.
Bitmask32(rr::RValue<sw::SIMD::UInt> const & bitCount)721 rr::RValue<sw::SIMD::UInt> Bitmask32(rr::RValue<sw::SIMD::UInt> const &bitCount)
722 {
723 return NthBit32(bitCount) - sw::SIMD::UInt(1);
724 }
725
726 // Performs a fused-multiply add, returning a * b + c.
FMA(rr::RValue<sw::SIMD::Float> const & a,rr::RValue<sw::SIMD::Float> const & b,rr::RValue<sw::SIMD::Float> const & c)727 rr::RValue<sw::SIMD::Float> FMA(
728 rr::RValue<sw::SIMD::Float> const &a,
729 rr::RValue<sw::SIMD::Float> const &b,
730 rr::RValue<sw::SIMD::Float> const &c)
731 {
732 return a * b + c;
733 }
734
735 // Returns the exponent of the floating point number f.
736 // Assumes IEEE 754
Exponent(rr::RValue<sw::SIMD::Float> f)737 rr::RValue<sw::SIMD::Int> Exponent(rr::RValue<sw::SIMD::Float> f)
738 {
739 auto v = rr::As<sw::SIMD::UInt>(f);
740 return (sw::SIMD::Int((v >> sw::SIMD::UInt(23)) & sw::SIMD::UInt(0xFF)) - sw::SIMD::Int(126));
741 }
742
743 // Returns y if y < x; otherwise result is x.
744 // If one operand is a NaN, the other operand is the result.
745 // If both operands are NaN, the result is a NaN.
NMin(rr::RValue<sw::SIMD::Float> const & x,rr::RValue<sw::SIMD::Float> const & y)746 rr::RValue<sw::SIMD::Float> NMin(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y)
747 {
748 using namespace rr;
749 auto xIsNan = IsNan(x);
750 auto yIsNan = IsNan(y);
751 return As<sw::SIMD::Float>(
752 // If neither are NaN, return min
753 ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Min(x, y))) |
754 // If one operand is a NaN, the other operand is the result
755 // If both operands are NaN, the result is a NaN.
756 ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) |
757 (xIsNan & As<sw::SIMD::Int>(y)));
758 }
759
760 // Returns y if y > x; otherwise result is x.
761 // If one operand is a NaN, the other operand is the result.
762 // If both operands are NaN, the result is a NaN.
NMax(rr::RValue<sw::SIMD::Float> const & x,rr::RValue<sw::SIMD::Float> const & y)763 rr::RValue<sw::SIMD::Float> NMax(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y)
764 {
765 using namespace rr;
766 auto xIsNan = IsNan(x);
767 auto yIsNan = IsNan(y);
768 return As<sw::SIMD::Float>(
769 // If neither are NaN, return max
770 ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Max(x, y))) |
771 // If one operand is a NaN, the other operand is the result
772 // If both operands are NaN, the result is a NaN.
773 ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) |
774 (xIsNan & As<sw::SIMD::Int>(y)));
775 }
776
777 // Returns the determinant of a 2x2 matrix.
Determinant(rr::RValue<sw::SIMD::Float> const & a,rr::RValue<sw::SIMD::Float> const & b,rr::RValue<sw::SIMD::Float> const & c,rr::RValue<sw::SIMD::Float> const & d)778 rr::RValue<sw::SIMD::Float> Determinant(
779 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b,
780 rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d)
781 {
782 return a * d - b * c;
783 }
784
785 // Returns the determinant of a 3x3 matrix.
Determinant(rr::RValue<sw::SIMD::Float> const & a,rr::RValue<sw::SIMD::Float> const & b,rr::RValue<sw::SIMD::Float> const & c,rr::RValue<sw::SIMD::Float> const & d,rr::RValue<sw::SIMD::Float> const & e,rr::RValue<sw::SIMD::Float> const & f,rr::RValue<sw::SIMD::Float> const & g,rr::RValue<sw::SIMD::Float> const & h,rr::RValue<sw::SIMD::Float> const & i)786 rr::RValue<sw::SIMD::Float> Determinant(
787 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c,
788 rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f,
789 rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i)
790 {
791 return a * e * i + b * f * g + c * d * h - c * e * g - b * d * i - a * f * h;
792 }
793
794 // Returns the determinant of a 4x4 matrix.
Determinant(rr::RValue<sw::SIMD::Float> const & a,rr::RValue<sw::SIMD::Float> const & b,rr::RValue<sw::SIMD::Float> const & c,rr::RValue<sw::SIMD::Float> const & d,rr::RValue<sw::SIMD::Float> const & e,rr::RValue<sw::SIMD::Float> const & f,rr::RValue<sw::SIMD::Float> const & g,rr::RValue<sw::SIMD::Float> const & h,rr::RValue<sw::SIMD::Float> const & i,rr::RValue<sw::SIMD::Float> const & j,rr::RValue<sw::SIMD::Float> const & k,rr::RValue<sw::SIMD::Float> const & l,rr::RValue<sw::SIMD::Float> const & m,rr::RValue<sw::SIMD::Float> const & n,rr::RValue<sw::SIMD::Float> const & o,rr::RValue<sw::SIMD::Float> const & p)795 rr::RValue<sw::SIMD::Float> Determinant(
796 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d,
797 rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f, rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h,
798 rr::RValue<sw::SIMD::Float> const &i, rr::RValue<sw::SIMD::Float> const &j, rr::RValue<sw::SIMD::Float> const &k, rr::RValue<sw::SIMD::Float> const &l,
799 rr::RValue<sw::SIMD::Float> const &m, rr::RValue<sw::SIMD::Float> const &n, rr::RValue<sw::SIMD::Float> const &o, rr::RValue<sw::SIMD::Float> const &p)
800 {
801 return a * Determinant(f, g, h,
802 j, k, l,
803 n, o, p) -
804 b * Determinant(e, g, h,
805 i, k, l,
806 m, o, p) +
807 c * Determinant(e, f, h,
808 i, j, l,
809 m, n, p) -
810 d * Determinant(e, f, g,
811 i, j, k,
812 m, n, o);
813 }
814
815 // Returns the inverse of a 2x2 matrix.
MatrixInverse(rr::RValue<sw::SIMD::Float> const & a,rr::RValue<sw::SIMD::Float> const & b,rr::RValue<sw::SIMD::Float> const & c,rr::RValue<sw::SIMD::Float> const & d)816 std::array<rr::RValue<sw::SIMD::Float>, 4> MatrixInverse(
817 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b,
818 rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d)
819 {
820 auto s = sw::SIMD::Float(1.0f) / Determinant(a, b, c, d);
821 return { { s * d, -s * b, -s * c, s * a } };
822 }
823
824 // Returns the inverse of a 3x3 matrix.
MatrixInverse(rr::RValue<sw::SIMD::Float> const & a,rr::RValue<sw::SIMD::Float> const & b,rr::RValue<sw::SIMD::Float> const & c,rr::RValue<sw::SIMD::Float> const & d,rr::RValue<sw::SIMD::Float> const & e,rr::RValue<sw::SIMD::Float> const & f,rr::RValue<sw::SIMD::Float> const & g,rr::RValue<sw::SIMD::Float> const & h,rr::RValue<sw::SIMD::Float> const & i)825 std::array<rr::RValue<sw::SIMD::Float>, 9> MatrixInverse(
826 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c,
827 rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f,
828 rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i)
829 {
830 auto s = sw::SIMD::Float(1.0f) / Determinant(
831 a, b, c,
832 d, e, f,
833 g, h, i); // TODO: duplicate arithmetic calculating the det and below.
834
835 return { {
836 s * (e * i - f * h),
837 s * (c * h - b * i),
838 s * (b * f - c * e),
839 s * (f * g - d * i),
840 s * (a * i - c * g),
841 s * (c * d - a * f),
842 s * (d * h - e * g),
843 s * (b * g - a * h),
844 s * (a * e - b * d),
845 } };
846 }
847
848 // Returns the inverse of a 4x4 matrix.
MatrixInverse(rr::RValue<sw::SIMD::Float> const & a,rr::RValue<sw::SIMD::Float> const & b,rr::RValue<sw::SIMD::Float> const & c,rr::RValue<sw::SIMD::Float> const & d,rr::RValue<sw::SIMD::Float> const & e,rr::RValue<sw::SIMD::Float> const & f,rr::RValue<sw::SIMD::Float> const & g,rr::RValue<sw::SIMD::Float> const & h,rr::RValue<sw::SIMD::Float> const & i,rr::RValue<sw::SIMD::Float> const & j,rr::RValue<sw::SIMD::Float> const & k,rr::RValue<sw::SIMD::Float> const & l,rr::RValue<sw::SIMD::Float> const & m,rr::RValue<sw::SIMD::Float> const & n,rr::RValue<sw::SIMD::Float> const & o,rr::RValue<sw::SIMD::Float> const & p)849 std::array<rr::RValue<sw::SIMD::Float>, 16> MatrixInverse(
850 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d,
851 rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f, rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h,
852 rr::RValue<sw::SIMD::Float> const &i, rr::RValue<sw::SIMD::Float> const &j, rr::RValue<sw::SIMD::Float> const &k, rr::RValue<sw::SIMD::Float> const &l,
853 rr::RValue<sw::SIMD::Float> const &m, rr::RValue<sw::SIMD::Float> const &n, rr::RValue<sw::SIMD::Float> const &o, rr::RValue<sw::SIMD::Float> const &p)
854 {
855 auto s = sw::SIMD::Float(1.0f) / Determinant(
856 a, b, c, d,
857 e, f, g, h,
858 i, j, k, l,
859 m, n, o, p); // TODO: duplicate arithmetic calculating the det and below.
860
861 auto kplo = k * p - l * o, jpln = j * p - l * n, jokn = j * o - k * n;
862 auto gpho = g * p - h * o, fphn = f * p - h * n, fogn = f * o - g * n;
863 auto glhk = g * l - h * k, flhj = f * l - h * j, fkgj = f * k - g * j;
864 auto iplm = i * p - l * m, iokm = i * o - k * m, ephm = e * p - h * m;
865 auto eogm = e * o - g * m, elhi = e * l - h * i, ekgi = e * k - g * i;
866 auto injm = i * n - j * m, enfm = e * n - f * m, ejfi = e * j - f * i;
867
868 return { {
869 s * (f * kplo - g * jpln + h * jokn),
870 s * (-b * kplo + c * jpln - d * jokn),
871 s * (b * gpho - c * fphn + d * fogn),
872 s * (-b * glhk + c * flhj - d * fkgj),
873
874 s * (-e * kplo + g * iplm - h * iokm),
875 s * (a * kplo - c * iplm + d * iokm),
876 s * (-a * gpho + c * ephm - d * eogm),
877 s * (a * glhk - c * elhi + d * ekgi),
878
879 s * (e * jpln - f * iplm + h * injm),
880 s * (-a * jpln + b * iplm - d * injm),
881 s * (a * fphn - b * ephm + d * enfm),
882 s * (-a * flhj + b * elhi - d * ejfi),
883
884 s * (-e * jokn + f * iokm - g * injm),
885 s * (a * jokn - b * iokm + c * injm),
886 s * (-a * fogn + b * eogm - c * enfm),
887 s * (a * fkgj - b * ekgi + c * ejfi),
888 } };
889 }
890
891 namespace SIMD {
892
Pointer(rr::Pointer<Byte> base,rr::Int limit)893 Pointer::Pointer(rr::Pointer<Byte> base, rr::Int limit)
894 : base(base)
895 , dynamicLimit(limit)
896 , staticLimit(0)
897 , dynamicOffsets(0)
898 , staticOffsets{}
899 , hasDynamicLimit(true)
900 , hasDynamicOffsets(false)
901 {}
902
Pointer(rr::Pointer<Byte> base,unsigned int limit)903 Pointer::Pointer(rr::Pointer<Byte> base, unsigned int limit)
904 : base(base)
905 , dynamicLimit(0)
906 , staticLimit(limit)
907 , dynamicOffsets(0)
908 , staticOffsets{}
909 , hasDynamicLimit(false)
910 , hasDynamicOffsets(false)
911 {}
912
Pointer(rr::Pointer<Byte> base,rr::Int limit,SIMD::Int offset)913 Pointer::Pointer(rr::Pointer<Byte> base, rr::Int limit, SIMD::Int offset)
914 : base(base)
915 , dynamicLimit(limit)
916 , staticLimit(0)
917 , dynamicOffsets(offset)
918 , staticOffsets{}
919 , hasDynamicLimit(true)
920 , hasDynamicOffsets(true)
921 {}
922
Pointer(rr::Pointer<Byte> base,unsigned int limit,SIMD::Int offset)923 Pointer::Pointer(rr::Pointer<Byte> base, unsigned int limit, SIMD::Int offset)
924 : base(base)
925 , dynamicLimit(0)
926 , staticLimit(limit)
927 , dynamicOffsets(offset)
928 , staticOffsets{}
929 , hasDynamicLimit(false)
930 , hasDynamicOffsets(true)
931 {}
932
operator +=(Int i)933 Pointer &Pointer::operator+=(Int i)
934 {
935 dynamicOffsets += i;
936 hasDynamicOffsets = true;
937 return *this;
938 }
939
operator *=(Int i)940 Pointer &Pointer::operator*=(Int i)
941 {
942 dynamicOffsets = offsets() * i;
943 staticOffsets = {};
944 hasDynamicOffsets = true;
945 return *this;
946 }
947
operator +(SIMD::Int i)948 Pointer Pointer::operator+(SIMD::Int i)
949 {
950 Pointer p = *this;
951 p += i;
952 return p;
953 }
operator *(SIMD::Int i)954 Pointer Pointer::operator*(SIMD::Int i)
955 {
956 Pointer p = *this;
957 p *= i;
958 return p;
959 }
960
operator +=(int i)961 Pointer &Pointer::operator+=(int i)
962 {
963 for(int el = 0; el < SIMD::Width; el++) { staticOffsets[el] += i; }
964 return *this;
965 }
966
operator *=(int i)967 Pointer &Pointer::operator*=(int i)
968 {
969 for(int el = 0; el < SIMD::Width; el++) { staticOffsets[el] *= i; }
970 if(hasDynamicOffsets)
971 {
972 dynamicOffsets *= SIMD::Int(i);
973 }
974 return *this;
975 }
976
operator +(int i)977 Pointer Pointer::operator+(int i)
978 {
979 Pointer p = *this;
980 p += i;
981 return p;
982 }
operator *(int i)983 Pointer Pointer::operator*(int i)
984 {
985 Pointer p = *this;
986 p *= i;
987 return p;
988 }
989
offsets() const990 SIMD::Int Pointer::offsets() const
991 {
992 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
993 return dynamicOffsets + SIMD::Int(staticOffsets[0], staticOffsets[1], staticOffsets[2], staticOffsets[3]);
994 }
995
isInBounds(unsigned int accessSize,OutOfBoundsBehavior robustness) const996 SIMD::Int Pointer::isInBounds(unsigned int accessSize, OutOfBoundsBehavior robustness) const
997 {
998 ASSERT(accessSize > 0);
999
1000 if(isStaticallyInBounds(accessSize, robustness))
1001 {
1002 return SIMD::Int(0xffffffff);
1003 }
1004
1005 if(!hasDynamicOffsets && !hasDynamicLimit)
1006 {
1007 // Common fast paths.
1008 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1009 return SIMD::Int(
1010 (staticOffsets[0] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
1011 (staticOffsets[1] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
1012 (staticOffsets[2] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
1013 (staticOffsets[3] + accessSize - 1 < staticLimit) ? 0xffffffff : 0);
1014 }
1015
1016 return CmpLT(offsets() + SIMD::Int(accessSize - 1), SIMD::Int(limit()));
1017 }
1018
isStaticallyInBounds(unsigned int accessSize,OutOfBoundsBehavior robustness) const1019 bool Pointer::isStaticallyInBounds(unsigned int accessSize, OutOfBoundsBehavior robustness) const
1020 {
1021 if(hasDynamicOffsets)
1022 {
1023 return false;
1024 }
1025
1026 if(hasDynamicLimit)
1027 {
1028 if(hasStaticEqualOffsets() || hasStaticSequentialOffsets(accessSize))
1029 {
1030 switch(robustness)
1031 {
1032 case OutOfBoundsBehavior::UndefinedBehavior:
1033 // With this robustness setting the application/compiler guarantees in-bounds accesses on active lanes,
1034 // but since it can't know in advance which branches are taken this must be true even for inactives lanes.
1035 return true;
1036 case OutOfBoundsBehavior::Nullify:
1037 case OutOfBoundsBehavior::RobustBufferAccess:
1038 case OutOfBoundsBehavior::UndefinedValue:
1039 return false;
1040 }
1041 }
1042 }
1043
1044 for(int i = 0; i < SIMD::Width; i++)
1045 {
1046 if(staticOffsets[i] + accessSize - 1 >= staticLimit)
1047 {
1048 return false;
1049 }
1050 }
1051
1052 return true;
1053 }
1054
limit() const1055 rr::Int Pointer::limit() const
1056 {
1057 return dynamicLimit + staticLimit;
1058 }
1059
1060 // Returns true if all offsets are sequential
1061 // (N+0*step, N+1*step, N+2*step, N+3*step)
hasSequentialOffsets(unsigned int step) const1062 rr::Bool Pointer::hasSequentialOffsets(unsigned int step) const
1063 {
1064 if(hasDynamicOffsets)
1065 {
1066 auto o = offsets();
1067 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1068 return rr::SignMask(~CmpEQ(o.yzww, o + SIMD::Int(1 * step, 2 * step, 3 * step, 0))) == 0;
1069 }
1070 return hasStaticSequentialOffsets(step);
1071 }
1072
1073 // Returns true if all offsets are are compile-time static and
1074 // sequential (N+0*step, N+1*step, N+2*step, N+3*step)
hasStaticSequentialOffsets(unsigned int step) const1075 bool Pointer::hasStaticSequentialOffsets(unsigned int step) const
1076 {
1077 if(hasDynamicOffsets)
1078 {
1079 return false;
1080 }
1081 for(int i = 1; i < SIMD::Width; i++)
1082 {
1083 if(staticOffsets[i - 1] + int32_t(step) != staticOffsets[i]) { return false; }
1084 }
1085 return true;
1086 }
1087
1088 // Returns true if all offsets are equal (N, N, N, N)
hasEqualOffsets() const1089 rr::Bool Pointer::hasEqualOffsets() const
1090 {
1091 if(hasDynamicOffsets)
1092 {
1093 auto o = offsets();
1094 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1095 return rr::SignMask(~CmpEQ(o, o.yzwx)) == 0;
1096 }
1097 return hasStaticEqualOffsets();
1098 }
1099
1100 // Returns true if all offsets are compile-time static and are equal
1101 // (N, N, N, N)
hasStaticEqualOffsets() const1102 bool Pointer::hasStaticEqualOffsets() const
1103 {
1104 if(hasDynamicOffsets)
1105 {
1106 return false;
1107 }
1108 for(int i = 1; i < SIMD::Width; i++)
1109 {
1110 if(staticOffsets[i - 1] != staticOffsets[i]) { return false; }
1111 }
1112 return true;
1113 }
1114
1115 } // namespace SIMD
1116
1117 } // namespace sw
1118