• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // TODO(chromium:1299047)
23 #ifndef SWIFTSHADER_LEGACY_PRECISION
24 #	define SWIFTSHADER_LEGACY_PRECISION false
25 #endif
26 
27 namespace sw {
28 
Vector4s()29 Vector4s::Vector4s()
30 {
31 }
32 
Vector4s(unsigned short x,unsigned short y,unsigned short z,unsigned short w)33 Vector4s::Vector4s(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
34 {
35 	this->x = Short4(x);
36 	this->y = Short4(y);
37 	this->z = Short4(z);
38 	this->w = Short4(w);
39 }
40 
Vector4s(const Vector4s & rhs)41 Vector4s::Vector4s(const Vector4s &rhs)
42 {
43 	x = rhs.x;
44 	y = rhs.y;
45 	z = rhs.z;
46 	w = rhs.w;
47 }
48 
operator =(const Vector4s & rhs)49 Vector4s &Vector4s::operator=(const Vector4s &rhs)
50 {
51 	x = rhs.x;
52 	y = rhs.y;
53 	z = rhs.z;
54 	w = rhs.w;
55 
56 	return *this;
57 }
58 
operator [](int i)59 Short4 &Vector4s::operator[](int i)
60 {
61 	switch(i)
62 	{
63 	case 0: return x;
64 	case 1: return y;
65 	case 2: return z;
66 	case 3: return w;
67 	}
68 
69 	return x;
70 }
71 
Vector4f()72 Vector4f::Vector4f()
73 {
74 }
75 
Vector4f(float x,float y,float z,float w)76 Vector4f::Vector4f(float x, float y, float z, float w)
77 {
78 	this->x = Float4(x);
79 	this->y = Float4(y);
80 	this->z = Float4(z);
81 	this->w = Float4(w);
82 }
83 
Vector4f(const Vector4f & rhs)84 Vector4f::Vector4f(const Vector4f &rhs)
85 {
86 	x = rhs.x;
87 	y = rhs.y;
88 	z = rhs.z;
89 	w = rhs.w;
90 }
91 
operator =(const Vector4f & rhs)92 Vector4f &Vector4f::operator=(const Vector4f &rhs)
93 {
94 	x = rhs.x;
95 	y = rhs.y;
96 	z = rhs.z;
97 	w = rhs.w;
98 
99 	return *this;
100 }
101 
operator [](int i)102 Float4 &Vector4f::operator[](int i)
103 {
104 	switch(i)
105 	{
106 	case 0: return x;
107 	case 1: return y;
108 	case 2: return z;
109 	case 3: return w;
110 	}
111 
112 	return x;
113 }
114 
Vector4i()115 Vector4i::Vector4i()
116 {
117 }
118 
Vector4i(int x,int y,int z,int w)119 Vector4i::Vector4i(int x, int y, int z, int w)
120 {
121 	this->x = Int4(x);
122 	this->y = Int4(y);
123 	this->z = Int4(z);
124 	this->w = Int4(w);
125 }
126 
Vector4i(const Vector4i & rhs)127 Vector4i::Vector4i(const Vector4i &rhs)
128 {
129 	x = rhs.x;
130 	y = rhs.y;
131 	z = rhs.z;
132 	w = rhs.w;
133 }
134 
operator =(const Vector4i & rhs)135 Vector4i &Vector4i::operator=(const Vector4i &rhs)
136 {
137 	x = rhs.x;
138 	y = rhs.y;
139 	z = rhs.z;
140 	w = rhs.w;
141 
142 	return *this;
143 }
144 
operator [](int i)145 Int4 &Vector4i::operator[](int i)
146 {
147 	switch(i)
148 	{
149 	case 0: return x;
150 	case 1: return y;
151 	case 2: return z;
152 	case 3: return w;
153 	}
154 
155 	return x;
156 }
157 
158 // Approximation of atan in [0..1]
Atan_01(Float4 x)159 static RValue<Float4> Atan_01(Float4 x)
160 {
161 	// From 4.4.49, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
162 	const Float4 a2(-0.3333314528f);
163 	const Float4 a4(0.1999355085f);
164 	const Float4 a6(-0.1420889944f);
165 	const Float4 a8(0.1065626393f);
166 	const Float4 a10(-0.0752896400f);
167 	const Float4 a12(0.0429096138f);
168 	const Float4 a14(-0.0161657367f);
169 	const Float4 a16(0.0028662257f);
170 	Float4 x2 = x * x;
171 	return (x + x * (x2 * (a2 + x2 * (a4 + x2 * (a6 + x2 * (a8 + x2 * (a10 + x2 * (a12 + x2 * (a14 + x2 * a16)))))))));
172 }
173 
174 // Polynomial approximation of order 5 for sin(x * 2 * pi) in the range [-1/4, 1/4]
Sin5(Float4 x)175 static RValue<Float4> Sin5(Float4 x)
176 {
177 	// A * x^5 + B * x^3 + C * x
178 	// Exact at x = 0, 1/12, 1/6, 1/4, and their negatives, which correspond to x * 2 * pi = 0, pi/6, pi/3, pi/2
179 	const Float4 A = (36288 - 20736 * sqrt(3)) / 5;
180 	const Float4 B = 288 * sqrt(3) - 540;
181 	const Float4 C = (47 - 9 * sqrt(3)) / 5;
182 
183 	Float4 x2 = x * x;
184 
185 	return MulAdd(MulAdd(A, x2, B), x2, C) * x;
186 }
187 
Sin(RValue<Float4> x,bool relaxedPrecision)188 RValue<Float4> Sin(RValue<Float4> x, bool relaxedPrecision)
189 {
190 	const Float4 q = 0.25f;
191 	const Float4 pi2 = 1 / (2 * 3.1415926535f);
192 
193 	// Range reduction and mirroring
194 	Float4 x_2 = MulAdd(x, -pi2, q);
195 	Float4 z = q - Abs(x_2 - Round(x_2));
196 
197 	return Sin5(z);
198 }
199 
Cos(RValue<Float4> x,bool relaxedPrecision)200 RValue<Float4> Cos(RValue<Float4> x, bool relaxedPrecision)
201 {
202 	const Float4 q = 0.25f;
203 	const Float4 pi2 = 1 / (2 * 3.1415926535f);
204 
205 	// Phase shift, range reduction, and mirroring
206 	Float4 x_2 = x * pi2;
207 	Float4 z = q - Abs(x_2 - Round(x_2));
208 
209 	return Sin5(z);
210 }
211 
Tan(RValue<Float4> x,bool relaxedPrecision)212 RValue<Float4> Tan(RValue<Float4> x, bool relaxedPrecision)
213 {
214 	return sw::Sin(x, relaxedPrecision) / sw::Cos(x, relaxedPrecision);
215 }
216 
Asin_4_terms(RValue<Float4> x)217 static RValue<Float4> Asin_4_terms(RValue<Float4> x)
218 {
219 	// From 4.4.45, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
220 	// |e(x)| <= 5e-8
221 	const Float4 half_pi(1.57079632f);
222 	const Float4 a0(1.5707288f);
223 	const Float4 a1(-0.2121144f);
224 	const Float4 a2(0.0742610f);
225 	const Float4 a3(-0.0187293f);
226 	Float4 absx = Abs(x);
227 	return As<Float4>(As<Int4>(half_pi - Sqrt<Highp>(1.0f - absx) * (a0 + absx * (a1 + absx * (a2 + absx * a3)))) ^
228 	                  (As<Int4>(x) & Int4(0x80000000)));
229 }
230 
Asin_8_terms(RValue<Float4> x)231 static RValue<Float4> Asin_8_terms(RValue<Float4> x)
232 {
233 	// From 4.4.46, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
234 	// |e(x)| <= 0e-8
235 	const Float4 half_pi(1.5707963268f);
236 	const Float4 a0(1.5707963050f);
237 	const Float4 a1(-0.2145988016f);
238 	const Float4 a2(0.0889789874f);
239 	const Float4 a3(-0.0501743046f);
240 	const Float4 a4(0.0308918810f);
241 	const Float4 a5(-0.0170881256f);
242 	const Float4 a6(0.006700901f);
243 	const Float4 a7(-0.0012624911f);
244 	Float4 absx = Abs(x);
245 	return As<Float4>(As<Int4>(half_pi - Sqrt<Highp>(1.0f - absx) * (a0 + absx * (a1 + absx * (a2 + absx * (a3 + absx * (a4 + absx * (a5 + absx * (a6 + absx * a7)))))))) ^
246 	                  (As<Int4>(x) & Int4(0x80000000)));
247 }
248 
Asin(RValue<Float4> x,bool relaxedPrecision)249 RValue<Float4> Asin(RValue<Float4> x, bool relaxedPrecision)
250 {
251 	// TODO(b/169755566): Surprisingly, deqp-vk's precision.acos.highp/mediump tests pass when using the 4-term polynomial
252 	// approximation version of acos, unlike for Asin, which requires higher precision algorithms.
253 
254 	if(!relaxedPrecision)
255 	{
256 		return rr::Asin(x);
257 	}
258 
259 	return Asin_8_terms(x);
260 }
261 
Acos(RValue<Float4> x,bool relaxedPrecision)262 RValue<Float4> Acos(RValue<Float4> x, bool relaxedPrecision)
263 {
264 	// pi/2 - arcsin(x)
265 	return 1.57079632e+0f - Asin_4_terms(x);
266 }
267 
Atan(RValue<Float4> x,bool relaxedPrecision)268 RValue<Float4> Atan(RValue<Float4> x, bool relaxedPrecision)
269 {
270 	Float4 absx = Abs(x);
271 	Int4 O = CmpNLT(absx, 1.0f);
272 	Float4 y = As<Float4>((O & As<Int4>(1.0f / absx)) | (~O & As<Int4>(absx)));  // FIXME: Vector select
273 
274 	const Float4 half_pi(1.57079632f);
275 	Float4 theta = Atan_01(y);
276 	return As<Float4>(((O & As<Int4>(half_pi - theta)) | (~O & As<Int4>(theta))) ^  // FIXME: Vector select
277 	                  (As<Int4>(x) & Int4(0x80000000)));
278 }
279 
Atan2(RValue<Float4> y,RValue<Float4> x,bool relaxedPrecision)280 RValue<Float4> Atan2(RValue<Float4> y, RValue<Float4> x, bool relaxedPrecision)
281 {
282 	const Float4 pi(3.14159265f);             // pi
283 	const Float4 minus_pi(-3.14159265f);      // -pi
284 	const Float4 half_pi(1.57079632f);        // pi/2
285 	const Float4 quarter_pi(7.85398163e-1f);  // pi/4
286 
287 	// Rotate to upper semicircle when in lower semicircle
288 	Int4 S = CmpLT(y, 0.0f);
289 	Float4 theta = As<Float4>(S & As<Int4>(minus_pi));
290 	Float4 x0 = As<Float4>((As<Int4>(y) & Int4(0x80000000)) ^ As<Int4>(x));
291 	Float4 y0 = Abs(y);
292 
293 	// Rotate to right quadrant when in left quadrant
294 	Int4 Q = CmpLT(x0, 0.0f);
295 	theta += As<Float4>(Q & As<Int4>(half_pi));
296 	Float4 x1 = As<Float4>((Q & As<Int4>(y0)) | (~Q & As<Int4>(x0)));   // FIXME: Vector select
297 	Float4 y1 = As<Float4>((Q & As<Int4>(-x0)) | (~Q & As<Int4>(y0)));  // FIXME: Vector select
298 
299 	// Mirror to first octant when in second octant
300 	Int4 O = CmpNLT(y1, x1);
301 	Float4 x2 = As<Float4>((O & As<Int4>(y1)) | (~O & As<Int4>(x1)));  // FIXME: Vector select
302 	Float4 y2 = As<Float4>((O & As<Int4>(x1)) | (~O & As<Int4>(y1)));  // FIXME: Vector select
303 
304 	// Approximation of atan in [0..1]
305 	Int4 zero_x = CmpEQ(x2, 0.0f);
306 	Int4 inf_y = IsInf(y2);  // Since x2 >= y2, this means x2 == y2 == inf, so we use 45 degrees or pi/4
307 	Float4 atan2_theta = Atan_01(y2 / x2);
308 	theta += As<Float4>((~zero_x & ~inf_y & ((O & As<Int4>(half_pi - atan2_theta)) | (~O & (As<Int4>(atan2_theta))))) |  // FIXME: Vector select
309 	                    (inf_y & As<Int4>(quarter_pi)));
310 
311 	// Recover loss of precision for tiny theta angles
312 	// This combination results in (-pi + half_pi + half_pi - atan2_theta) which is equivalent to -atan2_theta
313 	Int4 precision_loss = S & Q & O & ~inf_y;
314 
315 	return As<Float4>((precision_loss & As<Int4>(-atan2_theta)) | (~precision_loss & As<Int4>(theta)));  // FIXME: Vector select
316 }
317 
318 // TODO(chromium:1299047)
Exp2_legacy(RValue<Float4> x0)319 static RValue<Float4> Exp2_legacy(RValue<Float4> x0)
320 {
321 	Int4 i = RoundInt(x0 - 0.5f);
322 	Float4 ii = As<Float4>((i + Int4(127)) << 23);
323 
324 	Float4 f = x0 - Float4(i);
325 	Float4 ff = As<Float4>(Int4(0x3AF61905));
326 	ff = ff * f + As<Float4>(Int4(0x3C134806));
327 	ff = ff * f + As<Float4>(Int4(0x3D64AA23));
328 	ff = ff * f + As<Float4>(Int4(0x3E75EAD4));
329 	ff = ff * f + As<Float4>(Int4(0x3F31727B));
330 	ff = ff * f + 1.0f;
331 
332 	return ii * ff;
333 }
334 
Exp2(RValue<Float4> x,bool relaxedPrecision)335 RValue<Float4> Exp2(RValue<Float4> x, bool relaxedPrecision)
336 {
337 	// Clamp to prevent overflow past the representation of infinity.
338 	Float4 x0 = x;
339 	x0 = Min(x0, 128.0f);
340 	x0 = Max(x0, As<Float4>(Int4(0xC2FDFFFF)));  // -126.999992
341 
342 	if(SWIFTSHADER_LEGACY_PRECISION)  // TODO(chromium:1299047)
343 	{
344 		return Exp2_legacy(x0);
345 	}
346 
347 	Float4 xi = Floor(x0);
348 	Float4 f = x0 - xi;
349 
350 	if(!relaxedPrecision)  // highp
351 	{
352 		// Polynomial which approximates (2^x-x-1)/x. Multiplying with x
353 		// gives us a correction term to be added to 1+x to obtain 2^x.
354 		const Float4 a = 1.8852974e-3f;
355 		const Float4 b = 8.9733787e-3f;
356 		const Float4 c = 5.5835927e-2f;
357 		const Float4 d = 2.4015281e-1f;
358 		const Float4 e = -3.0684753e-1f;
359 
360 		Float4 r = MulAdd(MulAdd(MulAdd(MulAdd(a, f, b), f, c), f, d), f, e);
361 
362 		// bit_cast<float>(int(x * 2^23)) is a piecewise linear approximation of 2^x.
363 		// See "Fast Exponential Computation on SIMD Architectures" by Malossi et al.
364 		Float4 y = MulAdd(r, f, x0);
365 		Int4 i = Int4(y * (1 << 23)) + (127 << 23);
366 
367 		return As<Float4>(i);
368 	}
369 	else  // RelaxedPrecision / mediump
370 	{
371 		// Polynomial which approximates (2^x-x-1)/x. Multiplying with x
372 		// gives us a correction term to be added to 1+x to obtain 2^x.
373 		const Float4 a = 7.8145574e-2f;
374 		const Float4 b = 2.2617357e-1f;
375 		const Float4 c = -3.0444314e-1f;
376 
377 		Float4 r = MulAdd(MulAdd(a, f, b), f, c);
378 
379 		// bit_cast<float>(int(x * 2^23)) is a piecewise linear approximation of 2^x.
380 		// See "Fast Exponential Computation on SIMD Architectures" by Malossi et al.
381 		Float4 y = MulAdd(r, f, x0);
382 		Int4 i = Int4(MulAdd((1 << 23), y, (127 << 23)));
383 
384 		return As<Float4>(i);
385 	}
386 }
387 
Log2_legacy(RValue<Float4> x)388 RValue<Float4> Log2_legacy(RValue<Float4> x)
389 {
390 	Float4 x1 = As<Float4>(As<Int4>(x) & Int4(0x7F800000));
391 	x1 = As<Float4>(As<UInt4>(x1) >> 8);
392 	x1 = As<Float4>(As<Int4>(x1) | As<Int4>(Float4(1.0f)));
393 	x1 = (x1 - 1.4960938f) * 256.0f;
394 	Float4 x0 = As<Float4>((As<Int4>(x) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
395 
396 	Float4 x2 = MulAdd(MulAdd(9.5428179e-2f, x0, 4.7779095e-1f), x0, 1.9782813e-1f);
397 	Float4 x3 = MulAdd(MulAdd(MulAdd(1.6618466e-2f, x0, 2.0350508e-1f), x0, 2.7382900e-1f), x0, 4.0496687e-2f);
398 
399 	x1 += (x0 - 1.0f) * (x2 / x3);
400 
401 	Int4 pos_inf_x = CmpEQ(As<Int4>(x), Int4(0x7F800000));
402 	return As<Float4>((pos_inf_x & As<Int4>(x)) | (~pos_inf_x & As<Int4>(x1)));
403 }
404 
Log2(RValue<Float4> x,bool relaxedPrecision)405 RValue<Float4> Log2(RValue<Float4> x, bool relaxedPrecision)
406 {
407 	if(SWIFTSHADER_LEGACY_PRECISION)  // TODO(chromium:1299047)
408 	{
409 		return Log2_legacy(x);
410 	}
411 
412 	if(!relaxedPrecision)  // highp
413 	{
414 		// Reinterpretation as an integer provides a piecewise linear
415 		// approximation of log2(). Scale to the radix and subtract exponent bias.
416 		Int4 im = As<Int4>(x);
417 		Float4 y = Float4(im - (127 << 23)) * (1.0f / (1 << 23));
418 
419 		// Handle log2(inf) = inf.
420 		y = As<Float4>(As<Int4>(y) | (CmpEQ(im, 0x7F800000) & As<Int4>(Float4::infinity())));
421 
422 		Float4 m = Float4(im & 0x007FFFFF) * (1.0f / (1 << 23));  // Normalized mantissa of x.
423 
424 		// Add a polynomial approximation of log2(m+1)-m to the result's mantissa.
425 		const Float4 a = -9.3091638e-3f;
426 		const Float4 b = 5.2059003e-2f;
427 		const Float4 c = -1.3752135e-1f;
428 		const Float4 d = 2.4186478e-1f;
429 		const Float4 e = -3.4730109e-1f;
430 		const Float4 f = 4.786837e-1f;
431 		const Float4 g = -7.2116581e-1f;
432 		const Float4 h = 4.4268988e-1f;
433 
434 		Float4 z = MulAdd(MulAdd(MulAdd(MulAdd(MulAdd(MulAdd(MulAdd(a, m, b), m, c), m, d), m, e), m, f), m, g), m, h);
435 
436 		return MulAdd(z, m, y);
437 	}
438 	else  // RelaxedPrecision / mediump
439 	{
440 		// Reinterpretation as an integer provides a piecewise linear
441 		// approximation of log2(). Scale to the radix and subtract exponent bias.
442 		Int4 im = As<Int4>(x);
443 		Float4 y = MulAdd(Float4(im), (1.0f / (1 << 23)), -127.0f);
444 
445 		// Handle log2(inf) = inf.
446 		y = As<Float4>(As<Int4>(y) | (CmpEQ(im, 0x7F800000) & As<Int4>(Float4::infinity())));
447 
448 		Float4 m = Float4(im & 0x007FFFFF);  // Unnormalized mantissa of x.
449 
450 		// Add a polynomial approximation of log2(m+1)-m to the result's mantissa.
451 		const Float4 a = 2.8017103e-22f;
452 		const Float4 b = -8.373131e-15f;
453 		const Float4 c = 5.0615534e-8f;
454 
455 		Float4 f = MulAdd(MulAdd(a, m, b), m, c);
456 
457 		return MulAdd(f, m, y);
458 	}
459 }
460 
Exp(RValue<Float4> x,bool relaxedPrecision)461 RValue<Float4> Exp(RValue<Float4> x, bool relaxedPrecision)
462 {
463 	return sw::Exp2(1.44269504f * x, relaxedPrecision);  // 1/ln(2)
464 }
465 
Log(RValue<Float4> x,bool relaxedPrecision)466 RValue<Float4> Log(RValue<Float4> x, bool relaxedPrecision)
467 {
468 	return 6.93147181e-1f * sw::Log2(x, relaxedPrecision);  // ln(2)
469 }
470 
Pow(RValue<Float4> x,RValue<Float4> y,bool relaxedPrecision)471 RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y, bool relaxedPrecision)
472 {
473 	Float4 log = sw::Log2(x, relaxedPrecision);
474 	log *= y;
475 	return sw::Exp2(log, relaxedPrecision);
476 }
477 
Sinh(RValue<Float4> x,bool relaxedPrecision)478 RValue<Float4> Sinh(RValue<Float4> x, bool relaxedPrecision)
479 {
480 	return (sw::Exp(x, relaxedPrecision) - sw::Exp(-x, relaxedPrecision)) * 0.5f;
481 }
482 
Cosh(RValue<Float4> x,bool relaxedPrecision)483 RValue<Float4> Cosh(RValue<Float4> x, bool relaxedPrecision)
484 {
485 	return (sw::Exp(x, relaxedPrecision) + sw::Exp(-x, relaxedPrecision)) * 0.5f;
486 }
487 
Tanh(RValue<Float4> x,bool relaxedPrecision)488 RValue<Float4> Tanh(RValue<Float4> x, bool relaxedPrecision)
489 {
490 	Float4 e_x = sw::Exp(x, relaxedPrecision);
491 	Float4 e_minus_x = sw::Exp(-x, relaxedPrecision);
492 	return (e_x - e_minus_x) / (e_x + e_minus_x);
493 }
494 
Asinh(RValue<Float4> x,bool relaxedPrecision)495 RValue<Float4> Asinh(RValue<Float4> x, bool relaxedPrecision)
496 {
497 	return sw::Log(x + Sqrt(x * x + 1.0f, relaxedPrecision), relaxedPrecision);
498 }
499 
Acosh(RValue<Float4> x,bool relaxedPrecision)500 RValue<Float4> Acosh(RValue<Float4> x, bool relaxedPrecision)
501 {
502 	return sw::Log(x + Sqrt(x + 1.0f, relaxedPrecision) * Sqrt(x - 1.0f, relaxedPrecision), relaxedPrecision);
503 }
504 
Atanh(RValue<Float4> x,bool relaxedPrecision)505 RValue<Float4> Atanh(RValue<Float4> x, bool relaxedPrecision)
506 {
507 	return sw::Log((1.0f + x) / (1.0f - x), relaxedPrecision) * 0.5f;
508 }
509 
Sqrt(RValue<Float4> x,bool relaxedPrecision)510 RValue<Float4> Sqrt(RValue<Float4> x, bool relaxedPrecision)
511 {
512 	return rr::Sqrt(x);  // TODO(b/222218659): Optimize for relaxed precision.
513 }
514 
reciprocal(RValue<Float4> x,bool pp,bool exactAtPow2)515 RValue<Float4> reciprocal(RValue<Float4> x, bool pp, bool exactAtPow2)
516 {
517 	return Rcp(x, pp, exactAtPow2);
518 }
519 
reciprocalSquareRoot(RValue<Float4> x,bool absolute,bool pp)520 RValue<Float4> reciprocalSquareRoot(RValue<Float4> x, bool absolute, bool pp)
521 {
522 	Float4 abs = x;
523 
524 	if(absolute)
525 	{
526 		abs = Abs(abs);
527 	}
528 
529 	return Rcp(abs, pp);
530 }
531 
532 // TODO(chromium:1299047): Eliminate when Chromium tests accept both fused and unfused multiply-add.
mulAdd(RValue<Float4> x,RValue<Float4> y,RValue<Float4> z)533 RValue<Float4> mulAdd(RValue<Float4> x, RValue<Float4> y, RValue<Float4> z)
534 {
535 	if(SWIFTSHADER_LEGACY_PRECISION)
536 	{
537 		return x * y + z;
538 	}
539 
540 	return rr::MulAdd(x, y, z);
541 }
542 
transpose4x4(Short4 & row0,Short4 & row1,Short4 & row2,Short4 & row3)543 void transpose4x4(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
544 {
545 	Int2 tmp0 = UnpackHigh(row0, row1);
546 	Int2 tmp1 = UnpackHigh(row2, row3);
547 	Int2 tmp2 = UnpackLow(row0, row1);
548 	Int2 tmp3 = UnpackLow(row2, row3);
549 
550 	row0 = UnpackLow(tmp2, tmp3);
551 	row1 = UnpackHigh(tmp2, tmp3);
552 	row2 = UnpackLow(tmp0, tmp1);
553 	row3 = UnpackHigh(tmp0, tmp1);
554 }
555 
transpose4x3(Short4 & row0,Short4 & row1,Short4 & row2,Short4 & row3)556 void transpose4x3(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
557 {
558 	Int2 tmp0 = UnpackHigh(row0, row1);
559 	Int2 tmp1 = UnpackHigh(row2, row3);
560 	Int2 tmp2 = UnpackLow(row0, row1);
561 	Int2 tmp3 = UnpackLow(row2, row3);
562 
563 	row0 = UnpackLow(tmp2, tmp3);
564 	row1 = UnpackHigh(tmp2, tmp3);
565 	row2 = UnpackLow(tmp0, tmp1);
566 }
567 
transpose4x4(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)568 void transpose4x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
569 {
570 	Float4 tmp0 = UnpackLow(row0, row1);
571 	Float4 tmp1 = UnpackLow(row2, row3);
572 	Float4 tmp2 = UnpackHigh(row0, row1);
573 	Float4 tmp3 = UnpackHigh(row2, row3);
574 
575 	row0 = Float4(tmp0.xy, tmp1.xy);
576 	row1 = Float4(tmp0.zw, tmp1.zw);
577 	row2 = Float4(tmp2.xy, tmp3.xy);
578 	row3 = Float4(tmp2.zw, tmp3.zw);
579 }
580 
transpose4x3(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)581 void transpose4x3(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
582 {
583 	Float4 tmp0 = UnpackLow(row0, row1);
584 	Float4 tmp1 = UnpackLow(row2, row3);
585 	Float4 tmp2 = UnpackHigh(row0, row1);
586 	Float4 tmp3 = UnpackHigh(row2, row3);
587 
588 	row0 = Float4(tmp0.xy, tmp1.xy);
589 	row1 = Float4(tmp0.zw, tmp1.zw);
590 	row2 = Float4(tmp2.xy, tmp3.xy);
591 }
592 
transpose4x2(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)593 void transpose4x2(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
594 {
595 	Float4 tmp0 = UnpackLow(row0, row1);
596 	Float4 tmp1 = UnpackLow(row2, row3);
597 
598 	row0 = Float4(tmp0.xy, tmp1.xy);
599 	row1 = Float4(tmp0.zw, tmp1.zw);
600 }
601 
transpose4x1(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)602 void transpose4x1(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
603 {
604 	Float4 tmp0 = UnpackLow(row0, row1);
605 	Float4 tmp1 = UnpackLow(row2, row3);
606 
607 	row0 = Float4(tmp0.xy, tmp1.xy);
608 }
609 
transpose2x4(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3)610 void transpose2x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
611 {
612 	Float4 tmp01 = UnpackLow(row0, row1);
613 	Float4 tmp23 = UnpackHigh(row0, row1);
614 
615 	row0 = tmp01;
616 	row1 = Float4(tmp01.zw, row1.zw);
617 	row2 = tmp23;
618 	row3 = Float4(tmp23.zw, row3.zw);
619 }
620 
transpose4xN(Float4 & row0,Float4 & row1,Float4 & row2,Float4 & row3,int N)621 void transpose4xN(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3, int N)
622 {
623 	switch(N)
624 	{
625 	case 1: transpose4x1(row0, row1, row2, row3); break;
626 	case 2: transpose4x2(row0, row1, row2, row3); break;
627 	case 3: transpose4x3(row0, row1, row2, row3); break;
628 	case 4: transpose4x4(row0, row1, row2, row3); break;
629 	}
630 }
631 
halfToFloatBits(SIMD::UInt halfBits)632 SIMD::UInt halfToFloatBits(SIMD::UInt halfBits)
633 {
634 	auto magic = SIMD::UInt(126 << 23);
635 
636 	auto sign16 = halfBits & SIMD::UInt(0x8000);
637 	auto man16 = halfBits & SIMD::UInt(0x03FF);
638 	auto exp16 = halfBits & SIMD::UInt(0x7C00);
639 
640 	auto isDnormOrZero = CmpEQ(exp16, SIMD::UInt(0));
641 	auto isInfOrNaN = CmpEQ(exp16, SIMD::UInt(0x7C00));
642 
643 	auto sign32 = sign16 << 16;
644 	auto man32 = man16 << 13;
645 	auto exp32 = (exp16 + SIMD::UInt(0x1C000)) << 13;
646 	auto norm32 = (man32 | exp32) | (isInfOrNaN & SIMD::UInt(0x7F800000));
647 
648 	auto denorm32 = As<SIMD::UInt>(As<SIMD::Float>(magic + man16) - As<SIMD::Float>(magic));
649 
650 	return sign32 | (norm32 & ~isDnormOrZero) | (denorm32 & isDnormOrZero);
651 }
652 
floatToHalfBits(SIMD::UInt floatBits,bool storeInUpperBits)653 SIMD::UInt floatToHalfBits(SIMD::UInt floatBits, bool storeInUpperBits)
654 {
655 	SIMD::UInt sign = floatBits & SIMD::UInt(0x80000000);
656 	SIMD::UInt abs = floatBits & SIMD::UInt(0x7FFFFFFF);
657 
658 	SIMD::UInt normal = CmpNLE(abs, SIMD::UInt(0x38800000));
659 
660 	SIMD::UInt mantissa = (abs & SIMD::UInt(0x007FFFFF)) | SIMD::UInt(0x00800000);
661 	SIMD::UInt e = SIMD::UInt(113) - (abs >> 23);
662 	SIMD::UInt denormal = CmpLT(e, SIMD::UInt(24)) & (mantissa >> e);
663 
664 	SIMD::UInt base = (normal & abs) | (~normal & denormal);  // TODO: IfThenElse()
665 
666 	// float exponent bias is 127, half bias is 15, so adjust by -112
667 	SIMD::UInt bias = normal & SIMD::UInt(0xC8000000);
668 
669 	SIMD::UInt rounded = base + bias + SIMD::UInt(0x00000FFF) + ((base >> 13) & SIMD::UInt(1));
670 	SIMD::UInt fp16u = rounded >> 13;
671 
672 	// Infinity
673 	fp16u |= CmpNLE(abs, SIMD::UInt(0x47FFEFFF)) & SIMD::UInt(0x7FFF);
674 
675 	return storeInUpperBits ? (sign | (fp16u << 16)) : ((sign >> 16) | fp16u);
676 }
677 
r11g11b10Unpack(UInt r11g11b10bits)678 Float4 r11g11b10Unpack(UInt r11g11b10bits)
679 {
680 	// 10 (or 11) bit float formats are unsigned formats with a 5 bit exponent and a 5 (or 6) bit mantissa.
681 	// Since the Half float format also has a 5 bit exponent, we can convert these formats to half by
682 	// copy/pasting the bits so the the exponent bits and top mantissa bits are aligned to the half format.
683 	// In this case, we have:
684 	// 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
685 	UInt4 halfBits;
686 	halfBits = Insert(halfBits, (r11g11b10bits & UInt(0x000007FFu)) << 4, 0);
687 	halfBits = Insert(halfBits, (r11g11b10bits & UInt(0x003FF800u)) >> 7, 1);
688 	halfBits = Insert(halfBits, (r11g11b10bits & UInt(0xFFC00000u)) >> 17, 2);
689 	halfBits = Insert(halfBits, UInt(0x00003C00u), 3);
690 	return As<Float4>(halfToFloatBits(halfBits));
691 }
692 
r11g11b10Pack(const Float4 & value)693 UInt r11g11b10Pack(const Float4 &value)
694 {
695 	// 10 and 11 bit floats are unsigned, so their minimal value is 0
696 	auto halfBits = floatToHalfBits(As<UInt4>(Max(value, Float4(0.0f))), true);
697 	// Truncates instead of rounding. See b/147900455
698 	UInt4 truncBits = halfBits & UInt4(0x7FF00000, 0x7FF00000, 0x7FE00000, 0);
699 	return (UInt(truncBits.x) >> 20) | (UInt(truncBits.y) >> 9) | (UInt(truncBits.z) << 1);
700 }
701 
linearToSRGB(const Float4 & c)702 Float4 linearToSRGB(const Float4 &c)
703 {
704 	Float4 lc = Min(c, 0.0031308f) * 12.92f;
705 	Float4 ec = MulAdd(1.055f, Pow<Mediump>(c, (1.0f / 2.4f)), -0.055f);  // TODO(b/149574741): Use a custom approximation.
706 
707 	return Max(lc, ec);
708 }
709 
sRGBtoLinear(const Float4 & c)710 Float4 sRGBtoLinear(const Float4 &c)
711 {
712 	Float4 lc = c * (1.0f / 12.92f);
713 	Float4 ec = Pow<Mediump>(MulAdd(c, 1.0f / 1.055f, 0.055f / 1.055f), 2.4f);  // TODO(b/149574741): Use a custom approximation.
714 
715 	Int4 linear = CmpLT(c, 0.04045f);
716 	return As<Float4>((linear & As<Int4>(lc)) | (~linear & As<Int4>(ec)));  // TODO: IfThenElse()
717 }
718 
AnyTrue(const RValue<SIMD::Int> & bools)719 RValue<Bool> AnyTrue(const RValue<SIMD::Int> &bools)
720 {
721 	return SignMask(bools) != 0;
722 }
723 
AnyFalse(const RValue<SIMD::Int> & bools)724 RValue<Bool> AnyFalse(const RValue<SIMD::Int> &bools)
725 {
726 	return SignMask(~bools) != 0;  // TODO(b/214588983): Compare against mask of SIMD::Width 1's to avoid bitwise NOT.
727 }
728 
AllTrue(const RValue<SIMD::Int> & bools)729 RValue<Bool> AllTrue(const RValue<SIMD::Int> &bools)
730 {
731 	return SignMask(~bools) == 0;  // TODO(b/214588983): Compare against mask of SIMD::Width 1's to avoid bitwise NOT.
732 }
733 
AllFalse(const RValue<SIMD::Int> & bools)734 RValue<Bool> AllFalse(const RValue<SIMD::Int> &bools)
735 {
736 	return SignMask(bools) == 0;
737 }
738 
Divergent(const RValue<SIMD::Int> & ints)739 RValue<Bool> Divergent(const RValue<SIMD::Int> &ints)
740 {
741 	auto broadcastFirst = SIMD::Int(Extract(ints, 0));
742 	return AnyTrue(CmpNEQ(broadcastFirst, ints));
743 }
744 
Divergent(const RValue<SIMD::Float> & floats)745 RValue<Bool> Divergent(const RValue<SIMD::Float> &floats)
746 {
747 	auto broadcastFirst = SIMD::Float(Extract(floats, 0));
748 	return AnyTrue(CmpNEQ(broadcastFirst, floats));
749 }
750 
Uniform(const RValue<SIMD::Int> & ints)751 RValue<Bool> Uniform(const RValue<SIMD::Int> &ints)
752 {
753 	auto broadcastFirst = SIMD::Int(Extract(ints, 0));
754 	return AllFalse(CmpNEQ(broadcastFirst, ints));
755 }
756 
Uniform(const RValue<SIMD::Float> & floats)757 RValue<Bool> Uniform(const RValue<SIMD::Float> &floats)
758 {
759 	auto broadcastFirst = SIMD::Float(rr::Extract(floats, 0));
760 	return AllFalse(CmpNEQ(broadcastFirst, floats));
761 }
762 
Sign(rr::RValue<sw::SIMD::Float> const & val)763 rr::RValue<sw::SIMD::Float> Sign(rr::RValue<sw::SIMD::Float> const &val)
764 {
765 	return rr::As<sw::SIMD::Float>((rr::As<sw::SIMD::UInt>(val) & sw::SIMD::UInt(0x80000000)) | sw::SIMD::UInt(0x3f800000));
766 }
767 
768 // Returns the <whole, frac> of val.
769 // Both whole and frac will have the same sign as val.
770 std::pair<rr::RValue<sw::SIMD::Float>, rr::RValue<sw::SIMD::Float>>
Modf(rr::RValue<sw::SIMD::Float> const & val)771 Modf(rr::RValue<sw::SIMD::Float> const &val)
772 {
773 	auto abs = Abs(val);
774 	auto sign = Sign(val);
775 	auto whole = Floor(abs) * sign;
776 	auto frac = Frac(abs) * sign;
777 	return std::make_pair(whole, frac);
778 }
779 
780 // Returns the number of 1s in bits, per lane.
CountBits(rr::RValue<sw::SIMD::UInt> const & bits)781 sw::SIMD::UInt CountBits(rr::RValue<sw::SIMD::UInt> const &bits)
782 {
783 	// TODO: Add an intrinsic to reactor. Even if there isn't a
784 	// single vector instruction, there may be target-dependent
785 	// ways to make this faster.
786 	// https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
787 	sw::SIMD::UInt c = bits - ((bits >> 1) & sw::SIMD::UInt(0x55555555));
788 	c = ((c >> 2) & sw::SIMD::UInt(0x33333333)) + (c & sw::SIMD::UInt(0x33333333));
789 	c = ((c >> 4) + c) & sw::SIMD::UInt(0x0F0F0F0F);
790 	c = ((c >> 8) + c) & sw::SIMD::UInt(0x00FF00FF);
791 	c = ((c >> 16) + c) & sw::SIMD::UInt(0x0000FFFF);
792 	return c;
793 }
794 
795 // Returns 1 << bits.
796 // If the resulting bit overflows a 32 bit integer, 0 is returned.
NthBit32(rr::RValue<sw::SIMD::UInt> const & bits)797 rr::RValue<sw::SIMD::UInt> NthBit32(rr::RValue<sw::SIMD::UInt> const &bits)
798 {
799 	return ((sw::SIMD::UInt(1) << bits) & rr::CmpLT(bits, sw::SIMD::UInt(32)));
800 }
801 
802 // Returns bitCount number of of 1's starting from the LSB.
Bitmask32(rr::RValue<sw::SIMD::UInt> const & bitCount)803 rr::RValue<sw::SIMD::UInt> Bitmask32(rr::RValue<sw::SIMD::UInt> const &bitCount)
804 {
805 	return NthBit32(bitCount) - sw::SIMD::UInt(1);
806 }
807 
808 // Returns the exponent of the floating point number f.
809 // Assumes IEEE 754
Exponent(rr::RValue<sw::SIMD::Float> f)810 rr::RValue<sw::SIMD::Int> Exponent(rr::RValue<sw::SIMD::Float> f)
811 {
812 	auto v = rr::As<sw::SIMD::UInt>(f);
813 	return (sw::SIMD::Int((v >> sw::SIMD::UInt(23)) & sw::SIMD::UInt(0xFF)) - sw::SIMD::Int(126));
814 }
815 
816 // Returns y if y < x; otherwise result is x.
817 // If one operand is a NaN, the other operand is the result.
818 // 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)819 rr::RValue<sw::SIMD::Float> NMin(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y)
820 {
821 	auto xIsNan = IsNan(x);
822 	auto yIsNan = IsNan(y);
823 	return As<sw::SIMD::Float>(
824 	    // If neither are NaN, return min
825 	    ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Min(x, y))) |
826 	    // If one operand is a NaN, the other operand is the result
827 	    // If both operands are NaN, the result is a NaN.
828 	    ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) |
829 	    (xIsNan & As<sw::SIMD::Int>(y)));
830 }
831 
832 // Returns y if y > x; otherwise result is x.
833 // If one operand is a NaN, the other operand is the result.
834 // 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)835 rr::RValue<sw::SIMD::Float> NMax(rr::RValue<sw::SIMD::Float> const &x, rr::RValue<sw::SIMD::Float> const &y)
836 {
837 	auto xIsNan = IsNan(x);
838 	auto yIsNan = IsNan(y);
839 	return As<sw::SIMD::Float>(
840 	    // If neither are NaN, return max
841 	    ((~xIsNan & ~yIsNan) & As<sw::SIMD::Int>(Max(x, y))) |
842 	    // If one operand is a NaN, the other operand is the result
843 	    // If both operands are NaN, the result is a NaN.
844 	    ((~xIsNan & yIsNan) & As<sw::SIMD::Int>(x)) |
845 	    (xIsNan & As<sw::SIMD::Int>(y)));
846 }
847 
848 // 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)849 rr::RValue<sw::SIMD::Float> Determinant(
850     rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b,
851     rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d)
852 {
853 	return a * d - b * c;
854 }
855 
856 // 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)857 rr::RValue<sw::SIMD::Float> Determinant(
858     rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c,
859     rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f,
860     rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i)
861 {
862 	return a * e * i + b * f * g + c * d * h - c * e * g - b * d * i - a * f * h;
863 }
864 
865 // 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)866 rr::RValue<sw::SIMD::Float> Determinant(
867     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,
868     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,
869     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,
870     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)
871 {
872 	return a * Determinant(f, g, h,
873 	                       j, k, l,
874 	                       n, o, p) -
875 	       b * Determinant(e, g, h,
876 	                       i, k, l,
877 	                       m, o, p) +
878 	       c * Determinant(e, f, h,
879 	                       i, j, l,
880 	                       m, n, p) -
881 	       d * Determinant(e, f, g,
882 	                       i, j, k,
883 	                       m, n, o);
884 }
885 
886 // 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)887 std::array<rr::RValue<sw::SIMD::Float>, 4> MatrixInverse(
888     rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b,
889     rr::RValue<sw::SIMD::Float> const &c, rr::RValue<sw::SIMD::Float> const &d)
890 {
891 	auto s = sw::SIMD::Float(1.0f) / Determinant(a, b, c, d);
892 	return { { s * d, -s * b, -s * c, s * a } };
893 }
894 
895 // 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)896 std::array<rr::RValue<sw::SIMD::Float>, 9> MatrixInverse(
897     rr::RValue<sw::SIMD::Float> const &a, rr::RValue<sw::SIMD::Float> const &b, rr::RValue<sw::SIMD::Float> const &c,
898     rr::RValue<sw::SIMD::Float> const &d, rr::RValue<sw::SIMD::Float> const &e, rr::RValue<sw::SIMD::Float> const &f,
899     rr::RValue<sw::SIMD::Float> const &g, rr::RValue<sw::SIMD::Float> const &h, rr::RValue<sw::SIMD::Float> const &i)
900 {
901 	auto s = sw::SIMD::Float(1.0f) / Determinant(
902 	                                     a, b, c,
903 	                                     d, e, f,
904 	                                     g, h, i);  // TODO: duplicate arithmetic calculating the det and below.
905 
906 	return { {
907 		s * (e * i - f * h),
908 		s * (c * h - b * i),
909 		s * (b * f - c * e),
910 		s * (f * g - d * i),
911 		s * (a * i - c * g),
912 		s * (c * d - a * f),
913 		s * (d * h - e * g),
914 		s * (b * g - a * h),
915 		s * (a * e - b * d),
916 	} };
917 }
918 
919 // 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)920 std::array<rr::RValue<sw::SIMD::Float>, 16> MatrixInverse(
921     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,
922     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,
923     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,
924     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)
925 {
926 	auto s = sw::SIMD::Float(1.0f) / Determinant(
927 	                                     a, b, c, d,
928 	                                     e, f, g, h,
929 	                                     i, j, k, l,
930 	                                     m, n, o, p);  // TODO: duplicate arithmetic calculating the det and below.
931 
932 	auto kplo = k * p - l * o, jpln = j * p - l * n, jokn = j * o - k * n;
933 	auto gpho = g * p - h * o, fphn = f * p - h * n, fogn = f * o - g * n;
934 	auto glhk = g * l - h * k, flhj = f * l - h * j, fkgj = f * k - g * j;
935 	auto iplm = i * p - l * m, iokm = i * o - k * m, ephm = e * p - h * m;
936 	auto eogm = e * o - g * m, elhi = e * l - h * i, ekgi = e * k - g * i;
937 	auto injm = i * n - j * m, enfm = e * n - f * m, ejfi = e * j - f * i;
938 
939 	return { {
940 		s * (f * kplo - g * jpln + h * jokn),
941 		s * (-b * kplo + c * jpln - d * jokn),
942 		s * (b * gpho - c * fphn + d * fogn),
943 		s * (-b * glhk + c * flhj - d * fkgj),
944 
945 		s * (-e * kplo + g * iplm - h * iokm),
946 		s * (a * kplo - c * iplm + d * iokm),
947 		s * (-a * gpho + c * ephm - d * eogm),
948 		s * (a * glhk - c * elhi + d * ekgi),
949 
950 		s * (e * jpln - f * iplm + h * injm),
951 		s * (-a * jpln + b * iplm - d * injm),
952 		s * (a * fphn - b * ephm + d * enfm),
953 		s * (-a * flhj + b * elhi - d * ejfi),
954 
955 		s * (-e * jokn + f * iokm - g * injm),
956 		s * (a * jokn - b * iokm + c * injm),
957 		s * (-a * fogn + b * eogm - c * enfm),
958 		s * (a * fkgj - b * ekgi + c * ejfi),
959 	} };
960 }
961 
962 namespace SIMD {
963 
Pointer(rr::Pointer<Byte> base,rr::Int limit)964 Pointer::Pointer(rr::Pointer<Byte> base, rr::Int limit)
965     : base(base)
966     , dynamicLimit(limit)
967     , staticLimit(0)
968     , dynamicOffsets(0)
969     , staticOffsets{}
970     , hasDynamicLimit(true)
971     , hasDynamicOffsets(false)
972 {}
973 
Pointer(rr::Pointer<Byte> base,unsigned int limit)974 Pointer::Pointer(rr::Pointer<Byte> base, unsigned int limit)
975     : base(base)
976     , dynamicLimit(0)
977     , staticLimit(limit)
978     , dynamicOffsets(0)
979     , staticOffsets{}
980     , hasDynamicLimit(false)
981     , hasDynamicOffsets(false)
982 {}
983 
Pointer(rr::Pointer<Byte> base,rr::Int limit,SIMD::Int offset)984 Pointer::Pointer(rr::Pointer<Byte> base, rr::Int limit, SIMD::Int offset)
985     : base(base)
986     , dynamicLimit(limit)
987     , staticLimit(0)
988     , dynamicOffsets(offset)
989     , staticOffsets{}
990     , hasDynamicLimit(true)
991     , hasDynamicOffsets(true)
992 {}
993 
Pointer(rr::Pointer<Byte> base,unsigned int limit,SIMD::Int offset)994 Pointer::Pointer(rr::Pointer<Byte> base, unsigned int limit, SIMD::Int offset)
995     : base(base)
996     , dynamicLimit(0)
997     , staticLimit(limit)
998     , dynamicOffsets(offset)
999     , staticOffsets{}
1000     , hasDynamicLimit(false)
1001     , hasDynamicOffsets(true)
1002 {}
1003 
operator +=(Int i)1004 Pointer &Pointer::operator+=(Int i)
1005 {
1006 	dynamicOffsets += i;
1007 	hasDynamicOffsets = true;
1008 	return *this;
1009 }
1010 
operator *=(Int i)1011 Pointer &Pointer::operator*=(Int i)
1012 {
1013 	dynamicOffsets = offsets() * i;
1014 	staticOffsets = {};
1015 	hasDynamicOffsets = true;
1016 	return *this;
1017 }
1018 
operator +(SIMD::Int i)1019 Pointer Pointer::operator+(SIMD::Int i)
1020 {
1021 	Pointer p = *this;
1022 	p += i;
1023 	return p;
1024 }
operator *(SIMD::Int i)1025 Pointer Pointer::operator*(SIMD::Int i)
1026 {
1027 	Pointer p = *this;
1028 	p *= i;
1029 	return p;
1030 }
1031 
operator +=(int i)1032 Pointer &Pointer::operator+=(int i)
1033 {
1034 	for(int el = 0; el < SIMD::Width; el++) { staticOffsets[el] += i; }
1035 	return *this;
1036 }
1037 
operator *=(int i)1038 Pointer &Pointer::operator*=(int i)
1039 {
1040 	for(int el = 0; el < SIMD::Width; el++) { staticOffsets[el] *= i; }
1041 	if(hasDynamicOffsets)
1042 	{
1043 		dynamicOffsets *= SIMD::Int(i);
1044 	}
1045 	return *this;
1046 }
1047 
operator +(int i)1048 Pointer Pointer::operator+(int i)
1049 {
1050 	Pointer p = *this;
1051 	p += i;
1052 	return p;
1053 }
operator *(int i)1054 Pointer Pointer::operator*(int i)
1055 {
1056 	Pointer p = *this;
1057 	p *= i;
1058 	return p;
1059 }
1060 
offsets() const1061 SIMD::Int Pointer::offsets() const
1062 {
1063 	static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1064 	return dynamicOffsets + SIMD::Int(staticOffsets[0], staticOffsets[1], staticOffsets[2], staticOffsets[3]);
1065 }
1066 
isInBounds(unsigned int accessSize,OutOfBoundsBehavior robustness) const1067 SIMD::Int Pointer::isInBounds(unsigned int accessSize, OutOfBoundsBehavior robustness) const
1068 {
1069 	ASSERT(accessSize > 0);
1070 
1071 	if(isStaticallyInBounds(accessSize, robustness))
1072 	{
1073 		return SIMD::Int(0xffffffff);
1074 	}
1075 
1076 	if(!hasDynamicOffsets && !hasDynamicLimit)
1077 	{
1078 		// Common fast paths.
1079 		static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1080 		return SIMD::Int(
1081 		    (staticOffsets[0] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
1082 		    (staticOffsets[1] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
1083 		    (staticOffsets[2] + accessSize - 1 < staticLimit) ? 0xffffffff : 0,
1084 		    (staticOffsets[3] + accessSize - 1 < staticLimit) ? 0xffffffff : 0);
1085 	}
1086 
1087 	return CmpGE(offsets(), SIMD::Int(0)) & CmpLT(offsets() + SIMD::Int(accessSize - 1), SIMD::Int(limit()));
1088 }
1089 
isStaticallyInBounds(unsigned int accessSize,OutOfBoundsBehavior robustness) const1090 bool Pointer::isStaticallyInBounds(unsigned int accessSize, OutOfBoundsBehavior robustness) const
1091 {
1092 	if(hasDynamicOffsets)
1093 	{
1094 		return false;
1095 	}
1096 
1097 	if(hasDynamicLimit)
1098 	{
1099 		if(hasStaticEqualOffsets() || hasStaticSequentialOffsets(accessSize))
1100 		{
1101 			switch(robustness)
1102 			{
1103 			case OutOfBoundsBehavior::UndefinedBehavior:
1104 				// With this robustness setting the application/compiler guarantees in-bounds accesses on active lanes,
1105 				// but since it can't know in advance which branches are taken this must be true even for inactives lanes.
1106 				return true;
1107 			case OutOfBoundsBehavior::Nullify:
1108 			case OutOfBoundsBehavior::RobustBufferAccess:
1109 			case OutOfBoundsBehavior::UndefinedValue:
1110 				return false;
1111 			}
1112 		}
1113 	}
1114 
1115 	for(int i = 0; i < SIMD::Width; i++)
1116 	{
1117 		if(staticOffsets[i] + accessSize - 1 >= staticLimit)
1118 		{
1119 			return false;
1120 		}
1121 	}
1122 
1123 	return true;
1124 }
1125 
limit() const1126 rr::Int Pointer::limit() const
1127 {
1128 	return dynamicLimit + staticLimit;
1129 }
1130 
1131 // Returns true if all offsets are sequential
1132 // (N+0*step, N+1*step, N+2*step, N+3*step)
hasSequentialOffsets(unsigned int step) const1133 rr::Bool Pointer::hasSequentialOffsets(unsigned int step) const
1134 {
1135 	if(hasDynamicOffsets)
1136 	{
1137 		auto o = offsets();
1138 		static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1139 		return rr::SignMask(~CmpEQ(o.yzww, o + SIMD::Int(1 * step, 2 * step, 3 * step, 0))) == 0;
1140 	}
1141 	return hasStaticSequentialOffsets(step);
1142 }
1143 
1144 // Returns true if all offsets are are compile-time static and
1145 // sequential (N+0*step, N+1*step, N+2*step, N+3*step)
hasStaticSequentialOffsets(unsigned int step) const1146 bool Pointer::hasStaticSequentialOffsets(unsigned int step) const
1147 {
1148 	if(hasDynamicOffsets)
1149 	{
1150 		return false;
1151 	}
1152 	for(int i = 1; i < SIMD::Width; i++)
1153 	{
1154 		if(staticOffsets[i - 1] + int32_t(step) != staticOffsets[i]) { return false; }
1155 	}
1156 	return true;
1157 }
1158 
1159 // Returns true if all offsets are equal (N, N, N, N)
hasEqualOffsets() const1160 rr::Bool Pointer::hasEqualOffsets() const
1161 {
1162 	if(hasDynamicOffsets)
1163 	{
1164 		auto o = offsets();
1165 		static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4");
1166 		return rr::SignMask(~CmpEQ(o, o.yzwx)) == 0;
1167 	}
1168 	return hasStaticEqualOffsets();
1169 }
1170 
1171 // Returns true if all offsets are compile-time static and are equal
1172 // (N, N, N, N)
hasStaticEqualOffsets() const1173 bool Pointer::hasStaticEqualOffsets() const
1174 {
1175 	if(hasDynamicOffsets)
1176 	{
1177 		return false;
1178 	}
1179 	for(int i = 1; i < SIMD::Width; i++)
1180 	{
1181 		if(staticOffsets[i - 1] != staticOffsets[i]) { return false; }
1182 	}
1183 	return true;
1184 }
1185 
1186 }  // namespace SIMD
1187 
1188 }  // namespace sw
1189