blob: 851bd499dbe09d8cd02a63920968c0959148b89a [file] [log] [blame]
Jamie Madillb1a85f42014-08-19 15:23:24 -04001//
2// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Build the intermediate representation.
9//
10
11#include <float.h>
12#include <limits.h>
Arun Patole9dea48f2015-04-02 11:45:09 +053013#include <math.h>
Arun Patole97dc22e2015-04-06 17:35:38 +053014#include <stdlib.h>
Jamie Madillb1a85f42014-08-19 15:23:24 -040015#include <algorithm>
Arun Patole274f0702015-05-05 13:33:30 +053016#include <vector>
Jamie Madillb1a85f42014-08-19 15:23:24 -040017
Arun Patole274f0702015-05-05 13:33:30 +053018#include "common/mathutil.h"
Arun Patole7fa33552015-06-10 15:15:18 +053019#include "common/matrix_utils.h"
Olli Etuaho3fdec912016-08-18 15:08:06 +030020#include "compiler/translator/Diagnostics.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040021#include "compiler/translator/IntermNode.h"
22#include "compiler/translator/SymbolTable.h"
Corentin Wallez509e4562016-08-25 14:55:44 -040023#include "compiler/translator/util.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040024
Jamie Madill45bcc782016-11-07 13:58:48 -050025namespace sh
26{
27
Jamie Madillb1a85f42014-08-19 15:23:24 -040028namespace
29{
30
Jamie Madilld7b1ab52016-12-12 14:42:19 -050031const float kPi = 3.14159265358979323846f;
Arun Patole9dea48f2015-04-02 11:45:09 +053032const float kDegreesToRadiansMultiplier = kPi / 180.0f;
33const float kRadiansToDegreesMultiplier = 180.0f / kPi;
34
Jamie Madillb1a85f42014-08-19 15:23:24 -040035TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
36{
37 return left > right ? left : right;
38}
39
Arun Patole274f0702015-05-05 13:33:30 +053040TConstantUnion *Vectorize(const TConstantUnion &constant, size_t size)
41{
42 TConstantUnion *constUnion = new TConstantUnion[size];
43 for (unsigned int i = 0; i < size; ++i)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050044 constUnion[i] = constant;
Arun Patole274f0702015-05-05 13:33:30 +053045
46 return constUnion;
47}
48
Olli Etuahof119a262016-08-19 15:54:22 +030049void UndefinedConstantFoldingError(const TSourceLoc &loc,
50 TOperator op,
51 TBasicType basicType,
52 TDiagnostics *diagnostics,
53 TConstantUnion *result)
Arun Patolebf790422015-05-18 17:53:04 +053054{
Olli Etuahof119a262016-08-19 15:54:22 +030055 diagnostics->warning(loc, "operation result is undefined for the values passed in",
Olli Etuaho4de340a2016-12-16 09:32:03 +000056 GetOperatorString(op));
Arun Patolebf790422015-05-18 17:53:04 +053057
58 switch (basicType)
59 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -050060 case EbtFloat:
61 result->setFConst(0.0f);
62 break;
63 case EbtInt:
64 result->setIConst(0);
65 break;
66 case EbtUInt:
67 result->setUConst(0u);
68 break;
69 case EbtBool:
70 result->setBConst(false);
71 break;
72 default:
73 break;
Arun Patolebf790422015-05-18 17:53:04 +053074 }
75}
76
Olli Etuaho5c0e0232015-11-11 15:55:59 +020077float VectorLength(const TConstantUnion *paramArray, size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053078{
79 float result = 0.0f;
80 for (size_t i = 0; i < paramArraySize; i++)
81 {
82 float f = paramArray[i].getFConst();
83 result += f * f;
84 }
85 return sqrtf(result);
86}
87
Olli Etuaho5c0e0232015-11-11 15:55:59 +020088float VectorDotProduct(const TConstantUnion *paramArray1,
89 const TConstantUnion *paramArray2,
90 size_t paramArraySize)
Arun Patole1155ddd2015-06-05 18:04:36 +053091{
92 float result = 0.0f;
93 for (size_t i = 0; i < paramArraySize; i++)
94 result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
95 return result;
96}
97
Olli Etuaho2768bc82017-12-12 11:51:48 +020098TIntermTyped *CreateFoldedNode(const TConstantUnion *constArray, const TIntermTyped *originalNode)
Olli Etuahob43846e2015-06-02 18:18:57 +030099{
Olli Etuaho2768bc82017-12-12 11:51:48 +0200100 ASSERT(constArray != nullptr);
101 // Note that we inherit whatever qualifier the folded node had. Nodes may be constant folded
102 // without being qualified as constant.
Olli Etuahob43846e2015-06-02 18:18:57 +0300103 TIntermTyped *folded = new TIntermConstantUnion(constArray, originalNode->getType());
Olli Etuahob43846e2015-06-02 18:18:57 +0300104 folded->setLine(originalNode->getLine());
105 return folded;
106}
107
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200108angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray,
109 const unsigned int &rows,
110 const unsigned int &cols)
Arun Patole7fa33552015-06-10 15:15:18 +0530111{
112 std::vector<float> elements;
113 for (size_t i = 0; i < rows * cols; i++)
114 elements.push_back(paramArray[i].getFConst());
115 // Transpose is used since the Matrix constructor expects arguments in row-major order,
Olli Etuahod5da5052016-08-29 13:16:55 +0300116 // whereas the paramArray is in column-major order. Rows/cols parameters are also flipped below
117 // so that the created matrix will have the expected dimensions after the transpose.
118 return angle::Matrix<float>(elements, cols, rows).transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530119}
120
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200121angle::Matrix<float> GetMatrix(const TConstantUnion *paramArray, const unsigned int &size)
Arun Patole7fa33552015-06-10 15:15:18 +0530122{
123 std::vector<float> elements;
124 for (size_t i = 0; i < size * size; i++)
125 elements.push_back(paramArray[i].getFConst());
126 // Transpose is used since the Matrix constructor expects arguments in row-major order,
127 // whereas the paramArray is in column-major order.
128 return angle::Matrix<float>(elements, size).transpose();
129}
130
131void SetUnionArrayFromMatrix(const angle::Matrix<float> &m, TConstantUnion *resultArray)
132{
133 // Transpose is used since the input Matrix is in row-major order,
134 // whereas the actual result should be in column-major order.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500135 angle::Matrix<float> result = m.transpose();
Arun Patole7fa33552015-06-10 15:15:18 +0530136 std::vector<float> resultElements = result.elements();
137 for (size_t i = 0; i < resultElements.size(); i++)
138 resultArray[i].setFConst(resultElements[i]);
139}
140
Olli Etuaho765924f2018-01-04 12:48:36 +0200141bool CanFoldAggregateBuiltInOp(TOperator op)
142{
143 switch (op)
144 {
145 case EOpAtan:
146 case EOpPow:
147 case EOpMod:
148 case EOpMin:
149 case EOpMax:
150 case EOpClamp:
151 case EOpMix:
152 case EOpStep:
153 case EOpSmoothStep:
154 case EOpLdexp:
155 case EOpMulMatrixComponentWise:
156 case EOpOuterProduct:
157 case EOpEqualComponentWise:
158 case EOpNotEqualComponentWise:
159 case EOpLessThanComponentWise:
160 case EOpLessThanEqualComponentWise:
161 case EOpGreaterThanComponentWise:
162 case EOpGreaterThanEqualComponentWise:
163 case EOpDistance:
164 case EOpDot:
165 case EOpCross:
166 case EOpFaceforward:
167 case EOpReflect:
168 case EOpRefract:
169 case EOpBitfieldExtract:
170 case EOpBitfieldInsert:
171 return true;
172 default:
173 return false;
174 }
175}
176
Jamie Madillb1a85f42014-08-19 15:23:24 -0400177} // namespace anonymous
178
Jamie Madillb1a85f42014-08-19 15:23:24 -0400179////////////////////////////////////////////////////////////////
180//
181// Member functions of the nodes used for building the tree.
182//
183////////////////////////////////////////////////////////////////
184
Olli Etuahod2a67b92014-10-21 16:42:57 +0300185void TIntermTyped::setTypePreservePrecision(const TType &t)
186{
187 TPrecision precision = getPrecision();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500188 mType = t;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300189 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
190 mType.setPrecision(precision);
191}
192
Jamie Madillb1a85f42014-08-19 15:23:24 -0400193#define REPLACE_IF_IS(node, type, original, replacement) \
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500194 if (node == original) \
195 { \
196 node = static_cast<type *>(replacement); \
197 return true; \
Jamie Madillb1a85f42014-08-19 15:23:24 -0400198 }
199
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500200bool TIntermLoop::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400201{
Olli Etuaho3cbb27a2016-07-14 11:55:48 +0300202 ASSERT(original != nullptr); // This risks replacing multiple children.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400203 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
204 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
205 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100206 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400207 return false;
208}
209
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500210bool TIntermBranch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400211{
212 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
213 return false;
214}
215
Olli Etuahob6fa0432016-09-28 16:28:05 +0100216bool TIntermSwizzle::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
217{
218 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
219 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
220 return false;
221}
222
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500223bool TIntermBinary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400224{
225 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
226 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
227 return false;
228}
229
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500230bool TIntermUnary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400231{
Olli Etuahoa2234302016-08-31 12:05:39 +0300232 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400233 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
234 return false;
235}
236
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000237bool TIntermInvariantDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
238{
239 REPLACE_IF_IS(mSymbol, TIntermSymbol, original, replacement);
240 return false;
241}
242
Olli Etuaho336b1472016-10-05 16:37:55 +0100243bool TIntermFunctionDefinition::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
244{
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000245 REPLACE_IF_IS(mPrototype, TIntermFunctionPrototype, original, replacement);
Olli Etuaho336b1472016-10-05 16:37:55 +0100246 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
247 return false;
248}
249
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500250bool TIntermAggregate::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400251{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100252 return replaceChildNodeInternal(original, replacement);
253}
254
255bool TIntermBlock::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
256{
257 return replaceChildNodeInternal(original, replacement);
258}
259
Olli Etuaho16c745a2017-01-16 17:02:27 +0000260bool TIntermFunctionPrototype::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
261{
262 return replaceChildNodeInternal(original, replacement);
263}
264
Olli Etuaho13389b62016-10-16 11:48:18 +0100265bool TIntermDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
266{
267 return replaceChildNodeInternal(original, replacement);
268}
269
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100270bool TIntermAggregateBase::replaceChildNodeInternal(TIntermNode *original, TIntermNode *replacement)
271{
272 for (size_t ii = 0; ii < getSequence()->size(); ++ii)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400273 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100274 REPLACE_IF_IS((*getSequence())[ii], TIntermNode, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400275 }
276 return false;
277}
278
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100279bool TIntermAggregateBase::replaceChildNodeWithMultiple(TIntermNode *original,
280 const TIntermSequence &replacements)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300281{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100282 for (auto it = getSequence()->begin(); it < getSequence()->end(); ++it)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300283 {
284 if (*it == original)
285 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100286 it = getSequence()->erase(it);
287 getSequence()->insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300288 return true;
289 }
290 }
291 return false;
292}
293
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100294bool TIntermAggregateBase::insertChildNodes(TIntermSequence::size_type position,
295 const TIntermSequence &insertions)
Olli Etuahoa6f22092015-05-08 18:31:10 +0300296{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100297 if (position > getSequence()->size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300298 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300299 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300300 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100301 auto it = getSequence()->begin() + position;
302 getSequence()->insert(it, insertions.begin(), insertions.end());
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300303 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300304}
305
Olli Etuaho195be942017-12-04 23:40:14 +0200306TIntermSymbol::TIntermSymbol(const TVariable *variable)
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200307 : TIntermTyped(variable->getType()), mVariable(variable)
Olli Etuaho195be942017-12-04 23:40:14 +0200308{
Olli Etuaho195be942017-12-04 23:40:14 +0200309}
310
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200311bool TIntermSymbol::hasConstantValue() const
312{
313 return variable().getConstPointer() != nullptr;
314}
315
316const TConstantUnion *TIntermSymbol::getConstantValue() const
317{
318 return variable().getConstPointer();
319}
320
Olli Etuahob6af22b2017-12-15 14:05:44 +0200321const TSymbolUniqueId &TIntermSymbol::uniqueId() const
322{
323 return mVariable->uniqueId();
324}
325
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200326const TString &TIntermSymbol::getName() const
327{
328 return mVariable->name();
329}
330
Olli Etuahofe486322017-03-21 09:30:54 +0000331TIntermAggregate *TIntermAggregate::CreateFunctionCall(const TFunction &func,
332 TIntermSequence *arguments)
333{
Olli Etuaho0c371002017-12-13 17:00:25 +0400334 return new TIntermAggregate(&func, func.getReturnType(), EOpCallFunctionInAST, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000335}
336
Olli Etuaho0c371002017-12-13 17:00:25 +0400337TIntermAggregate *TIntermAggregate::CreateRawFunctionCall(const TFunction &func,
338 TIntermSequence *arguments)
Olli Etuahofe486322017-03-21 09:30:54 +0000339{
Olli Etuaho0c371002017-12-13 17:00:25 +0400340 return new TIntermAggregate(&func, func.getReturnType(), EOpCallInternalRawFunction, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000341}
342
343TIntermAggregate *TIntermAggregate::CreateBuiltInFunctionCall(const TFunction &func,
344 TIntermSequence *arguments)
345{
346 TIntermAggregate *callNode =
Olli Etuaho0c371002017-12-13 17:00:25 +0400347 new TIntermAggregate(&func, func.getReturnType(), EOpCallBuiltInFunction, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000348 // Note that name needs to be set before texture function type is determined.
349 callNode->setBuiltInFunctionPrecision();
350 return callNode;
351}
352
353TIntermAggregate *TIntermAggregate::CreateConstructor(const TType &type,
Olli Etuahofe486322017-03-21 09:30:54 +0000354 TIntermSequence *arguments)
355{
Olli Etuaho0c371002017-12-13 17:00:25 +0400356 return new TIntermAggregate(nullptr, type, EOpConstruct, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000357}
358
359TIntermAggregate *TIntermAggregate::Create(const TType &type,
360 TOperator op,
361 TIntermSequence *arguments)
362{
Olli Etuahofe486322017-03-21 09:30:54 +0000363 ASSERT(op != EOpCallFunctionInAST); // Should use CreateFunctionCall
Olli Etuaho0c371002017-12-13 17:00:25 +0400364 ASSERT(op != EOpCallInternalRawFunction); // Should use CreateRawFunctionCall
Olli Etuahofe486322017-03-21 09:30:54 +0000365 ASSERT(op != EOpCallBuiltInFunction); // Should use CreateBuiltInFunctionCall
Olli Etuaho0c371002017-12-13 17:00:25 +0400366 ASSERT(op != EOpConstruct); // Should use CreateConstructor
367 return new TIntermAggregate(nullptr, type, op, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000368}
369
Olli Etuaho0c371002017-12-13 17:00:25 +0400370TIntermAggregate::TIntermAggregate(const TFunction *func,
371 const TType &type,
372 TOperator op,
373 TIntermSequence *arguments)
374 : TIntermOperator(op),
375 mUseEmulatedFunction(false),
376 mGotPrecisionFromChildren(false),
377 mFunction(func)
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800378{
379 if (arguments != nullptr)
380 {
381 mArguments.swap(*arguments);
382 }
Olli Etuaho1bb85282017-12-14 13:39:53 +0200383 ASSERT(mFunction == nullptr || mFunction->symbolType() != SymbolType::Empty);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800384 setTypePrecisionAndQualifier(type);
385}
386
387void TIntermAggregate::setTypePrecisionAndQualifier(const TType &type)
388{
389 setType(type);
390 mType.setQualifier(EvqTemporary);
391 if (!isFunctionCall())
392 {
393 if (isConstructor())
394 {
395 // Structs should not be precision qualified, the individual members may be.
396 // Built-in types on the other hand should be precision qualified.
Olli Etuaho8fab3202017-05-08 18:22:22 +0300397 if (getBasicType() != EbtStruct)
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800398 {
399 setPrecisionFromChildren();
400 }
401 }
402 else
403 {
404 setPrecisionForBuiltInOp();
405 }
406 if (areChildrenConstQualified())
407 {
408 mType.setQualifier(EvqConst);
409 }
410 }
411}
412
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200413bool TIntermAggregate::areChildrenConstQualified()
414{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800415 for (TIntermNode *&arg : mArguments)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200416 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800417 TIntermTyped *typedArg = arg->getAsTyped();
418 if (typedArg && typedArg->getQualifier() != EvqConst)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200419 {
420 return false;
421 }
422 }
423 return true;
424}
425
Olli Etuahod2a67b92014-10-21 16:42:57 +0300426void TIntermAggregate::setPrecisionFromChildren()
427{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300428 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300429 if (getBasicType() == EbtBool)
430 {
431 mType.setPrecision(EbpUndefined);
432 return;
433 }
434
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500435 TPrecision precision = EbpUndefined;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800436 TIntermSequence::iterator childIter = mArguments.begin();
437 while (childIter != mArguments.end())
Olli Etuahod2a67b92014-10-21 16:42:57 +0300438 {
439 TIntermTyped *typed = (*childIter)->getAsTyped();
440 if (typed)
441 precision = GetHigherPrecision(typed->getPrecision(), precision);
442 ++childIter;
443 }
444 mType.setPrecision(precision);
445}
446
Olli Etuaho9250cb22017-01-21 10:51:27 +0000447void TIntermAggregate::setPrecisionForBuiltInOp()
448{
449 ASSERT(!isConstructor());
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800450 ASSERT(!isFunctionCall());
Olli Etuaho9250cb22017-01-21 10:51:27 +0000451 if (!setPrecisionForSpecialBuiltInOp())
452 {
453 setPrecisionFromChildren();
454 }
455}
456
457bool TIntermAggregate::setPrecisionForSpecialBuiltInOp()
458{
459 switch (mOp)
460 {
461 case EOpBitfieldExtract:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800462 mType.setPrecision(mArguments[0]->getAsTyped()->getPrecision());
463 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000464 return true;
465 case EOpBitfieldInsert:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800466 mType.setPrecision(GetHigherPrecision(mArguments[0]->getAsTyped()->getPrecision(),
467 mArguments[1]->getAsTyped()->getPrecision()));
468 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000469 return true;
470 case EOpUaddCarry:
471 case EOpUsubBorrow:
472 mType.setPrecision(EbpHigh);
473 return true;
474 default:
475 return false;
476 }
477}
478
Olli Etuahod2a67b92014-10-21 16:42:57 +0300479void TIntermAggregate::setBuiltInFunctionPrecision()
480{
481 // All built-ins returning bool should be handled as ops, not functions.
482 ASSERT(getBasicType() != EbtBool);
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800483 ASSERT(mOp == EOpCallBuiltInFunction);
Olli Etuahod2a67b92014-10-21 16:42:57 +0300484
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800485 TPrecision precision = EbpUndefined;
486 for (TIntermNode *arg : mArguments)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300487 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800488 TIntermTyped *typed = arg->getAsTyped();
Olli Etuahod2a67b92014-10-21 16:42:57 +0300489 // ESSL spec section 8: texture functions get their precision from the sampler.
490 if (typed && IsSampler(typed->getBasicType()))
491 {
492 precision = typed->getPrecision();
493 break;
494 }
Olli Etuahod2a67b92014-10-21 16:42:57 +0300495 }
496 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
497 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuahobed35d72017-12-20 16:36:26 +0200498 if (mFunction->name().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300499 mType.setPrecision(EbpHigh);
500 else
501 mType.setPrecision(precision);
502}
503
Olli Etuahof2209f72017-04-01 12:45:55 +0300504TString TIntermAggregate::getSymbolTableMangledName() const
505{
506 ASSERT(!isConstructor());
507 switch (mOp)
508 {
509 case EOpCallInternalRawFunction:
510 case EOpCallBuiltInFunction:
511 case EOpCallFunctionInAST:
Olli Etuahobed35d72017-12-20 16:36:26 +0200512 return TFunction::GetMangledNameFromCall(mFunction->name(), mArguments);
Olli Etuahof2209f72017-04-01 12:45:55 +0300513 default:
514 TString opString = GetOperatorString(mOp);
515 return TFunction::GetMangledNameFromCall(opString, mArguments);
516 }
517}
518
Olli Etuaho0c371002017-12-13 17:00:25 +0400519const char *TIntermAggregate::functionName() const
520{
521 ASSERT(!isConstructor());
522 switch (mOp)
523 {
524 case EOpCallInternalRawFunction:
525 case EOpCallBuiltInFunction:
526 case EOpCallFunctionInAST:
Olli Etuahobed35d72017-12-20 16:36:26 +0200527 return mFunction->name().c_str();
Olli Etuaho0c371002017-12-13 17:00:25 +0400528 default:
529 return GetOperatorString(mOp);
530 }
531}
532
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200533bool TIntermAggregate::hasConstantValue() const
534{
535 if (!isConstructor())
536 {
537 return false;
538 }
539 for (TIntermNode *constructorArg : mArguments)
540 {
541 if (!constructorArg->getAsTyped()->hasConstantValue())
542 {
543 return false;
544 }
545 }
546 return true;
547}
548
549const TConstantUnion *TIntermAggregate::getConstantValue() const
550{
551 if (!hasConstantValue())
552 {
553 return nullptr;
554 }
555 ASSERT(isConstructor());
556 ASSERT(mArguments.size() > 0u);
557
558 TConstantUnion *constArray = nullptr;
559 if (isArray())
560 {
561 size_t elementSize = mArguments.front()->getAsTyped()->getType().getObjectSize();
562 constArray = new TConstantUnion[elementSize * getOutermostArraySize()];
563
564 size_t elementOffset = 0u;
565 for (TIntermNode *constructorArg : mArguments)
566 {
567 const TConstantUnion *elementConstArray =
568 constructorArg->getAsTyped()->getConstantValue();
569 ASSERT(elementConstArray);
570 size_t elementSizeBytes = sizeof(TConstantUnion) * elementSize;
571 memcpy(static_cast<void *>(&constArray[elementOffset]),
572 static_cast<const void *>(elementConstArray), elementSizeBytes);
573 elementOffset += elementSize;
574 }
575 return constArray;
576 }
577
578 size_t resultSize = getType().getObjectSize();
579 constArray = new TConstantUnion[resultSize];
580 TBasicType basicType = getBasicType();
581
582 size_t resultIndex = 0u;
583
584 if (mArguments.size() == 1u)
585 {
586 TIntermNode *argument = mArguments.front();
587 TIntermTyped *argumentTyped = argument->getAsTyped();
588 const TConstantUnion *argumentConstantValue = argumentTyped->getConstantValue();
589 // Check the special case of constructing a matrix diagonal from a single scalar,
590 // or a vector from a single scalar.
591 if (argumentTyped->getType().getObjectSize() == 1u)
592 {
593 if (isMatrix())
594 {
595 int resultCols = getType().getCols();
596 int resultRows = getType().getRows();
597 for (int col = 0; col < resultCols; ++col)
598 {
599 for (int row = 0; row < resultRows; ++row)
600 {
601 if (col == row)
602 {
603 constArray[resultIndex].cast(basicType, argumentConstantValue[0]);
604 }
605 else
606 {
607 constArray[resultIndex].setFConst(0.0f);
608 }
609 ++resultIndex;
610 }
611 }
612 }
613 else
614 {
615 while (resultIndex < resultSize)
616 {
617 constArray[resultIndex].cast(basicType, argumentConstantValue[0]);
618 ++resultIndex;
619 }
620 }
621 ASSERT(resultIndex == resultSize);
622 return constArray;
623 }
624 else if (isMatrix() && argumentTyped->isMatrix())
625 {
626 // The special case of constructing a matrix from a matrix.
627 int argumentCols = argumentTyped->getType().getCols();
628 int argumentRows = argumentTyped->getType().getRows();
629 int resultCols = getType().getCols();
630 int resultRows = getType().getRows();
631 for (int col = 0; col < resultCols; ++col)
632 {
633 for (int row = 0; row < resultRows; ++row)
634 {
635 if (col < argumentCols && row < argumentRows)
636 {
637 constArray[resultIndex].cast(
638 basicType, argumentConstantValue[col * argumentRows + row]);
639 }
640 else if (col == row)
641 {
642 constArray[resultIndex].setFConst(1.0f);
643 }
644 else
645 {
646 constArray[resultIndex].setFConst(0.0f);
647 }
648 ++resultIndex;
649 }
650 }
651 ASSERT(resultIndex == resultSize);
652 return constArray;
653 }
654 }
655
656 for (TIntermNode *argument : mArguments)
657 {
658 TIntermTyped *argumentTyped = argument->getAsTyped();
659 size_t argumentSize = argumentTyped->getType().getObjectSize();
660 const TConstantUnion *argumentConstantValue = argumentTyped->getConstantValue();
661 for (size_t i = 0u; i < argumentSize; ++i)
662 {
663 if (resultIndex >= resultSize)
664 break;
665 constArray[resultIndex].cast(basicType, argumentConstantValue[i]);
666 ++resultIndex;
667 }
668 }
669 ASSERT(resultIndex == resultSize);
670 return constArray;
671}
672
Olli Etuahoa22aa4e2017-05-24 18:17:23 +0300673bool TIntermAggregate::hasSideEffects() const
674{
Olli Etuahoea78d2b2018-01-09 12:55:27 +0200675 if (getQualifier() == EvqConst)
676 {
677 return false;
678 }
679 bool calledFunctionHasNoSideEffects =
680 isFunctionCall() && mFunction != nullptr && mFunction->isKnownToNotHaveSideEffects();
681 if (calledFunctionHasNoSideEffects || isConstructor())
Olli Etuahoa22aa4e2017-05-24 18:17:23 +0300682 {
683 for (TIntermNode *arg : mArguments)
684 {
685 if (arg->getAsTyped()->hasSideEffects())
686 {
687 return true;
688 }
689 }
690 return false;
691 }
692 // Conservatively assume most aggregate operators have side-effects
693 return true;
694}
695
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100696void TIntermBlock::appendStatement(TIntermNode *statement)
697{
Olli Etuaho923ecef2017-10-11 12:01:38 +0300698 // Declaration nodes with no children can appear if it was an empty declaration or if all the
699 // declarators just added constants to the symbol table instead of generating code. We still
700 // need to add the declaration to the AST in that case because it might be relevant to the
701 // validity of switch/case.
702 if (statement != nullptr)
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100703 {
704 mStatements.push_back(statement);
705 }
706}
707
Olli Etuaho16c745a2017-01-16 17:02:27 +0000708void TIntermFunctionPrototype::appendParameter(TIntermSymbol *parameter)
709{
710 ASSERT(parameter != nullptr);
711 mParameters.push_back(parameter);
712}
713
Olli Etuaho13389b62016-10-16 11:48:18 +0100714void TIntermDeclaration::appendDeclarator(TIntermTyped *declarator)
715{
716 ASSERT(declarator != nullptr);
717 ASSERT(declarator->getAsSymbolNode() != nullptr ||
718 (declarator->getAsBinaryNode() != nullptr &&
719 declarator->getAsBinaryNode()->getOp() == EOpInitialize));
720 ASSERT(mDeclarators.empty() ||
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300721 declarator->getType().sameNonArrayType(mDeclarators.back()->getAsTyped()->getType()));
Olli Etuaho13389b62016-10-16 11:48:18 +0100722 mDeclarators.push_back(declarator);
723}
724
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300725bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
726{
727 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
728 REPLACE_IF_IS(mTrueExpression, TIntermTyped, original, replacement);
729 REPLACE_IF_IS(mFalseExpression, TIntermTyped, original, replacement);
730 return false;
731}
732
Olli Etuaho57961272016-09-14 13:57:46 +0300733bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400734{
735 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100736 REPLACE_IF_IS(mTrueBlock, TIntermBlock, original, replacement);
737 REPLACE_IF_IS(mFalseBlock, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400738 return false;
739}
740
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500741bool TIntermSwitch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200742{
743 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100744 REPLACE_IF_IS(mStatementList, TIntermBlock, original, replacement);
Olli Etuaho923ecef2017-10-11 12:01:38 +0300745 ASSERT(mStatementList);
Olli Etuahoa3a36662015-02-17 13:46:51 +0200746 return false;
747}
748
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500749bool TIntermCase::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200750{
751 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
752 return false;
753}
754
Olli Etuahod7a25242015-08-18 13:49:45 +0300755TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode(), mType(node.mType)
756{
757 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
758 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
759 // We need to manually copy any fields of TIntermNode besides handling fields in TIntermTyped.
760 mLine = node.mLine;
761}
762
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200763bool TIntermTyped::hasConstantValue() const
Olli Etuahod4f4c112016-04-15 15:11:24 +0300764{
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200765 return false;
766}
767
768const TConstantUnion *TIntermTyped::getConstantValue() const
769{
770 return nullptr;
Olli Etuahod4f4c112016-04-15 15:11:24 +0300771}
772
Olli Etuahod7a25242015-08-18 13:49:45 +0300773TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node) : TIntermTyped(node)
774{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200775 mUnionArrayPointer = node.mUnionArrayPointer;
Olli Etuahod7a25242015-08-18 13:49:45 +0300776}
777
Olli Etuahobeb6dc72017-12-14 16:03:03 +0200778TIntermFunctionPrototype::TIntermFunctionPrototype(const TFunction *function)
779 : TIntermTyped(function->getReturnType()), mFunction(function)
Olli Etuahobd674552016-10-06 13:28:42 +0100780{
Olli Etuahobeb6dc72017-12-14 16:03:03 +0200781 ASSERT(mFunction->symbolType() != SymbolType::Empty);
Olli Etuahobd674552016-10-06 13:28:42 +0100782}
783
Olli Etuahod7a25242015-08-18 13:49:45 +0300784TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
785 : TIntermOperator(node),
Olli Etuahod7a25242015-08-18 13:49:45 +0300786 mUseEmulatedFunction(node.mUseEmulatedFunction),
Olli Etuahobd674552016-10-06 13:28:42 +0100787 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren),
Olli Etuaho0c371002017-12-13 17:00:25 +0400788 mFunction(node.mFunction)
Olli Etuahod7a25242015-08-18 13:49:45 +0300789{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800790 for (TIntermNode *arg : node.mArguments)
Olli Etuahod7a25242015-08-18 13:49:45 +0300791 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800792 TIntermTyped *typedArg = arg->getAsTyped();
793 ASSERT(typedArg != nullptr);
794 TIntermTyped *argCopy = typedArg->deepCopy();
795 mArguments.push_back(argCopy);
Olli Etuahod7a25242015-08-18 13:49:45 +0300796 }
797}
798
Olli Etuahofe486322017-03-21 09:30:54 +0000799TIntermAggregate *TIntermAggregate::shallowCopy() const
800{
801 TIntermSequence *copySeq = new TIntermSequence();
802 copySeq->insert(copySeq->begin(), getSequence()->begin(), getSequence()->end());
Olli Etuaho0c371002017-12-13 17:00:25 +0400803 TIntermAggregate *copyNode = new TIntermAggregate(mFunction, mType, mOp, copySeq);
Olli Etuahofe486322017-03-21 09:30:54 +0000804 copyNode->setLine(mLine);
805 return copyNode;
806}
807
Olli Etuahob6fa0432016-09-28 16:28:05 +0100808TIntermSwizzle::TIntermSwizzle(const TIntermSwizzle &node) : TIntermTyped(node)
809{
810 TIntermTyped *operandCopy = node.mOperand->deepCopy();
811 ASSERT(operandCopy != nullptr);
812 mOperand = operandCopy;
Olli Etuahoc9da71f2017-03-06 16:28:54 +0000813 mSwizzleOffsets = node.mSwizzleOffsets;
Olli Etuahob6fa0432016-09-28 16:28:05 +0100814}
815
Olli Etuahod7a25242015-08-18 13:49:45 +0300816TIntermBinary::TIntermBinary(const TIntermBinary &node)
817 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
818{
819 TIntermTyped *leftCopy = node.mLeft->deepCopy();
820 TIntermTyped *rightCopy = node.mRight->deepCopy();
821 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
822 mLeft = leftCopy;
823 mRight = rightCopy;
824}
825
826TIntermUnary::TIntermUnary(const TIntermUnary &node)
827 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
828{
829 TIntermTyped *operandCopy = node.mOperand->deepCopy();
830 ASSERT(operandCopy != nullptr);
831 mOperand = operandCopy;
832}
833
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300834TIntermTernary::TIntermTernary(const TIntermTernary &node) : TIntermTyped(node)
Olli Etuahod7a25242015-08-18 13:49:45 +0300835{
Olli Etuahod7a25242015-08-18 13:49:45 +0300836 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300837 TIntermTyped *trueCopy = node.mTrueExpression->deepCopy();
838 TIntermTyped *falseCopy = node.mFalseExpression->deepCopy();
Olli Etuahod7a25242015-08-18 13:49:45 +0300839 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300840 mCondition = conditionCopy;
841 mTrueExpression = trueCopy;
842 mFalseExpression = falseCopy;
Olli Etuahod7a25242015-08-18 13:49:45 +0300843}
844
Jamie Madillb1a85f42014-08-19 15:23:24 -0400845bool TIntermOperator::isAssignment() const
846{
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300847 return IsAssignment(mOp);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400848}
849
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300850bool TIntermOperator::isMultiplication() const
851{
852 switch (mOp)
853 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500854 case EOpMul:
855 case EOpMatrixTimesMatrix:
856 case EOpMatrixTimesVector:
857 case EOpMatrixTimesScalar:
858 case EOpVectorTimesMatrix:
859 case EOpVectorTimesScalar:
860 return true;
861 default:
862 return false;
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300863 }
864}
865
Jamie Madillb1a85f42014-08-19 15:23:24 -0400866bool TIntermOperator::isConstructor() const
867{
Olli Etuaho8fab3202017-05-08 18:22:22 +0300868 return (mOp == EOpConstruct);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400869}
870
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800871bool TIntermOperator::isFunctionCall() const
872{
873 switch (mOp)
874 {
875 case EOpCallFunctionInAST:
876 case EOpCallBuiltInFunction:
877 case EOpCallInternalRawFunction:
878 return true;
879 default:
880 return false;
881 }
882}
883
Olli Etuaho1dded802016-08-18 18:13:13 +0300884TOperator TIntermBinary::GetMulOpBasedOnOperands(const TType &left, const TType &right)
885{
886 if (left.isMatrix())
887 {
888 if (right.isMatrix())
889 {
890 return EOpMatrixTimesMatrix;
891 }
892 else
893 {
894 if (right.isVector())
895 {
896 return EOpMatrixTimesVector;
897 }
898 else
899 {
900 return EOpMatrixTimesScalar;
901 }
902 }
903 }
904 else
905 {
906 if (right.isMatrix())
907 {
908 if (left.isVector())
909 {
910 return EOpVectorTimesMatrix;
911 }
912 else
913 {
914 return EOpMatrixTimesScalar;
915 }
916 }
917 else
918 {
919 // Neither operand is a matrix.
920 if (left.isVector() == right.isVector())
921 {
922 // Leave as component product.
923 return EOpMul;
924 }
925 else
926 {
927 return EOpVectorTimesScalar;
928 }
929 }
930 }
931}
932
933TOperator TIntermBinary::GetMulAssignOpBasedOnOperands(const TType &left, const TType &right)
934{
935 if (left.isMatrix())
936 {
937 if (right.isMatrix())
938 {
939 return EOpMatrixTimesMatrixAssign;
940 }
941 else
942 {
943 // right should be scalar, but this may not be validated yet.
944 return EOpMatrixTimesScalarAssign;
945 }
946 }
947 else
948 {
949 if (right.isMatrix())
950 {
951 // Left should be a vector, but this may not be validated yet.
952 return EOpVectorTimesMatrixAssign;
953 }
954 else
955 {
956 // Neither operand is a matrix.
957 if (left.isVector() == right.isVector())
958 {
959 // Leave as component product.
960 return EOpMulAssign;
961 }
962 else
963 {
964 // left should be vector and right should be scalar, but this may not be validated
965 // yet.
966 return EOpVectorTimesScalarAssign;
967 }
968 }
969 }
970}
971
Jamie Madillb1a85f42014-08-19 15:23:24 -0400972//
973// Make sure the type of a unary operator is appropriate for its
974// combination of operation and operand type.
975//
Olli Etuahoa2234302016-08-31 12:05:39 +0300976void TIntermUnary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400977{
Olli Etuahobb2bbfb2017-08-24 15:43:33 +0300978 if (mOp == EOpArrayLength)
979 {
980 // Special case: the qualifier of .length() doesn't depend on the operand qualifier.
981 setType(TType(EbtInt, EbpUndefined, EvqConst));
982 return;
983 }
984
Olli Etuahoa2234302016-08-31 12:05:39 +0300985 TQualifier resultQualifier = EvqTemporary;
986 if (mOperand->getQualifier() == EvqConst)
987 resultQualifier = EvqConst;
988
989 unsigned char operandPrimarySize =
990 static_cast<unsigned char>(mOperand->getType().getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400991 switch (mOp)
992 {
Olli Etuahoa2234302016-08-31 12:05:39 +0300993 case EOpFloatBitsToInt:
994 setType(TType(EbtInt, EbpHigh, resultQualifier, operandPrimarySize));
995 break;
996 case EOpFloatBitsToUint:
997 setType(TType(EbtUInt, EbpHigh, resultQualifier, operandPrimarySize));
998 break;
999 case EOpIntBitsToFloat:
1000 case EOpUintBitsToFloat:
1001 setType(TType(EbtFloat, EbpHigh, resultQualifier, operandPrimarySize));
1002 break;
1003 case EOpPackSnorm2x16:
1004 case EOpPackUnorm2x16:
1005 case EOpPackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001006 case EOpPackUnorm4x8:
1007 case EOpPackSnorm4x8:
Olli Etuahoa2234302016-08-31 12:05:39 +03001008 setType(TType(EbtUInt, EbpHigh, resultQualifier));
1009 break;
1010 case EOpUnpackSnorm2x16:
1011 case EOpUnpackUnorm2x16:
1012 setType(TType(EbtFloat, EbpHigh, resultQualifier, 2));
1013 break;
1014 case EOpUnpackHalf2x16:
1015 setType(TType(EbtFloat, EbpMedium, resultQualifier, 2));
1016 break;
Olli Etuaho25aef452017-01-29 16:15:44 -08001017 case EOpUnpackUnorm4x8:
1018 case EOpUnpackSnorm4x8:
1019 setType(TType(EbtFloat, EbpMedium, resultQualifier, 4));
1020 break;
Olli Etuahoa2234302016-08-31 12:05:39 +03001021 case EOpAny:
1022 case EOpAll:
1023 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1024 break;
1025 case EOpLength:
1026 case EOpDeterminant:
1027 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier));
1028 break;
1029 case EOpTranspose:
1030 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier,
1031 static_cast<unsigned char>(mOperand->getType().getRows()),
1032 static_cast<unsigned char>(mOperand->getType().getCols())));
1033 break;
1034 case EOpIsInf:
1035 case EOpIsNan:
1036 setType(TType(EbtBool, EbpUndefined, resultQualifier, operandPrimarySize));
1037 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001038 case EOpBitfieldReverse:
1039 setType(TType(mOperand->getBasicType(), EbpHigh, resultQualifier, operandPrimarySize));
1040 break;
1041 case EOpBitCount:
1042 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
1043 break;
1044 case EOpFindLSB:
1045 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
1046 break;
1047 case EOpFindMSB:
1048 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
1049 break;
Olli Etuahoa2234302016-08-31 12:05:39 +03001050 default:
1051 setType(mOperand->getType());
1052 mType.setQualifier(resultQualifier);
1053 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001054 }
Olli Etuahoa2234302016-08-31 12:05:39 +03001055}
Jamie Madillb1a85f42014-08-19 15:23:24 -04001056
Olli Etuahob6fa0432016-09-28 16:28:05 +01001057TIntermSwizzle::TIntermSwizzle(TIntermTyped *operand, const TVector<int> &swizzleOffsets)
1058 : TIntermTyped(TType(EbtFloat, EbpUndefined)),
1059 mOperand(operand),
1060 mSwizzleOffsets(swizzleOffsets)
1061{
1062 ASSERT(mSwizzleOffsets.size() <= 4);
1063 promote();
1064}
1065
Olli Etuahoa2234302016-08-31 12:05:39 +03001066TIntermUnary::TIntermUnary(TOperator op, TIntermTyped *operand)
1067 : TIntermOperator(op), mOperand(operand), mUseEmulatedFunction(false)
1068{
1069 promote();
Jamie Madillb1a85f42014-08-19 15:23:24 -04001070}
1071
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001072TIntermBinary::TIntermBinary(TOperator op, TIntermTyped *left, TIntermTyped *right)
1073 : TIntermOperator(op), mLeft(left), mRight(right), mAddIndexClamp(false)
1074{
1075 promote();
1076}
1077
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001078TIntermInvariantDeclaration::TIntermInvariantDeclaration(TIntermSymbol *symbol, const TSourceLoc &line)
1079 : TIntermNode(), mSymbol(symbol)
1080{
1081 ASSERT(symbol);
1082 setLine(line);
1083}
1084
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001085TIntermTernary::TIntermTernary(TIntermTyped *cond,
1086 TIntermTyped *trueExpression,
1087 TIntermTyped *falseExpression)
1088 : TIntermTyped(trueExpression->getType()),
1089 mCondition(cond),
1090 mTrueExpression(trueExpression),
1091 mFalseExpression(falseExpression)
1092{
1093 getTypePointer()->setQualifier(
1094 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression));
1095}
1096
Olli Etuaho81629262017-04-19 11:56:01 +03001097TIntermLoop::TIntermLoop(TLoopType type,
1098 TIntermNode *init,
1099 TIntermTyped *cond,
1100 TIntermTyped *expr,
1101 TIntermBlock *body)
1102 : mType(type), mInit(init), mCond(cond), mExpr(expr), mBody(body)
1103{
1104 // Declaration nodes with no children can appear if all the declarators just added constants to
1105 // the symbol table instead of generating code. They're no-ops so don't add them to the tree.
1106 if (mInit && mInit->getAsDeclarationNode() &&
1107 mInit->getAsDeclarationNode()->getSequence()->empty())
1108 {
1109 mInit = nullptr;
1110 }
1111}
1112
Olli Etuaho923ecef2017-10-11 12:01:38 +03001113TIntermIfElse::TIntermIfElse(TIntermTyped *cond, TIntermBlock *trueB, TIntermBlock *falseB)
1114 : TIntermNode(), mCondition(cond), mTrueBlock(trueB), mFalseBlock(falseB)
1115{
1116 // Prune empty false blocks so that there won't be unnecessary operations done on it.
1117 if (mFalseBlock && mFalseBlock->getSequence()->empty())
1118 {
1119 mFalseBlock = nullptr;
1120 }
1121}
1122
1123TIntermSwitch::TIntermSwitch(TIntermTyped *init, TIntermBlock *statementList)
1124 : TIntermNode(), mInit(init), mStatementList(statementList)
1125{
1126 ASSERT(mStatementList);
1127}
1128
1129void TIntermSwitch::setStatementList(TIntermBlock *statementList)
1130{
1131 ASSERT(statementList);
1132 mStatementList = statementList;
1133}
1134
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001135// static
1136TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond,
1137 TIntermTyped *trueExpression,
1138 TIntermTyped *falseExpression)
1139{
1140 if (cond->getQualifier() == EvqConst && trueExpression->getQualifier() == EvqConst &&
1141 falseExpression->getQualifier() == EvqConst)
1142 {
1143 return EvqConst;
1144 }
1145 return EvqTemporary;
1146}
1147
Olli Etuaho765924f2018-01-04 12:48:36 +02001148TIntermTyped *TIntermTernary::fold(TDiagnostics * /* diagnostics */)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001149{
1150 if (mCondition->getAsConstantUnion())
1151 {
1152 if (mCondition->getAsConstantUnion()->getBConst(0))
1153 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001154 return mTrueExpression;
1155 }
1156 else
1157 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001158 return mFalseExpression;
1159 }
1160 }
1161 return this;
1162}
1163
Olli Etuahob6fa0432016-09-28 16:28:05 +01001164void TIntermSwizzle::promote()
1165{
1166 TQualifier resultQualifier = EvqTemporary;
1167 if (mOperand->getQualifier() == EvqConst)
1168 resultQualifier = EvqConst;
1169
1170 auto numFields = mSwizzleOffsets.size();
1171 setType(TType(mOperand->getBasicType(), mOperand->getPrecision(), resultQualifier,
1172 static_cast<unsigned char>(numFields)));
1173}
1174
1175bool TIntermSwizzle::hasDuplicateOffsets() const
1176{
1177 int offsetCount[4] = {0u, 0u, 0u, 0u};
1178 for (const auto offset : mSwizzleOffsets)
1179 {
1180 offsetCount[offset]++;
1181 if (offsetCount[offset] > 1)
1182 {
1183 return true;
1184 }
1185 }
1186 return false;
1187}
1188
Olli Etuaho09b04a22016-12-15 13:30:26 +00001189bool TIntermSwizzle::offsetsMatch(int offset) const
1190{
1191 return mSwizzleOffsets.size() == 1 && mSwizzleOffsets[0] == offset;
1192}
1193
Olli Etuahob6fa0432016-09-28 16:28:05 +01001194void TIntermSwizzle::writeOffsetsAsXYZW(TInfoSinkBase *out) const
1195{
1196 for (const int offset : mSwizzleOffsets)
1197 {
1198 switch (offset)
1199 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001200 case 0:
1201 *out << "x";
1202 break;
1203 case 1:
1204 *out << "y";
1205 break;
1206 case 2:
1207 *out << "z";
1208 break;
1209 case 3:
1210 *out << "w";
1211 break;
1212 default:
1213 UNREACHABLE();
Olli Etuahob6fa0432016-09-28 16:28:05 +01001214 }
1215 }
1216}
1217
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001218TQualifier TIntermBinary::GetCommaQualifier(int shaderVersion,
1219 const TIntermTyped *left,
1220 const TIntermTyped *right)
1221{
1222 // ESSL3.00 section 12.43: The result of a sequence operator is not a constant-expression.
1223 if (shaderVersion >= 300 || left->getQualifier() != EvqConst ||
1224 right->getQualifier() != EvqConst)
1225 {
1226 return EvqTemporary;
1227 }
1228 return EvqConst;
1229}
Olli Etuahob6fa0432016-09-28 16:28:05 +01001230
1231// Establishes the type of the result of the binary operation.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001232void TIntermBinary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -04001233{
Olli Etuaho1dded802016-08-18 18:13:13 +03001234 ASSERT(!isMultiplication() ||
1235 mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1236
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001237 // Comma is handled as a special case. Note that the comma node qualifier depends on the shader
1238 // version and so is not being set here.
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001239 if (mOp == EOpComma)
1240 {
1241 setType(mRight->getType());
1242 return;
1243 }
1244
Jamie Madillb1a85f42014-08-19 15:23:24 -04001245 // Base assumption: just make the type the same as the left
1246 // operand. Then only deviations from this need be coded.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001247 setType(mLeft->getType());
1248
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001249 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001250 // Binary operations results in temporary variables unless both
1251 // operands are const.
1252 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
1253 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001254 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001255 getTypePointer()->setQualifier(EvqTemporary);
1256 }
1257
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001258 // Handle indexing ops.
1259 switch (mOp)
1260 {
1261 case EOpIndexDirect:
1262 case EOpIndexIndirect:
1263 if (mLeft->isArray())
1264 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001265 mType.toArrayElementType();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001266 }
1267 else if (mLeft->isMatrix())
1268 {
1269 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
1270 static_cast<unsigned char>(mLeft->getRows())));
1271 }
1272 else if (mLeft->isVector())
1273 {
1274 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier));
1275 }
1276 else
1277 {
1278 UNREACHABLE();
1279 }
1280 return;
1281 case EOpIndexDirectStruct:
1282 {
1283 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1284 const int i = mRight->getAsConstantUnion()->getIConst(0);
1285 setType(*fields[i]->type());
1286 getTypePointer()->setQualifier(resultQualifier);
1287 return;
1288 }
1289 case EOpIndexDirectInterfaceBlock:
1290 {
1291 const TFieldList &fields = mLeft->getType().getInterfaceBlock()->fields();
1292 const int i = mRight->getAsConstantUnion()->getIConst(0);
1293 setType(*fields[i]->type());
1294 getTypePointer()->setQualifier(resultQualifier);
1295 return;
1296 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001297 default:
1298 break;
1299 }
1300
1301 ASSERT(mLeft->isArray() == mRight->isArray());
1302
1303 // The result gets promoted to the highest precision.
1304 TPrecision higherPrecision = GetHigherPrecision(mLeft->getPrecision(), mRight->getPrecision());
1305 getTypePointer()->setPrecision(higherPrecision);
1306
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001307 const int nominalSize = std::max(mLeft->getNominalSize(), mRight->getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -04001308
1309 //
1310 // All scalars or structs. Code after this test assumes this case is removed!
1311 //
1312 if (nominalSize == 1)
1313 {
1314 switch (mOp)
1315 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001316 //
1317 // Promote to conditional
1318 //
1319 case EOpEqual:
1320 case EOpNotEqual:
1321 case EOpLessThan:
1322 case EOpGreaterThan:
1323 case EOpLessThanEqual:
1324 case EOpGreaterThanEqual:
1325 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1326 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001327
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001328 //
1329 // And and Or operate on conditionals
1330 //
1331 case EOpLogicalAnd:
1332 case EOpLogicalXor:
1333 case EOpLogicalOr:
1334 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
1335 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1336 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001337
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001338 default:
1339 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001340 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001341 return;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001342 }
1343
1344 // If we reach here, at least one of the operands is vector or matrix.
1345 // The other operand could be a scalar, vector, or matrix.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001346 TBasicType basicType = mLeft->getBasicType();
Olli Etuaho1dded802016-08-18 18:13:13 +03001347
Jamie Madillb1a85f42014-08-19 15:23:24 -04001348 switch (mOp)
1349 {
Olli Etuaho1dded802016-08-18 18:13:13 +03001350 case EOpMul:
1351 break;
1352 case EOpMatrixTimesScalar:
1353 if (mRight->isMatrix())
Jamie Madillb1a85f42014-08-19 15:23:24 -04001354 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001355 setType(TType(basicType, higherPrecision, resultQualifier,
1356 static_cast<unsigned char>(mRight->getCols()),
1357 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -04001358 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001359 break;
1360 case EOpMatrixTimesVector:
1361 setType(TType(basicType, higherPrecision, resultQualifier,
1362 static_cast<unsigned char>(mLeft->getRows()), 1));
1363 break;
1364 case EOpMatrixTimesMatrix:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001365 setType(TType(basicType, higherPrecision, resultQualifier,
1366 static_cast<unsigned char>(mRight->getCols()),
1367 static_cast<unsigned char>(mLeft->getRows())));
Olli Etuaho1dded802016-08-18 18:13:13 +03001368 break;
1369 case EOpVectorTimesScalar:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001370 setType(TType(basicType, higherPrecision, resultQualifier,
Olli Etuaho1dded802016-08-18 18:13:13 +03001371 static_cast<unsigned char>(nominalSize), 1));
1372 break;
1373 case EOpVectorTimesMatrix:
1374 setType(TType(basicType, higherPrecision, resultQualifier,
1375 static_cast<unsigned char>(mRight->getCols()), 1));
1376 break;
1377 case EOpMulAssign:
1378 case EOpVectorTimesScalarAssign:
1379 case EOpVectorTimesMatrixAssign:
1380 case EOpMatrixTimesScalarAssign:
1381 case EOpMatrixTimesMatrixAssign:
1382 ASSERT(mOp == GetMulAssignOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1383 break;
1384 case EOpAssign:
1385 case EOpInitialize:
Olli Etuaho1dded802016-08-18 18:13:13 +03001386 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1387 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
1388 break;
1389 case EOpAdd:
1390 case EOpSub:
1391 case EOpDiv:
1392 case EOpIMod:
1393 case EOpBitShiftLeft:
1394 case EOpBitShiftRight:
1395 case EOpBitwiseAnd:
1396 case EOpBitwiseXor:
1397 case EOpBitwiseOr:
1398 case EOpAddAssign:
1399 case EOpSubAssign:
1400 case EOpDivAssign:
1401 case EOpIModAssign:
1402 case EOpBitShiftLeftAssign:
1403 case EOpBitShiftRightAssign:
1404 case EOpBitwiseAndAssign:
1405 case EOpBitwiseXorAssign:
1406 case EOpBitwiseOrAssign:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001407 {
1408 const int secondarySize =
1409 std::max(mLeft->getSecondarySize(), mRight->getSecondarySize());
1410 setType(TType(basicType, higherPrecision, resultQualifier,
1411 static_cast<unsigned char>(nominalSize),
1412 static_cast<unsigned char>(secondarySize)));
1413 ASSERT(!mLeft->isArray() && !mRight->isArray());
Olli Etuaho1dded802016-08-18 18:13:13 +03001414 break;
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001415 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001416 case EOpEqual:
1417 case EOpNotEqual:
1418 case EOpLessThan:
1419 case EOpGreaterThan:
1420 case EOpLessThanEqual:
1421 case EOpGreaterThanEqual:
1422 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1423 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001424 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Olli Etuaho1dded802016-08-18 18:13:13 +03001425 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001426
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001427 case EOpIndexDirect:
1428 case EOpIndexIndirect:
1429 case EOpIndexDirectInterfaceBlock:
1430 case EOpIndexDirectStruct:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001431 // These ops should be already fully handled.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001432 UNREACHABLE();
1433 break;
Olli Etuaho1dded802016-08-18 18:13:13 +03001434 default:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001435 UNREACHABLE();
1436 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001437 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001438}
1439
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001440bool TIntermConstantUnion::hasConstantValue() const
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001441{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001442 return true;
1443}
1444
1445const TConstantUnion *TIntermConstantUnion::getConstantValue() const
1446{
1447 return mUnionArrayPointer;
1448}
1449
1450const TConstantUnion *TIntermConstantUnion::FoldIndexing(const TType &type,
1451 const TConstantUnion *constArray,
1452 int index)
1453{
1454 if (type.isArray())
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001455 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001456 ASSERT(index < static_cast<int>(type.getOutermostArraySize()));
1457 TType arrayElementType(type);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001458 arrayElementType.toArrayElementType();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001459 size_t arrayElementSize = arrayElementType.getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001460 return &constArray[arrayElementSize * index];
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001461 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001462 else if (type.isMatrix())
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001463 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001464 ASSERT(index < type.getCols());
1465 int size = type.getRows();
1466 return &constArray[size * index];
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001467 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001468 else if (type.isVector())
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001469 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001470 ASSERT(index < type.getNominalSize());
1471 return &constArray[index];
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001472 }
1473 else
1474 {
1475 UNREACHABLE();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001476 return nullptr;
1477 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001478}
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001479
Olli Etuaho765924f2018-01-04 12:48:36 +02001480TIntermTyped *TIntermSwizzle::fold(TDiagnostics * /* diagnostics */)
Olli Etuahob6fa0432016-09-28 16:28:05 +01001481{
1482 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1483 if (operandConstant == nullptr)
1484 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001485 return this;
Olli Etuahob6fa0432016-09-28 16:28:05 +01001486 }
1487
1488 TConstantUnion *constArray = new TConstantUnion[mSwizzleOffsets.size()];
1489 for (size_t i = 0; i < mSwizzleOffsets.size(); ++i)
1490 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001491 constArray[i] = *TIntermConstantUnion::FoldIndexing(
1492 operandConstant->getType(), operandConstant->getConstantValue(), mSwizzleOffsets.at(i));
Olli Etuahob6fa0432016-09-28 16:28:05 +01001493 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001494 return CreateFoldedNode(constArray, this);
Olli Etuahob6fa0432016-09-28 16:28:05 +01001495}
1496
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001497TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
1498{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001499 const TConstantUnion *rightConstant = mRight->getConstantValue();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001500 switch (mOp)
1501 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001502 case EOpComma:
1503 {
1504 if (mLeft->hasSideEffects())
1505 {
1506 return this;
1507 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001508 return mRight;
1509 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001510 case EOpIndexDirect:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001511 case EOpIndexDirectStruct:
1512 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001513 if (rightConstant == nullptr)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001514 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001515 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001516 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001517 size_t index = static_cast<size_t>(rightConstant->getIConst());
1518 TIntermAggregate *leftAggregate = mLeft->getAsAggregate();
1519 if (leftAggregate && leftAggregate->isConstructor() && leftAggregate->isArray() &&
1520 !leftAggregate->hasSideEffects())
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001521 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001522 ASSERT(index < leftAggregate->getSequence()->size());
1523 // This transformation can't add complexity as we're eliminating the constructor
1524 // entirely.
1525 return leftAggregate->getSequence()->at(index)->getAsTyped();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001526 }
1527
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001528 // If the indexed value is already a constant union, we can't increase duplication of
1529 // data by folding the indexing. Also fold the node in case it's generally beneficial to
1530 // replace this type of node with a constant union even if that would mean duplicating
1531 // data.
1532 if (mLeft->getAsConstantUnion() || getType().canReplaceWithConstantUnion())
1533 {
1534 const TConstantUnion *constantValue = getConstantValue();
1535 if (constantValue == nullptr)
1536 {
1537 return this;
1538 }
1539 return CreateFoldedNode(constantValue, this);
1540 }
1541 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001542 }
1543 case EOpIndexIndirect:
1544 case EOpIndexDirectInterfaceBlock:
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001545 case EOpInitialize:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001546 // Can never be constant folded.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001547 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001548 default:
1549 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001550 if (rightConstant == nullptr)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001551 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001552 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001553 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001554 const TConstantUnion *leftConstant = mLeft->getConstantValue();
1555 if (leftConstant == nullptr)
1556 {
1557 return this;
1558 }
1559 const TConstantUnion *constArray =
1560 TIntermConstantUnion::FoldBinary(mOp, leftConstant, mLeft->getType(), rightConstant,
1561 mRight->getType(), diagnostics, mLeft->getLine());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001562 if (!constArray)
1563 {
1564 return this;
1565 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001566 return CreateFoldedNode(constArray, this);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001567 }
1568 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001569}
1570
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001571bool TIntermBinary::hasConstantValue() const
1572{
1573 switch (mOp)
1574 {
1575 case EOpIndexDirect:
1576 case EOpIndexDirectStruct:
1577 {
1578 if (mLeft->hasConstantValue() && mRight->hasConstantValue())
1579 {
1580 return true;
1581 }
1582 }
1583 default:
1584 break;
1585 }
1586 return false;
1587}
1588
1589const TConstantUnion *TIntermBinary::getConstantValue() const
1590{
1591 if (!hasConstantValue())
1592 {
1593 return nullptr;
1594 }
1595
1596 const TConstantUnion *leftConstantValue = mLeft->getConstantValue();
1597 int index = mRight->getConstantValue()->getIConst();
1598 const TConstantUnion *constIndexingResult = nullptr;
1599 if (mOp == EOpIndexDirect)
1600 {
1601 constIndexingResult =
1602 TIntermConstantUnion::FoldIndexing(mLeft->getType(), leftConstantValue, index);
1603 }
1604 else
1605 {
1606 ASSERT(mOp == EOpIndexDirectStruct);
1607 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1608
1609 size_t previousFieldsSize = 0;
1610 for (int i = 0; i < index; ++i)
1611 {
1612 previousFieldsSize += fields[i]->type()->getObjectSize();
1613 }
1614 constIndexingResult = leftConstantValue + previousFieldsSize;
1615 }
1616 return constIndexingResult;
1617}
1618
Olli Etuahof119a262016-08-19 15:54:22 +03001619TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
Olli Etuaho95310b02015-06-02 17:43:38 +03001620{
Arun Patoleab2b9a22015-07-06 18:27:56 +05301621 TConstantUnion *constArray = nullptr;
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03001622
1623 if (mOp == EOpArrayLength)
Arun Patoleab2b9a22015-07-06 18:27:56 +05301624 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02001625 // The size of runtime-sized arrays may only be determined at runtime.
1626 if (mOperand->hasSideEffects() || mOperand->getType().isUnsizedArray())
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03001627 {
1628 return this;
1629 }
1630 constArray = new TConstantUnion[1];
1631 constArray->setIConst(mOperand->getOutermostArraySize());
1632 }
1633 else
1634 {
1635 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1636 if (operandConstant == nullptr)
1637 {
1638 return this;
1639 }
1640
1641 switch (mOp)
1642 {
1643 case EOpAny:
1644 case EOpAll:
1645 case EOpLength:
1646 case EOpTranspose:
1647 case EOpDeterminant:
1648 case EOpInverse:
1649 case EOpPackSnorm2x16:
1650 case EOpUnpackSnorm2x16:
1651 case EOpPackUnorm2x16:
1652 case EOpUnpackUnorm2x16:
1653 case EOpPackHalf2x16:
1654 case EOpUnpackHalf2x16:
1655 case EOpPackUnorm4x8:
1656 case EOpPackSnorm4x8:
1657 case EOpUnpackUnorm4x8:
1658 case EOpUnpackSnorm4x8:
1659 constArray = operandConstant->foldUnaryNonComponentWise(mOp);
1660 break;
1661 default:
1662 constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
1663 break;
1664 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301665 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001666 if (constArray == nullptr)
1667 {
1668 return this;
1669 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001670 return CreateFoldedNode(constArray, this);
Olli Etuahob43846e2015-06-02 18:18:57 +03001671}
1672
Olli Etuahof119a262016-08-19 15:54:22 +03001673TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
Olli Etuahob43846e2015-06-02 18:18:57 +03001674{
1675 // Make sure that all params are constant before actual constant folding.
1676 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +03001677 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001678 if (param->getAsConstantUnion() == nullptr)
1679 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001680 return this;
Olli Etuahob43846e2015-06-02 18:18:57 +03001681 }
Olli Etuaho95310b02015-06-02 17:43:38 +03001682 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001683 const TConstantUnion *constArray = nullptr;
Olli Etuaho1d122782015-11-06 15:35:17 +02001684 if (isConstructor())
Olli Etuaho2768bc82017-12-12 11:51:48 +02001685 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001686 if (mType.canReplaceWithConstantUnion())
Olli Etuaho765924f2018-01-04 12:48:36 +02001687 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001688 constArray = getConstantValue();
Olli Etuaho765924f2018-01-04 12:48:36 +02001689 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001690 }
Olli Etuaho765924f2018-01-04 12:48:36 +02001691 else if (CanFoldAggregateBuiltInOp(mOp))
Olli Etuaho2768bc82017-12-12 11:51:48 +02001692 {
Olli Etuahof119a262016-08-19 15:54:22 +03001693 constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, diagnostics);
Olli Etuaho2768bc82017-12-12 11:51:48 +02001694 }
Olli Etuaho765924f2018-01-04 12:48:36 +02001695 if (constArray == nullptr)
1696 {
1697 return this;
1698 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001699 return CreateFoldedNode(constArray, this);
Olli Etuaho95310b02015-06-02 17:43:38 +03001700}
1701
Jamie Madillb1a85f42014-08-19 15:23:24 -04001702//
1703// The fold functions see if an operation on a constant can be done in place,
1704// without generating run-time code.
1705//
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001706// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001707//
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001708const TConstantUnion *TIntermConstantUnion::FoldBinary(TOperator op,
1709 const TConstantUnion *leftArray,
1710 const TType &leftType,
1711 const TConstantUnion *rightArray,
1712 const TType &rightType,
1713 TDiagnostics *diagnostics,
1714 const TSourceLoc &line)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001715{
Olli Etuahof119a262016-08-19 15:54:22 +03001716 ASSERT(leftArray && rightArray);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001717
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001718 size_t objectSize = leftType.getObjectSize();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001719
1720 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001721 if (rightType.getObjectSize() == 1 && objectSize > 1)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001722 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001723 rightArray = Vectorize(*rightArray, objectSize);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001724 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001725 else if (rightType.getObjectSize() > 1 && objectSize == 1)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001726 {
1727 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001728 leftArray = Vectorize(*leftArray, rightType.getObjectSize());
1729 objectSize = rightType.getObjectSize();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001730 }
1731
1732 TConstantUnion *resultArray = nullptr;
1733
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001734 switch (op)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001735 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001736 case EOpAdd:
1737 resultArray = new TConstantUnion[objectSize];
1738 for (size_t i = 0; i < objectSize; i++)
1739 resultArray[i] =
1740 TConstantUnion::add(leftArray[i], rightArray[i], diagnostics, line);
1741 break;
1742 case EOpSub:
1743 resultArray = new TConstantUnion[objectSize];
1744 for (size_t i = 0; i < objectSize; i++)
1745 resultArray[i] =
1746 TConstantUnion::sub(leftArray[i], rightArray[i], diagnostics, line);
1747 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001748
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001749 case EOpMul:
1750 case EOpVectorTimesScalar:
1751 case EOpMatrixTimesScalar:
1752 resultArray = new TConstantUnion[objectSize];
1753 for (size_t i = 0; i < objectSize; i++)
1754 resultArray[i] =
1755 TConstantUnion::mul(leftArray[i], rightArray[i], diagnostics, line);
1756 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001757
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001758 case EOpMatrixTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001759 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001760 // TODO(jmadll): This code should check for overflows.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001761 ASSERT(leftType.getBasicType() == EbtFloat && rightType.getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001762
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001763 const int leftCols = leftType.getCols();
1764 const int leftRows = leftType.getRows();
1765 const int rightCols = rightType.getCols();
1766 const int rightRows = rightType.getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001767 const int resultCols = rightCols;
1768 const int resultRows = leftRows;
1769
1770 resultArray = new TConstantUnion[resultCols * resultRows];
1771 for (int row = 0; row < resultRows; row++)
1772 {
1773 for (int column = 0; column < resultCols; column++)
1774 {
1775 resultArray[resultRows * column + row].setFConst(0.0f);
1776 for (int i = 0; i < leftCols; i++)
1777 {
1778 resultArray[resultRows * column + row].setFConst(
1779 resultArray[resultRows * column + row].getFConst() +
1780 leftArray[i * leftRows + row].getFConst() *
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001781 rightArray[column * rightRows + i].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001782 }
1783 }
1784 }
1785 }
1786 break;
1787
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001788 case EOpDiv:
1789 case EOpIMod:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001790 {
1791 resultArray = new TConstantUnion[objectSize];
1792 for (size_t i = 0; i < objectSize; i++)
1793 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001794 switch (leftType.getBasicType())
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001795 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001796 case EbtFloat:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001797 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001798 ASSERT(op == EOpDiv);
1799 float dividend = leftArray[i].getFConst();
1800 float divisor = rightArray[i].getFConst();
1801 if (divisor == 0.0f)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001802 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001803 if (dividend == 0.0f)
Olli Etuahod4453572016-09-27 13:21:46 +01001804 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001805 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001806 line,
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001807 "Zero divided by zero during constant folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001808 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001809 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
Olli Etuahod4453572016-09-27 13:21:46 +01001810 }
1811 else
1812 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001813 diagnostics->warning(line, "Divide by zero during constant folding",
1814 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001815 bool negativeResult =
1816 std::signbit(dividend) != std::signbit(divisor);
1817 resultArray[i].setFConst(
1818 negativeResult ? -std::numeric_limits<float>::infinity()
1819 : std::numeric_limits<float>::infinity());
Olli Etuahod4453572016-09-27 13:21:46 +01001820 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001821 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001822 else if (gl::isInf(dividend) && gl::isInf(divisor))
1823 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001824 diagnostics->warning(line,
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001825 "Infinity divided by infinity during constant "
1826 "folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001827 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001828 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
1829 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001830 else
1831 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001832 float result = dividend / divisor;
1833 if (!gl::isInf(dividend) && gl::isInf(result))
Olli Etuahod4453572016-09-27 13:21:46 +01001834 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001835 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001836 line, "Constant folded division overflowed to infinity", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001837 }
1838 resultArray[i].setFConst(result);
1839 }
1840 break;
1841 }
1842 case EbtInt:
1843 if (rightArray[i] == 0)
1844 {
1845 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001846 line, "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001847 resultArray[i].setIConst(INT_MAX);
1848 }
1849 else
1850 {
1851 int lhs = leftArray[i].getIConst();
1852 int divisor = rightArray[i].getIConst();
1853 if (op == EOpDiv)
1854 {
1855 // Check for the special case where the minimum representable number
1856 // is
1857 // divided by -1. If left alone this leads to integer overflow in
1858 // C++.
1859 // ESSL 3.00.6 section 4.1.3 Integers:
1860 // "However, for the case where the minimum representable value is
1861 // divided by -1, it is allowed to return either the minimum
1862 // representable value or the maximum representable value."
1863 if (lhs == -0x7fffffff - 1 && divisor == -1)
1864 {
1865 resultArray[i].setIConst(0x7fffffff);
1866 }
1867 else
1868 {
1869 resultArray[i].setIConst(lhs / divisor);
1870 }
Olli Etuahod4453572016-09-27 13:21:46 +01001871 }
1872 else
1873 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001874 ASSERT(op == EOpIMod);
1875 if (lhs < 0 || divisor < 0)
1876 {
1877 // ESSL 3.00.6 section 5.9: Results of modulus are undefined
1878 // when
1879 // either one of the operands is negative.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001880 diagnostics->warning(line,
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001881 "Negative modulus operator operand "
1882 "encountered during constant folding",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001883 "%");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001884 resultArray[i].setIConst(0);
1885 }
1886 else
1887 {
1888 resultArray[i].setIConst(lhs % divisor);
1889 }
Olli Etuahod4453572016-09-27 13:21:46 +01001890 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001891 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001892 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001893
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001894 case EbtUInt:
1895 if (rightArray[i] == 0)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001896 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001897 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001898 line, "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001899 resultArray[i].setUConst(UINT_MAX);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001900 }
1901 else
1902 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001903 if (op == EOpDiv)
1904 {
1905 resultArray[i].setUConst(leftArray[i].getUConst() /
1906 rightArray[i].getUConst());
1907 }
1908 else
1909 {
1910 ASSERT(op == EOpIMod);
1911 resultArray[i].setUConst(leftArray[i].getUConst() %
1912 rightArray[i].getUConst());
1913 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001914 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001915 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001916
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001917 default:
1918 UNREACHABLE();
1919 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001920 }
1921 }
1922 }
1923 break;
1924
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001925 case EOpMatrixTimesVector:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001926 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001927 // TODO(jmadll): This code should check for overflows.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001928 ASSERT(rightType.getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001929
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001930 const int matrixCols = leftType.getCols();
1931 const int matrixRows = leftType.getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001932
1933 resultArray = new TConstantUnion[matrixRows];
1934
1935 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1936 {
1937 resultArray[matrixRow].setFConst(0.0f);
1938 for (int col = 0; col < matrixCols; col++)
1939 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001940 resultArray[matrixRow].setFConst(
1941 resultArray[matrixRow].getFConst() +
1942 leftArray[col * matrixRows + matrixRow].getFConst() *
1943 rightArray[col].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001944 }
1945 }
1946 }
1947 break;
1948
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001949 case EOpVectorTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001950 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001951 // TODO(jmadll): This code should check for overflows.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001952 ASSERT(leftType.getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001953
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001954 const int matrixCols = rightType.getCols();
1955 const int matrixRows = rightType.getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001956
1957 resultArray = new TConstantUnion[matrixCols];
1958
1959 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1960 {
1961 resultArray[matrixCol].setFConst(0.0f);
1962 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1963 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001964 resultArray[matrixCol].setFConst(
1965 resultArray[matrixCol].getFConst() +
1966 leftArray[matrixRow].getFConst() *
1967 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001968 }
1969 }
1970 }
1971 break;
1972
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001973 case EOpLogicalAnd:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001974 {
1975 resultArray = new TConstantUnion[objectSize];
1976 for (size_t i = 0; i < objectSize; i++)
1977 {
1978 resultArray[i] = leftArray[i] && rightArray[i];
1979 }
1980 }
1981 break;
1982
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001983 case EOpLogicalOr:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001984 {
1985 resultArray = new TConstantUnion[objectSize];
1986 for (size_t i = 0; i < objectSize; i++)
1987 {
1988 resultArray[i] = leftArray[i] || rightArray[i];
1989 }
1990 }
1991 break;
1992
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001993 case EOpLogicalXor:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001994 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001995 ASSERT(leftType.getBasicType() == EbtBool);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001996 resultArray = new TConstantUnion[objectSize];
1997 for (size_t i = 0; i < objectSize; i++)
1998 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03001999 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002000 }
2001 }
2002 break;
2003
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002004 case EOpBitwiseAnd:
2005 resultArray = new TConstantUnion[objectSize];
2006 for (size_t i = 0; i < objectSize; i++)
2007 resultArray[i] = leftArray[i] & rightArray[i];
2008 break;
2009 case EOpBitwiseXor:
2010 resultArray = new TConstantUnion[objectSize];
2011 for (size_t i = 0; i < objectSize; i++)
2012 resultArray[i] = leftArray[i] ^ rightArray[i];
2013 break;
2014 case EOpBitwiseOr:
2015 resultArray = new TConstantUnion[objectSize];
2016 for (size_t i = 0; i < objectSize; i++)
2017 resultArray[i] = leftArray[i] | rightArray[i];
2018 break;
2019 case EOpBitShiftLeft:
2020 resultArray = new TConstantUnion[objectSize];
2021 for (size_t i = 0; i < objectSize; i++)
2022 resultArray[i] =
2023 TConstantUnion::lshift(leftArray[i], rightArray[i], diagnostics, line);
2024 break;
2025 case EOpBitShiftRight:
2026 resultArray = new TConstantUnion[objectSize];
2027 for (size_t i = 0; i < objectSize; i++)
2028 resultArray[i] =
2029 TConstantUnion::rshift(leftArray[i], rightArray[i], diagnostics, line);
2030 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002031
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002032 case EOpLessThan:
2033 ASSERT(objectSize == 1);
2034 resultArray = new TConstantUnion[1];
2035 resultArray->setBConst(*leftArray < *rightArray);
2036 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002037
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002038 case EOpGreaterThan:
2039 ASSERT(objectSize == 1);
2040 resultArray = new TConstantUnion[1];
2041 resultArray->setBConst(*leftArray > *rightArray);
2042 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002043
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002044 case EOpLessThanEqual:
2045 ASSERT(objectSize == 1);
2046 resultArray = new TConstantUnion[1];
2047 resultArray->setBConst(!(*leftArray > *rightArray));
2048 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002049
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002050 case EOpGreaterThanEqual:
2051 ASSERT(objectSize == 1);
2052 resultArray = new TConstantUnion[1];
2053 resultArray->setBConst(!(*leftArray < *rightArray));
2054 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002055
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002056 case EOpEqual:
2057 case EOpNotEqual:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002058 {
2059 resultArray = new TConstantUnion[1];
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002060 bool equal = true;
Olli Etuaho40d9edf2015-11-12 17:30:34 +02002061 for (size_t i = 0; i < objectSize; i++)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002062 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02002063 if (leftArray[i] != rightArray[i])
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002064 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02002065 equal = false;
2066 break; // break out of for loop
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002067 }
2068 }
2069 if (op == EOpEqual)
2070 {
2071 resultArray->setBConst(equal);
2072 }
2073 else
2074 {
2075 resultArray->setBConst(!equal);
2076 }
2077 }
2078 break;
2079
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002080 default:
2081 UNREACHABLE();
2082 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002083 }
2084 return resultArray;
2085}
2086
Olli Etuahof119a262016-08-19 15:54:22 +03002087// The fold functions do operations on a constant at GLSL compile time, without generating run-time
2088// code. Returns the constant value to keep using. Nullptr should not be returned.
2089TConstantUnion *TIntermConstantUnion::foldUnaryNonComponentWise(TOperator op)
Jamie Madillb1a85f42014-08-19 15:23:24 -04002090{
Olli Etuahof119a262016-08-19 15:54:22 +03002091 // Do operations where the return type may have a different number of components compared to the
2092 // operand type.
Jamie Madillb1a85f42014-08-19 15:23:24 -04002093
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002094 const TConstantUnion *operandArray = getConstantValue();
Olli Etuahof119a262016-08-19 15:54:22 +03002095 ASSERT(operandArray);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302096
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002097 size_t objectSize = getType().getObjectSize();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302098 TConstantUnion *resultArray = nullptr;
2099 switch (op)
2100 {
Olli Etuahof119a262016-08-19 15:54:22 +03002101 case EOpAny:
2102 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302103 resultArray = new TConstantUnion();
2104 resultArray->setBConst(false);
2105 for (size_t i = 0; i < objectSize; i++)
2106 {
2107 if (operandArray[i].getBConst())
2108 {
2109 resultArray->setBConst(true);
2110 break;
2111 }
2112 }
2113 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302114
Olli Etuahof119a262016-08-19 15:54:22 +03002115 case EOpAll:
2116 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302117 resultArray = new TConstantUnion();
2118 resultArray->setBConst(true);
2119 for (size_t i = 0; i < objectSize; i++)
2120 {
2121 if (!operandArray[i].getBConst())
2122 {
2123 resultArray->setBConst(false);
2124 break;
2125 }
2126 }
2127 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302128
Olli Etuahof119a262016-08-19 15:54:22 +03002129 case EOpLength:
2130 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302131 resultArray = new TConstantUnion();
2132 resultArray->setFConst(VectorLength(operandArray, objectSize));
2133 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302134
Olli Etuahof119a262016-08-19 15:54:22 +03002135 case EOpTranspose:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302136 {
Olli Etuahof119a262016-08-19 15:54:22 +03002137 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302138 resultArray = new TConstantUnion[objectSize];
2139 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03002140 GetMatrix(operandArray, getType().getRows(), getType().getCols()).transpose();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302141 SetUnionArrayFromMatrix(result, resultArray);
2142 break;
2143 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302144
Olli Etuahof119a262016-08-19 15:54:22 +03002145 case EOpDeterminant:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302146 {
Olli Etuahof119a262016-08-19 15:54:22 +03002147 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302148 unsigned int size = getType().getNominalSize();
2149 ASSERT(size >= 2 && size <= 4);
2150 resultArray = new TConstantUnion();
2151 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
2152 break;
2153 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302154
Olli Etuahof119a262016-08-19 15:54:22 +03002155 case EOpInverse:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302156 {
Olli Etuahof119a262016-08-19 15:54:22 +03002157 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302158 unsigned int size = getType().getNominalSize();
2159 ASSERT(size >= 2 && size <= 4);
Olli Etuahof119a262016-08-19 15:54:22 +03002160 resultArray = new TConstantUnion[objectSize];
Arun Patoleab2b9a22015-07-06 18:27:56 +05302161 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
2162 SetUnionArrayFromMatrix(result, resultArray);
2163 break;
2164 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302165
Olli Etuahof119a262016-08-19 15:54:22 +03002166 case EOpPackSnorm2x16:
2167 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302168 ASSERT(getType().getNominalSize() == 2);
2169 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002170 resultArray->setUConst(
2171 gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05302172 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302173
Olli Etuahof119a262016-08-19 15:54:22 +03002174 case EOpUnpackSnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302175 {
Olli Etuahof119a262016-08-19 15:54:22 +03002176 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302177 resultArray = new TConstantUnion[2];
2178 float f1, f2;
2179 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
2180 resultArray[0].setFConst(f1);
2181 resultArray[1].setFConst(f2);
2182 break;
2183 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302184
Olli Etuahof119a262016-08-19 15:54:22 +03002185 case EOpPackUnorm2x16:
2186 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302187 ASSERT(getType().getNominalSize() == 2);
2188 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002189 resultArray->setUConst(
2190 gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05302191 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302192
Olli Etuahof119a262016-08-19 15:54:22 +03002193 case EOpUnpackUnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302194 {
Olli Etuahof119a262016-08-19 15:54:22 +03002195 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302196 resultArray = new TConstantUnion[2];
2197 float f1, f2;
2198 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
2199 resultArray[0].setFConst(f1);
2200 resultArray[1].setFConst(f2);
2201 break;
2202 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302203
Olli Etuahof119a262016-08-19 15:54:22 +03002204 case EOpPackHalf2x16:
2205 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302206 ASSERT(getType().getNominalSize() == 2);
2207 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002208 resultArray->setUConst(
2209 gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05302210 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302211
Olli Etuahof119a262016-08-19 15:54:22 +03002212 case EOpUnpackHalf2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302213 {
Olli Etuahof119a262016-08-19 15:54:22 +03002214 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302215 resultArray = new TConstantUnion[2];
2216 float f1, f2;
2217 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
2218 resultArray[0].setFConst(f1);
2219 resultArray[1].setFConst(f2);
2220 break;
2221 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302222
Olli Etuaho25aef452017-01-29 16:15:44 -08002223 case EOpPackUnorm4x8:
2224 {
2225 ASSERT(getType().getBasicType() == EbtFloat);
2226 resultArray = new TConstantUnion();
2227 resultArray->setUConst(
2228 gl::PackUnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2229 operandArray[2].getFConst(), operandArray[3].getFConst()));
2230 break;
2231 }
2232 case EOpPackSnorm4x8:
2233 {
2234 ASSERT(getType().getBasicType() == EbtFloat);
2235 resultArray = new TConstantUnion();
2236 resultArray->setUConst(
2237 gl::PackSnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2238 operandArray[2].getFConst(), operandArray[3].getFConst()));
2239 break;
2240 }
2241 case EOpUnpackUnorm4x8:
2242 {
2243 ASSERT(getType().getBasicType() == EbtUInt);
2244 resultArray = new TConstantUnion[4];
2245 float f[4];
2246 gl::UnpackUnorm4x8(operandArray[0].getUConst(), f);
2247 for (size_t i = 0; i < 4; ++i)
2248 {
2249 resultArray[i].setFConst(f[i]);
2250 }
2251 break;
2252 }
2253 case EOpUnpackSnorm4x8:
2254 {
2255 ASSERT(getType().getBasicType() == EbtUInt);
2256 resultArray = new TConstantUnion[4];
2257 float f[4];
2258 gl::UnpackSnorm4x8(operandArray[0].getUConst(), f);
2259 for (size_t i = 0; i < 4; ++i)
2260 {
2261 resultArray[i].setFConst(f[i]);
2262 }
2263 break;
2264 }
2265
Olli Etuahof119a262016-08-19 15:54:22 +03002266 default:
2267 UNREACHABLE();
2268 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302269 }
2270
2271 return resultArray;
2272}
2273
Olli Etuahof119a262016-08-19 15:54:22 +03002274TConstantUnion *TIntermConstantUnion::foldUnaryComponentWise(TOperator op,
2275 TDiagnostics *diagnostics)
Arun Patoleab2b9a22015-07-06 18:27:56 +05302276{
Olli Etuahof119a262016-08-19 15:54:22 +03002277 // Do unary operations where each component of the result is computed based on the corresponding
2278 // component of the operand. Also folds normalize, though the divisor in that case takes all
2279 // components into account.
Arun Patoleab2b9a22015-07-06 18:27:56 +05302280
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002281 const TConstantUnion *operandArray = getConstantValue();
Olli Etuahof119a262016-08-19 15:54:22 +03002282 ASSERT(operandArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -04002283
2284 size_t objectSize = getType().getObjectSize();
2285
Arun Patoleab2b9a22015-07-06 18:27:56 +05302286 TConstantUnion *resultArray = new TConstantUnion[objectSize];
2287 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302288 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002289 switch (op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302290 {
Olli Etuahof119a262016-08-19 15:54:22 +03002291 case EOpNegative:
2292 switch (getType().getBasicType())
2293 {
2294 case EbtFloat:
2295 resultArray[i].setFConst(-operandArray[i].getFConst());
2296 break;
2297 case EbtInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002298 if (operandArray[i] == std::numeric_limits<int>::min())
2299 {
2300 // The minimum representable integer doesn't have a positive
2301 // counterpart, rather the negation overflows and in ESSL is supposed to
2302 // wrap back to the minimum representable integer. Make sure that we
2303 // don't actually let the negation overflow, which has undefined
2304 // behavior in C++.
2305 resultArray[i].setIConst(std::numeric_limits<int>::min());
2306 }
2307 else
2308 {
2309 resultArray[i].setIConst(-operandArray[i].getIConst());
2310 }
Olli Etuahof119a262016-08-19 15:54:22 +03002311 break;
2312 case EbtUInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002313 if (operandArray[i] == 0x80000000u)
2314 {
2315 resultArray[i].setUConst(0x80000000u);
2316 }
2317 else
2318 {
2319 resultArray[i].setUConst(static_cast<unsigned int>(
2320 -static_cast<int>(operandArray[i].getUConst())));
2321 }
Olli Etuahof119a262016-08-19 15:54:22 +03002322 break;
2323 default:
2324 UNREACHABLE();
2325 return nullptr;
2326 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302327 break;
Arun Patolecdfa8f52015-06-30 17:48:25 +05302328
Olli Etuahof119a262016-08-19 15:54:22 +03002329 case EOpPositive:
2330 switch (getType().getBasicType())
2331 {
2332 case EbtFloat:
2333 resultArray[i].setFConst(operandArray[i].getFConst());
2334 break;
2335 case EbtInt:
2336 resultArray[i].setIConst(operandArray[i].getIConst());
2337 break;
2338 case EbtUInt:
2339 resultArray[i].setUConst(static_cast<unsigned int>(
2340 static_cast<int>(operandArray[i].getUConst())));
2341 break;
2342 default:
2343 UNREACHABLE();
2344 return nullptr;
2345 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302346 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302347
Olli Etuahof119a262016-08-19 15:54:22 +03002348 case EOpLogicalNot:
2349 switch (getType().getBasicType())
2350 {
2351 case EbtBool:
2352 resultArray[i].setBConst(!operandArray[i].getBConst());
2353 break;
2354 default:
2355 UNREACHABLE();
2356 return nullptr;
2357 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302358 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302359
Olli Etuahof119a262016-08-19 15:54:22 +03002360 case EOpBitwiseNot:
2361 switch (getType().getBasicType())
2362 {
2363 case EbtInt:
2364 resultArray[i].setIConst(~operandArray[i].getIConst());
2365 break;
2366 case EbtUInt:
2367 resultArray[i].setUConst(~operandArray[i].getUConst());
2368 break;
2369 default:
2370 UNREACHABLE();
2371 return nullptr;
2372 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302373 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302374
Olli Etuahof119a262016-08-19 15:54:22 +03002375 case EOpRadians:
2376 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302377 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
2378 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302379
Olli Etuahof119a262016-08-19 15:54:22 +03002380 case EOpDegrees:
2381 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302382 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
2383 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302384
Olli Etuahof119a262016-08-19 15:54:22 +03002385 case EOpSin:
2386 foldFloatTypeUnary(operandArray[i], &sinf, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302387 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302388
Olli Etuahof119a262016-08-19 15:54:22 +03002389 case EOpCos:
2390 foldFloatTypeUnary(operandArray[i], &cosf, &resultArray[i]);
2391 break;
2392
2393 case EOpTan:
2394 foldFloatTypeUnary(operandArray[i], &tanf, &resultArray[i]);
2395 break;
2396
2397 case EOpAsin:
2398 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to
2399 // 0.
2400 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2401 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2402 diagnostics, &resultArray[i]);
2403 else
2404 foldFloatTypeUnary(operandArray[i], &asinf, &resultArray[i]);
2405 break;
2406
2407 case EOpAcos:
2408 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to
2409 // 0.
2410 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2411 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2412 diagnostics, &resultArray[i]);
2413 else
2414 foldFloatTypeUnary(operandArray[i], &acosf, &resultArray[i]);
2415 break;
2416
2417 case EOpAtan:
2418 foldFloatTypeUnary(operandArray[i], &atanf, &resultArray[i]);
2419 break;
2420
2421 case EOpSinh:
2422 foldFloatTypeUnary(operandArray[i], &sinhf, &resultArray[i]);
2423 break;
2424
2425 case EOpCosh:
2426 foldFloatTypeUnary(operandArray[i], &coshf, &resultArray[i]);
2427 break;
2428
2429 case EOpTanh:
2430 foldFloatTypeUnary(operandArray[i], &tanhf, &resultArray[i]);
2431 break;
2432
2433 case EOpAsinh:
2434 foldFloatTypeUnary(operandArray[i], &asinhf, &resultArray[i]);
2435 break;
2436
2437 case EOpAcosh:
2438 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
2439 if (operandArray[i].getFConst() < 1.0f)
2440 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2441 diagnostics, &resultArray[i]);
2442 else
2443 foldFloatTypeUnary(operandArray[i], &acoshf, &resultArray[i]);
2444 break;
2445
2446 case EOpAtanh:
2447 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to
2448 // 0.
2449 if (fabsf(operandArray[i].getFConst()) >= 1.0f)
2450 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2451 diagnostics, &resultArray[i]);
2452 else
2453 foldFloatTypeUnary(operandArray[i], &atanhf, &resultArray[i]);
2454 break;
2455
2456 case EOpAbs:
2457 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302458 {
Olli Etuahof119a262016-08-19 15:54:22 +03002459 case EbtFloat:
2460 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
2461 break;
2462 case EbtInt:
2463 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
2464 break;
2465 default:
2466 UNREACHABLE();
2467 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302468 }
2469 break;
Olli Etuahof119a262016-08-19 15:54:22 +03002470
2471 case EOpSign:
2472 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302473 {
Olli Etuahof119a262016-08-19 15:54:22 +03002474 case EbtFloat:
2475 {
2476 float fConst = operandArray[i].getFConst();
2477 float fResult = 0.0f;
2478 if (fConst > 0.0f)
2479 fResult = 1.0f;
2480 else if (fConst < 0.0f)
2481 fResult = -1.0f;
2482 resultArray[i].setFConst(fResult);
2483 break;
2484 }
2485 case EbtInt:
2486 {
2487 int iConst = operandArray[i].getIConst();
2488 int iResult = 0;
2489 if (iConst > 0)
2490 iResult = 1;
2491 else if (iConst < 0)
2492 iResult = -1;
2493 resultArray[i].setIConst(iResult);
2494 break;
2495 }
2496 default:
2497 UNREACHABLE();
2498 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302499 }
2500 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302501
Olli Etuahof119a262016-08-19 15:54:22 +03002502 case EOpFloor:
2503 foldFloatTypeUnary(operandArray[i], &floorf, &resultArray[i]);
2504 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302505
Olli Etuahof119a262016-08-19 15:54:22 +03002506 case EOpTrunc:
2507 foldFloatTypeUnary(operandArray[i], &truncf, &resultArray[i]);
2508 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302509
Olli Etuahof119a262016-08-19 15:54:22 +03002510 case EOpRound:
2511 foldFloatTypeUnary(operandArray[i], &roundf, &resultArray[i]);
2512 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302513
Olli Etuahof119a262016-08-19 15:54:22 +03002514 case EOpRoundEven:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302515 {
Olli Etuahof119a262016-08-19 15:54:22 +03002516 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302517 float x = operandArray[i].getFConst();
2518 float result;
2519 float fractPart = modff(x, &result);
2520 if (fabsf(fractPart) == 0.5f)
2521 result = 2.0f * roundf(x / 2.0f);
2522 else
2523 result = roundf(x);
2524 resultArray[i].setFConst(result);
2525 break;
2526 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302527
Olli Etuahof119a262016-08-19 15:54:22 +03002528 case EOpCeil:
2529 foldFloatTypeUnary(operandArray[i], &ceilf, &resultArray[i]);
2530 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302531
Olli Etuahof119a262016-08-19 15:54:22 +03002532 case EOpFract:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302533 {
Olli Etuahof119a262016-08-19 15:54:22 +03002534 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302535 float x = operandArray[i].getFConst();
2536 resultArray[i].setFConst(x - floorf(x));
2537 break;
2538 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302539
Olli Etuahof119a262016-08-19 15:54:22 +03002540 case EOpIsNan:
2541 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302542 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
2543 break;
Arun Patole551279e2015-07-07 18:18:23 +05302544
Olli Etuahof119a262016-08-19 15:54:22 +03002545 case EOpIsInf:
2546 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302547 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
2548 break;
Arun Patole551279e2015-07-07 18:18:23 +05302549
Olli Etuahof119a262016-08-19 15:54:22 +03002550 case EOpFloatBitsToInt:
2551 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302552 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
2553 break;
Arun Patole551279e2015-07-07 18:18:23 +05302554
Olli Etuahof119a262016-08-19 15:54:22 +03002555 case EOpFloatBitsToUint:
2556 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302557 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
2558 break;
Arun Patole551279e2015-07-07 18:18:23 +05302559
Olli Etuahof119a262016-08-19 15:54:22 +03002560 case EOpIntBitsToFloat:
2561 ASSERT(getType().getBasicType() == EbtInt);
Arun Patole551279e2015-07-07 18:18:23 +05302562 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
2563 break;
Arun Patole551279e2015-07-07 18:18:23 +05302564
Olli Etuahof119a262016-08-19 15:54:22 +03002565 case EOpUintBitsToFloat:
2566 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patole551279e2015-07-07 18:18:23 +05302567 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
2568 break;
Arun Patole551279e2015-07-07 18:18:23 +05302569
Olli Etuahof119a262016-08-19 15:54:22 +03002570 case EOpExp:
2571 foldFloatTypeUnary(operandArray[i], &expf, &resultArray[i]);
2572 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302573
Olli Etuahof119a262016-08-19 15:54:22 +03002574 case EOpLog:
2575 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
2576 if (operandArray[i].getFConst() <= 0.0f)
2577 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2578 diagnostics, &resultArray[i]);
2579 else
2580 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2581 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302582
Olli Etuahof119a262016-08-19 15:54:22 +03002583 case EOpExp2:
2584 foldFloatTypeUnary(operandArray[i], &exp2f, &resultArray[i]);
2585 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302586
Olli Etuahof119a262016-08-19 15:54:22 +03002587 case EOpLog2:
2588 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
2589 // And log2f is not available on some plarforms like old android, so just using
2590 // log(x)/log(2) here.
2591 if (operandArray[i].getFConst() <= 0.0f)
2592 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2593 diagnostics, &resultArray[i]);
2594 else
2595 {
2596 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2597 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
2598 }
2599 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302600
Olli Etuahof119a262016-08-19 15:54:22 +03002601 case EOpSqrt:
2602 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
2603 if (operandArray[i].getFConst() < 0.0f)
2604 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2605 diagnostics, &resultArray[i]);
2606 else
2607 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2608 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302609
Olli Etuahof119a262016-08-19 15:54:22 +03002610 case EOpInverseSqrt:
2611 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
2612 // so getting the square root first using builtin function sqrt() and then taking
2613 // its inverse.
2614 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set
2615 // result to 0.
2616 if (operandArray[i].getFConst() <= 0.0f)
2617 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2618 diagnostics, &resultArray[i]);
2619 else
2620 {
2621 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2622 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
2623 }
2624 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302625
Olli Etuahod68924e2017-01-02 17:34:40 +00002626 case EOpLogicalNotComponentWise:
Olli Etuahof119a262016-08-19 15:54:22 +03002627 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302628 resultArray[i].setBConst(!operandArray[i].getBConst());
2629 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302630
Olli Etuahof119a262016-08-19 15:54:22 +03002631 case EOpNormalize:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302632 {
Olli Etuahof119a262016-08-19 15:54:22 +03002633 ASSERT(getType().getBasicType() == EbtFloat);
2634 float x = operandArray[i].getFConst();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302635 float length = VectorLength(operandArray, objectSize);
2636 if (length)
2637 resultArray[i].setFConst(x / length);
2638 else
Olli Etuahof119a262016-08-19 15:54:22 +03002639 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2640 diagnostics, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302641 break;
2642 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00002643 case EOpBitfieldReverse:
2644 {
2645 uint32_t value;
2646 if (getType().getBasicType() == EbtInt)
2647 {
2648 value = static_cast<uint32_t>(operandArray[i].getIConst());
2649 }
2650 else
2651 {
2652 ASSERT(getType().getBasicType() == EbtUInt);
2653 value = operandArray[i].getUConst();
2654 }
2655 uint32_t result = gl::BitfieldReverse(value);
2656 if (getType().getBasicType() == EbtInt)
2657 {
2658 resultArray[i].setIConst(static_cast<int32_t>(result));
2659 }
2660 else
2661 {
2662 resultArray[i].setUConst(result);
2663 }
2664 break;
2665 }
2666 case EOpBitCount:
2667 {
2668 uint32_t value;
2669 if (getType().getBasicType() == EbtInt)
2670 {
2671 value = static_cast<uint32_t>(operandArray[i].getIConst());
2672 }
2673 else
2674 {
2675 ASSERT(getType().getBasicType() == EbtUInt);
2676 value = operandArray[i].getUConst();
2677 }
2678 int result = gl::BitCount(value);
2679 resultArray[i].setIConst(result);
2680 break;
2681 }
2682 case EOpFindLSB:
2683 {
2684 uint32_t value;
2685 if (getType().getBasicType() == EbtInt)
2686 {
2687 value = static_cast<uint32_t>(operandArray[i].getIConst());
2688 }
2689 else
2690 {
2691 ASSERT(getType().getBasicType() == EbtUInt);
2692 value = operandArray[i].getUConst();
2693 }
2694 resultArray[i].setIConst(gl::FindLSB(value));
2695 break;
2696 }
2697 case EOpFindMSB:
2698 {
2699 uint32_t value;
2700 if (getType().getBasicType() == EbtInt)
2701 {
2702 int intValue = operandArray[i].getIConst();
2703 value = static_cast<uint32_t>(intValue);
2704 if (intValue < 0)
2705 {
2706 // Look for zero instead of one in value. This also handles the intValue ==
2707 // -1 special case, where the return value needs to be -1.
2708 value = ~value;
2709 }
2710 }
2711 else
2712 {
2713 ASSERT(getType().getBasicType() == EbtUInt);
2714 value = operandArray[i].getUConst();
2715 }
2716 resultArray[i].setIConst(gl::FindMSB(value));
2717 break;
2718 }
Olli Etuahof119a262016-08-19 15:54:22 +03002719 case EOpDFdx:
2720 case EOpDFdy:
2721 case EOpFwidth:
2722 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole0c5409f2015-07-08 15:17:53 +05302723 // Derivatives of constant arguments should be 0.
2724 resultArray[i].setFConst(0.0f);
2725 break;
Arun Patole0c5409f2015-07-08 15:17:53 +05302726
Olli Etuahof119a262016-08-19 15:54:22 +03002727 default:
2728 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302729 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302730 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04002731
Arun Patoleab2b9a22015-07-06 18:27:56 +05302732 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04002733}
2734
Olli Etuahof119a262016-08-19 15:54:22 +03002735void TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter,
2736 FloatTypeUnaryFunc builtinFunc,
2737 TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05302738{
2739 ASSERT(builtinFunc);
2740
Olli Etuahof119a262016-08-19 15:54:22 +03002741 ASSERT(getType().getBasicType() == EbtFloat);
2742 result->setFConst(builtinFunc(parameter.getFConst()));
Arun Patole9dea48f2015-04-02 11:45:09 +05302743}
2744
Jamie Madillb1a85f42014-08-19 15:23:24 -04002745// static
Olli Etuahof119a262016-08-19 15:54:22 +03002746TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
2747 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +05302748{
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002749 TOperator op = aggregate->getOp();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002750 TIntermSequence *arguments = aggregate->getSequence();
2751 unsigned int argsCount = static_cast<unsigned int>(arguments->size());
2752 std::vector<const TConstantUnion *> unionArrays(argsCount);
2753 std::vector<size_t> objectSizes(argsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03002754 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05302755 TBasicType basicType = EbtVoid;
2756 TSourceLoc loc;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002757 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302758 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002759 TIntermConstantUnion *argConstant = (*arguments)[i]->getAsConstantUnion();
2760 ASSERT(argConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05302761
2762 if (i == 0)
2763 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002764 basicType = argConstant->getType().getBasicType();
2765 loc = argConstant->getLine();
Arun Patole274f0702015-05-05 13:33:30 +05302766 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002767 unionArrays[i] = argConstant->getConstantValue();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002768 objectSizes[i] = argConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03002769 if (objectSizes[i] > maxObjectSize)
2770 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05302771 }
2772
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002773 if (!(*arguments)[0]->getAsTyped()->isMatrix() && aggregate->getOp() != EOpOuterProduct)
Arun Patole7fa33552015-06-10 15:15:18 +05302774 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002775 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole7fa33552015-06-10 15:15:18 +05302776 if (objectSizes[i] != maxObjectSize)
2777 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
2778 }
Arun Patole274f0702015-05-05 13:33:30 +05302779
Olli Etuahob43846e2015-06-02 18:18:57 +03002780 TConstantUnion *resultArray = nullptr;
Olli Etuaho51182ab2017-01-22 00:12:29 +00002781
2782 switch (op)
Arun Patole274f0702015-05-05 13:33:30 +05302783 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002784 case EOpAtan:
Arun Patole274f0702015-05-05 13:33:30 +05302785 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002786 ASSERT(basicType == EbtFloat);
2787 resultArray = new TConstantUnion[maxObjectSize];
2788 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302789 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002790 float y = unionArrays[0][i].getFConst();
2791 float x = unionArrays[1][i].getFConst();
2792 // Results are undefined if x and y are both 0.
2793 if (x == 0.0f && y == 0.0f)
2794 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2795 else
2796 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05302797 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002798 break;
2799 }
Arun Patolebf790422015-05-18 17:53:04 +05302800
Olli Etuaho51182ab2017-01-22 00:12:29 +00002801 case EOpPow:
2802 {
2803 ASSERT(basicType == EbtFloat);
2804 resultArray = new TConstantUnion[maxObjectSize];
2805 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302806 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002807 float x = unionArrays[0][i].getFConst();
2808 float y = unionArrays[1][i].getFConst();
2809 // Results are undefined if x < 0.
2810 // Results are undefined if x = 0 and y <= 0.
2811 if (x < 0.0f)
2812 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2813 else if (x == 0.0f && y <= 0.0f)
2814 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2815 else
2816 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05302817 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002818 break;
2819 }
Arun Patolebf790422015-05-18 17:53:04 +05302820
Olli Etuaho51182ab2017-01-22 00:12:29 +00002821 case EOpMod:
2822 {
2823 ASSERT(basicType == EbtFloat);
2824 resultArray = new TConstantUnion[maxObjectSize];
2825 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302826 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002827 float x = unionArrays[0][i].getFConst();
2828 float y = unionArrays[1][i].getFConst();
2829 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05302830 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002831 break;
2832 }
Arun Patolebf790422015-05-18 17:53:04 +05302833
Olli Etuaho51182ab2017-01-22 00:12:29 +00002834 case EOpMin:
2835 {
2836 resultArray = new TConstantUnion[maxObjectSize];
2837 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302838 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002839 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302840 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002841 case EbtFloat:
2842 resultArray[i].setFConst(
2843 std::min(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2844 break;
2845 case EbtInt:
2846 resultArray[i].setIConst(
2847 std::min(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2848 break;
2849 case EbtUInt:
2850 resultArray[i].setUConst(
2851 std::min(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2852 break;
2853 default:
2854 UNREACHABLE();
2855 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302856 }
2857 }
2858 break;
Arun Patole274f0702015-05-05 13:33:30 +05302859 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002860
2861 case EOpMax:
Arun Patole274f0702015-05-05 13:33:30 +05302862 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002863 resultArray = new TConstantUnion[maxObjectSize];
2864 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302865 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002866 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302867 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002868 case EbtFloat:
2869 resultArray[i].setFConst(
2870 std::max(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2871 break;
2872 case EbtInt:
2873 resultArray[i].setIConst(
2874 std::max(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2875 break;
2876 case EbtUInt:
2877 resultArray[i].setUConst(
2878 std::max(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2879 break;
2880 default:
2881 UNREACHABLE();
2882 break;
Arun Patole274f0702015-05-05 13:33:30 +05302883 }
2884 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002885 break;
Arun Patole274f0702015-05-05 13:33:30 +05302886 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002887
2888 case EOpStep:
2889 {
2890 ASSERT(basicType == EbtFloat);
2891 resultArray = new TConstantUnion[maxObjectSize];
2892 for (size_t i = 0; i < maxObjectSize; i++)
2893 resultArray[i].setFConst(
2894 unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f : 1.0f);
2895 break;
2896 }
2897
2898 case EOpLessThanComponentWise:
2899 {
2900 resultArray = new TConstantUnion[maxObjectSize];
2901 for (size_t i = 0; i < maxObjectSize; i++)
2902 {
2903 switch (basicType)
2904 {
2905 case EbtFloat:
2906 resultArray[i].setBConst(unionArrays[0][i].getFConst() <
2907 unionArrays[1][i].getFConst());
2908 break;
2909 case EbtInt:
2910 resultArray[i].setBConst(unionArrays[0][i].getIConst() <
2911 unionArrays[1][i].getIConst());
2912 break;
2913 case EbtUInt:
2914 resultArray[i].setBConst(unionArrays[0][i].getUConst() <
2915 unionArrays[1][i].getUConst());
2916 break;
2917 default:
2918 UNREACHABLE();
2919 break;
2920 }
2921 }
2922 break;
2923 }
2924
2925 case EOpLessThanEqualComponentWise:
2926 {
2927 resultArray = new TConstantUnion[maxObjectSize];
2928 for (size_t i = 0; i < maxObjectSize; i++)
2929 {
2930 switch (basicType)
2931 {
2932 case EbtFloat:
2933 resultArray[i].setBConst(unionArrays[0][i].getFConst() <=
2934 unionArrays[1][i].getFConst());
2935 break;
2936 case EbtInt:
2937 resultArray[i].setBConst(unionArrays[0][i].getIConst() <=
2938 unionArrays[1][i].getIConst());
2939 break;
2940 case EbtUInt:
2941 resultArray[i].setBConst(unionArrays[0][i].getUConst() <=
2942 unionArrays[1][i].getUConst());
2943 break;
2944 default:
2945 UNREACHABLE();
2946 break;
2947 }
2948 }
2949 break;
2950 }
2951
2952 case EOpGreaterThanComponentWise:
2953 {
2954 resultArray = new TConstantUnion[maxObjectSize];
2955 for (size_t i = 0; i < maxObjectSize; i++)
2956 {
2957 switch (basicType)
2958 {
2959 case EbtFloat:
2960 resultArray[i].setBConst(unionArrays[0][i].getFConst() >
2961 unionArrays[1][i].getFConst());
2962 break;
2963 case EbtInt:
2964 resultArray[i].setBConst(unionArrays[0][i].getIConst() >
2965 unionArrays[1][i].getIConst());
2966 break;
2967 case EbtUInt:
2968 resultArray[i].setBConst(unionArrays[0][i].getUConst() >
2969 unionArrays[1][i].getUConst());
2970 break;
2971 default:
2972 UNREACHABLE();
2973 break;
2974 }
2975 }
2976 break;
2977 }
2978 case EOpGreaterThanEqualComponentWise:
2979 {
2980 resultArray = new TConstantUnion[maxObjectSize];
2981 for (size_t i = 0; i < maxObjectSize; i++)
2982 {
2983 switch (basicType)
2984 {
2985 case EbtFloat:
2986 resultArray[i].setBConst(unionArrays[0][i].getFConst() >=
2987 unionArrays[1][i].getFConst());
2988 break;
2989 case EbtInt:
2990 resultArray[i].setBConst(unionArrays[0][i].getIConst() >=
2991 unionArrays[1][i].getIConst());
2992 break;
2993 case EbtUInt:
2994 resultArray[i].setBConst(unionArrays[0][i].getUConst() >=
2995 unionArrays[1][i].getUConst());
2996 break;
2997 default:
2998 UNREACHABLE();
2999 break;
3000 }
3001 }
3002 }
3003 break;
3004
3005 case EOpEqualComponentWise:
3006 {
3007 resultArray = new TConstantUnion[maxObjectSize];
3008 for (size_t i = 0; i < maxObjectSize; i++)
3009 {
3010 switch (basicType)
3011 {
3012 case EbtFloat:
3013 resultArray[i].setBConst(unionArrays[0][i].getFConst() ==
3014 unionArrays[1][i].getFConst());
3015 break;
3016 case EbtInt:
3017 resultArray[i].setBConst(unionArrays[0][i].getIConst() ==
3018 unionArrays[1][i].getIConst());
3019 break;
3020 case EbtUInt:
3021 resultArray[i].setBConst(unionArrays[0][i].getUConst() ==
3022 unionArrays[1][i].getUConst());
3023 break;
3024 case EbtBool:
3025 resultArray[i].setBConst(unionArrays[0][i].getBConst() ==
3026 unionArrays[1][i].getBConst());
3027 break;
3028 default:
3029 UNREACHABLE();
3030 break;
3031 }
3032 }
3033 break;
3034 }
3035
3036 case EOpNotEqualComponentWise:
3037 {
3038 resultArray = new TConstantUnion[maxObjectSize];
3039 for (size_t i = 0; i < maxObjectSize; i++)
3040 {
3041 switch (basicType)
3042 {
3043 case EbtFloat:
3044 resultArray[i].setBConst(unionArrays[0][i].getFConst() !=
3045 unionArrays[1][i].getFConst());
3046 break;
3047 case EbtInt:
3048 resultArray[i].setBConst(unionArrays[0][i].getIConst() !=
3049 unionArrays[1][i].getIConst());
3050 break;
3051 case EbtUInt:
3052 resultArray[i].setBConst(unionArrays[0][i].getUConst() !=
3053 unionArrays[1][i].getUConst());
3054 break;
3055 case EbtBool:
3056 resultArray[i].setBConst(unionArrays[0][i].getBConst() !=
3057 unionArrays[1][i].getBConst());
3058 break;
3059 default:
3060 UNREACHABLE();
3061 break;
3062 }
3063 }
3064 break;
3065 }
3066
3067 case EOpDistance:
3068 {
3069 ASSERT(basicType == EbtFloat);
3070 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
3071 resultArray = new TConstantUnion();
3072 for (size_t i = 0; i < maxObjectSize; i++)
3073 {
3074 float x = unionArrays[0][i].getFConst();
3075 float y = unionArrays[1][i].getFConst();
3076 distanceArray[i].setFConst(x - y);
3077 }
3078 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
3079 break;
3080 }
3081
3082 case EOpDot:
3083 ASSERT(basicType == EbtFloat);
3084 resultArray = new TConstantUnion();
3085 resultArray->setFConst(VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
3086 break;
3087
3088 case EOpCross:
3089 {
3090 ASSERT(basicType == EbtFloat && maxObjectSize == 3);
3091 resultArray = new TConstantUnion[maxObjectSize];
3092 float x0 = unionArrays[0][0].getFConst();
3093 float x1 = unionArrays[0][1].getFConst();
3094 float x2 = unionArrays[0][2].getFConst();
3095 float y0 = unionArrays[1][0].getFConst();
3096 float y1 = unionArrays[1][1].getFConst();
3097 float y2 = unionArrays[1][2].getFConst();
3098 resultArray[0].setFConst(x1 * y2 - y1 * x2);
3099 resultArray[1].setFConst(x2 * y0 - y2 * x0);
3100 resultArray[2].setFConst(x0 * y1 - y0 * x1);
3101 break;
3102 }
3103
3104 case EOpReflect:
3105 {
3106 ASSERT(basicType == EbtFloat);
3107 // genType reflect (genType I, genType N) :
3108 // For the incident vector I and surface orientation N, returns the reflection
3109 // direction:
3110 // I - 2 * dot(N, I) * N.
3111 resultArray = new TConstantUnion[maxObjectSize];
3112 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
3113 for (size_t i = 0; i < maxObjectSize; i++)
3114 {
3115 float result = unionArrays[0][i].getFConst() -
3116 2.0f * dotProduct * unionArrays[1][i].getFConst();
3117 resultArray[i].setFConst(result);
3118 }
3119 break;
3120 }
3121
3122 case EOpMulMatrixComponentWise:
3123 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003124 ASSERT(basicType == EbtFloat && (*arguments)[0]->getAsTyped()->isMatrix() &&
3125 (*arguments)[1]->getAsTyped()->isMatrix());
Olli Etuaho51182ab2017-01-22 00:12:29 +00003126 // Perform component-wise matrix multiplication.
3127 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003128 int size = (*arguments)[0]->getAsTyped()->getNominalSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003129 angle::Matrix<float> result =
3130 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
3131 SetUnionArrayFromMatrix(result, resultArray);
3132 break;
3133 }
3134
3135 case EOpOuterProduct:
3136 {
3137 ASSERT(basicType == EbtFloat);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003138 size_t numRows = (*arguments)[0]->getAsTyped()->getType().getObjectSize();
3139 size_t numCols = (*arguments)[1]->getAsTyped()->getType().getObjectSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003140 resultArray = new TConstantUnion[numRows * numCols];
3141 angle::Matrix<float> result =
3142 GetMatrix(unionArrays[0], static_cast<int>(numRows), 1)
3143 .outerProduct(GetMatrix(unionArrays[1], 1, static_cast<int>(numCols)));
3144 SetUnionArrayFromMatrix(result, resultArray);
3145 break;
3146 }
3147
3148 case EOpClamp:
3149 {
3150 resultArray = new TConstantUnion[maxObjectSize];
3151 for (size_t i = 0; i < maxObjectSize; i++)
3152 {
3153 switch (basicType)
3154 {
3155 case EbtFloat:
3156 {
3157 float x = unionArrays[0][i].getFConst();
3158 float min = unionArrays[1][i].getFConst();
3159 float max = unionArrays[2][i].getFConst();
3160 // Results are undefined if min > max.
3161 if (min > max)
3162 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3163 &resultArray[i]);
3164 else
3165 resultArray[i].setFConst(gl::clamp(x, min, max));
3166 break;
3167 }
3168
3169 case EbtInt:
3170 {
3171 int x = unionArrays[0][i].getIConst();
3172 int min = unionArrays[1][i].getIConst();
3173 int max = unionArrays[2][i].getIConst();
3174 // Results are undefined if min > max.
3175 if (min > max)
3176 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3177 &resultArray[i]);
3178 else
3179 resultArray[i].setIConst(gl::clamp(x, min, max));
3180 break;
3181 }
3182 case EbtUInt:
3183 {
3184 unsigned int x = unionArrays[0][i].getUConst();
3185 unsigned int min = unionArrays[1][i].getUConst();
3186 unsigned int max = unionArrays[2][i].getUConst();
3187 // Results are undefined if min > max.
3188 if (min > max)
3189 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3190 &resultArray[i]);
3191 else
3192 resultArray[i].setUConst(gl::clamp(x, min, max));
3193 break;
3194 }
3195 default:
3196 UNREACHABLE();
3197 break;
3198 }
3199 }
3200 break;
3201 }
3202
3203 case EOpMix:
3204 {
3205 ASSERT(basicType == EbtFloat);
3206 resultArray = new TConstantUnion[maxObjectSize];
3207 for (size_t i = 0; i < maxObjectSize; i++)
3208 {
3209 float x = unionArrays[0][i].getFConst();
3210 float y = unionArrays[1][i].getFConst();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003211 TBasicType type = (*arguments)[2]->getAsTyped()->getType().getBasicType();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003212 if (type == EbtFloat)
3213 {
3214 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
3215 float a = unionArrays[2][i].getFConst();
3216 resultArray[i].setFConst(x * (1.0f - a) + y * a);
3217 }
3218 else // 3rd parameter is EbtBool
3219 {
3220 ASSERT(type == EbtBool);
3221 // Selects which vector each returned component comes from.
3222 // For a component of a that is false, the corresponding component of x is
3223 // returned.
3224 // For a component of a that is true, the corresponding component of y is
3225 // returned.
3226 bool a = unionArrays[2][i].getBConst();
3227 resultArray[i].setFConst(a ? y : x);
3228 }
3229 }
3230 break;
3231 }
3232
3233 case EOpSmoothStep:
3234 {
3235 ASSERT(basicType == EbtFloat);
3236 resultArray = new TConstantUnion[maxObjectSize];
3237 for (size_t i = 0; i < maxObjectSize; i++)
3238 {
3239 float edge0 = unionArrays[0][i].getFConst();
3240 float edge1 = unionArrays[1][i].getFConst();
3241 float x = unionArrays[2][i].getFConst();
3242 // Results are undefined if edge0 >= edge1.
3243 if (edge0 >= edge1)
3244 {
3245 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3246 }
3247 else
3248 {
3249 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
3250 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
3251 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
3252 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
3253 }
3254 }
3255 break;
3256 }
3257
Olli Etuaho74da73f2017-02-01 15:37:48 +00003258 case EOpLdexp:
3259 {
3260 resultArray = new TConstantUnion[maxObjectSize];
3261 for (size_t i = 0; i < maxObjectSize; i++)
3262 {
3263 float x = unionArrays[0][i].getFConst();
3264 int exp = unionArrays[1][i].getIConst();
3265 if (exp > 128)
3266 {
3267 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3268 }
3269 else
3270 {
3271 resultArray[i].setFConst(gl::Ldexp(x, exp));
3272 }
3273 }
3274 break;
3275 }
3276
Jamie Madille72595b2017-06-06 15:12:26 -04003277 case EOpFaceforward:
Olli Etuaho51182ab2017-01-22 00:12:29 +00003278 {
3279 ASSERT(basicType == EbtFloat);
3280 // genType faceforward(genType N, genType I, genType Nref) :
3281 // If dot(Nref, I) < 0 return N, otherwise return -N.
3282 resultArray = new TConstantUnion[maxObjectSize];
3283 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
3284 for (size_t i = 0; i < maxObjectSize; i++)
3285 {
3286 if (dotProduct < 0)
3287 resultArray[i].setFConst(unionArrays[0][i].getFConst());
3288 else
3289 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
3290 }
3291 break;
3292 }
3293
3294 case EOpRefract:
3295 {
3296 ASSERT(basicType == EbtFloat);
3297 // genType refract(genType I, genType N, float eta) :
3298 // For the incident vector I and surface normal N, and the ratio of indices of
3299 // refraction eta,
3300 // return the refraction vector. The result is computed by
3301 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
3302 // if (k < 0.0)
3303 // return genType(0.0)
3304 // else
3305 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
3306 resultArray = new TConstantUnion[maxObjectSize];
3307 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
3308 for (size_t i = 0; i < maxObjectSize; i++)
3309 {
3310 float eta = unionArrays[2][i].getFConst();
3311 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
3312 if (k < 0.0f)
3313 resultArray[i].setFConst(0.0f);
3314 else
3315 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
3316 (eta * dotProduct + sqrtf(k)) *
3317 unionArrays[1][i].getFConst());
3318 }
3319 break;
3320 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00003321 case EOpBitfieldExtract:
3322 {
3323 resultArray = new TConstantUnion[maxObjectSize];
3324 for (size_t i = 0; i < maxObjectSize; ++i)
3325 {
3326 int offset = unionArrays[1][0].getIConst();
3327 int bits = unionArrays[2][0].getIConst();
3328 if (bits == 0)
3329 {
3330 if (aggregate->getBasicType() == EbtInt)
3331 {
3332 resultArray[i].setIConst(0);
3333 }
3334 else
3335 {
3336 ASSERT(aggregate->getBasicType() == EbtUInt);
3337 resultArray[i].setUConst(0);
3338 }
3339 }
3340 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3341 {
3342 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3343 &resultArray[i]);
3344 }
3345 else
3346 {
3347 // bits can be 32 here, so we need to avoid bit shift overflow.
3348 uint32_t maskMsb = 1u << (bits - 1);
3349 uint32_t mask = ((maskMsb - 1u) | maskMsb) << offset;
3350 if (aggregate->getBasicType() == EbtInt)
3351 {
3352 uint32_t value = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3353 uint32_t resultUnsigned = (value & mask) >> offset;
3354 if ((resultUnsigned & maskMsb) != 0)
3355 {
3356 // The most significant bits (from bits+1 to the most significant bit)
3357 // should be set to 1.
3358 uint32_t higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;
3359 resultUnsigned |= higherBitsMask;
3360 }
3361 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3362 }
3363 else
3364 {
3365 ASSERT(aggregate->getBasicType() == EbtUInt);
3366 uint32_t value = unionArrays[0][i].getUConst();
3367 resultArray[i].setUConst((value & mask) >> offset);
3368 }
3369 }
3370 }
3371 break;
3372 }
3373 case EOpBitfieldInsert:
3374 {
3375 resultArray = new TConstantUnion[maxObjectSize];
3376 for (size_t i = 0; i < maxObjectSize; ++i)
3377 {
3378 int offset = unionArrays[2][0].getIConst();
3379 int bits = unionArrays[3][0].getIConst();
3380 if (bits == 0)
3381 {
3382 if (aggregate->getBasicType() == EbtInt)
3383 {
3384 int32_t base = unionArrays[0][i].getIConst();
3385 resultArray[i].setIConst(base);
3386 }
3387 else
3388 {
3389 ASSERT(aggregate->getBasicType() == EbtUInt);
3390 uint32_t base = unionArrays[0][i].getUConst();
3391 resultArray[i].setUConst(base);
3392 }
3393 }
3394 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3395 {
3396 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3397 &resultArray[i]);
3398 }
3399 else
3400 {
3401 // bits can be 32 here, so we need to avoid bit shift overflow.
3402 uint32_t maskMsb = 1u << (bits - 1);
3403 uint32_t insertMask = ((maskMsb - 1u) | maskMsb) << offset;
3404 uint32_t baseMask = ~insertMask;
3405 if (aggregate->getBasicType() == EbtInt)
3406 {
3407 uint32_t base = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3408 uint32_t insert = static_cast<uint32_t>(unionArrays[1][i].getIConst());
3409 uint32_t resultUnsigned =
3410 (base & baseMask) | ((insert << offset) & insertMask);
3411 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3412 }
3413 else
3414 {
3415 ASSERT(aggregate->getBasicType() == EbtUInt);
3416 uint32_t base = unionArrays[0][i].getUConst();
3417 uint32_t insert = unionArrays[1][i].getUConst();
3418 resultArray[i].setUConst((base & baseMask) |
3419 ((insert << offset) & insertMask));
3420 }
3421 }
3422 }
3423 break;
3424 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00003425
3426 default:
3427 UNREACHABLE();
3428 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05303429 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003430 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05303431}
3432
Jamie Madill45bcc782016-11-07 13:58:48 -05003433} // namespace sh