1 // Copyright 2020 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 "OptimalIntrinsics.hpp"
16 
17 namespace rr {
18 namespace {
Reciprocal(RValue<Float4> x,bool pp=false,bool finite=false,bool exactAtPow2=false)19 Float4 Reciprocal(RValue<Float4> x, bool pp = false, bool finite = false, bool exactAtPow2 = false)
20 {
21 	Float4 rcp = Rcp_pp(x, exactAtPow2);
22 
23 	if(!pp)
24 	{
25 		rcp = (rcp + rcp) - (x * rcp * rcp);
26 	}
27 
28 	if(finite)
29 	{
30 		int big = 0x7F7FFFFF;
31 		rcp = Min(rcp, Float4((float &)big));
32 	}
33 
34 	return rcp;
35 }
36 
SinOrCos(RValue<Float4> x,bool sin)37 Float4 SinOrCos(RValue<Float4> x, bool sin)
38 {
39 	// Reduce to [-0.5, 0.5] range
40 	Float4 y = x * Float4(1.59154943e-1f);  // 1/2pi
41 	y = y - Round(y);
42 
43 	// From the paper: "A Fast, Vectorizable Algorithm for Producing Single-Precision Sine-Cosine Pairs"
44 	// This implementation passes OpenGL ES 3.0 precision requirements, at the cost of more operations:
45 	// !pp : 17 mul, 7 add, 1 sub, 1 reciprocal
46 	//  pp : 4 mul, 2 add, 2 abs
47 
48 	Float4 y2 = y * y;
49 	Float4 c1 = y2 * (y2 * (y2 * Float4(-0.0204391631f) + Float4(0.2536086171f)) + Float4(-1.2336977925f)) + Float4(1.0f);
50 	Float4 s1 = y * (y2 * (y2 * (y2 * Float4(-0.0046075748f) + Float4(0.0796819754f)) + Float4(-0.645963615f)) + Float4(1.5707963235f));
51 	Float4 c2 = (c1 * c1) - (s1 * s1);
52 	Float4 s2 = Float4(2.0f) * s1 * c1;
53 	Float4 r = Reciprocal(s2 * s2 + c2 * c2, false, true, false);
54 
55 	if(sin)
56 	{
57 		return Float4(2.0f) * s2 * c2 * r;
58 	}
59 	else
60 	{
61 		return ((c2 * c2) - (s2 * s2)) * r;
62 	}
63 }
64 
65 // Approximation of atan in [0..1]
Atan_01(Float4 x)66 Float4 Atan_01(Float4 x)
67 {
68 	// From 4.4.49, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
69 	const Float4 a2(-0.3333314528f);
70 	const Float4 a4(0.1999355085f);
71 	const Float4 a6(-0.1420889944f);
72 	const Float4 a8(0.1065626393f);
73 	const Float4 a10(-0.0752896400f);
74 	const Float4 a12(0.0429096138f);
75 	const Float4 a14(-0.0161657367f);
76 	const Float4 a16(0.0028662257f);
77 	Float4 x2 = x * x;
78 	return (x + x * (x2 * (a2 + x2 * (a4 + x2 * (a6 + x2 * (a8 + x2 * (a10 + x2 * (a12 + x2 * (a14 + x2 * a16)))))))));
79 }
80 }  // namespace
81 
82 namespace optimal {
83 
Sin(RValue<Float4> x)84 Float4 Sin(RValue<Float4> x)
85 {
86 	return SinOrCos(x, true);
87 }
88 
Cos(RValue<Float4> x)89 Float4 Cos(RValue<Float4> x)
90 {
91 	return SinOrCos(x, false);
92 }
93 
Tan(RValue<Float4> x)94 Float4 Tan(RValue<Float4> x)
95 {
96 	return SinOrCos(x, true) / SinOrCos(x, false);
97 }
98 
Asin_4_terms(RValue<Float4> x)99 Float4 Asin_4_terms(RValue<Float4> x)
100 {
101 	// From 4.4.45, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
102 	// |e(x)| <= 5e-8
103 	const Float4 half_pi(1.57079632f);
104 	const Float4 a0(1.5707288f);
105 	const Float4 a1(-0.2121144f);
106 	const Float4 a2(0.0742610f);
107 	const Float4 a3(-0.0187293f);
108 	Float4 absx = Abs(x);
109 	return As<Float4>(As<Int4>(half_pi - Sqrt(Float4(1.0f) - absx) * (a0 + absx * (a1 + absx * (a2 + absx * a3)))) ^
110 	                  (As<Int4>(x) & Int4(0x80000000)));
111 }
112 
Asin_8_terms(RValue<Float4> x)113 Float4 Asin_8_terms(RValue<Float4> x)
114 {
115 	// From 4.4.46, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
116 	// |e(x)| <= 0e-8
117 	const Float4 half_pi(1.5707963268f);
118 	const Float4 a0(1.5707963050f);
119 	const Float4 a1(-0.2145988016f);
120 	const Float4 a2(0.0889789874f);
121 	const Float4 a3(-0.0501743046f);
122 	const Float4 a4(0.0308918810f);
123 	const Float4 a5(-0.0170881256f);
124 	const Float4 a6(0.006700901f);
125 	const Float4 a7(-0.0012624911f);
126 	Float4 absx = Abs(x);
127 	return As<Float4>(As<Int4>(half_pi - Sqrt(Float4(1.0f) - absx) * (a0 + absx * (a1 + absx * (a2 + absx * (a3 + absx * (a4 + absx * (a5 + absx * (a6 + absx * a7)))))))) ^
128 	                  (As<Int4>(x) & Int4(0x80000000)));
129 }
130 
Acos_4_terms(RValue<Float4> x)131 Float4 Acos_4_terms(RValue<Float4> x)
132 {
133 	// pi/2 - arcsin(x)
134 	return Float4(1.57079632e+0f) - Asin_4_terms(x);
135 }
136 
Acos_8_terms(RValue<Float4> x)137 Float4 Acos_8_terms(RValue<Float4> x)
138 {
139 	// pi/2 - arcsin(x)
140 	return Float4(1.57079632e+0f) - Asin_8_terms(x);
141 }
142 
Atan(RValue<Float4> x)143 Float4 Atan(RValue<Float4> x)
144 {
145 	Float4 absx = Abs(x);
146 	Int4 O = CmpNLT(absx, Float4(1.0f));
147 	Float4 y = As<Float4>((O & As<Int4>(Float4(1.0f) / absx)) | (~O & As<Int4>(absx)));  // FIXME: Vector select
148 
149 	const Float4 half_pi(1.57079632f);
150 	Float4 theta = Atan_01(y);
151 	return As<Float4>(((O & As<Int4>(half_pi - theta)) | (~O & As<Int4>(theta))) ^  // FIXME: Vector select
152 	                  (As<Int4>(x) & Int4(0x80000000)));
153 }
154 
Atan2(RValue<Float4> y,RValue<Float4> x)155 Float4 Atan2(RValue<Float4> y, RValue<Float4> x)
156 {
157 	const Float4 pi(3.14159265f);             // pi
158 	const Float4 minus_pi(-3.14159265f);      // -pi
159 	const Float4 half_pi(1.57079632f);        // pi/2
160 	const Float4 quarter_pi(7.85398163e-1f);  // pi/4
161 
162 	// Rotate to upper semicircle when in lower semicircle
163 	Int4 S = CmpLT(y, Float4(0.0f));
164 	Float4 theta = As<Float4>(S & As<Int4>(minus_pi));
165 	Float4 x0 = As<Float4>((As<Int4>(y) & Int4(0x80000000)) ^ As<Int4>(x));
166 	Float4 y0 = Abs(y);
167 
168 	// Rotate to right quadrant when in left quadrant
169 	Int4 Q = CmpLT(x0, Float4(0.0f));
170 	theta += As<Float4>(Q & As<Int4>(half_pi));
171 	Float4 x1 = As<Float4>((Q & As<Int4>(y0)) | (~Q & As<Int4>(x0)));   // FIXME: Vector select
172 	Float4 y1 = As<Float4>((Q & As<Int4>(-x0)) | (~Q & As<Int4>(y0)));  // FIXME: Vector select
173 
174 	// Mirror to first octant when in second octant
175 	Int4 O = CmpNLT(y1, x1);
176 	Float4 x2 = As<Float4>((O & As<Int4>(y1)) | (~O & As<Int4>(x1)));  // FIXME: Vector select
177 	Float4 y2 = As<Float4>((O & As<Int4>(x1)) | (~O & As<Int4>(y1)));  // FIXME: Vector select
178 
179 	// Approximation of atan in [0..1]
180 	Int4 zero_x = CmpEQ(x2, Float4(0.0f));
181 	Int4 inf_y = IsInf(y2);  // Since x2 >= y2, this means x2 == y2 == inf, so we use 45 degrees or pi/4
182 	Float4 atan2_theta = Atan_01(y2 / x2);
183 	theta += As<Float4>((~zero_x & ~inf_y & ((O & As<Int4>(half_pi - atan2_theta)) | (~O & (As<Int4>(atan2_theta))))) |  // FIXME: Vector select
184 	                    (inf_y & As<Int4>(quarter_pi)));
185 
186 	// Recover loss of precision for tiny theta angles
187 	// This combination results in (-pi + half_pi + half_pi - atan2_theta) which is equivalent to -atan2_theta
188 	Int4 precision_loss = S & Q & O & ~inf_y;
189 
190 	return As<Float4>((precision_loss & As<Int4>(-atan2_theta)) | (~precision_loss & As<Int4>(theta)));  // FIXME: Vector select
191 }
192 
Exp2(RValue<Float4> x)193 Float4 Exp2(RValue<Float4> x)
194 {
195 	// This implementation is based on 2^(i + f) = 2^i * 2^f,
196 	// where i is the integer part of x and f is the fraction.
197 
198 	// For 2^i we can put the integer part directly in the exponent of
199 	// the IEEE-754 floating-point number. Clamp to prevent overflow
200 	// past the representation of infinity.
201 	Float4 x0 = x;
202 	x0 = Min(x0, As<Float4>(Int4(0x43010000)));  // 129.00000e+0f
203 	x0 = Max(x0, As<Float4>(Int4(0xC2FDFFFF)));  // -126.99999e+0f
204 
205 	Int4 i = RoundInt(x0 - Float4(0.5f));
206 	Float4 ii = As<Float4>((i + Int4(127)) << 23);  // Add single-precision bias, and shift into exponent.
207 
208 	// For the fractional part use a polynomial
209 	// which approximates 2^f in the 0 to 1 range.
210 	Float4 f = x0 - Float4(i);
211 	Float4 ff = As<Float4>(Int4(0x3AF61905));    // 1.8775767e-3f
212 	ff = ff * f + As<Float4>(Int4(0x3C134806));  // 8.9893397e-3f
213 	ff = ff * f + As<Float4>(Int4(0x3D64AA23));  // 5.5826318e-2f
214 	ff = ff * f + As<Float4>(Int4(0x3E75EAD4));  // 2.4015361e-1f
215 	ff = ff * f + As<Float4>(Int4(0x3F31727B));  // 6.9315308e-1f
216 	ff = ff * f + Float4(1.0f);
217 
218 	return ii * ff;
219 }
220 
Log2(RValue<Float4> x)221 Float4 Log2(RValue<Float4> x)
222 {
223 	Float4 x0;
224 	Float4 x1;
225 	Float4 x2;
226 	Float4 x3;
227 
228 	x0 = x;
229 
230 	x1 = As<Float4>(As<Int4>(x0) & Int4(0x7F800000));
231 	x1 = As<Float4>(As<UInt4>(x1) >> 8);
232 	x1 = As<Float4>(As<Int4>(x1) | As<Int4>(Float4(1.0f)));
233 	x1 = (x1 - Float4(1.4960938f)) * Float4(256.0f);  // FIXME: (x1 - 1.4960938f) * 256.0f;
234 	x0 = As<Float4>((As<Int4>(x0) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
235 
236 	x2 = (Float4(9.5428179e-2f) * x0 + Float4(4.7779095e-1f)) * x0 + Float4(1.9782813e-1f);
237 	x3 = ((Float4(1.6618466e-2f) * x0 + Float4(2.0350508e-1f)) * x0 + Float4(2.7382900e-1f)) * x0 + Float4(4.0496687e-2f);
238 	x2 /= x3;
239 
240 	x1 += (x0 - Float4(1.0f)) * x2;
241 
242 	Int4 pos_inf_x = CmpEQ(As<Int4>(x), Int4(0x7F800000));
243 	return As<Float4>((pos_inf_x & As<Int4>(x)) | (~pos_inf_x & As<Int4>(x1)));
244 }
245 
Exp(RValue<Float4> x)246 Float4 Exp(RValue<Float4> x)
247 {
248 	// TODO: Propagate the constant
249 	return optimal::Exp2(Float4(1.44269504f) * x);  // 1/ln(2)
250 }
251 
Log(RValue<Float4> x)252 Float4 Log(RValue<Float4> x)
253 {
254 	// TODO: Propagate the constant
255 	return Float4(6.93147181e-1f) * optimal::Log2(x);  // ln(2)
256 }
257 
Pow(RValue<Float4> x,RValue<Float4> y)258 Float4 Pow(RValue<Float4> x, RValue<Float4> y)
259 {
260 	Float4 log = optimal::Log2(x);
261 	log *= y;
262 	return optimal::Exp2(log);
263 }
264 
Sinh(RValue<Float4> x)265 Float4 Sinh(RValue<Float4> x)
266 {
267 	return (optimal::Exp(x) - optimal::Exp(-x)) * Float4(0.5f);
268 }
269 
Cosh(RValue<Float4> x)270 Float4 Cosh(RValue<Float4> x)
271 {
272 	return (optimal::Exp(x) + optimal::Exp(-x)) * Float4(0.5f);
273 }
274 
Tanh(RValue<Float4> x)275 Float4 Tanh(RValue<Float4> x)
276 {
277 	Float4 e_x = optimal::Exp(x);
278 	Float4 e_minus_x = optimal::Exp(-x);
279 	return (e_x - e_minus_x) / (e_x + e_minus_x);
280 }
281 
Asinh(RValue<Float4> x)282 Float4 Asinh(RValue<Float4> x)
283 {
284 	return optimal::Log(x + Sqrt(x * x + Float4(1.0f)));
285 }
286 
Acosh(RValue<Float4> x)287 Float4 Acosh(RValue<Float4> x)
288 {
289 	return optimal::Log(x + Sqrt(x + Float4(1.0f)) * Sqrt(x - Float4(1.0f)));
290 }
291 
Atanh(RValue<Float4> x)292 Float4 Atanh(RValue<Float4> x)
293 {
294 	return optimal::Log((Float4(1.0f) + x) / (Float4(1.0f) - x)) * Float4(0.5f);
295 }
296 
297 }  // namespace optimal
298 }  // namespace rr