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