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
AnyTrue(rr::RValue<sw::SIMD::Int> const & ints)631 rr::RValue<rr::Bool> AnyTrue(rr::RValue<sw::SIMD::Int> const &ints)
632 {
633 return rr::SignMask(ints) != 0;
634 }
635
AnyFalse(rr::RValue<sw::SIMD::Int> const & ints)636 rr::RValue<rr::Bool> AnyFalse(rr::RValue<sw::SIMD::Int> const &ints)
637 {
638 return rr::SignMask(~ints) != 0;
639 }
640
Sign(rr::RValue<sw::SIMD::Float> const & val)641 rr::RValue<sw::SIMD::Float> Sign(rr::RValue<sw::SIMD::Float> const &val)
642 {
643 return rr::As<sw::SIMD::Float>((rr::As<sw::SIMD::UInt>(val) & sw::SIMD::UInt(0x80000000)) | sw::SIMD::UInt(0x3f800000));
644 }
645
646 // Returns the <whole, frac> of val.
647 // Both whole and frac will have the same sign as val.
648 std::pair<rr::RValue<sw::SIMD::Float>, rr::RValue<sw::SIMD::Float>>
Modf(rr::RValue<sw::SIMD::Float> const & val)649 Modf(rr::RValue<sw::SIMD::Float> const &val)
650 {
651 auto abs = Abs(val);
652 auto sign = Sign(val);
653 auto whole = Floor(abs) * sign;
654 auto frac = Frac(abs) * sign;
655 return std::make_pair(whole, frac);
656 }
657
658 // Returns the number of 1s in bits, per lane.
CountBits(rr::RValue<sw::SIMD::UInt> const & bits)659 sw::SIMD::UInt CountBits(rr::RValue<sw::SIMD::UInt> const &bits)
660 {
661 // TODO: Add an intrinsic to reactor. Even if there isn't a
662 // single vector instruction, there may be target-dependent
663 // ways to make this faster.
664 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
665 sw::SIMD::UInt c = bits - ((bits >> 1) & sw::SIMD::UInt(0x55555555));
666 c = ((c >> 2) & sw::SIMD::UInt(0x33333333)) + (c & sw::SIMD::UInt(0x33333333));
667 c = ((c >> 4) + c) & sw::SIMD::UInt(0x0F0F0F0F);
668 c = ((c >> 8) + c) & sw::SIMD::UInt(0x00FF00FF);
669 c = ((c >> 16) + c) & sw::SIMD::UInt(0x0000FFFF);
670 return c;
671 }
672
673 // Returns 1 << bits.
674 // If the resulting bit overflows a 32 bit integer, 0 is returned.
NthBit32(rr::RValue<sw::SIMD::UInt> const & bits)675 rr::RValue<sw::SIMD::UInt> NthBit32(rr::RValue<sw::SIMD::UInt> const &bits)
676 {
677 return ((sw::SIMD::UInt(1) << bits) & rr::CmpLT(bits, sw::SIMD::UInt(32)));
678 }
679
680 // Returns bitCount number of of 1's starting from the LSB.
Bitmask32(rr::RValue<sw::SIMD::UInt> const & bitCount)681 rr::RValue<sw::SIMD::UInt> Bitmask32(rr::RValue<sw::SIMD::UInt> const &bitCount)
682 {
683 return NthBit32(bitCount) - sw::SIMD::UInt(1);
684 }
685
686 // 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)687 rr::RValue<sw::SIMD::Float> FMA(
688 rr::RValue<sw::SIMD::Float> const &a,
689 rr::RValue<sw::SIMD::Float> const &b,
690 rr::RValue<sw::SIMD::Float> const &c)
691 {
692 return a * b + c;
693 }
694
695 // Returns the exponent of the floating point number f.
696 // Assumes IEEE 754
Exponent(rr::RValue<sw::SIMD::Float> f)697 rr::RValue<sw::SIMD::Int> Exponent(rr::RValue<sw::SIMD::Float> f)
698 {
699 auto v = rr::As<sw::SIMD::UInt>(f);
700 return (sw::SIMD::Int((v >> sw::SIMD::UInt(23)) & sw::SIMD::UInt(0xFF)) - sw::SIMD::Int(126));
701 }
702
703 // Returns y if y < x; otherwise result is x.
704 // If one operand is a NaN, the other operand is the result.
705 // 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)706 rr::RValue<sw::SIMD::Float> NMin(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y)
707 {
708 using namespace rr;
709 auto xIsNan = IsNan(x);
710 auto yIsNan = IsNan(y);
711 return As<sw::SIMD::Float>(
712 // If neither are NaN, return min
713 ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Min(x, y))) |
714 // If one operand is a NaN, the other operand is the result
715 // If both operands are NaN, the result is a NaN.
716 ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) |
717 (xIsNan & As<sw::SIMD::Int>(y)));
718 }
719
720 // Returns y if y > x; otherwise result is x.
721 // If one operand is a NaN, the other operand is the result.
722 // 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)723 rr::RValue<sw::SIMD::Float> NMax(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y)
724 {
725 using namespace rr;
726 auto xIsNan = IsNan(x);
727 auto yIsNan = IsNan(y);
728 return As<sw::SIMD::Float>(
729 // If neither are NaN, return max
730 ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Max(x, y))) |
731 // If one operand is a NaN, the other operand is the result
732 // If both operands are NaN, the result is a NaN.
733 ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) |
734 (xIsNan & As<sw::SIMD::Int>(y)));
735 }
736
737 // 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)738 rr::RValue<sw::SIMD::Float> Determinant(
739 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b,
740 rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d)
741 {
742 return a * d - b * c;
743 }
744
745 // 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)746 rr::RValue<sw::SIMD::Float> Determinant(
747 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c,
748 rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f,
749 rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i)
750 {
751 return a * e * i + b * f * g + c * d * h - c * e * g - b * d * i - a * f * h;
752 }
753
754 // 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)755 rr::RValue<sw::SIMD::Float> Determinant(
756 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,
757 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,
758 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,
759 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)
760 {
761 return a * Determinant(f, g, h,
762 j, k, l,
763 n, o, p) -
764 b * Determinant(e, g, h,
765 i, k, l,
766 m, o, p) +
767 c * Determinant(e, f, h,
768 i, j, l,
769 m, n, p) -
770 d * Determinant(e, f, g,
771 i, j, k,
772 m, n, o);
773 }
774
775 // 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)776 std::array<rr::RValue<sw::SIMD::Float>, 4> MatrixInverse(
777 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b,
778 rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d)
779 {
780 auto s = sw::SIMD::Float(1.0f) / Determinant(a, b, c, d);
781 return { { s * d, -s * b, -s * c, s * a } };
782 }
783
784 // 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)785 std::array<rr::RValue<sw::SIMD::Float>, 9> MatrixInverse(
786 rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c,
787 rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f,
788 rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i)
789 {
790 auto s = sw::SIMD::Float(1.0f) / Determinant(
791 a, b, c,
792 d, e, f,
793 g, h, i); // TODO: duplicate arithmetic calculating the det and below.
794
795 return { {
796 s * (e * i - f * h),
797 s * (c * h - b * i),
798 s * (b * f - c * e),
799 s * (f * g - d * i),
800 s * (a * i - c * g),
801 s * (c * d - a * f),
802 s * (d * h - e * g),
803 s * (b * g - a * h),
804 s * (a * e - b * d),
805 } };
806 }
807
808 // 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)809 std::array<rr::RValue<sw::SIMD::Float>, 16> MatrixInverse(
810 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,
811 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,
812 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,
813 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)
814 {
815 auto s = sw::SIMD::Float(1.0f) / Determinant(
816 a, b, c, d,
817 e, f, g, h,
818 i, j, k, l,
819 m, n, o, p); // TODO: duplicate arithmetic calculating the det and below.
820
821 auto kplo = k * p - l * o, jpln = j * p - l * n, jokn = j * o - k * n;
822 auto gpho = g * p - h * o, fphn = f * p - h * n, fogn = f * o - g * n;
823 auto glhk = g * l - h * k, flhj = f * l - h * j, fkgj = f * k - g * j;
824 auto iplm = i * p - l * m, iokm = i * o - k * m, ephm = e * p - h * m;
825 auto eogm = e * o - g * m, elhi = e * l - h * i, ekgi = e * k - g * i;
826 auto injm = i * n - j * m, enfm = e * n - f * m, ejfi = e * j - f * i;
827
828 return { {
829 s * (f * kplo - g * jpln + h * jokn),
830 s * (-b * kplo + c * jpln - d * jokn),
831 s * (b * gpho - c * fphn + d * fogn),
832 s * (-b * glhk + c * flhj - d * fkgj),
833
834 s * (-e * kplo + g * iplm - h * iokm),
835 s * (a * kplo - c * iplm + d * iokm),
836 s * (-a * gpho + c * ephm - d * eogm),
837 s * (a * glhk - c * elhi + d * ekgi),
838
839 s * (e * jpln - f * iplm + h * injm),
840 s * (-a * jpln + b * iplm - d * injm),
841 s * (a * fphn - b * ephm + d * enfm),
842 s * (-a * flhj + b * elhi - d * ejfi),
843
844 s * (-e * jokn + f * iokm - g * injm),
845 s * (a * jokn - b * iokm + c * injm),
846 s * (-a * fogn + b * eogm - c * enfm),
847 s * (a * fkgj - b * ekgi + c * ejfi),
848 } };
849 }
850
851 namespace SIMD {
852
Pointer(rr::Pointer<Byte> base,rr::Int limit)853 Pointer::Pointer(rr::Pointer<Byte> base, rr::Int limit)
854 : base(base)
855 , dynamicLimit(limit)
856 , staticLimit(0)
857 , dynamicOffsets(0)
858 , staticOffsets{}
859 , hasDynamicLimit(true)
860 , hasDynamicOffsets(false)
861 {}
862
Pointer(rr::Pointer<Byte> base,unsigned int limit)863 Pointer::Pointer(rr::Pointer<Byte> base, unsigned int limit)
864 : base(base)
865 , dynamicLimit(0)
866 , staticLimit(limit)
867 , dynamicOffsets(0)
868 , staticOffsets{}
869 , hasDynamicLimit(false)
870 , hasDynamicOffsets(false)
871 {}
872
Pointer(rr::Pointer<Byte> base,rr::Int limit,SIMD::Int offset)873 Pointer::Pointer(rr::Pointer<Byte> base, rr::Int limit, SIMD::Int offset)
874 : base(base)
875 , dynamicLimit(limit)
876 , staticLimit(0)
877 , dynamicOffsets(offset)
878 , staticOffsets{}
879 , hasDynamicLimit(true)
880 , hasDynamicOffsets(true)
881 {}
882
Pointer(rr::Pointer<Byte> base,unsigned int limit,SIMD::Int offset)883 Pointer::Pointer(rr::Pointer<Byte> base, unsigned int limit, SIMD::Int offset)
884 : base(base)
885 , dynamicLimit(0)
886 , staticLimit(limit)
887 , dynamicOffsets(offset)
888 , staticOffsets{}
889 , hasDynamicLimit(false)
890 , hasDynamicOffsets(true)
891 {}
892
operator +=(Int i)893 Pointer &Pointer::operator+=(Int i)
894 {
895 dynamicOffsets += i;
896 hasDynamicOffsets = true;
897 return *this;
898 }
899
operator *=(Int i)900 Pointer &Pointer::operator*=(Int i)
901 {
902 dynamicOffsets = offsets() * i;
903 staticOffsets = {};
904 hasDynamicOffsets = true;
905 return *this;
906 }
907
operator +(SIMD::Int i)908 Pointer Pointer::operator+(SIMD::Int i)
909 {
910 Pointer p = *this;
911 p += i;
912 return p;
913 }
operator *(SIMD::Int i)914 Pointer Pointer::operator*(SIMD::Int i)
915 {
916 Pointer p = *this;
917 p *= i;
918 return p;
919 }
920
operator +=(int i)921 Pointer &Pointer::operator+=(int i)
922 {
923 for(int el = 0; el < SIMD::Width; el++) { staticOffsets[el] += i; }
924 return *this;
925 }
926
operator *=(int i)927 Pointer &Pointer::operator*=(int i)
928 {
929 for(int el = 0; el < SIMD::Width; el++) { staticOffsets[el] *= i; }
930 if(hasDynamicOffsets)
931 {
932 dynamicOffsets *= SIMD::Int(i);
933 }
934 return *this;
935 }
936
operator +(int i)937 Pointer Pointer::operator+(int i)
938 {
939 Pointer p = *this;
940 p += i;
941 return p;
942 }
operator *(int i)943 Pointer Pointer::operator*(int i)
944 {
945 Pointer p = *this;
946 p *= i;
947 return p;
948 }
949
offsets() const950 SIMD::Int Pointer::offsets() const
951 {
952 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
953 return dynamicOffsets + SIMD::Int(staticOffsets[0], staticOffsets[1], staticOffsets[2], staticOffsets[3]);
954 }
955
isInBounds(unsigned int accessSize,OutOfBoundsBehavior robustness) const956 SIMD::Int Pointer::isInBounds(unsigned int accessSize, OutOfBoundsBehavior robustness) const
957 {
958 ASSERT(accessSize > 0);
959
960 if(isStaticallyInBounds(accessSize, robustness))
961 {
962 return SIMD::Int(0xffffffff);
963 }
964
965 if(!hasDynamicOffsets && !hasDynamicLimit)
966 {
967 // Common fast paths.
968 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
969 return SIMD::Int(
970 (staticOffsets[0] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
971 (staticOffsets[1] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
972 (staticOffsets[2] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
973 (staticOffsets[3] + accessSize - 1 < staticLimit) ? 0xffffffff : 0);
974 }
975
976 return CmpLT(offsets() + SIMD::Int(accessSize - 1), SIMD::Int(limit()));
977 }
978
isStaticallyInBounds(unsigned int accessSize,OutOfBoundsBehavior robustness) const979 bool Pointer::isStaticallyInBounds(unsigned int accessSize, OutOfBoundsBehavior robustness) const
980 {
981 if(hasDynamicOffsets)
982 {
983 return false;
984 }
985
986 if(hasDynamicLimit)
987 {
988 if(hasStaticEqualOffsets() || hasStaticSequentialOffsets(accessSize))
989 {
990 switch(robustness)
991 {
992 case OutOfBoundsBehavior::UndefinedBehavior:
993 // With this robustness setting the application/compiler guarantees in-bounds accesses on active lanes,
994 // but since it can't know in advance which branches are taken this must be true even for inactives lanes.
995 return true;
996 case OutOfBoundsBehavior::Nullify:
997 case OutOfBoundsBehavior::RobustBufferAccess:
998 case OutOfBoundsBehavior::UndefinedValue:
999 return false;
1000 }
1001 }
1002 }
1003
1004 for(int i = 0; i < SIMD::Width; i++)
1005 {
1006 if(staticOffsets[i] + accessSize - 1 >= staticLimit)
1007 {
1008 return false;
1009 }
1010 }
1011
1012 return true;
1013 }
1014
limit() const1015 rr::Int Pointer::limit() const
1016 {
1017 return dynamicLimit + staticLimit;
1018 }
1019
1020 // Returns true if all offsets are sequential
1021 // (N+0*step, N+1*step, N+2*step, N+3*step)
hasSequentialOffsets(unsigned int step) const1022 rr::Bool Pointer::hasSequentialOffsets(unsigned int step) const
1023 {
1024 if(hasDynamicOffsets)
1025 {
1026 auto o = offsets();
1027 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1028 return rr::SignMask(~CmpEQ(o.yzww, o + SIMD::Int(1 * step, 2 * step, 3 * step, 0))) == 0;
1029 }
1030 return hasStaticSequentialOffsets(step);
1031 }
1032
1033 // Returns true if all offsets are are compile-time static and
1034 // sequential (N+0*step, N+1*step, N+2*step, N+3*step)
hasStaticSequentialOffsets(unsigned int step) const1035 bool Pointer::hasStaticSequentialOffsets(unsigned int step) const
1036 {
1037 if(hasDynamicOffsets)
1038 {
1039 return false;
1040 }
1041 for(int i = 1; i < SIMD::Width; i++)
1042 {
1043 if(staticOffsets[i - 1] + int32_t(step) != staticOffsets[i]) { return false; }
1044 }
1045 return true;
1046 }
1047
1048 // Returns true if all offsets are equal (N, N, N, N)
hasEqualOffsets() const1049 rr::Bool Pointer::hasEqualOffsets() const
1050 {
1051 if(hasDynamicOffsets)
1052 {
1053 auto o = offsets();
1054 static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1055 return rr::SignMask(~CmpEQ(o, o.yzwx)) == 0;
1056 }
1057 return hasStaticEqualOffsets();
1058 }
1059
1060 // Returns true if all offsets are compile-time static and are equal
1061 // (N, N, N, N)
hasStaticEqualOffsets() const1062 bool Pointer::hasStaticEqualOffsets() const
1063 {
1064 if(hasDynamicOffsets)
1065 {
1066 return false;
1067 }
1068 for(int i = 1; i < SIMD::Width; i++)
1069 {
1070 if(staticOffsets[i - 1] != staticOffsets[i]) { return false; }
1071 }
1072 return true;
1073 }
1074
1075 } // namespace SIMD
1076
1077 } // namespace sw
1078