blob: 338605c82336944d99bbc2d63dfbcf3c40da3ae9 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// 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
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// 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.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "ShaderCore.hpp"
16
John Bauman19bac1e2014-05-06 15:23:49 -040017#include "Renderer/Renderer.hpp"
18#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040019
Alexis Hetud5c31da2015-08-28 14:39:13 -040020#include <limits.h>
21
John Bauman89401822014-05-06 15:04:28 -040022namespace sw
23{
John Bauman19bac1e2014-05-06 15:23:49 -040024 extern TranscendentalPrecision logPrecision;
25 extern TranscendentalPrecision expPrecision;
26 extern TranscendentalPrecision rcpPrecision;
27 extern TranscendentalPrecision rsqPrecision;
28
Alexis Hetu96517182015-04-15 10:30:23 -040029 Vector4s::Vector4s()
John Bauman19bac1e2014-05-06 15:23:49 -040030 {
31 }
32
Alexis Hetu96517182015-04-15 10:30:23 -040033 Vector4s::Vector4s(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
John Bauman19bac1e2014-05-06 15:23:49 -040034 {
35 this->x = Short4(x);
36 this->y = Short4(y);
37 this->z = Short4(z);
38 this->w = Short4(w);
39 }
40
Alexis Hetu96517182015-04-15 10:30:23 -040041 Vector4s::Vector4s(const Vector4s &rhs)
John Bauman19bac1e2014-05-06 15:23:49 -040042 {
43 x = rhs.x;
44 y = rhs.y;
45 z = rhs.z;
46 w = rhs.w;
47 }
48
Alexis Hetu96517182015-04-15 10:30:23 -040049 Vector4s &Vector4s::operator=(const Vector4s &rhs)
John Bauman19bac1e2014-05-06 15:23:49 -040050 {
51 x = rhs.x;
52 y = rhs.y;
53 z = rhs.z;
54 w = rhs.w;
55
56 return *this;
57 }
58
Alexis Hetu96517182015-04-15 10:30:23 -040059 Short4 &Vector4s::operator[](int i)
John Bauman19bac1e2014-05-06 15:23:49 -040060 {
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
72 Vector4f::Vector4f()
73 {
74 }
75
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
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
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
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
115 Float4 exponential2(RValue<Float4> x, bool pp)
116 {
Nicolas Capens41bcdc72018-01-11 21:19:34 -0500117 // This implementation is based on 2^(i + f) = 2^i * 2^f,
118 // where i is the integer part of x and f is the fraction.
Nicolas Capens0bac2852016-05-07 06:09:58 -0400119
Nicolas Capens41bcdc72018-01-11 21:19:34 -0500120 // For 2^i we can put the integer part directly in the exponent of
121 // the IEEE-754 floating-point number. Clamp to prevent overflow
122 // past the representation of infinity.
123 Float4 x0 = x;
John Bauman19bac1e2014-05-06 15:23:49 -0400124 x0 = Min(x0, As<Float4>(Int4(0x43010000))); // 129.00000e+0f
125 x0 = Max(x0, As<Float4>(Int4(0xC2FDFFFF))); // -126.99999e+0f
Nicolas Capens0bac2852016-05-07 06:09:58 -0400126
Nicolas Capens41bcdc72018-01-11 21:19:34 -0500127 Int4 i = RoundInt(x0 - Float4(0.5f));
128 Float4 ii = As<Float4>((i + Int4(127)) << 23); // Add single-precision bias, and shift into exponent.
129
130 // For the fractional part use a polynomial
131 // which approximates 2^f in the 0 to 1 range.
132 Float4 f = x0 - Float4(i);
133 Float4 ff = As<Float4>(Int4(0x3AF61905)); // 1.8775767e-3f
134 ff = ff * f + As<Float4>(Int4(0x3C134806)); // 8.9893397e-3f
135 ff = ff * f + As<Float4>(Int4(0x3D64AA23)); // 5.5826318e-2f
136 ff = ff * f + As<Float4>(Int4(0x3E75EAD4)); // 2.4015361e-1f
137 ff = ff * f + As<Float4>(Int4(0x3F31727B)); // 6.9315308e-1f
138 ff = ff * f + Float4(1.0f);
139
140 return ii * ff;
John Bauman19bac1e2014-05-06 15:23:49 -0400141 }
142
143 Float4 logarithm2(RValue<Float4> x, bool absolute, bool pp)
144 {
145 Float4 x0;
146 Float4 x1;
147 Float4 x2;
148 Float4 x3;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400149
John Bauman19bac1e2014-05-06 15:23:49 -0400150 x0 = x;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400151
John Bauman19bac1e2014-05-06 15:23:49 -0400152 x1 = As<Float4>(As<Int4>(x0) & Int4(0x7F800000));
153 x1 = As<Float4>(As<UInt4>(x1) >> 8);
154 x1 = As<Float4>(As<Int4>(x1) | As<Int4>(Float4(1.0f)));
155 x1 = (x1 - Float4(1.4960938f)) * Float4(256.0f); // FIXME: (x1 - 1.4960938f) * 256.0f;
156 x0 = As<Float4>((As<Int4>(x0) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
157
158 x2 = (Float4(9.5428179e-2f) * x0 + Float4(4.7779095e-1f)) * x0 + Float4(1.9782813e-1f);
159 x3 = ((Float4(1.6618466e-2f) * x0 + Float4(2.0350508e-1f)) * x0 + Float4(2.7382900e-1f)) * x0 + Float4(4.0496687e-2f);
160 x2 /= x3;
161
162 x1 += (x0 - Float4(1.0f)) * x2;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400163
Alexis Hetu0b7003b2017-11-13 16:21:11 -0500164 Int4 pos_inf_x = CmpEQ(As<Int4>(x), Int4(0x7F800000));
165 return As<Float4>((pos_inf_x & As<Int4>(x)) | (~pos_inf_x & As<Int4>(x1)));
John Bauman19bac1e2014-05-06 15:23:49 -0400166 }
167
168 Float4 exponential(RValue<Float4> x, bool pp)
169 {
170 // FIXME: Propagate the constant
Alexis Hetu0b7003b2017-11-13 16:21:11 -0500171 return exponential2(Float4(1.44269504f) * x, pp); // 1/ln(2)
John Bauman19bac1e2014-05-06 15:23:49 -0400172 }
173
174 Float4 logarithm(RValue<Float4> x, bool absolute, bool pp)
175 {
176 // FIXME: Propagate the constant
177 return Float4(6.93147181e-1f) * logarithm2(x, absolute, pp); // ln(2)
178 }
179
180 Float4 power(RValue<Float4> x, RValue<Float4> y, bool pp)
181 {
182 Float4 log = logarithm2(x, true, pp);
183 log *= y;
184 return exponential2(log, pp);
185 }
186
Nicolas Capens05b3d662016-02-25 23:58:33 -0500187 Float4 reciprocal(RValue<Float4> x, bool pp, bool finite, bool exactAtPow2)
John Bauman19bac1e2014-05-06 15:23:49 -0400188 {
189 Float4 rcp;
190
191 if(!pp && rcpPrecision >= WHQL)
192 {
193 rcp = Float4(1.0f) / x;
194 }
195 else
196 {
Nicolas Capens05b3d662016-02-25 23:58:33 -0500197 rcp = Rcp_pp(x, exactAtPow2);
John Bauman19bac1e2014-05-06 15:23:49 -0400198
199 if(!pp)
200 {
201 rcp = (rcp + rcp) - (x * rcp * rcp);
202 }
203 }
204
205 if(finite)
206 {
207 int big = 0x7F7FFFFF;
208 rcp = Min(rcp, Float4((float&)big));
209 }
210
211 return rcp;
212 }
213
214 Float4 reciprocalSquareRoot(RValue<Float4> x, bool absolute, bool pp)
215 {
216 Float4 abs = x;
217
218 if(absolute)
219 {
220 abs = Abs(abs);
221 }
222
223 Float4 rsq;
224
Alexis Hetua0ef97a2017-11-13 17:31:20 -0500225 if(!pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400226 {
227 rsq = Float4(1.0f) / Sqrt(abs);
228 }
229 else
230 {
231 rsq = RcpSqrt_pp(abs);
232
233 if(!pp)
234 {
235 rsq = rsq * (Float4(3.0f) - rsq * rsq * abs) * Float4(0.5f);
236 }
John Bauman19bac1e2014-05-06 15:23:49 -0400237
Alexis Hetua0ef97a2017-11-13 17:31:20 -0500238 rsq = As<Float4>(CmpNEQ(As<Int4>(abs), Int4(0x7F800000)) & As<Int4>(rsq));
239 }
John Bauman19bac1e2014-05-06 15:23:49 -0400240
241 return rsq;
242 }
243
244 Float4 modulo(RValue<Float4> x, RValue<Float4> y)
245 {
246 return x - y * Floor(x / y);
247 }
248
249 Float4 sine_pi(RValue<Float4> x, bool pp)
250 {
251 const Float4 A = Float4(-4.05284734e-1f); // -4/pi^2
252 const Float4 B = Float4(1.27323954e+0f); // 4/pi
253 const Float4 C = Float4(7.75160950e-1f);
254 const Float4 D = Float4(2.24839049e-1f);
255
256 // Parabola approximating sine
257 Float4 sin = x * (Abs(x) * A + B);
258
259 // Improve precision from 0.06 to 0.001
260 if(true)
261 {
262 sin = sin * (Abs(sin) * D + C);
263 }
264
265 return sin;
266 }
267
268 Float4 cosine_pi(RValue<Float4> x, bool pp)
269 {
270 // cos(x) = sin(x + pi/2)
271 Float4 y = x + Float4(1.57079632e+0f);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400272
John Bauman19bac1e2014-05-06 15:23:49 -0400273 // Wrap around
274 y -= As<Float4>(CmpNLT(y, Float4(3.14159265e+0f)) & As<Int4>(Float4(6.28318530e+0f)));
275
276 return sine_pi(y, pp);
277 }
278
279 Float4 sine(RValue<Float4> x, bool pp)
280 {
281 // Reduce to [-0.5, 0.5] range
282 Float4 y = x * Float4(1.59154943e-1f); // 1/2pi
283 y = y - Round(y);
284
Alexis Hetu929c6b02017-11-07 16:04:25 -0500285 if(!pp)
286 {
287 // From the paper: "A Fast, Vectorizable Algorithm for Producing Single-Precision Sine-Cosine Pairs"
288 // This implementation passes OpenGL ES 3.0 precision requirements, at the cost of more operations:
289 // !pp : 17 mul, 7 add, 1 sub, 1 reciprocal
290 // pp : 4 mul, 2 add, 2 abs
291
292 Float4 y2 = y * y;
293 Float4 c1 = y2 * (y2 * (y2 * Float4(-0.0204391631f) + Float4(0.2536086171f)) + Float4(-1.2336977925f)) + Float4(1.0f);
294 Float4 s1 = y * (y2 * (y2 * (y2 * Float4(-0.0046075748f) + Float4(0.0796819754f)) + Float4(-0.645963615f)) + Float4(1.5707963235f));
295 Float4 c2 = (c1 * c1) - (s1 * s1);
296 Float4 s2 = Float4(2.0f) * s1 * c1;
297 return Float4(2.0f) * s2 * c2 * reciprocal(s2 * s2 + c2 * c2, pp, true);
298 }
299
John Bauman19bac1e2014-05-06 15:23:49 -0400300 const Float4 A = Float4(-16.0f);
301 const Float4 B = Float4(8.0f);
302 const Float4 C = Float4(7.75160950e-1f);
303 const Float4 D = Float4(2.24839049e-1f);
304
305 // Parabola approximating sine
306 Float4 sin = y * (Abs(y) * A + B);
307
308 // Improve precision from 0.06 to 0.001
309 if(true)
310 {
311 sin = sin * (Abs(sin) * D + C);
312 }
313
314 return sin;
315 }
316
317 Float4 cosine(RValue<Float4> x, bool pp)
318 {
319 // cos(x) = sin(x + pi/2)
320 Float4 y = x + Float4(1.57079632e+0f);
321 return sine(y, pp);
322 }
323
324 Float4 tangent(RValue<Float4> x, bool pp)
325 {
326 return sine(x, pp) / cosine(x, pp);
327 }
328
329 Float4 arccos(RValue<Float4> x, bool pp)
330 {
331 // pi/2 - arcsin(x)
332 return Float4(1.57079632e+0f) - arcsin(x);
333 }
334
335 Float4 arcsin(RValue<Float4> x, bool pp)
336 {
Alexis Hetu1728dde2017-11-08 13:43:16 -0500337 if(false) // Simpler implementation fails even lowp precision tests
338 {
339 // x*(pi/2-sqrt(1-x*x)*pi/5)
340 return x * (Float4(1.57079632e+0f) - Sqrt(Float4(1.0f) - x*x) * Float4(6.28318531e-1f));
341 }
342 else
343 {
344 // From 4.4.45, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
345 const Float4 half_pi(1.57079632f);
346 const Float4 a0(1.5707288f);
347 const Float4 a1(-0.2121144f);
348 const Float4 a2(0.0742610f);
349 const Float4 a3(-0.0187293f);
350 Float4 absx = Abs(x);
351 return As<Float4>(As<Int4>(half_pi - Sqrt(Float4(1.0f) - absx) * (a0 + absx * (a1 + absx * (a2 + absx * a3)))) ^
352 (As<Int4>(x) & Int4(0x80000000)));
353 }
354 }
355
356 // Approximation of atan in [0..1]
357 Float4 arctan_01(Float4 x, bool pp)
358 {
359 if(pp)
360 {
361 return x * (Float4(-0.27f) * x + Float4(1.05539816f));
362 }
363 else
364 {
365 // From 4.4.49, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
366 const Float4 a2(-0.3333314528f);
367 const Float4 a4(0.1999355085f);
368 const Float4 a6(-0.1420889944f);
369 const Float4 a8(0.1065626393f);
370 const Float4 a10(-0.0752896400f);
371 const Float4 a12(0.0429096138f);
372 const Float4 a14(-0.0161657367f);
373 const Float4 a16(0.0028662257f);
374 Float4 x2 = x * x;
375 return (x + x * (x2 * (a2 + x2 * (a4 + x2 * (a6 + x2 * (a8 + x2 * (a10 + x2 * (a12 + x2 * (a14 + x2 * a16)))))))));
376 }
John Bauman19bac1e2014-05-06 15:23:49 -0400377 }
378
379 Float4 arctan(RValue<Float4> x, bool pp)
380 {
Alexis Hetu1728dde2017-11-08 13:43:16 -0500381 Float4 absx = Abs(x);
382 Int4 O = CmpNLT(absx, Float4(1.0f));
383 Float4 y = As<Float4>((O & As<Int4>(Float4(1.0f) / absx)) | (~O & As<Int4>(absx))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400384
Alexis Hetu1728dde2017-11-08 13:43:16 -0500385 const Float4 half_pi(1.57079632f);
386 Float4 theta = arctan_01(y, pp);
387 return As<Float4>(((O & As<Int4>(half_pi - theta)) | (~O & As<Int4>(theta))) ^ // FIXME: Vector select
388 (As<Int4>(x) & Int4(0x80000000)));
John Bauman19bac1e2014-05-06 15:23:49 -0400389 }
390
391 Float4 arctan(RValue<Float4> y, RValue<Float4> x, bool pp)
392 {
Alexis Hetu1728dde2017-11-08 13:43:16 -0500393 const Float4 pi(3.14159265f); // pi
394 const Float4 minus_pi(-3.14159265f); // -pi
395 const Float4 half_pi(1.57079632f); // pi/2
396 const Float4 quarter_pi(7.85398163e-1f); // pi/4
397
John Bauman19bac1e2014-05-06 15:23:49 -0400398 // Rotate to upper semicircle when in lower semicircle
399 Int4 S = CmpLT(y, Float4(0.0f));
Alexis Hetu1728dde2017-11-08 13:43:16 -0500400 Float4 theta = As<Float4>(S & As<Int4>(minus_pi));
John Bauman19bac1e2014-05-06 15:23:49 -0400401 Float4 x0 = As<Float4>((As<Int4>(y) & Int4(0x80000000)) ^ As<Int4>(x));
402 Float4 y0 = Abs(y);
403
404 // Rotate to right quadrant when in left quadrant
Alexis Hetu596f6532018-05-15 14:07:19 -0400405 Int4 Q = CmpLT(x0, Float4(0.0f));
Alexis Hetu1728dde2017-11-08 13:43:16 -0500406 theta += As<Float4>(Q & As<Int4>(half_pi));
407 Float4 x1 = As<Float4>((Q & As<Int4>(y0)) | (~Q & As<Int4>(x0))); // FIXME: Vector select
408 Float4 y1 = As<Float4>((Q & As<Int4>(-x0)) | (~Q & As<Int4>(y0))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400409
Alexis Hetu1728dde2017-11-08 13:43:16 -0500410 // Mirror to first octant when in second octant
Alexis Hetu596f6532018-05-15 14:07:19 -0400411 Int4 O = CmpNLT(y1, x1);
Alexis Hetu1728dde2017-11-08 13:43:16 -0500412 Float4 x2 = As<Float4>((O & As<Int4>(y1)) | (~O & As<Int4>(x1))); // FIXME: Vector select
413 Float4 y2 = As<Float4>((O & As<Int4>(x1)) | (~O & As<Int4>(y1))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400414
415 // Approximation of atan in [0..1]
Alexis Hetu1728dde2017-11-08 13:43:16 -0500416 Int4 zero_x = CmpEQ(x2, Float4(0.0f));
417 Int4 inf_y = IsInf(y2); // Since x2 >= y2, this means x2 == y2 == inf, so we use 45 degrees or pi/4
418 Float4 atan2_theta = arctan_01(y2 / x2, pp);
Alexis Hetu596f6532018-05-15 14:07:19 -0400419 theta += As<Float4>((~zero_x & ~inf_y & ((O & As<Int4>(half_pi - atan2_theta)) | (~O & (As<Int4>(atan2_theta))))) | // FIXME: Vector select
Alexis Hetu1728dde2017-11-08 13:43:16 -0500420 (inf_y & As<Int4>(quarter_pi)));
John Bauman19bac1e2014-05-06 15:23:49 -0400421
Alexis Hetu1728dde2017-11-08 13:43:16 -0500422 // Recover loss of precision for tiny theta angles
423 Int4 precision_loss = S & Q & O & ~inf_y; // This combination results in (-pi + half_pi + half_pi - atan2_theta) which is equivalent to -atan2_theta
424 return As<Float4>((precision_loss & As<Int4>(-atan2_theta)) | (~precision_loss & As<Int4>(theta))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400425 }
426
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -0400427 Float4 sineh(RValue<Float4> x, bool pp)
428 {
429 return (exponential(x, pp) - exponential(-x, pp)) * Float4(0.5f);
430 }
431
432 Float4 cosineh(RValue<Float4> x, bool pp)
433 {
434 return (exponential(x, pp) + exponential(-x, pp)) * Float4(0.5f);
435 }
436
437 Float4 tangenth(RValue<Float4> x, bool pp)
438 {
439 Float4 e_x = exponential(x, pp);
440 Float4 e_minus_x = exponential(-x, pp);
441 return (e_x - e_minus_x) / (e_x + e_minus_x);
442 }
443
444 Float4 arccosh(RValue<Float4> x, bool pp)
445 {
446 return logarithm(x + Sqrt(x + Float4(1.0f)) * Sqrt(x - Float4(1.0f)), pp);
447 }
448
449 Float4 arcsinh(RValue<Float4> x, bool pp)
450 {
451 return logarithm(x + Sqrt(x * x + Float4(1.0f)), pp);
452 }
453
454 Float4 arctanh(RValue<Float4> x, bool pp)
455 {
456 return logarithm((Float4(1.0f) + x) / (Float4(1.0f) - x), pp) * Float4(0.5f);
457 }
458
Alexis Hetuecad5192015-06-05 13:42:05 -0400459 Float4 dot2(const Vector4f &v0, const Vector4f &v1)
John Bauman19bac1e2014-05-06 15:23:49 -0400460 {
461 return v0.x * v1.x + v0.y * v1.y;
462 }
463
Alexis Hetuecad5192015-06-05 13:42:05 -0400464 Float4 dot3(const Vector4f &v0, const Vector4f &v1)
John Bauman19bac1e2014-05-06 15:23:49 -0400465 {
466 return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
467 }
468
Alexis Hetuecad5192015-06-05 13:42:05 -0400469 Float4 dot4(const Vector4f &v0, const Vector4f &v1)
John Bauman19bac1e2014-05-06 15:23:49 -0400470 {
471 return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z + v0.w * v1.w;
472 }
473
474 void transpose4x4(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
475 {
476 Int2 tmp0 = UnpackHigh(row0, row1);
477 Int2 tmp1 = UnpackHigh(row2, row3);
478 Int2 tmp2 = UnpackLow(row0, row1);
479 Int2 tmp3 = UnpackLow(row2, row3);
480
Nicolas Capens45f187a2016-12-02 15:30:56 -0500481 row0 = UnpackLow(tmp2, tmp3);
482 row1 = UnpackHigh(tmp2, tmp3);
483 row2 = UnpackLow(tmp0, tmp1);
484 row3 = UnpackHigh(tmp0, tmp1);
John Bauman19bac1e2014-05-06 15:23:49 -0400485 }
486
Nicolas Capense4a88b92017-11-30 00:14:57 -0500487 void transpose4x3(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
488 {
489 Int2 tmp0 = UnpackHigh(row0, row1);
490 Int2 tmp1 = UnpackHigh(row2, row3);
491 Int2 tmp2 = UnpackLow(row0, row1);
492 Int2 tmp3 = UnpackLow(row2, row3);
493
494 row0 = UnpackLow(tmp2, tmp3);
495 row1 = UnpackHigh(tmp2, tmp3);
496 row2 = UnpackLow(tmp0, tmp1);
497 }
498
John Bauman19bac1e2014-05-06 15:23:49 -0400499 void transpose4x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
500 {
501 Float4 tmp0 = UnpackLow(row0, row1);
502 Float4 tmp1 = UnpackLow(row2, row3);
503 Float4 tmp2 = UnpackHigh(row0, row1);
504 Float4 tmp3 = UnpackHigh(row2, row3);
505
506 row0 = Float4(tmp0.xy, tmp1.xy);
507 row1 = Float4(tmp0.zw, tmp1.zw);
508 row2 = Float4(tmp2.xy, tmp3.xy);
509 row3 = Float4(tmp2.zw, tmp3.zw);
510 }
511
512 void transpose4x3(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
513 {
514 Float4 tmp0 = UnpackLow(row0, row1);
515 Float4 tmp1 = UnpackLow(row2, row3);
516 Float4 tmp2 = UnpackHigh(row0, row1);
517 Float4 tmp3 = UnpackHigh(row2, row3);
518
519 row0 = Float4(tmp0.xy, tmp1.xy);
520 row1 = Float4(tmp0.zw, tmp1.zw);
521 row2 = Float4(tmp2.xy, tmp3.xy);
522 }
523
524 void transpose4x2(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
525 {
526 Float4 tmp0 = UnpackLow(row0, row1);
527 Float4 tmp1 = UnpackLow(row2, row3);
528
529 row0 = Float4(tmp0.xy, tmp1.xy);
530 row1 = Float4(tmp0.zw, tmp1.zw);
531 }
532
533 void transpose4x1(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
534 {
535 Float4 tmp0 = UnpackLow(row0, row1);
536 Float4 tmp1 = UnpackLow(row2, row3);
537
538 row0 = Float4(tmp0.xy, tmp1.xy);
539 }
540
541 void transpose2x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
542 {
Nicolas Capens54ac5e82016-12-09 14:07:50 -0500543 Float4 tmp01 = UnpackLow(row0, row1);
544 Float4 tmp23 = UnpackHigh(row0, row1);
John Bauman19bac1e2014-05-06 15:23:49 -0400545
Nicolas Capens54ac5e82016-12-09 14:07:50 -0500546 row0 = tmp01;
547 row1 = Float4(tmp01.zw, row1.zw);
548 row2 = tmp23;
549 row3 = Float4(tmp23.zw, row3.zw);
John Bauman19bac1e2014-05-06 15:23:49 -0400550 }
551
552 void transpose4xN(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3, int N)
553 {
554 switch(N)
555 {
556 case 1: transpose4x1(row0, row1, row2, row3); break;
557 case 2: transpose4x2(row0, row1, row2, row3); break;
558 case 3: transpose4x3(row0, row1, row2, row3); break;
559 case 4: transpose4x4(row0, row1, row2, row3); break;
560 }
561 }
562
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400563 void ShaderCore::mov(Vector4f &dst, const Vector4f &src, bool integerDestination)
John Bauman89401822014-05-06 15:04:28 -0400564 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400565 if(integerDestination)
John Bauman89401822014-05-06 15:04:28 -0400566 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400567 dst.x = As<Float4>(RoundInt(src.x));
568 dst.y = As<Float4>(RoundInt(src.y));
569 dst.z = As<Float4>(RoundInt(src.z));
570 dst.w = As<Float4>(RoundInt(src.w));
John Bauman89401822014-05-06 15:04:28 -0400571 }
572 else
573 {
574 dst = src;
575 }
576 }
577
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400578 void ShaderCore::neg(Vector4f &dst, const Vector4f &src)
579 {
580 dst.x = -src.x;
581 dst.y = -src.y;
582 dst.z = -src.z;
583 dst.w = -src.w;
584 }
585
586 void ShaderCore::ineg(Vector4f &dst, const Vector4f &src)
587 {
588 dst.x = As<Float4>(-As<Int4>(src.x));
589 dst.y = As<Float4>(-As<Int4>(src.y));
590 dst.z = As<Float4>(-As<Int4>(src.z));
591 dst.w = As<Float4>(-As<Int4>(src.w));
592 }
593
Alexis Hetuecad5192015-06-05 13:42:05 -0400594 void ShaderCore::f2b(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -0400595 {
596 dst.x = As<Float4>(CmpNEQ(src.x, Float4(0.0f)));
597 dst.y = As<Float4>(CmpNEQ(src.y, Float4(0.0f)));
598 dst.z = As<Float4>(CmpNEQ(src.z, Float4(0.0f)));
599 dst.w = As<Float4>(CmpNEQ(src.w, Float4(0.0f)));
600 }
601
Alexis Hetuecad5192015-06-05 13:42:05 -0400602 void ShaderCore::b2f(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -0400603 {
604 dst.x = As<Float4>(As<Int4>(src.x) & As<Int4>(Float4(1.0f)));
605 dst.y = As<Float4>(As<Int4>(src.y) & As<Int4>(Float4(1.0f)));
606 dst.z = As<Float4>(As<Int4>(src.z) & As<Int4>(Float4(1.0f)));
607 dst.w = As<Float4>(As<Int4>(src.w) & As<Int4>(Float4(1.0f)));
608 }
609
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400610 void ShaderCore::f2i(Vector4f &dst, const Vector4f &src)
611 {
612 dst.x = As<Float4>(Int4(src.x));
613 dst.y = As<Float4>(Int4(src.y));
614 dst.z = As<Float4>(Int4(src.z));
615 dst.w = As<Float4>(Int4(src.w));
616 }
617
618 void ShaderCore::i2f(Vector4f &dst, const Vector4f &src)
619 {
620 dst.x = Float4(As<Int4>(src.x));
621 dst.y = Float4(As<Int4>(src.y));
622 dst.z = Float4(As<Int4>(src.z));
623 dst.w = Float4(As<Int4>(src.w));
624 }
625
626 void ShaderCore::f2u(Vector4f &dst, const Vector4f &src)
627 {
628 dst.x = As<Float4>(UInt4(src.x));
629 dst.y = As<Float4>(UInt4(src.y));
630 dst.z = As<Float4>(UInt4(src.z));
631 dst.w = As<Float4>(UInt4(src.w));
632 }
633
634 void ShaderCore::u2f(Vector4f &dst, const Vector4f &src)
635 {
636 dst.x = Float4(As<UInt4>(src.x));
637 dst.y = Float4(As<UInt4>(src.y));
638 dst.z = Float4(As<UInt4>(src.z));
639 dst.w = Float4(As<UInt4>(src.w));
640 }
641
642 void ShaderCore::i2b(Vector4f &dst, const Vector4f &src)
643 {
644 dst.x = As<Float4>(CmpNEQ(As<Int4>(src.x), Int4(0)));
645 dst.y = As<Float4>(CmpNEQ(As<Int4>(src.y), Int4(0)));
646 dst.z = As<Float4>(CmpNEQ(As<Int4>(src.z), Int4(0)));
647 dst.w = As<Float4>(CmpNEQ(As<Int4>(src.w), Int4(0)));
648 }
649
650 void ShaderCore::b2i(Vector4f &dst, const Vector4f &src)
651 {
652 dst.x = As<Float4>(As<Int4>(src.x) & Int4(1));
653 dst.y = As<Float4>(As<Int4>(src.y) & Int4(1));
654 dst.z = As<Float4>(As<Int4>(src.z) & Int4(1));
655 dst.w = As<Float4>(As<Int4>(src.w) & Int4(1));
656 }
657
Alexis Hetuecad5192015-06-05 13:42:05 -0400658 void ShaderCore::add(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400659 {
660 dst.x = src0.x + src1.x;
661 dst.y = src0.y + src1.y;
662 dst.z = src0.z + src1.z;
663 dst.w = src0.w + src1.w;
664 }
665
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400666 void ShaderCore::iadd(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
667 {
668 dst.x = As<Float4>(As<Int4>(src0.x) + As<Int4>(src1.x));
669 dst.y = As<Float4>(As<Int4>(src0.y) + As<Int4>(src1.y));
670 dst.z = As<Float4>(As<Int4>(src0.z) + As<Int4>(src1.z));
671 dst.w = As<Float4>(As<Int4>(src0.w) + As<Int4>(src1.w));
672 }
673
Alexis Hetuecad5192015-06-05 13:42:05 -0400674 void ShaderCore::sub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400675 {
676 dst.x = src0.x - src1.x;
677 dst.y = src0.y - src1.y;
678 dst.z = src0.z - src1.z;
679 dst.w = src0.w - src1.w;
680 }
681
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400682 void ShaderCore::isub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
683 {
684 dst.x = As<Float4>(As<Int4>(src0.x) - As<Int4>(src1.x));
685 dst.y = As<Float4>(As<Int4>(src0.y) - As<Int4>(src1.y));
686 dst.z = As<Float4>(As<Int4>(src0.z) - As<Int4>(src1.z));
687 dst.w = As<Float4>(As<Int4>(src0.w) - As<Int4>(src1.w));
688 }
689
Alexis Hetuecad5192015-06-05 13:42:05 -0400690 void ShaderCore::mad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman89401822014-05-06 15:04:28 -0400691 {
692 dst.x = src0.x * src1.x + src2.x;
693 dst.y = src0.y * src1.y + src2.y;
694 dst.z = src0.z * src1.z + src2.z;
695 dst.w = src0.w * src1.w + src2.w;
696 }
697
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400698 void ShaderCore::imad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
699 {
700 dst.x = As<Float4>(As<Int4>(src0.x) * As<Int4>(src1.x) + As<Int4>(src2.x));
701 dst.y = As<Float4>(As<Int4>(src0.y) * As<Int4>(src1.y) + As<Int4>(src2.y));
702 dst.z = As<Float4>(As<Int4>(src0.z) * As<Int4>(src1.z) + As<Int4>(src2.z));
703 dst.w = As<Float4>(As<Int4>(src0.w) * As<Int4>(src1.w) + As<Int4>(src2.w));
704 }
705
Alexis Hetuecad5192015-06-05 13:42:05 -0400706 void ShaderCore::mul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400707 {
708 dst.x = src0.x * src1.x;
709 dst.y = src0.y * src1.y;
710 dst.z = src0.z * src1.z;
711 dst.w = src0.w * src1.w;
712 }
713
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400714 void ShaderCore::imul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
715 {
716 dst.x = As<Float4>(As<Int4>(src0.x) * As<Int4>(src1.x));
717 dst.y = As<Float4>(As<Int4>(src0.y) * As<Int4>(src1.y));
718 dst.z = As<Float4>(As<Int4>(src0.z) * As<Int4>(src1.z));
719 dst.w = As<Float4>(As<Int4>(src0.w) * As<Int4>(src1.w));
720 }
721
Alexis Hetuecad5192015-06-05 13:42:05 -0400722 void ShaderCore::rcpx(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -0400723 {
Nicolas Capensaf13df42018-01-09 16:27:15 -0500724 Float4 rcp = reciprocal(src.x, pp, true, true);
John Bauman89401822014-05-06 15:04:28 -0400725
726 dst.x = rcp;
727 dst.y = rcp;
728 dst.z = rcp;
729 dst.w = rcp;
730 }
731
Alexis Hetuecad5192015-06-05 13:42:05 -0400732 void ShaderCore::div(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400733 {
734 dst.x = src0.x / src1.x;
735 dst.y = src0.y / src1.y;
736 dst.z = src0.z / src1.z;
737 dst.w = src0.w / src1.w;
738 }
739
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400740 void ShaderCore::idiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
741 {
742 Float4 intMax(As<Float4>(Int4(INT_MAX)));
743 cmp0i(dst.x, src1.x, intMax, src1.x);
744 dst.x = As<Float4>(As<Int4>(src0.x) / As<Int4>(dst.x));
745 cmp0i(dst.y, src1.y, intMax, src1.y);
746 dst.y = As<Float4>(As<Int4>(src0.y) / As<Int4>(dst.y));
747 cmp0i(dst.z, src1.z, intMax, src1.z);
748 dst.z = As<Float4>(As<Int4>(src0.z) / As<Int4>(dst.z));
749 cmp0i(dst.w, src1.w, intMax, src1.w);
750 dst.w = As<Float4>(As<Int4>(src0.w) / As<Int4>(dst.w));
751 }
752
753 void ShaderCore::udiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
754 {
755 Float4 uintMax(As<Float4>(UInt4(UINT_MAX)));
756 cmp0i(dst.x, src1.x, uintMax, src1.x);
757 dst.x = As<Float4>(As<UInt4>(src0.x) / As<UInt4>(dst.x));
758 cmp0i(dst.y, src1.y, uintMax, src1.y);
759 dst.y = As<Float4>(As<UInt4>(src0.y) / As<UInt4>(dst.y));
760 cmp0i(dst.z, src1.z, uintMax, src1.z);
761 dst.z = As<Float4>(As<UInt4>(src0.z) / As<UInt4>(dst.z));
762 cmp0i(dst.w, src1.w, uintMax, src1.w);
763 dst.w = As<Float4>(As<UInt4>(src0.w) / As<UInt4>(dst.w));
764 }
765
Alexis Hetuecad5192015-06-05 13:42:05 -0400766 void ShaderCore::mod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400767 {
768 dst.x = modulo(src0.x, src1.x);
769 dst.y = modulo(src0.y, src1.y);
770 dst.z = modulo(src0.z, src1.z);
771 dst.w = modulo(src0.w, src1.w);
772 }
773
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400774 void ShaderCore::imod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
775 {
Alexis Hetu28958102017-10-02 13:48:19 -0400776 Float4 intMax(As<Float4>(Int4(INT_MAX)));
777 cmp0i(dst.x, src1.x, intMax, src1.x);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400778 dst.x = As<Float4>(As<Int4>(src0.x) % As<Int4>(dst.x));
Alexis Hetu28958102017-10-02 13:48:19 -0400779 cmp0i(dst.y, src1.y, intMax, src1.y);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400780 dst.y = As<Float4>(As<Int4>(src0.y) % As<Int4>(dst.y));
Alexis Hetu28958102017-10-02 13:48:19 -0400781 cmp0i(dst.z, src1.z, intMax, src1.z);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400782 dst.z = As<Float4>(As<Int4>(src0.z) % As<Int4>(dst.z));
Alexis Hetu28958102017-10-02 13:48:19 -0400783 cmp0i(dst.w, src1.w, intMax, src1.w);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400784 dst.w = As<Float4>(As<Int4>(src0.w) % As<Int4>(dst.w));
785 }
Alexis Hetu28958102017-10-02 13:48:19 -0400786
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400787 void ShaderCore::umod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
788 {
Alexis Hetu28958102017-10-02 13:48:19 -0400789 Float4 uintMax(As<Float4>(UInt4(UINT_MAX)));
790 cmp0i(dst.x, src1.x, uintMax, src1.x);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400791 dst.x = As<Float4>(As<UInt4>(src0.x) % As<UInt4>(dst.x));
Alexis Hetu28958102017-10-02 13:48:19 -0400792 cmp0i(dst.y, src1.y, uintMax, src1.y);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400793 dst.y = As<Float4>(As<UInt4>(src0.y) % As<UInt4>(dst.y));
Alexis Hetu28958102017-10-02 13:48:19 -0400794 cmp0i(dst.z, src1.z, uintMax, src1.z);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400795 dst.z = As<Float4>(As<UInt4>(src0.z) % As<UInt4>(dst.z));
Alexis Hetu28958102017-10-02 13:48:19 -0400796 cmp0i(dst.w, src1.w, uintMax, src1.w);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400797 dst.w = As<Float4>(As<UInt4>(src0.w) % As<UInt4>(dst.w));
798 }
799
800 void ShaderCore::shl(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
801 {
802 dst.x = As<Float4>(As<Int4>(src0.x) << As<Int4>(src1.x));
803 dst.y = As<Float4>(As<Int4>(src0.y) << As<Int4>(src1.y));
804 dst.z = As<Float4>(As<Int4>(src0.z) << As<Int4>(src1.z));
805 dst.w = As<Float4>(As<Int4>(src0.w) << As<Int4>(src1.w));
806 }
807
808 void ShaderCore::ishr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
809 {
810 dst.x = As<Float4>(As<Int4>(src0.x) >> As<Int4>(src1.x));
811 dst.y = As<Float4>(As<Int4>(src0.y) >> As<Int4>(src1.y));
812 dst.z = As<Float4>(As<Int4>(src0.z) >> As<Int4>(src1.z));
813 dst.w = As<Float4>(As<Int4>(src0.w) >> As<Int4>(src1.w));
814 }
815
816 void ShaderCore::ushr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
817 {
818 dst.x = As<Float4>(As<UInt4>(src0.x) >> As<UInt4>(src1.x));
819 dst.y = As<Float4>(As<UInt4>(src0.y) >> As<UInt4>(src1.y));
820 dst.z = As<Float4>(As<UInt4>(src0.z) >> As<UInt4>(src1.z));
821 dst.w = As<Float4>(As<UInt4>(src0.w) >> As<UInt4>(src1.w));
822 }
823
Alexis Hetuecad5192015-06-05 13:42:05 -0400824 void ShaderCore::rsqx(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -0400825 {
826 Float4 rsq = reciprocalSquareRoot(src.x, true, pp);
827
John Bauman19bac1e2014-05-06 15:23:49 -0400828 dst.x = rsq;
829 dst.y = rsq;
830 dst.z = rsq;
831 dst.w = rsq;
John Bauman89401822014-05-06 15:04:28 -0400832 }
833
Alexis Hetuecad5192015-06-05 13:42:05 -0400834 void ShaderCore::sqrt(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400835 {
836 dst.x = Sqrt(src.x);
837 dst.y = Sqrt(src.y);
838 dst.z = Sqrt(src.z);
839 dst.w = Sqrt(src.w);
840 }
841
Alexis Hetuecad5192015-06-05 13:42:05 -0400842 void ShaderCore::rsq(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400843 {
844 dst.x = reciprocalSquareRoot(src.x, false, pp);
845 dst.y = reciprocalSquareRoot(src.y, false, pp);
846 dst.z = reciprocalSquareRoot(src.z, false, pp);
847 dst.w = reciprocalSquareRoot(src.w, false, pp);
848 }
849
Alexis Hetuecad5192015-06-05 13:42:05 -0400850 void ShaderCore::len2(Float4 &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400851 {
852 dst = Sqrt(dot2(src, src));
853 }
854
Alexis Hetuecad5192015-06-05 13:42:05 -0400855 void ShaderCore::len3(Float4 &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400856 {
857 dst = Sqrt(dot3(src, src));
858 }
859
Alexis Hetuecad5192015-06-05 13:42:05 -0400860 void ShaderCore::len4(Float4 &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400861 {
862 dst = Sqrt(dot4(src, src));
863 }
864
Alexis Hetuecad5192015-06-05 13:42:05 -0400865 void ShaderCore::dist1(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400866 {
867 dst = Abs(src0.x - src1.x);
868 }
869
Alexis Hetuecad5192015-06-05 13:42:05 -0400870 void ShaderCore::dist2(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400871 {
872 Float4 dx = src0.x - src1.x;
873 Float4 dy = src0.y - src1.y;
874 Float4 dot2 = dx * dx + dy * dy;
875 dst = Sqrt(dot2);
876 }
877
Alexis Hetuecad5192015-06-05 13:42:05 -0400878 void ShaderCore::dist3(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400879 {
880 Float4 dx = src0.x - src1.x;
881 Float4 dy = src0.y - src1.y;
882 Float4 dz = src0.z - src1.z;
883 Float4 dot3 = dx * dx + dy * dy + dz * dz;
884 dst = Sqrt(dot3);
885 }
886
Alexis Hetuecad5192015-06-05 13:42:05 -0400887 void ShaderCore::dist4(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400888 {
889 Float4 dx = src0.x - src1.x;
890 Float4 dy = src0.y - src1.y;
891 Float4 dz = src0.z - src1.z;
892 Float4 dw = src0.w - src1.w;
893 Float4 dot4 = dx * dx + dy * dy + dz * dz + dw * dw;
894 dst = Sqrt(dot4);
895 }
896
Alexis Hetuecad5192015-06-05 13:42:05 -0400897 void ShaderCore::dp1(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400898 {
899 Float4 t = src0.x * src1.x;
900
901 dst.x = t;
902 dst.y = t;
903 dst.z = t;
904 dst.w = t;
905 }
906
Alexis Hetuecad5192015-06-05 13:42:05 -0400907 void ShaderCore::dp2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400908 {
909 Float4 t = dot2(src0, src1);
910
911 dst.x = t;
912 dst.y = t;
913 dst.z = t;
914 dst.w = t;
915 }
916
Alexis Hetuecad5192015-06-05 13:42:05 -0400917 void ShaderCore::dp2add(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman19bac1e2014-05-06 15:23:49 -0400918 {
919 Float4 t = dot2(src0, src1) + src2.x;
920
921 dst.x = t;
922 dst.y = t;
923 dst.z = t;
924 dst.w = t;
925 }
926
Alexis Hetuecad5192015-06-05 13:42:05 -0400927 void ShaderCore::dp3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400928 {
929 Float4 dot = dot3(src0, src1);
930
931 dst.x = dot;
932 dst.y = dot;
933 dst.z = dot;
934 dst.w = dot;
935 }
936
Alexis Hetuecad5192015-06-05 13:42:05 -0400937 void ShaderCore::dp4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400938 {
939 Float4 dot = dot4(src0, src1);
940
941 dst.x = dot;
942 dst.y = dot;
943 dst.z = dot;
944 dst.w = dot;
945 }
946
Alexis Hetuecad5192015-06-05 13:42:05 -0400947 void ShaderCore::min(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400948 {
949 dst.x = Min(src0.x, src1.x);
950 dst.y = Min(src0.y, src1.y);
951 dst.z = Min(src0.z, src1.z);
952 dst.w = Min(src0.w, src1.w);
953 }
954
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400955 void ShaderCore::imin(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
956 {
957 dst.x = As<Float4>(Min(As<Int4>(src0.x), As<Int4>(src1.x)));
958 dst.y = As<Float4>(Min(As<Int4>(src0.y), As<Int4>(src1.y)));
959 dst.z = As<Float4>(Min(As<Int4>(src0.z), As<Int4>(src1.z)));
960 dst.w = As<Float4>(Min(As<Int4>(src0.w), As<Int4>(src1.w)));
961 }
962
963 void ShaderCore::umin(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
964 {
965 dst.x = As<Float4>(Min(As<UInt4>(src0.x), As<UInt4>(src1.x)));
966 dst.y = As<Float4>(Min(As<UInt4>(src0.y), As<UInt4>(src1.y)));
967 dst.z = As<Float4>(Min(As<UInt4>(src0.z), As<UInt4>(src1.z)));
968 dst.w = As<Float4>(Min(As<UInt4>(src0.w), As<UInt4>(src1.w)));
969 }
970
Alexis Hetuecad5192015-06-05 13:42:05 -0400971 void ShaderCore::max(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400972 {
973 dst.x = Max(src0.x, src1.x);
974 dst.y = Max(src0.y, src1.y);
975 dst.z = Max(src0.z, src1.z);
976 dst.w = Max(src0.w, src1.w);
977 }
978
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400979 void ShaderCore::imax(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
980 {
981 dst.x = As<Float4>(Max(As<Int4>(src0.x), As<Int4>(src1.x)));
982 dst.y = As<Float4>(Max(As<Int4>(src0.y), As<Int4>(src1.y)));
983 dst.z = As<Float4>(Max(As<Int4>(src0.z), As<Int4>(src1.z)));
984 dst.w = As<Float4>(Max(As<Int4>(src0.w), As<Int4>(src1.w)));
985 }
986
987 void ShaderCore::umax(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
988 {
989 dst.x = As<Float4>(Max(As<Int4>(src0.x), As<Int4>(src1.x)));
990 dst.y = As<Float4>(Max(As<Int4>(src0.y), As<Int4>(src1.y)));
991 dst.z = As<Float4>(Max(As<Int4>(src0.z), As<Int4>(src1.z)));
992 dst.w = As<Float4>(Max(As<Int4>(src0.w), As<Int4>(src1.w)));
993 }
994
Alexis Hetuecad5192015-06-05 13:42:05 -0400995 void ShaderCore::slt(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400996 {
John Bauman19bac1e2014-05-06 15:23:49 -0400997 dst.x = As<Float4>(As<Int4>(CmpLT(src0.x, src1.x)) & As<Int4>(Float4(1.0f)));
998 dst.y = As<Float4>(As<Int4>(CmpLT(src0.y, src1.y)) & As<Int4>(Float4(1.0f)));
999 dst.z = As<Float4>(As<Int4>(CmpLT(src0.z, src1.z)) & As<Int4>(Float4(1.0f)));
1000 dst.w = As<Float4>(As<Int4>(CmpLT(src0.w, src1.w)) & As<Int4>(Float4(1.0f)));
John Bauman89401822014-05-06 15:04:28 -04001001 }
1002
Alexis Hetuecad5192015-06-05 13:42:05 -04001003 void ShaderCore::step(Vector4f &dst, const Vector4f &edge, const Vector4f &x)
John Bauman89401822014-05-06 15:04:28 -04001004 {
John Bauman19bac1e2014-05-06 15:23:49 -04001005 dst.x = As<Float4>(CmpNLT(x.x, edge.x) & As<Int4>(Float4(1.0f)));
1006 dst.y = As<Float4>(CmpNLT(x.y, edge.y) & As<Int4>(Float4(1.0f)));
1007 dst.z = As<Float4>(CmpNLT(x.z, edge.z) & As<Int4>(Float4(1.0f)));
1008 dst.w = As<Float4>(CmpNLT(x.w, edge.w) & As<Int4>(Float4(1.0f)));
John Bauman89401822014-05-06 15:04:28 -04001009 }
1010
Alexis Hetuecad5192015-06-05 13:42:05 -04001011 void ShaderCore::exp2x(Vector4f &dst, const Vector4f &src, bool pp)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001012 {
John Bauman19bac1e2014-05-06 15:23:49 -04001013 Float4 exp = exponential2(src.x, pp);
John Bauman89401822014-05-06 15:04:28 -04001014
1015 dst.x = exp;
1016 dst.y = exp;
1017 dst.z = exp;
1018 dst.w = exp;
1019 }
1020
Alexis Hetuecad5192015-06-05 13:42:05 -04001021 void ShaderCore::exp2(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001022 {
John Bauman19bac1e2014-05-06 15:23:49 -04001023 dst.x = exponential2(src.x, pp);
1024 dst.y = exponential2(src.y, pp);
1025 dst.z = exponential2(src.z, pp);
1026 dst.w = exponential2(src.w, pp);
1027 }
1028
Alexis Hetuecad5192015-06-05 13:42:05 -04001029 void ShaderCore::exp(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001030 {
1031 dst.x = exponential(src.x, pp);
1032 dst.y = exponential(src.y, pp);
1033 dst.z = exponential(src.z, pp);
1034 dst.w = exponential(src.w, pp);
1035 }
1036
Alexis Hetuecad5192015-06-05 13:42:05 -04001037 void ShaderCore::log2x(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001038 {
1039 Float4 log = logarithm2(src.x, true, pp);
John Bauman89401822014-05-06 15:04:28 -04001040
1041 dst.x = log;
1042 dst.y = log;
1043 dst.z = log;
1044 dst.w = log;
1045 }
1046
Alexis Hetuecad5192015-06-05 13:42:05 -04001047 void ShaderCore::log2(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001048 {
Alexis Hetua781af72017-07-06 17:12:47 -04001049 dst.x = logarithm2(src.x, false, pp);
1050 dst.y = logarithm2(src.y, false, pp);
1051 dst.z = logarithm2(src.z, false, pp);
1052 dst.w = logarithm2(src.w, false, pp);
John Bauman19bac1e2014-05-06 15:23:49 -04001053 }
1054
Alexis Hetuecad5192015-06-05 13:42:05 -04001055 void ShaderCore::log(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001056 {
1057 dst.x = logarithm(src.x, false, pp);
1058 dst.y = logarithm(src.y, false, pp);
1059 dst.z = logarithm(src.z, false, pp);
1060 dst.w = logarithm(src.w, false, pp);
1061 }
1062
Alexis Hetuecad5192015-06-05 13:42:05 -04001063 void ShaderCore::lit(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001064 {
1065 dst.x = Float4(1.0f);
1066 dst.y = Max(src.x, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -04001067
1068 Float4 pow;
1069
1070 pow = src.w;
John Bauman19bac1e2014-05-06 15:23:49 -04001071 pow = Min(pow, Float4(127.9961f));
1072 pow = Max(pow, Float4(-127.9961f));
John Bauman89401822014-05-06 15:04:28 -04001073
1074 dst.z = power(src.y, pow);
John Bauman19bac1e2014-05-06 15:23:49 -04001075 dst.z = As<Float4>(As<Int4>(dst.z) & CmpNLT(src.x, Float4(0.0f)));
1076 dst.z = As<Float4>(As<Int4>(dst.z) & CmpNLT(src.y, Float4(0.0f)));
John Bauman89401822014-05-06 15:04:28 -04001077
John Bauman19bac1e2014-05-06 15:23:49 -04001078 dst.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04001079 }
1080
Alexis Hetuecad5192015-06-05 13:42:05 -04001081 void ShaderCore::att(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -04001082 {
John Bauman19bac1e2014-05-06 15:23:49 -04001083 // Computes attenuation factors (1, d, d^2, 1/d) assuming src0 = d^2, src1 = 1/d
John Bauman89401822014-05-06 15:04:28 -04001084 dst.x = 1;
1085 dst.y = src0.y * src1.y;
1086 dst.z = src0.z;
1087 dst.w = src1.w;
1088 }
1089
Alexis Hetuecad5192015-06-05 13:42:05 -04001090 void ShaderCore::lrp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman89401822014-05-06 15:04:28 -04001091 {
1092 dst.x = src0.x * (src1.x - src2.x) + src2.x;
1093 dst.y = src0.y * (src1.y - src2.y) + src2.y;
1094 dst.z = src0.z * (src1.z - src2.z) + src2.z;
1095 dst.w = src0.w * (src1.w - src2.w) + src2.w;
1096 }
1097
Alexis Hetu8ef6d102017-11-09 15:49:09 -05001098 void ShaderCore::isinf(Vector4f &dst, const Vector4f &src)
1099 {
1100 dst.x = As<Float4>(IsInf(src.x));
1101 dst.y = As<Float4>(IsInf(src.y));
1102 dst.z = As<Float4>(IsInf(src.z));
1103 dst.w = As<Float4>(IsInf(src.w));
1104 }
1105
1106 void ShaderCore::isnan(Vector4f &dst, const Vector4f &src)
1107 {
1108 dst.x = As<Float4>(IsNan(src.x));
1109 dst.y = As<Float4>(IsNan(src.y));
1110 dst.z = As<Float4>(IsNan(src.z));
1111 dst.w = As<Float4>(IsNan(src.w));
1112 }
1113
Alexis Hetuecad5192015-06-05 13:42:05 -04001114 void ShaderCore::smooth(Vector4f &dst, const Vector4f &edge0, const Vector4f &edge1, const Vector4f &x)
John Bauman89401822014-05-06 15:04:28 -04001115 {
John Bauman19bac1e2014-05-06 15:23:49 -04001116 Float4 tx = Min(Max((x.x - edge0.x) / (edge1.x - edge0.x), Float4(0.0f)), Float4(1.0f)); dst.x = tx * tx * (Float4(3.0f) - Float4(2.0f) * tx);
1117 Float4 ty = Min(Max((x.y - edge0.y) / (edge1.y - edge0.y), Float4(0.0f)), Float4(1.0f)); dst.y = ty * ty * (Float4(3.0f) - Float4(2.0f) * ty);
1118 Float4 tz = Min(Max((x.z - edge0.z) / (edge1.z - edge0.z), Float4(0.0f)), Float4(1.0f)); dst.z = tz * tz * (Float4(3.0f) - Float4(2.0f) * tz);
1119 Float4 tw = Min(Max((x.w - edge0.w) / (edge1.w - edge0.w), Float4(0.0f)), Float4(1.0f)); dst.w = tw * tw * (Float4(3.0f) - Float4(2.0f) * tw);
John Bauman89401822014-05-06 15:04:28 -04001120 }
1121
Alexis Hetuffb35eb2016-04-06 18:05:00 -04001122 void ShaderCore::floatToHalfBits(Float4& dst, const Float4& floatBits, bool storeInUpperBits)
1123 {
1124 static const uint32_t mask_sign = 0x80000000u;
1125 static const uint32_t mask_round = ~0xfffu;
1126 static const uint32_t c_f32infty = 255 << 23;
1127 static const uint32_t c_magic = 15 << 23;
1128 static const uint32_t c_nanbit = 0x200;
1129 static const uint32_t c_infty_as_fp16 = 0x7c00;
1130 static const uint32_t c_clamp = (31 << 23) - 0x1000;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001131
Alexis Hetuffb35eb2016-04-06 18:05:00 -04001132 UInt4 justsign = UInt4(mask_sign) & As<UInt4>(floatBits);
1133 UInt4 absf = As<UInt4>(floatBits) ^ justsign;
1134 UInt4 b_isnormal = CmpNLE(UInt4(c_f32infty), absf);
1135
1136 // Note: this version doesn't round to the nearest even in case of a tie as defined by IEEE 754-2008, it rounds to +inf
1137 // instead of nearest even, since that's fine for GLSL ES 3.0's needs (see section 2.1.1 Floating-Point Computation)
1138 UInt4 joined = ((((As<UInt4>(Min(As<Float4>(absf & UInt4(mask_round)) * As<Float4>(UInt4(c_magic)),
1139 As<Float4>(UInt4(c_clamp))))) - UInt4(mask_round)) >> 13) & b_isnormal) |
1140 ((b_isnormal ^ UInt4(0xFFFFFFFF)) & ((CmpNLE(absf, UInt4(c_f32infty)) & UInt4(c_nanbit)) |
1141 UInt4(c_infty_as_fp16)));
1142
1143 dst = As<Float4>(storeInUpperBits ? As<UInt4>(dst) | ((joined << 16) | justsign) : joined | (justsign >> 16));
1144 }
1145
1146 void ShaderCore::halfToFloatBits(Float4& dst, const Float4& halfBits)
1147 {
1148 static const uint32_t mask_nosign = 0x7FFF;
1149 static const uint32_t magic = (254 - 15) << 23;
1150 static const uint32_t was_infnan = 0x7BFF;
1151 static const uint32_t exp_infnan = 255 << 23;
1152
1153 UInt4 expmant = As<UInt4>(halfBits) & UInt4(mask_nosign);
1154 dst = As<Float4>(As<UInt4>(As<Float4>(expmant << 13) * As<Float4>(UInt4(magic))) |
1155 ((As<UInt4>(halfBits) ^ UInt4(expmant)) << 16) |
1156 (CmpNLE(As<UInt4>(expmant), UInt4(was_infnan)) & UInt4(exp_infnan)));
1157 }
1158
1159 void ShaderCore::packHalf2x16(Vector4f &d, const Vector4f &s0)
1160 {
1161 // half2 | half1
1162 floatToHalfBits(d.x, s0.x, false);
1163 floatToHalfBits(d.x, s0.y, true);
1164 }
1165
1166 void ShaderCore::unpackHalf2x16(Vector4f &dst, const Vector4f &s0)
1167 {
1168 // half2 | half1
1169 halfToFloatBits(dst.x, As<Float4>(As<UInt4>(s0.x) & UInt4(0x0000FFFF)));
1170 halfToFloatBits(dst.y, As<Float4>((As<UInt4>(s0.x) & UInt4(0xFFFF0000)) >> 16));
1171 }
1172
Alexis Hetu9cde9742016-04-06 13:03:38 -04001173 void ShaderCore::packSnorm2x16(Vector4f &d, const Vector4f &s0)
1174 {
1175 // round(clamp(c, -1.0, 1.0) * 32767.0)
1176 d.x = As<Float4>((Int4(Round(Min(Max(s0.x, Float4(-1.0f)), Float4(1.0f)) * Float4(32767.0f))) & Int4(0xFFFF)) |
1177 ((Int4(Round(Min(Max(s0.y, Float4(-1.0f)), Float4(1.0f)) * Float4(32767.0f))) & Int4(0xFFFF)) << 16));
1178 }
1179
1180 void ShaderCore::packUnorm2x16(Vector4f &d, const Vector4f &s0)
1181 {
1182 // round(clamp(c, 0.0, 1.0) * 65535.0)
1183 d.x = As<Float4>((Int4(Round(Min(Max(s0.x, Float4(0.0f)), Float4(1.0f)) * Float4(65535.0f))) & Int4(0xFFFF)) |
1184 ((Int4(Round(Min(Max(s0.y, Float4(0.0f)), Float4(1.0f)) * Float4(65535.0f))) & Int4(0xFFFF)) << 16));
1185 }
1186
1187 void ShaderCore::unpackSnorm2x16(Vector4f &dst, const Vector4f &s0)
1188 {
1189 // clamp(f / 32727.0, -1.0, 1.0)
1190 dst.x = Min(Max(Float4(As<Int4>((As<UInt4>(s0.x) & UInt4(0x0000FFFF)) << 16)) * Float4(1.0f / float(0x7FFF0000)), Float4(-1.0f)), Float4(1.0f));
1191 dst.y = Min(Max(Float4(As<Int4>(As<UInt4>(s0.x) & UInt4(0xFFFF0000))) * Float4(1.0f / float(0x7FFF0000)), Float4(-1.0f)), Float4(1.0f));
1192 }
1193
1194 void ShaderCore::unpackUnorm2x16(Vector4f &dst, const Vector4f &s0)
1195 {
1196 // f / 65535.0
1197 dst.x = Float4((As<UInt4>(s0.x) & UInt4(0x0000FFFF)) << 16) * Float4(1.0f / float(0xFFFF0000));
1198 dst.y = Float4(As<UInt4>(s0.x) & UInt4(0xFFFF0000)) * Float4(1.0f / float(0xFFFF0000));
1199 }
1200
Alexis Hetuc3d95f32015-09-23 12:27:32 -04001201 void ShaderCore::det2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
1202 {
1203 dst.x = src0.x * src1.y - src0.y * src1.x;
1204 dst.y = dst.z = dst.w = dst.x;
1205 }
1206
1207 void ShaderCore::det3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
1208 {
1209 crs(dst, src1, src2);
1210 dp3(dst, dst, src0);
1211 }
1212
1213 void ShaderCore::det4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2, const Vector4f &src3)
1214 {
1215 dst.x = src2.z * src3.w - src2.w * src3.z;
1216 dst.y = src1.w * src3.z - src1.z * src3.w;
1217 dst.z = src1.z * src2.w - src1.w * src2.z;
1218 dst.x = src0.x * (src1.y * dst.x + src2.y * dst.y + src3.y * dst.z) -
1219 src0.y * (src1.x * dst.x + src2.x * dst.y + src3.x * dst.z) +
1220 src0.z * (src1.x * (src2.y * src3.w - src2.w * src3.y) +
1221 src2.x * (src1.w * src3.y - src1.y * src3.w) +
1222 src3.x * (src1.y * src2.w - src1.w * src2.y)) +
1223 src0.w * (src1.x * (src2.z * src3.y - src2.y * src3.z) +
1224 src2.x * (src1.y * src3.z - src1.z * src3.y) +
1225 src3.x * (src1.z * src2.y - src1.y * src2.z));
1226 dst.y = dst.z = dst.w = dst.x;
1227 }
1228
Alexis Hetuecad5192015-06-05 13:42:05 -04001229 void ShaderCore::frc(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001230 {
1231 dst.x = Frac(src.x);
1232 dst.y = Frac(src.y);
1233 dst.z = Frac(src.z);
1234 dst.w = Frac(src.w);
1235 }
1236
Alexis Hetuecad5192015-06-05 13:42:05 -04001237 void ShaderCore::trunc(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001238 {
1239 dst.x = Trunc(src.x);
1240 dst.y = Trunc(src.y);
1241 dst.z = Trunc(src.z);
1242 dst.w = Trunc(src.w);
1243 }
1244
Alexis Hetuecad5192015-06-05 13:42:05 -04001245 void ShaderCore::floor(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001246 {
1247 dst.x = Floor(src.x);
1248 dst.y = Floor(src.y);
1249 dst.z = Floor(src.z);
1250 dst.w = Floor(src.w);
1251 }
1252
Alexis Hetuecad5192015-06-05 13:42:05 -04001253 void ShaderCore::round(Vector4f &dst, const Vector4f &src)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001254 {
1255 dst.x = Round(src.x);
1256 dst.y = Round(src.y);
1257 dst.z = Round(src.z);
1258 dst.w = Round(src.w);
1259 }
1260
Alexis Hetuecad5192015-06-05 13:42:05 -04001261 void ShaderCore::roundEven(Vector4f &dst, const Vector4f &src)
Alexis Hetu8e851c12015-06-04 11:30:54 -04001262 {
1263 // dst = round(src) + ((round(src) < src) * 2 - 1) * (fract(src) == 0.5) * isOdd(round(src));
1264 // ex.: 1.5: 2 + (0 * 2 - 1) * 1 * 0 = 2
1265 // 2.5: 3 + (0 * 2 - 1) * 1 * 1 = 2
1266 // -1.5: -2 + (1 * 2 - 1) * 1 * 0 = -2
1267 // -2.5: -3 + (1 * 2 - 1) * 1 * 1 = -2
1268 // Even if the round implementation rounds the other way:
1269 // 1.5: 1 + (1 * 2 - 1) * 1 * 1 = 2
1270 // 2.5: 2 + (1 * 2 - 1) * 1 * 0 = 2
1271 // -1.5: -1 + (0 * 2 - 1) * 1 * 1 = -2
1272 // -2.5: -2 + (0 * 2 - 1) * 1 * 0 = -2
1273 round(dst, src);
1274 dst.x += ((Float4(CmpLT(dst.x, src.x) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.x), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.x) & Int4(1));
1275 dst.y += ((Float4(CmpLT(dst.y, src.y) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.y), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.y) & Int4(1));
1276 dst.z += ((Float4(CmpLT(dst.z, src.z) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.z), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.z) & Int4(1));
1277 dst.w += ((Float4(CmpLT(dst.w, src.w) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.w), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.w) & Int4(1));
1278 }
1279
Alexis Hetuecad5192015-06-05 13:42:05 -04001280 void ShaderCore::ceil(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001281 {
1282 dst.x = Ceil(src.x);
1283 dst.y = Ceil(src.y);
1284 dst.z = Ceil(src.z);
1285 dst.w = Ceil(src.w);
1286 }
1287
Alexis Hetuecad5192015-06-05 13:42:05 -04001288 void ShaderCore::powx(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001289 {
1290 Float4 pow = power(src0.x, src1.x, pp);
1291
1292 dst.x = pow;
1293 dst.y = pow;
1294 dst.z = pow;
1295 dst.w = pow;
1296 }
1297
Alexis Hetuecad5192015-06-05 13:42:05 -04001298 void ShaderCore::pow(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001299 {
1300 dst.x = power(src0.x, src1.x, pp);
1301 dst.y = power(src0.y, src1.y, pp);
1302 dst.z = power(src0.z, src1.z, pp);
1303 dst.w = power(src0.w, src1.w, pp);
1304 }
1305
Alexis Hetuecad5192015-06-05 13:42:05 -04001306 void ShaderCore::crs(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -04001307 {
1308 dst.x = src0.y * src1.z - src0.z * src1.y;
1309 dst.y = src0.z * src1.x - src0.x * src1.z;
1310 dst.z = src0.x * src1.y - src0.y * src1.x;
1311 }
1312
Alexis Hetuecad5192015-06-05 13:42:05 -04001313 void ShaderCore::forward1(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001314 {
1315 Int4 flip = CmpNLT(Nref.x * I.x, Float4(0.0f)) & Int4(0x80000000);
1316
1317 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1318 }
1319
Alexis Hetuecad5192015-06-05 13:42:05 -04001320 void ShaderCore::forward2(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001321 {
1322 Int4 flip = CmpNLT(dot2(Nref, I), Float4(0.0f)) & Int4(0x80000000);
1323
1324 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1325 dst.y = As<Float4>(flip ^ As<Int4>(N.y));
1326 }
1327
Alexis Hetuecad5192015-06-05 13:42:05 -04001328 void ShaderCore::forward3(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001329 {
1330 Int4 flip = CmpNLT(dot3(Nref, I), Float4(0.0f)) & Int4(0x80000000);
1331
1332 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1333 dst.y = As<Float4>(flip ^ As<Int4>(N.y));
1334 dst.z = As<Float4>(flip ^ As<Int4>(N.z));
1335 }
1336
Alexis Hetuecad5192015-06-05 13:42:05 -04001337 void ShaderCore::forward4(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001338 {
1339 Int4 flip = CmpNLT(dot4(Nref, I), Float4(0.0f)) & Int4(0x80000000);
1340
1341 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1342 dst.y = As<Float4>(flip ^ As<Int4>(N.y));
1343 dst.z = As<Float4>(flip ^ As<Int4>(N.z));
1344 dst.w = As<Float4>(flip ^ As<Int4>(N.w));
1345 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001346
Alexis Hetuecad5192015-06-05 13:42:05 -04001347 void ShaderCore::reflect1(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001348 {
1349 Float4 d = N.x * I.x;
1350
1351 dst.x = I.x - Float4(2.0f) * d * N.x;
1352 }
1353
Alexis Hetuecad5192015-06-05 13:42:05 -04001354 void ShaderCore::reflect2(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001355 {
1356 Float4 d = dot2(N, I);
1357
1358 dst.x = I.x - Float4(2.0f) * d * N.x;
1359 dst.y = I.y - Float4(2.0f) * d * N.y;
1360 }
1361
Alexis Hetuecad5192015-06-05 13:42:05 -04001362 void ShaderCore::reflect3(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001363 {
1364 Float4 d = dot3(N, I);
1365
1366 dst.x = I.x - Float4(2.0f) * d * N.x;
1367 dst.y = I.y - Float4(2.0f) * d * N.y;
1368 dst.z = I.z - Float4(2.0f) * d * N.z;
1369 }
1370
Alexis Hetuecad5192015-06-05 13:42:05 -04001371 void ShaderCore::reflect4(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001372 {
1373 Float4 d = dot4(N, I);
1374
1375 dst.x = I.x - Float4(2.0f) * d * N.x;
1376 dst.y = I.y - Float4(2.0f) * d * N.y;
1377 dst.z = I.z - Float4(2.0f) * d * N.z;
1378 dst.w = I.w - Float4(2.0f) * d * N.w;
1379 }
1380
Alexis Hetuecad5192015-06-05 13:42:05 -04001381 void ShaderCore::refract1(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001382 {
1383 Float4 d = N.x * I.x;
1384 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1385 Int4 pos = CmpNLT(k, Float4(0.0f));
1386 Float4 t = (eta * d + Sqrt(k));
1387
1388 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1389 }
1390
Alexis Hetuecad5192015-06-05 13:42:05 -04001391 void ShaderCore::refract2(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001392 {
1393 Float4 d = dot2(N, I);
1394 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1395 Int4 pos = CmpNLT(k, Float4(0.0f));
1396 Float4 t = (eta * d + Sqrt(k));
1397
1398 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1399 dst.y = As<Float4>(pos & As<Int4>(eta * I.y - t * N.y));
1400 }
1401
Alexis Hetuecad5192015-06-05 13:42:05 -04001402 void ShaderCore::refract3(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001403 {
1404 Float4 d = dot3(N, I);
1405 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1406 Int4 pos = CmpNLT(k, Float4(0.0f));
1407 Float4 t = (eta * d + Sqrt(k));
1408
1409 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1410 dst.y = As<Float4>(pos & As<Int4>(eta * I.y - t * N.y));
1411 dst.z = As<Float4>(pos & As<Int4>(eta * I.z - t * N.z));
1412 }
1413
Alexis Hetuecad5192015-06-05 13:42:05 -04001414 void ShaderCore::refract4(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001415 {
1416 Float4 d = dot4(N, I);
1417 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1418 Int4 pos = CmpNLT(k, Float4(0.0f));
1419 Float4 t = (eta * d + Sqrt(k));
1420
1421 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1422 dst.y = As<Float4>(pos & As<Int4>(eta * I.y - t * N.y));
1423 dst.z = As<Float4>(pos & As<Int4>(eta * I.z - t * N.z));
1424 dst.w = As<Float4>(pos & As<Int4>(eta * I.w - t * N.w));
1425 }
1426
Alexis Hetuecad5192015-06-05 13:42:05 -04001427 void ShaderCore::sgn(Vector4f &dst, const Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04001428 {
1429 sgn(dst.x, src.x);
1430 sgn(dst.y, src.y);
1431 sgn(dst.z, src.z);
1432 sgn(dst.w, src.w);
1433 }
1434
Alexis Hetu0f448072016-03-18 10:56:08 -04001435 void ShaderCore::isgn(Vector4f &dst, const Vector4f &src)
1436 {
1437 isgn(dst.x, src.x);
1438 isgn(dst.y, src.y);
1439 isgn(dst.z, src.z);
1440 isgn(dst.w, src.w);
1441 }
1442
Alexis Hetuecad5192015-06-05 13:42:05 -04001443 void ShaderCore::abs(Vector4f &dst, const Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04001444 {
1445 dst.x = Abs(src.x);
1446 dst.y = Abs(src.y);
1447 dst.z = Abs(src.z);
1448 dst.w = Abs(src.w);
1449 }
Alexis Hetu0f448072016-03-18 10:56:08 -04001450
1451 void ShaderCore::iabs(Vector4f &dst, const Vector4f &src)
1452 {
1453 dst.x = As<Float4>(Abs(As<Int4>(src.x)));
1454 dst.y = As<Float4>(Abs(As<Int4>(src.y)));
1455 dst.z = As<Float4>(Abs(As<Int4>(src.z)));
1456 dst.w = As<Float4>(Abs(As<Int4>(src.w)));
1457 }
1458
Alexis Hetuecad5192015-06-05 13:42:05 -04001459 void ShaderCore::nrm2(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001460 {
1461 Float4 dot = dot2(src, src);
1462 Float4 rsq = reciprocalSquareRoot(dot, false, pp);
John Bauman89401822014-05-06 15:04:28 -04001463
John Bauman19bac1e2014-05-06 15:23:49 -04001464 dst.x = src.x * rsq;
1465 dst.y = src.y * rsq;
1466 dst.z = src.z * rsq;
1467 dst.w = src.w * rsq;
1468 }
1469
Alexis Hetuecad5192015-06-05 13:42:05 -04001470 void ShaderCore::nrm3(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001471 {
1472 Float4 dot = dot3(src, src);
1473 Float4 rsq = reciprocalSquareRoot(dot, false, pp);
1474
1475 dst.x = src.x * rsq;
1476 dst.y = src.y * rsq;
1477 dst.z = src.z * rsq;
1478 dst.w = src.w * rsq;
1479 }
John Bauman19bac1e2014-05-06 15:23:49 -04001480
Alexis Hetuecad5192015-06-05 13:42:05 -04001481 void ShaderCore::nrm4(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001482 {
John Bauman19bac1e2014-05-06 15:23:49 -04001483 Float4 dot = dot4(src, src);
1484 Float4 rsq = reciprocalSquareRoot(dot, false, pp);
John Bauman89401822014-05-06 15:04:28 -04001485
John Bauman19bac1e2014-05-06 15:23:49 -04001486 dst.x = src.x * rsq;
1487 dst.y = src.y * rsq;
1488 dst.z = src.z * rsq;
1489 dst.w = src.w * rsq;
1490 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001491
Alexis Hetuecad5192015-06-05 13:42:05 -04001492 void ShaderCore::sincos(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001493 {
1494 dst.x = cosine_pi(src.x, pp);
1495 dst.y = sine_pi(src.x, pp);
John Bauman89401822014-05-06 15:04:28 -04001496 }
1497
Alexis Hetuecad5192015-06-05 13:42:05 -04001498 void ShaderCore::cos(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001499 {
1500 dst.x = cosine(src.x, pp);
1501 dst.y = cosine(src.y, pp);
1502 dst.z = cosine(src.z, pp);
1503 dst.w = cosine(src.w, pp);
1504 }
1505
Alexis Hetuecad5192015-06-05 13:42:05 -04001506 void ShaderCore::sin(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001507 {
1508 dst.x = sine(src.x, pp);
1509 dst.y = sine(src.y, pp);
1510 dst.z = sine(src.z, pp);
1511 dst.w = sine(src.w, pp);
1512 }
1513
Alexis Hetuecad5192015-06-05 13:42:05 -04001514 void ShaderCore::tan(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001515 {
1516 dst.x = tangent(src.x, pp);
1517 dst.y = tangent(src.y, pp);
1518 dst.z = tangent(src.z, pp);
1519 dst.w = tangent(src.w, pp);
1520 }
1521
Alexis Hetuecad5192015-06-05 13:42:05 -04001522 void ShaderCore::acos(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001523 {
1524 dst.x = arccos(src.x, pp);
1525 dst.y = arccos(src.y, pp);
1526 dst.z = arccos(src.z, pp);
1527 dst.w = arccos(src.w, pp);
1528 }
1529
Alexis Hetuecad5192015-06-05 13:42:05 -04001530 void ShaderCore::asin(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001531 {
1532 dst.x = arcsin(src.x, pp);
1533 dst.y = arcsin(src.y, pp);
1534 dst.z = arcsin(src.z, pp);
1535 dst.w = arcsin(src.w, pp);
1536 }
1537
Alexis Hetuecad5192015-06-05 13:42:05 -04001538 void ShaderCore::atan(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001539 {
1540 dst.x = arctan(src.x, pp);
1541 dst.y = arctan(src.y, pp);
1542 dst.z = arctan(src.z, pp);
1543 dst.w = arctan(src.w, pp);
1544 }
1545
Alexis Hetuecad5192015-06-05 13:42:05 -04001546 void ShaderCore::atan2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001547 {
1548 dst.x = arctan(src0.x, src1.x, pp);
1549 dst.y = arctan(src0.y, src1.y, pp);
1550 dst.z = arctan(src0.z, src1.z, pp);
1551 dst.w = arctan(src0.w, src1.w, pp);
1552 }
1553
Alexis Hetuecad5192015-06-05 13:42:05 -04001554 void ShaderCore::cosh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001555 {
1556 dst.x = cosineh(src.x, pp);
1557 dst.y = cosineh(src.y, pp);
1558 dst.z = cosineh(src.z, pp);
1559 dst.w = cosineh(src.w, pp);
1560 }
1561
Alexis Hetuecad5192015-06-05 13:42:05 -04001562 void ShaderCore::sinh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001563 {
1564 dst.x = sineh(src.x, pp);
1565 dst.y = sineh(src.y, pp);
1566 dst.z = sineh(src.z, pp);
1567 dst.w = sineh(src.w, pp);
1568 }
1569
Alexis Hetuecad5192015-06-05 13:42:05 -04001570 void ShaderCore::tanh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001571 {
1572 dst.x = tangenth(src.x, pp);
1573 dst.y = tangenth(src.y, pp);
1574 dst.z = tangenth(src.z, pp);
1575 dst.w = tangenth(src.w, pp);
1576 }
1577
Alexis Hetuecad5192015-06-05 13:42:05 -04001578 void ShaderCore::acosh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001579 {
1580 dst.x = arccosh(src.x, pp);
1581 dst.y = arccosh(src.y, pp);
1582 dst.z = arccosh(src.z, pp);
1583 dst.w = arccosh(src.w, pp);
1584 }
1585
Alexis Hetuecad5192015-06-05 13:42:05 -04001586 void ShaderCore::asinh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001587 {
1588 dst.x = arcsinh(src.x, pp);
1589 dst.y = arcsinh(src.y, pp);
1590 dst.z = arcsinh(src.z, pp);
1591 dst.w = arcsinh(src.w, pp);
1592 }
1593
Alexis Hetuecad5192015-06-05 13:42:05 -04001594 void ShaderCore::atanh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001595 {
1596 dst.x = arctanh(src.x, pp);
1597 dst.y = arctanh(src.y, pp);
1598 dst.z = arctanh(src.z, pp);
1599 dst.w = arctanh(src.w, pp);
1600 }
1601
Alexis Hetu53ad4af2017-12-06 14:49:07 -05001602 void ShaderCore::expp(Vector4f &dst, const Vector4f &src, unsigned short shaderModel)
John Bauman89401822014-05-06 15:04:28 -04001603 {
Alexis Hetu53ad4af2017-12-06 14:49:07 -05001604 if(shaderModel < 0x0200)
John Bauman89401822014-05-06 15:04:28 -04001605 {
John Bauman19bac1e2014-05-06 15:23:49 -04001606 Float4 frc = Frac(src.x);
John Bauman89401822014-05-06 15:04:28 -04001607 Float4 floor = src.x - frc;
1608
John Bauman19bac1e2014-05-06 15:23:49 -04001609 dst.x = exponential2(floor, true);
John Bauman89401822014-05-06 15:04:28 -04001610 dst.y = frc;
John Bauman19bac1e2014-05-06 15:23:49 -04001611 dst.z = exponential2(src.x, true);
1612 dst.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04001613 }
1614 else // Version >= 2.0
1615 {
John Bauman19bac1e2014-05-06 15:23:49 -04001616 exp2x(dst, src, true); // FIXME: 10-bit precision suffices
John Bauman89401822014-05-06 15:04:28 -04001617 }
1618 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001619
Alexis Hetu53ad4af2017-12-06 14:49:07 -05001620 void ShaderCore::logp(Vector4f &dst, const Vector4f &src, unsigned short shaderModel)
John Bauman89401822014-05-06 15:04:28 -04001621 {
Alexis Hetu53ad4af2017-12-06 14:49:07 -05001622 if(shaderModel < 0x0200)
John Bauman89401822014-05-06 15:04:28 -04001623 {
1624 Float4 tmp0;
1625 Float4 tmp1;
1626 Float4 t;
1627 Int4 r;
1628
1629 tmp0 = Abs(src.x);
1630 tmp1 = tmp0;
1631
1632 // X component
John Bauman19bac1e2014-05-06 15:23:49 -04001633 r = As<Int4>(As<UInt4>(tmp0) >> 23) - Int4(127);
John Bauman89401822014-05-06 15:04:28 -04001634 dst.x = Float4(r);
1635
1636 // Y component
1637 dst.y = As<Float4>((As<Int4>(tmp1) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
1638
1639 // Z component
John Bauman19bac1e2014-05-06 15:23:49 -04001640 dst.z = logarithm2(src.x, true, true);
John Bauman89401822014-05-06 15:04:28 -04001641
1642 // W component
1643 dst.w = 1.0f;
1644 }
1645 else
1646 {
John Bauman19bac1e2014-05-06 15:23:49 -04001647 log2x(dst, src, true);
John Bauman89401822014-05-06 15:04:28 -04001648 }
1649 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001650
Alexis Hetuecad5192015-06-05 13:42:05 -04001651 void ShaderCore::cmp0(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman89401822014-05-06 15:04:28 -04001652 {
John Bauman19bac1e2014-05-06 15:23:49 -04001653 cmp0(dst.x, src0.x, src1.x, src2.x);
1654 cmp0(dst.y, src0.y, src1.y, src2.y);
1655 cmp0(dst.z, src0.z, src1.z, src2.z);
1656 cmp0(dst.w, src0.w, src1.w, src2.w);
John Bauman89401822014-05-06 15:04:28 -04001657 }
John Bauman89401822014-05-06 15:04:28 -04001658
Alexis Hetuecad5192015-06-05 13:42:05 -04001659 void ShaderCore::select(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman19bac1e2014-05-06 15:23:49 -04001660 {
1661 select(dst.x, As<Int4>(src0.x), src1.x, src2.x);
1662 select(dst.y, As<Int4>(src0.y), src1.y, src2.y);
1663 select(dst.z, As<Int4>(src0.z), src1.z, src2.z);
1664 select(dst.w, As<Int4>(src0.w), src1.w, src2.w);
1665 }
1666
Alexis Hetuecad5192015-06-05 13:42:05 -04001667 void ShaderCore::extract(Float4 &dst, const Vector4f &src0, const Float4 &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001668 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001669 select(dst, CmpEQ(As<Int4>(src1), Int4(1)), src0.y, src0.x);
1670 select(dst, CmpEQ(As<Int4>(src1), Int4(2)), src0.z, dst);
1671 select(dst, CmpEQ(As<Int4>(src1), Int4(3)), src0.w, dst);
John Bauman19bac1e2014-05-06 15:23:49 -04001672 }
1673
Alexis Hetuecad5192015-06-05 13:42:05 -04001674 void ShaderCore::insert(Vector4f &dst, const Vector4f &src, const Float4 &element, const Float4 &index)
John Bauman19bac1e2014-05-06 15:23:49 -04001675 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001676 select(dst.x, CmpEQ(As<Int4>(index), Int4(0)), element, src.x);
1677 select(dst.y, CmpEQ(As<Int4>(index), Int4(1)), element, src.y);
1678 select(dst.z, CmpEQ(As<Int4>(index), Int4(2)), element, src.z);
1679 select(dst.w, CmpEQ(As<Int4>(index), Int4(3)), element, src.w);
John Bauman89401822014-05-06 15:04:28 -04001680 }
1681
Alexis Hetuecad5192015-06-05 13:42:05 -04001682 void ShaderCore::sgn(Float4 &dst, const Float4 &src)
John Bauman89401822014-05-06 15:04:28 -04001683 {
John Bauman19bac1e2014-05-06 15:23:49 -04001684 Int4 neg = As<Int4>(CmpLT(src, Float4(-0.0f))) & As<Int4>(Float4(-1.0f));
1685 Int4 pos = As<Int4>(CmpNLE(src, Float4(+0.0f))) & As<Int4>(Float4(1.0f));
John Bauman89401822014-05-06 15:04:28 -04001686 dst = As<Float4>(neg | pos);
1687 }
1688
Alexis Hetu0f448072016-03-18 10:56:08 -04001689 void ShaderCore::isgn(Float4 &dst, const Float4 &src)
1690 {
1691 Int4 neg = CmpLT(As<Int4>(src), Int4(0)) & Int4(-1);
1692 Int4 pos = CmpNLE(As<Int4>(src), Int4(0)) & Int4(1);
1693 dst = As<Float4>(neg | pos);
1694 }
1695
Alexis Hetuecad5192015-06-05 13:42:05 -04001696 void ShaderCore::cmp0(Float4 &dst, const Float4 &src0, const Float4 &src1, const Float4 &src2)
John Bauman89401822014-05-06 15:04:28 -04001697 {
John Bauman19bac1e2014-05-06 15:23:49 -04001698 Int4 pos = CmpLE(Float4(0.0f), src0);
1699 select(dst, pos, src1, src2);
John Bauman89401822014-05-06 15:04:28 -04001700 }
1701
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001702 void ShaderCore::cmp0i(Float4 &dst, const Float4 &src0, const Float4 &src1, const Float4 &src2)
1703 {
1704 Int4 pos = CmpEQ(Int4(0), As<Int4>(src0));
1705 select(dst, pos, src1, src2);
1706 }
1707
Alexis Hetuecad5192015-06-05 13:42:05 -04001708 void ShaderCore::select(Float4 &dst, RValue<Int4> src0, const Float4 &src1, const Float4 &src2)
John Bauman19bac1e2014-05-06 15:23:49 -04001709 {
1710 // FIXME: LLVM vector select
Tom Anderson69bc6e82017-03-20 11:54:29 -07001711 dst = As<Float4>((src0 & As<Int4>(src1)) | (~src0 & As<Int4>(src2)));
John Bauman19bac1e2014-05-06 15:23:49 -04001712 }
1713
Alexis Hetuecad5192015-06-05 13:42:05 -04001714 void ShaderCore::cmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001715 {
1716 switch(control)
1717 {
John Bauman19bac1e2014-05-06 15:23:49 -04001718 case Shader::CONTROL_GT:
John Bauman89401822014-05-06 15:04:28 -04001719 dst.x = As<Float4>(CmpNLE(src0.x, src1.x));
1720 dst.y = As<Float4>(CmpNLE(src0.y, src1.y));
1721 dst.z = As<Float4>(CmpNLE(src0.z, src1.z));
1722 dst.w = As<Float4>(CmpNLE(src0.w, src1.w));
1723 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001724 case Shader::CONTROL_EQ:
John Bauman89401822014-05-06 15:04:28 -04001725 dst.x = As<Float4>(CmpEQ(src0.x, src1.x));
1726 dst.y = As<Float4>(CmpEQ(src0.y, src1.y));
1727 dst.z = As<Float4>(CmpEQ(src0.z, src1.z));
1728 dst.w = As<Float4>(CmpEQ(src0.w, src1.w));
1729 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001730 case Shader::CONTROL_GE:
John Bauman89401822014-05-06 15:04:28 -04001731 dst.x = As<Float4>(CmpNLT(src0.x, src1.x));
1732 dst.y = As<Float4>(CmpNLT(src0.y, src1.y));
1733 dst.z = As<Float4>(CmpNLT(src0.z, src1.z));
1734 dst.w = As<Float4>(CmpNLT(src0.w, src1.w));
1735 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001736 case Shader::CONTROL_LT:
John Bauman89401822014-05-06 15:04:28 -04001737 dst.x = As<Float4>(CmpLT(src0.x, src1.x));
1738 dst.y = As<Float4>(CmpLT(src0.y, src1.y));
1739 dst.z = As<Float4>(CmpLT(src0.z, src1.z));
1740 dst.w = As<Float4>(CmpLT(src0.w, src1.w));
1741 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001742 case Shader::CONTROL_NE:
John Bauman89401822014-05-06 15:04:28 -04001743 dst.x = As<Float4>(CmpNEQ(src0.x, src1.x));
1744 dst.y = As<Float4>(CmpNEQ(src0.y, src1.y));
1745 dst.z = As<Float4>(CmpNEQ(src0.z, src1.z));
1746 dst.w = As<Float4>(CmpNEQ(src0.w, src1.w));
1747 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001748 case Shader::CONTROL_LE:
John Bauman89401822014-05-06 15:04:28 -04001749 dst.x = As<Float4>(CmpLE(src0.x, src1.x));
1750 dst.y = As<Float4>(CmpLE(src0.y, src1.y));
1751 dst.z = As<Float4>(CmpLE(src0.z, src1.z));
1752 dst.w = As<Float4>(CmpLE(src0.w, src1.w));
1753 break;
1754 default:
1755 ASSERT(false);
1756 }
1757 }
John Bauman19bac1e2014-05-06 15:23:49 -04001758
Alexis Hetuecad5192015-06-05 13:42:05 -04001759 void ShaderCore::icmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control)
John Bauman19bac1e2014-05-06 15:23:49 -04001760 {
1761 switch(control)
1762 {
1763 case Shader::CONTROL_GT:
1764 dst.x = As<Float4>(CmpNLE(As<Int4>(src0.x), As<Int4>(src1.x)));
1765 dst.y = As<Float4>(CmpNLE(As<Int4>(src0.y), As<Int4>(src1.y)));
1766 dst.z = As<Float4>(CmpNLE(As<Int4>(src0.z), As<Int4>(src1.z)));
1767 dst.w = As<Float4>(CmpNLE(As<Int4>(src0.w), As<Int4>(src1.w)));
1768 break;
1769 case Shader::CONTROL_EQ:
1770 dst.x = As<Float4>(CmpEQ(As<Int4>(src0.x), As<Int4>(src1.x)));
1771 dst.y = As<Float4>(CmpEQ(As<Int4>(src0.y), As<Int4>(src1.y)));
1772 dst.z = As<Float4>(CmpEQ(As<Int4>(src0.z), As<Int4>(src1.z)));
1773 dst.w = As<Float4>(CmpEQ(As<Int4>(src0.w), As<Int4>(src1.w)));
1774 break;
1775 case Shader::CONTROL_GE:
1776 dst.x = As<Float4>(CmpNLT(As<Int4>(src0.x), As<Int4>(src1.x)));
1777 dst.y = As<Float4>(CmpNLT(As<Int4>(src0.y), As<Int4>(src1.y)));
1778 dst.z = As<Float4>(CmpNLT(As<Int4>(src0.z), As<Int4>(src1.z)));
1779 dst.w = As<Float4>(CmpNLT(As<Int4>(src0.w), As<Int4>(src1.w)));
1780 break;
1781 case Shader::CONTROL_LT:
1782 dst.x = As<Float4>(CmpLT(As<Int4>(src0.x), As<Int4>(src1.x)));
1783 dst.y = As<Float4>(CmpLT(As<Int4>(src0.y), As<Int4>(src1.y)));
1784 dst.z = As<Float4>(CmpLT(As<Int4>(src0.z), As<Int4>(src1.z)));
1785 dst.w = As<Float4>(CmpLT(As<Int4>(src0.w), As<Int4>(src1.w)));
1786 break;
1787 case Shader::CONTROL_NE:
1788 dst.x = As<Float4>(CmpNEQ(As<Int4>(src0.x), As<Int4>(src1.x)));
1789 dst.y = As<Float4>(CmpNEQ(As<Int4>(src0.y), As<Int4>(src1.y)));
1790 dst.z = As<Float4>(CmpNEQ(As<Int4>(src0.z), As<Int4>(src1.z)));
1791 dst.w = As<Float4>(CmpNEQ(As<Int4>(src0.w), As<Int4>(src1.w)));
1792 break;
1793 case Shader::CONTROL_LE:
1794 dst.x = As<Float4>(CmpLE(As<Int4>(src0.x), As<Int4>(src1.x)));
1795 dst.y = As<Float4>(CmpLE(As<Int4>(src0.y), As<Int4>(src1.y)));
1796 dst.z = As<Float4>(CmpLE(As<Int4>(src0.z), As<Int4>(src1.z)));
1797 dst.w = As<Float4>(CmpLE(As<Int4>(src0.w), As<Int4>(src1.w)));
1798 break;
1799 default:
1800 ASSERT(false);
1801 }
1802 }
1803
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001804 void ShaderCore::ucmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control)
1805 {
1806 switch(control)
1807 {
1808 case Shader::CONTROL_GT:
1809 dst.x = As<Float4>(CmpNLE(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1810 dst.y = As<Float4>(CmpNLE(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1811 dst.z = As<Float4>(CmpNLE(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1812 dst.w = As<Float4>(CmpNLE(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1813 break;
1814 case Shader::CONTROL_EQ:
1815 dst.x = As<Float4>(CmpEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1816 dst.y = As<Float4>(CmpEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1817 dst.z = As<Float4>(CmpEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1818 dst.w = As<Float4>(CmpEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1819 break;
1820 case Shader::CONTROL_GE:
1821 dst.x = As<Float4>(CmpNLT(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1822 dst.y = As<Float4>(CmpNLT(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1823 dst.z = As<Float4>(CmpNLT(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1824 dst.w = As<Float4>(CmpNLT(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1825 break;
1826 case Shader::CONTROL_LT:
1827 dst.x = As<Float4>(CmpLT(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1828 dst.y = As<Float4>(CmpLT(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1829 dst.z = As<Float4>(CmpLT(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1830 dst.w = As<Float4>(CmpLT(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1831 break;
1832 case Shader::CONTROL_NE:
1833 dst.x = As<Float4>(CmpNEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1834 dst.y = As<Float4>(CmpNEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1835 dst.z = As<Float4>(CmpNEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1836 dst.w = As<Float4>(CmpNEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1837 break;
1838 case Shader::CONTROL_LE:
1839 dst.x = As<Float4>(CmpLE(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1840 dst.y = As<Float4>(CmpLE(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1841 dst.z = As<Float4>(CmpLE(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1842 dst.w = As<Float4>(CmpLE(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1843 break;
1844 default:
1845 ASSERT(false);
1846 }
1847 }
1848
Alexis Hetuecad5192015-06-05 13:42:05 -04001849 void ShaderCore::all(Float4 &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001850 {
1851 dst = As<Float4>(As<Int4>(src.x) & As<Int4>(src.y) & As<Int4>(src.z) & As<Int4>(src.w));
1852 }
1853
Alexis Hetuecad5192015-06-05 13:42:05 -04001854 void ShaderCore::any(Float4 &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001855 {
1856 dst = As<Float4>(As<Int4>(src.x) | As<Int4>(src.y) | As<Int4>(src.z) | As<Int4>(src.w));
1857 }
1858
Alexis Hetu24f454e2016-08-31 17:22:13 -04001859 void ShaderCore::bitwise_not(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001860 {
1861 dst.x = As<Float4>(As<Int4>(src.x) ^ Int4(0xFFFFFFFF));
1862 dst.y = As<Float4>(As<Int4>(src.y) ^ Int4(0xFFFFFFFF));
1863 dst.z = As<Float4>(As<Int4>(src.z) ^ Int4(0xFFFFFFFF));
1864 dst.w = As<Float4>(As<Int4>(src.w) ^ Int4(0xFFFFFFFF));
1865 }
1866
Alexis Hetu24f454e2016-08-31 17:22:13 -04001867 void ShaderCore::bitwise_or(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001868 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001869 dst.x = As<Float4>(As<Int4>(src0.x) | As<Int4>(src1.x));
1870 dst.y = As<Float4>(As<Int4>(src0.y) | As<Int4>(src1.y));
1871 dst.z = As<Float4>(As<Int4>(src0.z) | As<Int4>(src1.z));
1872 dst.w = As<Float4>(As<Int4>(src0.w) | As<Int4>(src1.w));
John Bauman19bac1e2014-05-06 15:23:49 -04001873 }
1874
Alexis Hetu24f454e2016-08-31 17:22:13 -04001875 void ShaderCore::bitwise_xor(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001876 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001877 dst.x = As<Float4>(As<Int4>(src0.x) ^ As<Int4>(src1.x));
1878 dst.y = As<Float4>(As<Int4>(src0.y) ^ As<Int4>(src1.y));
1879 dst.z = As<Float4>(As<Int4>(src0.z) ^ As<Int4>(src1.z));
1880 dst.w = As<Float4>(As<Int4>(src0.w) ^ As<Int4>(src1.w));
John Bauman19bac1e2014-05-06 15:23:49 -04001881 }
1882
Alexis Hetu24f454e2016-08-31 17:22:13 -04001883 void ShaderCore::bitwise_and(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001884 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001885 dst.x = As<Float4>(As<Int4>(src0.x) & As<Int4>(src1.x));
1886 dst.y = As<Float4>(As<Int4>(src0.y) & As<Int4>(src1.y));
1887 dst.z = As<Float4>(As<Int4>(src0.z) & As<Int4>(src1.z));
1888 dst.w = As<Float4>(As<Int4>(src0.w) & As<Int4>(src1.w));
1889 }
1890
1891 void ShaderCore::equal(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
1892 {
1893 dst.x = As<Float4>(CmpEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)) &
1894 CmpEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)) &
1895 CmpEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)) &
1896 CmpEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1897 dst.y = dst.x;
1898 dst.z = dst.x;
1899 dst.w = dst.x;
1900 }
1901
1902 void ShaderCore::notEqual(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
1903 {
1904 dst.x = As<Float4>(CmpNEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)) |
1905 CmpNEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)) |
1906 CmpNEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)) |
1907 CmpNEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1908 dst.y = dst.x;
1909 dst.z = dst.x;
1910 dst.w = dst.x;
John Bauman19bac1e2014-05-06 15:23:49 -04001911 }
John Bauman89401822014-05-06 15:04:28 -04001912}