blob: ee2b85c552df5107e4c45a826db8ccc8a7164146 [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 Etuaho2c9cc8b2018-01-09 16:13:02 +0200185TIntermExpression::TIntermExpression(const TType &t) : TIntermTyped(), mType(t)
186{
187}
188
189void TIntermExpression::setTypePreservePrecision(const TType &t)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300190{
191 TPrecision precision = getPrecision();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500192 mType = t;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300193 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
194 mType.setPrecision(precision);
195}
196
Jamie Madillb1a85f42014-08-19 15:23:24 -0400197#define REPLACE_IF_IS(node, type, original, replacement) \
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500198 if (node == original) \
199 { \
200 node = static_cast<type *>(replacement); \
201 return true; \
Jamie Madillb1a85f42014-08-19 15:23:24 -0400202 }
203
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500204bool TIntermLoop::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400205{
Olli Etuaho3cbb27a2016-07-14 11:55:48 +0300206 ASSERT(original != nullptr); // This risks replacing multiple children.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400207 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
208 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
209 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100210 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400211 return false;
212}
213
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500214bool TIntermBranch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400215{
216 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
217 return false;
218}
219
Olli Etuahob6fa0432016-09-28 16:28:05 +0100220bool TIntermSwizzle::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
221{
222 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
223 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
224 return false;
225}
226
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500227bool TIntermBinary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400228{
229 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
230 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
231 return false;
232}
233
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500234bool TIntermUnary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400235{
Olli Etuahoa2234302016-08-31 12:05:39 +0300236 ASSERT(original->getAsTyped()->getType() == replacement->getAsTyped()->getType());
Jamie Madillb1a85f42014-08-19 15:23:24 -0400237 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
238 return false;
239}
240
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000241bool TIntermInvariantDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
242{
243 REPLACE_IF_IS(mSymbol, TIntermSymbol, original, replacement);
244 return false;
245}
246
Olli Etuaho336b1472016-10-05 16:37:55 +0100247bool TIntermFunctionDefinition::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
248{
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000249 REPLACE_IF_IS(mPrototype, TIntermFunctionPrototype, original, replacement);
Olli Etuaho336b1472016-10-05 16:37:55 +0100250 REPLACE_IF_IS(mBody, TIntermBlock, original, replacement);
251 return false;
252}
253
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500254bool TIntermAggregate::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400255{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100256 return replaceChildNodeInternal(original, replacement);
257}
258
259bool TIntermBlock::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
260{
261 return replaceChildNodeInternal(original, replacement);
262}
263
Olli Etuaho16c745a2017-01-16 17:02:27 +0000264bool TIntermFunctionPrototype::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
265{
266 return replaceChildNodeInternal(original, replacement);
267}
268
Olli Etuaho13389b62016-10-16 11:48:18 +0100269bool TIntermDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
270{
271 return replaceChildNodeInternal(original, replacement);
272}
273
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100274bool TIntermAggregateBase::replaceChildNodeInternal(TIntermNode *original, TIntermNode *replacement)
275{
276 for (size_t ii = 0; ii < getSequence()->size(); ++ii)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400277 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100278 REPLACE_IF_IS((*getSequence())[ii], TIntermNode, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400279 }
280 return false;
281}
282
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100283bool TIntermAggregateBase::replaceChildNodeWithMultiple(TIntermNode *original,
284 const TIntermSequence &replacements)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300285{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100286 for (auto it = getSequence()->begin(); it < getSequence()->end(); ++it)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300287 {
288 if (*it == original)
289 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100290 it = getSequence()->erase(it);
291 getSequence()->insert(it, replacements.begin(), replacements.end());
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300292 return true;
293 }
294 }
295 return false;
296}
297
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100298bool TIntermAggregateBase::insertChildNodes(TIntermSequence::size_type position,
299 const TIntermSequence &insertions)
Olli Etuahoa6f22092015-05-08 18:31:10 +0300300{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100301 if (position > getSequence()->size())
Olli Etuahoa6f22092015-05-08 18:31:10 +0300302 {
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300303 return false;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300304 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100305 auto it = getSequence()->begin() + position;
306 getSequence()->insert(it, insertions.begin(), insertions.end());
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300307 return true;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300308}
309
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200310TIntermSymbol::TIntermSymbol(const TVariable *variable) : TIntermTyped(), mVariable(variable)
Olli Etuaho195be942017-12-04 23:40:14 +0200311{
Olli Etuaho195be942017-12-04 23:40:14 +0200312}
313
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200314bool TIntermSymbol::hasConstantValue() const
315{
316 return variable().getConstPointer() != nullptr;
317}
318
319const TConstantUnion *TIntermSymbol::getConstantValue() const
320{
321 return variable().getConstPointer();
322}
323
Olli Etuahob6af22b2017-12-15 14:05:44 +0200324const TSymbolUniqueId &TIntermSymbol::uniqueId() const
325{
326 return mVariable->uniqueId();
327}
328
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200329const TString &TIntermSymbol::getName() const
330{
331 return mVariable->name();
332}
333
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200334const TType &TIntermSymbol::getType() const
335{
336 return mVariable->getType();
337}
338
Olli Etuahofe486322017-03-21 09:30:54 +0000339TIntermAggregate *TIntermAggregate::CreateFunctionCall(const TFunction &func,
340 TIntermSequence *arguments)
341{
Olli Etuaho0c371002017-12-13 17:00:25 +0400342 return new TIntermAggregate(&func, func.getReturnType(), EOpCallFunctionInAST, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000343}
344
Olli Etuaho0c371002017-12-13 17:00:25 +0400345TIntermAggregate *TIntermAggregate::CreateRawFunctionCall(const TFunction &func,
346 TIntermSequence *arguments)
Olli Etuahofe486322017-03-21 09:30:54 +0000347{
Olli Etuaho0c371002017-12-13 17:00:25 +0400348 return new TIntermAggregate(&func, func.getReturnType(), EOpCallInternalRawFunction, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000349}
350
351TIntermAggregate *TIntermAggregate::CreateBuiltInFunctionCall(const TFunction &func,
352 TIntermSequence *arguments)
353{
354 TIntermAggregate *callNode =
Olli Etuaho0c371002017-12-13 17:00:25 +0400355 new TIntermAggregate(&func, func.getReturnType(), EOpCallBuiltInFunction, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000356 // Note that name needs to be set before texture function type is determined.
357 callNode->setBuiltInFunctionPrecision();
358 return callNode;
359}
360
361TIntermAggregate *TIntermAggregate::CreateConstructor(const TType &type,
Olli Etuahofe486322017-03-21 09:30:54 +0000362 TIntermSequence *arguments)
363{
Olli Etuaho0c371002017-12-13 17:00:25 +0400364 return new TIntermAggregate(nullptr, type, EOpConstruct, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000365}
366
367TIntermAggregate *TIntermAggregate::Create(const TType &type,
368 TOperator op,
369 TIntermSequence *arguments)
370{
Olli Etuahofe486322017-03-21 09:30:54 +0000371 ASSERT(op != EOpCallFunctionInAST); // Should use CreateFunctionCall
Olli Etuaho0c371002017-12-13 17:00:25 +0400372 ASSERT(op != EOpCallInternalRawFunction); // Should use CreateRawFunctionCall
Olli Etuahofe486322017-03-21 09:30:54 +0000373 ASSERT(op != EOpCallBuiltInFunction); // Should use CreateBuiltInFunctionCall
Olli Etuaho0c371002017-12-13 17:00:25 +0400374 ASSERT(op != EOpConstruct); // Should use CreateConstructor
375 return new TIntermAggregate(nullptr, type, op, arguments);
Olli Etuahofe486322017-03-21 09:30:54 +0000376}
377
Olli Etuaho0c371002017-12-13 17:00:25 +0400378TIntermAggregate::TIntermAggregate(const TFunction *func,
379 const TType &type,
380 TOperator op,
381 TIntermSequence *arguments)
382 : TIntermOperator(op),
383 mUseEmulatedFunction(false),
384 mGotPrecisionFromChildren(false),
385 mFunction(func)
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800386{
387 if (arguments != nullptr)
388 {
389 mArguments.swap(*arguments);
390 }
Olli Etuaho1bb85282017-12-14 13:39:53 +0200391 ASSERT(mFunction == nullptr || mFunction->symbolType() != SymbolType::Empty);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800392 setTypePrecisionAndQualifier(type);
393}
394
395void TIntermAggregate::setTypePrecisionAndQualifier(const TType &type)
396{
397 setType(type);
398 mType.setQualifier(EvqTemporary);
399 if (!isFunctionCall())
400 {
401 if (isConstructor())
402 {
403 // Structs should not be precision qualified, the individual members may be.
404 // Built-in types on the other hand should be precision qualified.
Olli Etuaho8fab3202017-05-08 18:22:22 +0300405 if (getBasicType() != EbtStruct)
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800406 {
407 setPrecisionFromChildren();
408 }
409 }
410 else
411 {
412 setPrecisionForBuiltInOp();
413 }
414 if (areChildrenConstQualified())
415 {
416 mType.setQualifier(EvqConst);
417 }
418 }
419}
420
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200421bool TIntermAggregate::areChildrenConstQualified()
422{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800423 for (TIntermNode *&arg : mArguments)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200424 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800425 TIntermTyped *typedArg = arg->getAsTyped();
426 if (typedArg && typedArg->getQualifier() != EvqConst)
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200427 {
428 return false;
429 }
430 }
431 return true;
432}
433
Olli Etuahod2a67b92014-10-21 16:42:57 +0300434void TIntermAggregate::setPrecisionFromChildren()
435{
Olli Etuahoa4aa4e32015-06-04 15:54:30 +0300436 mGotPrecisionFromChildren = true;
Olli Etuahod2a67b92014-10-21 16:42:57 +0300437 if (getBasicType() == EbtBool)
438 {
439 mType.setPrecision(EbpUndefined);
440 return;
441 }
442
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500443 TPrecision precision = EbpUndefined;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800444 TIntermSequence::iterator childIter = mArguments.begin();
445 while (childIter != mArguments.end())
Olli Etuahod2a67b92014-10-21 16:42:57 +0300446 {
447 TIntermTyped *typed = (*childIter)->getAsTyped();
448 if (typed)
449 precision = GetHigherPrecision(typed->getPrecision(), precision);
450 ++childIter;
451 }
452 mType.setPrecision(precision);
453}
454
Olli Etuaho9250cb22017-01-21 10:51:27 +0000455void TIntermAggregate::setPrecisionForBuiltInOp()
456{
457 ASSERT(!isConstructor());
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800458 ASSERT(!isFunctionCall());
Olli Etuaho9250cb22017-01-21 10:51:27 +0000459 if (!setPrecisionForSpecialBuiltInOp())
460 {
461 setPrecisionFromChildren();
462 }
463}
464
465bool TIntermAggregate::setPrecisionForSpecialBuiltInOp()
466{
467 switch (mOp)
468 {
469 case EOpBitfieldExtract:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800470 mType.setPrecision(mArguments[0]->getAsTyped()->getPrecision());
471 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000472 return true;
473 case EOpBitfieldInsert:
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800474 mType.setPrecision(GetHigherPrecision(mArguments[0]->getAsTyped()->getPrecision(),
475 mArguments[1]->getAsTyped()->getPrecision()));
476 mGotPrecisionFromChildren = true;
Olli Etuaho9250cb22017-01-21 10:51:27 +0000477 return true;
478 case EOpUaddCarry:
479 case EOpUsubBorrow:
480 mType.setPrecision(EbpHigh);
481 return true;
482 default:
483 return false;
484 }
485}
486
Olli Etuahod2a67b92014-10-21 16:42:57 +0300487void TIntermAggregate::setBuiltInFunctionPrecision()
488{
489 // All built-ins returning bool should be handled as ops, not functions.
490 ASSERT(getBasicType() != EbtBool);
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800491 ASSERT(mOp == EOpCallBuiltInFunction);
Olli Etuahod2a67b92014-10-21 16:42:57 +0300492
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800493 TPrecision precision = EbpUndefined;
494 for (TIntermNode *arg : mArguments)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300495 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800496 TIntermTyped *typed = arg->getAsTyped();
Olli Etuahod2a67b92014-10-21 16:42:57 +0300497 // ESSL spec section 8: texture functions get their precision from the sampler.
498 if (typed && IsSampler(typed->getBasicType()))
499 {
500 precision = typed->getPrecision();
501 break;
502 }
Olli Etuahod2a67b92014-10-21 16:42:57 +0300503 }
504 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
505 // All other functions that take a sampler are assumed to be texture functions.
Olli Etuahobed35d72017-12-20 16:36:26 +0200506 if (mFunction->name().find("textureSize") == 0)
Olli Etuahod2a67b92014-10-21 16:42:57 +0300507 mType.setPrecision(EbpHigh);
508 else
509 mType.setPrecision(precision);
510}
511
Olli Etuahof2209f72017-04-01 12:45:55 +0300512TString TIntermAggregate::getSymbolTableMangledName() const
513{
514 ASSERT(!isConstructor());
515 switch (mOp)
516 {
517 case EOpCallInternalRawFunction:
518 case EOpCallBuiltInFunction:
519 case EOpCallFunctionInAST:
Olli Etuahobed35d72017-12-20 16:36:26 +0200520 return TFunction::GetMangledNameFromCall(mFunction->name(), mArguments);
Olli Etuahof2209f72017-04-01 12:45:55 +0300521 default:
522 TString opString = GetOperatorString(mOp);
523 return TFunction::GetMangledNameFromCall(opString, mArguments);
524 }
525}
526
Olli Etuaho0c371002017-12-13 17:00:25 +0400527const char *TIntermAggregate::functionName() const
528{
529 ASSERT(!isConstructor());
530 switch (mOp)
531 {
532 case EOpCallInternalRawFunction:
533 case EOpCallBuiltInFunction:
534 case EOpCallFunctionInAST:
Olli Etuahobed35d72017-12-20 16:36:26 +0200535 return mFunction->name().c_str();
Olli Etuaho0c371002017-12-13 17:00:25 +0400536 default:
537 return GetOperatorString(mOp);
538 }
539}
540
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200541bool TIntermAggregate::hasConstantValue() const
542{
543 if (!isConstructor())
544 {
545 return false;
546 }
547 for (TIntermNode *constructorArg : mArguments)
548 {
549 if (!constructorArg->getAsTyped()->hasConstantValue())
550 {
551 return false;
552 }
553 }
554 return true;
555}
556
557const TConstantUnion *TIntermAggregate::getConstantValue() const
558{
559 if (!hasConstantValue())
560 {
561 return nullptr;
562 }
563 ASSERT(isConstructor());
564 ASSERT(mArguments.size() > 0u);
565
566 TConstantUnion *constArray = nullptr;
567 if (isArray())
568 {
569 size_t elementSize = mArguments.front()->getAsTyped()->getType().getObjectSize();
570 constArray = new TConstantUnion[elementSize * getOutermostArraySize()];
571
572 size_t elementOffset = 0u;
573 for (TIntermNode *constructorArg : mArguments)
574 {
575 const TConstantUnion *elementConstArray =
576 constructorArg->getAsTyped()->getConstantValue();
577 ASSERT(elementConstArray);
578 size_t elementSizeBytes = sizeof(TConstantUnion) * elementSize;
579 memcpy(static_cast<void *>(&constArray[elementOffset]),
580 static_cast<const void *>(elementConstArray), elementSizeBytes);
581 elementOffset += elementSize;
582 }
583 return constArray;
584 }
585
586 size_t resultSize = getType().getObjectSize();
587 constArray = new TConstantUnion[resultSize];
588 TBasicType basicType = getBasicType();
589
590 size_t resultIndex = 0u;
591
592 if (mArguments.size() == 1u)
593 {
594 TIntermNode *argument = mArguments.front();
595 TIntermTyped *argumentTyped = argument->getAsTyped();
596 const TConstantUnion *argumentConstantValue = argumentTyped->getConstantValue();
597 // Check the special case of constructing a matrix diagonal from a single scalar,
598 // or a vector from a single scalar.
599 if (argumentTyped->getType().getObjectSize() == 1u)
600 {
601 if (isMatrix())
602 {
603 int resultCols = getType().getCols();
604 int resultRows = getType().getRows();
605 for (int col = 0; col < resultCols; ++col)
606 {
607 for (int row = 0; row < resultRows; ++row)
608 {
609 if (col == row)
610 {
611 constArray[resultIndex].cast(basicType, argumentConstantValue[0]);
612 }
613 else
614 {
615 constArray[resultIndex].setFConst(0.0f);
616 }
617 ++resultIndex;
618 }
619 }
620 }
621 else
622 {
623 while (resultIndex < resultSize)
624 {
625 constArray[resultIndex].cast(basicType, argumentConstantValue[0]);
626 ++resultIndex;
627 }
628 }
629 ASSERT(resultIndex == resultSize);
630 return constArray;
631 }
632 else if (isMatrix() && argumentTyped->isMatrix())
633 {
634 // The special case of constructing a matrix from a matrix.
635 int argumentCols = argumentTyped->getType().getCols();
636 int argumentRows = argumentTyped->getType().getRows();
637 int resultCols = getType().getCols();
638 int resultRows = getType().getRows();
639 for (int col = 0; col < resultCols; ++col)
640 {
641 for (int row = 0; row < resultRows; ++row)
642 {
643 if (col < argumentCols && row < argumentRows)
644 {
645 constArray[resultIndex].cast(
646 basicType, argumentConstantValue[col * argumentRows + row]);
647 }
648 else if (col == row)
649 {
650 constArray[resultIndex].setFConst(1.0f);
651 }
652 else
653 {
654 constArray[resultIndex].setFConst(0.0f);
655 }
656 ++resultIndex;
657 }
658 }
659 ASSERT(resultIndex == resultSize);
660 return constArray;
661 }
662 }
663
664 for (TIntermNode *argument : mArguments)
665 {
666 TIntermTyped *argumentTyped = argument->getAsTyped();
667 size_t argumentSize = argumentTyped->getType().getObjectSize();
668 const TConstantUnion *argumentConstantValue = argumentTyped->getConstantValue();
669 for (size_t i = 0u; i < argumentSize; ++i)
670 {
671 if (resultIndex >= resultSize)
672 break;
673 constArray[resultIndex].cast(basicType, argumentConstantValue[i]);
674 ++resultIndex;
675 }
676 }
677 ASSERT(resultIndex == resultSize);
678 return constArray;
679}
680
Olli Etuahoa22aa4e2017-05-24 18:17:23 +0300681bool TIntermAggregate::hasSideEffects() const
682{
Olli Etuahoea78d2b2018-01-09 12:55:27 +0200683 if (getQualifier() == EvqConst)
684 {
685 return false;
686 }
687 bool calledFunctionHasNoSideEffects =
688 isFunctionCall() && mFunction != nullptr && mFunction->isKnownToNotHaveSideEffects();
689 if (calledFunctionHasNoSideEffects || isConstructor())
Olli Etuahoa22aa4e2017-05-24 18:17:23 +0300690 {
691 for (TIntermNode *arg : mArguments)
692 {
693 if (arg->getAsTyped()->hasSideEffects())
694 {
695 return true;
696 }
697 }
698 return false;
699 }
700 // Conservatively assume most aggregate operators have side-effects
701 return true;
702}
703
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100704void TIntermBlock::appendStatement(TIntermNode *statement)
705{
Olli Etuaho923ecef2017-10-11 12:01:38 +0300706 // Declaration nodes with no children can appear if it was an empty declaration or if all the
707 // declarators just added constants to the symbol table instead of generating code. We still
708 // need to add the declaration to the AST in that case because it might be relevant to the
709 // validity of switch/case.
710 if (statement != nullptr)
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100711 {
712 mStatements.push_back(statement);
713 }
714}
715
Olli Etuaho16c745a2017-01-16 17:02:27 +0000716void TIntermFunctionPrototype::appendParameter(TIntermSymbol *parameter)
717{
718 ASSERT(parameter != nullptr);
719 mParameters.push_back(parameter);
720}
721
Olli Etuaho13389b62016-10-16 11:48:18 +0100722void TIntermDeclaration::appendDeclarator(TIntermTyped *declarator)
723{
724 ASSERT(declarator != nullptr);
725 ASSERT(declarator->getAsSymbolNode() != nullptr ||
726 (declarator->getAsBinaryNode() != nullptr &&
727 declarator->getAsBinaryNode()->getOp() == EOpInitialize));
728 ASSERT(mDeclarators.empty() ||
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300729 declarator->getType().sameNonArrayType(mDeclarators.back()->getAsTyped()->getType()));
Olli Etuaho13389b62016-10-16 11:48:18 +0100730 mDeclarators.push_back(declarator);
731}
732
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300733bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
734{
735 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
736 REPLACE_IF_IS(mTrueExpression, TIntermTyped, original, replacement);
737 REPLACE_IF_IS(mFalseExpression, TIntermTyped, original, replacement);
738 return false;
739}
740
Olli Etuaho57961272016-09-14 13:57:46 +0300741bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400742{
743 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100744 REPLACE_IF_IS(mTrueBlock, TIntermBlock, original, replacement);
745 REPLACE_IF_IS(mFalseBlock, TIntermBlock, original, replacement);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400746 return false;
747}
748
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500749bool TIntermSwitch::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200750{
751 REPLACE_IF_IS(mInit, TIntermTyped, original, replacement);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100752 REPLACE_IF_IS(mStatementList, TIntermBlock, original, replacement);
Olli Etuaho923ecef2017-10-11 12:01:38 +0300753 ASSERT(mStatementList);
Olli Etuahoa3a36662015-02-17 13:46:51 +0200754 return false;
755}
756
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500757bool TIntermCase::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
Olli Etuahoa3a36662015-02-17 13:46:51 +0200758{
759 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
760 return false;
761}
762
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200763TIntermTyped::TIntermTyped(const TIntermTyped &node) : TIntermNode()
Olli Etuahod7a25242015-08-18 13:49:45 +0300764{
765 // Copy constructor is disallowed for TIntermNode in order to disallow it for subclasses that
766 // don't explicitly allow it, so normal TIntermNode constructor is used to construct the copy.
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200767 // We need to manually copy any fields of TIntermNode.
Olli Etuahod7a25242015-08-18 13:49:45 +0300768 mLine = node.mLine;
769}
770
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200771bool TIntermTyped::hasConstantValue() const
Olli Etuahod4f4c112016-04-15 15:11:24 +0300772{
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200773 return false;
774}
775
776const TConstantUnion *TIntermTyped::getConstantValue() const
777{
778 return nullptr;
Olli Etuahod4f4c112016-04-15 15:11:24 +0300779}
780
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200781TIntermConstantUnion::TIntermConstantUnion(const TIntermConstantUnion &node)
782 : TIntermExpression(node)
Olli Etuahod7a25242015-08-18 13:49:45 +0300783{
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200784 mUnionArrayPointer = node.mUnionArrayPointer;
Olli Etuahod7a25242015-08-18 13:49:45 +0300785}
786
Olli Etuahobeb6dc72017-12-14 16:03:03 +0200787TIntermFunctionPrototype::TIntermFunctionPrototype(const TFunction *function)
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200788 : TIntermTyped(), mFunction(function)
Olli Etuahobd674552016-10-06 13:28:42 +0100789{
Olli Etuahobeb6dc72017-12-14 16:03:03 +0200790 ASSERT(mFunction->symbolType() != SymbolType::Empty);
Olli Etuahobd674552016-10-06 13:28:42 +0100791}
792
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200793const TType &TIntermFunctionPrototype::getType() const
794{
795 return mFunction->getReturnType();
796}
797
Olli Etuahod7a25242015-08-18 13:49:45 +0300798TIntermAggregate::TIntermAggregate(const TIntermAggregate &node)
799 : TIntermOperator(node),
Olli Etuahod7a25242015-08-18 13:49:45 +0300800 mUseEmulatedFunction(node.mUseEmulatedFunction),
Olli Etuahobd674552016-10-06 13:28:42 +0100801 mGotPrecisionFromChildren(node.mGotPrecisionFromChildren),
Olli Etuaho0c371002017-12-13 17:00:25 +0400802 mFunction(node.mFunction)
Olli Etuahod7a25242015-08-18 13:49:45 +0300803{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800804 for (TIntermNode *arg : node.mArguments)
Olli Etuahod7a25242015-08-18 13:49:45 +0300805 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800806 TIntermTyped *typedArg = arg->getAsTyped();
807 ASSERT(typedArg != nullptr);
808 TIntermTyped *argCopy = typedArg->deepCopy();
809 mArguments.push_back(argCopy);
Olli Etuahod7a25242015-08-18 13:49:45 +0300810 }
811}
812
Olli Etuahofe486322017-03-21 09:30:54 +0000813TIntermAggregate *TIntermAggregate::shallowCopy() const
814{
815 TIntermSequence *copySeq = new TIntermSequence();
816 copySeq->insert(copySeq->begin(), getSequence()->begin(), getSequence()->end());
Olli Etuaho0c371002017-12-13 17:00:25 +0400817 TIntermAggregate *copyNode = new TIntermAggregate(mFunction, mType, mOp, copySeq);
Olli Etuahofe486322017-03-21 09:30:54 +0000818 copyNode->setLine(mLine);
819 return copyNode;
820}
821
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200822TIntermSwizzle::TIntermSwizzle(const TIntermSwizzle &node) : TIntermExpression(node)
Olli Etuahob6fa0432016-09-28 16:28:05 +0100823{
824 TIntermTyped *operandCopy = node.mOperand->deepCopy();
825 ASSERT(operandCopy != nullptr);
826 mOperand = operandCopy;
Olli Etuahoc9da71f2017-03-06 16:28:54 +0000827 mSwizzleOffsets = node.mSwizzleOffsets;
Olli Etuahob6fa0432016-09-28 16:28:05 +0100828}
829
Olli Etuahod7a25242015-08-18 13:49:45 +0300830TIntermBinary::TIntermBinary(const TIntermBinary &node)
831 : TIntermOperator(node), mAddIndexClamp(node.mAddIndexClamp)
832{
833 TIntermTyped *leftCopy = node.mLeft->deepCopy();
834 TIntermTyped *rightCopy = node.mRight->deepCopy();
835 ASSERT(leftCopy != nullptr && rightCopy != nullptr);
836 mLeft = leftCopy;
837 mRight = rightCopy;
838}
839
840TIntermUnary::TIntermUnary(const TIntermUnary &node)
841 : TIntermOperator(node), mUseEmulatedFunction(node.mUseEmulatedFunction)
842{
843 TIntermTyped *operandCopy = node.mOperand->deepCopy();
844 ASSERT(operandCopy != nullptr);
845 mOperand = operandCopy;
846}
847
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +0200848TIntermTernary::TIntermTernary(const TIntermTernary &node) : TIntermExpression(node)
Olli Etuahod7a25242015-08-18 13:49:45 +0300849{
Olli Etuahod7a25242015-08-18 13:49:45 +0300850 TIntermTyped *conditionCopy = node.mCondition->deepCopy();
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300851 TIntermTyped *trueCopy = node.mTrueExpression->deepCopy();
852 TIntermTyped *falseCopy = node.mFalseExpression->deepCopy();
Olli Etuahod7a25242015-08-18 13:49:45 +0300853 ASSERT(conditionCopy != nullptr && trueCopy != nullptr && falseCopy != nullptr);
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300854 mCondition = conditionCopy;
855 mTrueExpression = trueCopy;
856 mFalseExpression = falseCopy;
Olli Etuahod7a25242015-08-18 13:49:45 +0300857}
858
Jamie Madillb1a85f42014-08-19 15:23:24 -0400859bool TIntermOperator::isAssignment() const
860{
Olli Etuaho63e1ec52016-08-18 22:05:12 +0300861 return IsAssignment(mOp);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400862}
863
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300864bool TIntermOperator::isMultiplication() const
865{
866 switch (mOp)
867 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500868 case EOpMul:
869 case EOpMatrixTimesMatrix:
870 case EOpMatrixTimesVector:
871 case EOpMatrixTimesScalar:
872 case EOpVectorTimesMatrix:
873 case EOpVectorTimesScalar:
874 return true;
875 default:
876 return false;
Olli Etuaho8f76bcc2015-06-02 13:54:20 +0300877 }
878}
879
Jamie Madillb1a85f42014-08-19 15:23:24 -0400880bool TIntermOperator::isConstructor() const
881{
Olli Etuaho8fab3202017-05-08 18:22:22 +0300882 return (mOp == EOpConstruct);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400883}
884
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800885bool TIntermOperator::isFunctionCall() const
886{
887 switch (mOp)
888 {
889 case EOpCallFunctionInAST:
890 case EOpCallBuiltInFunction:
891 case EOpCallInternalRawFunction:
892 return true;
893 default:
894 return false;
895 }
896}
897
Olli Etuaho1dded802016-08-18 18:13:13 +0300898TOperator TIntermBinary::GetMulOpBasedOnOperands(const TType &left, const TType &right)
899{
900 if (left.isMatrix())
901 {
902 if (right.isMatrix())
903 {
904 return EOpMatrixTimesMatrix;
905 }
906 else
907 {
908 if (right.isVector())
909 {
910 return EOpMatrixTimesVector;
911 }
912 else
913 {
914 return EOpMatrixTimesScalar;
915 }
916 }
917 }
918 else
919 {
920 if (right.isMatrix())
921 {
922 if (left.isVector())
923 {
924 return EOpVectorTimesMatrix;
925 }
926 else
927 {
928 return EOpMatrixTimesScalar;
929 }
930 }
931 else
932 {
933 // Neither operand is a matrix.
934 if (left.isVector() == right.isVector())
935 {
936 // Leave as component product.
937 return EOpMul;
938 }
939 else
940 {
941 return EOpVectorTimesScalar;
942 }
943 }
944 }
945}
946
947TOperator TIntermBinary::GetMulAssignOpBasedOnOperands(const TType &left, const TType &right)
948{
949 if (left.isMatrix())
950 {
951 if (right.isMatrix())
952 {
953 return EOpMatrixTimesMatrixAssign;
954 }
955 else
956 {
957 // right should be scalar, but this may not be validated yet.
958 return EOpMatrixTimesScalarAssign;
959 }
960 }
961 else
962 {
963 if (right.isMatrix())
964 {
965 // Left should be a vector, but this may not be validated yet.
966 return EOpVectorTimesMatrixAssign;
967 }
968 else
969 {
970 // Neither operand is a matrix.
971 if (left.isVector() == right.isVector())
972 {
973 // Leave as component product.
974 return EOpMulAssign;
975 }
976 else
977 {
978 // left should be vector and right should be scalar, but this may not be validated
979 // yet.
980 return EOpVectorTimesScalarAssign;
981 }
982 }
983 }
984}
985
Jamie Madillb1a85f42014-08-19 15:23:24 -0400986//
987// Make sure the type of a unary operator is appropriate for its
988// combination of operation and operand type.
989//
Olli Etuahoa2234302016-08-31 12:05:39 +0300990void TIntermUnary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -0400991{
Olli Etuahobb2bbfb2017-08-24 15:43:33 +0300992 if (mOp == EOpArrayLength)
993 {
994 // Special case: the qualifier of .length() doesn't depend on the operand qualifier.
995 setType(TType(EbtInt, EbpUndefined, EvqConst));
996 return;
997 }
998
Olli Etuahoa2234302016-08-31 12:05:39 +0300999 TQualifier resultQualifier = EvqTemporary;
1000 if (mOperand->getQualifier() == EvqConst)
1001 resultQualifier = EvqConst;
1002
1003 unsigned char operandPrimarySize =
1004 static_cast<unsigned char>(mOperand->getType().getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -04001005 switch (mOp)
1006 {
Olli Etuahoa2234302016-08-31 12:05:39 +03001007 case EOpFloatBitsToInt:
1008 setType(TType(EbtInt, EbpHigh, resultQualifier, operandPrimarySize));
1009 break;
1010 case EOpFloatBitsToUint:
1011 setType(TType(EbtUInt, EbpHigh, resultQualifier, operandPrimarySize));
1012 break;
1013 case EOpIntBitsToFloat:
1014 case EOpUintBitsToFloat:
1015 setType(TType(EbtFloat, EbpHigh, resultQualifier, operandPrimarySize));
1016 break;
1017 case EOpPackSnorm2x16:
1018 case EOpPackUnorm2x16:
1019 case EOpPackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001020 case EOpPackUnorm4x8:
1021 case EOpPackSnorm4x8:
Olli Etuahoa2234302016-08-31 12:05:39 +03001022 setType(TType(EbtUInt, EbpHigh, resultQualifier));
1023 break;
1024 case EOpUnpackSnorm2x16:
1025 case EOpUnpackUnorm2x16:
1026 setType(TType(EbtFloat, EbpHigh, resultQualifier, 2));
1027 break;
1028 case EOpUnpackHalf2x16:
1029 setType(TType(EbtFloat, EbpMedium, resultQualifier, 2));
1030 break;
Olli Etuaho25aef452017-01-29 16:15:44 -08001031 case EOpUnpackUnorm4x8:
1032 case EOpUnpackSnorm4x8:
1033 setType(TType(EbtFloat, EbpMedium, resultQualifier, 4));
1034 break;
Olli Etuahoa2234302016-08-31 12:05:39 +03001035 case EOpAny:
1036 case EOpAll:
1037 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1038 break;
1039 case EOpLength:
1040 case EOpDeterminant:
1041 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier));
1042 break;
1043 case EOpTranspose:
1044 setType(TType(EbtFloat, mOperand->getType().getPrecision(), resultQualifier,
1045 static_cast<unsigned char>(mOperand->getType().getRows()),
1046 static_cast<unsigned char>(mOperand->getType().getCols())));
1047 break;
1048 case EOpIsInf:
1049 case EOpIsNan:
1050 setType(TType(EbtBool, EbpUndefined, resultQualifier, operandPrimarySize));
1051 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001052 case EOpBitfieldReverse:
1053 setType(TType(mOperand->getBasicType(), EbpHigh, resultQualifier, operandPrimarySize));
1054 break;
1055 case EOpBitCount:
1056 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
1057 break;
1058 case EOpFindLSB:
1059 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
1060 break;
1061 case EOpFindMSB:
1062 setType(TType(EbtInt, EbpLow, resultQualifier, operandPrimarySize));
1063 break;
Olli Etuahoa2234302016-08-31 12:05:39 +03001064 default:
1065 setType(mOperand->getType());
1066 mType.setQualifier(resultQualifier);
1067 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001068 }
Olli Etuahoa2234302016-08-31 12:05:39 +03001069}
Jamie Madillb1a85f42014-08-19 15:23:24 -04001070
Olli Etuahob6fa0432016-09-28 16:28:05 +01001071TIntermSwizzle::TIntermSwizzle(TIntermTyped *operand, const TVector<int> &swizzleOffsets)
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +02001072 : TIntermExpression(TType(EbtFloat, EbpUndefined)),
Olli Etuahob6fa0432016-09-28 16:28:05 +01001073 mOperand(operand),
1074 mSwizzleOffsets(swizzleOffsets)
1075{
1076 ASSERT(mSwizzleOffsets.size() <= 4);
1077 promote();
1078}
1079
Olli Etuahoa2234302016-08-31 12:05:39 +03001080TIntermUnary::TIntermUnary(TOperator op, TIntermTyped *operand)
1081 : TIntermOperator(op), mOperand(operand), mUseEmulatedFunction(false)
1082{
1083 promote();
Jamie Madillb1a85f42014-08-19 15:23:24 -04001084}
1085
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001086TIntermBinary::TIntermBinary(TOperator op, TIntermTyped *left, TIntermTyped *right)
1087 : TIntermOperator(op), mLeft(left), mRight(right), mAddIndexClamp(false)
1088{
1089 promote();
1090}
1091
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02001092TIntermBinary *TIntermBinary::CreateComma(TIntermTyped *left,
1093 TIntermTyped *right,
1094 int shaderVersion)
1095{
1096 TIntermBinary *node = new TIntermBinary(EOpComma, left, right);
1097 node->getTypePointer()->setQualifier(GetCommaQualifier(shaderVersion, left, right));
1098 return node;
1099}
1100
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001101TIntermInvariantDeclaration::TIntermInvariantDeclaration(TIntermSymbol *symbol, const TSourceLoc &line)
1102 : TIntermNode(), mSymbol(symbol)
1103{
1104 ASSERT(symbol);
1105 setLine(line);
1106}
1107
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001108TIntermTernary::TIntermTernary(TIntermTyped *cond,
1109 TIntermTyped *trueExpression,
1110 TIntermTyped *falseExpression)
Olli Etuaho2c9cc8b2018-01-09 16:13:02 +02001111 : TIntermExpression(trueExpression->getType()),
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001112 mCondition(cond),
1113 mTrueExpression(trueExpression),
1114 mFalseExpression(falseExpression)
1115{
1116 getTypePointer()->setQualifier(
1117 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression));
1118}
1119
Olli Etuaho81629262017-04-19 11:56:01 +03001120TIntermLoop::TIntermLoop(TLoopType type,
1121 TIntermNode *init,
1122 TIntermTyped *cond,
1123 TIntermTyped *expr,
1124 TIntermBlock *body)
1125 : mType(type), mInit(init), mCond(cond), mExpr(expr), mBody(body)
1126{
1127 // Declaration nodes with no children can appear if all the declarators just added constants to
1128 // the symbol table instead of generating code. They're no-ops so don't add them to the tree.
1129 if (mInit && mInit->getAsDeclarationNode() &&
1130 mInit->getAsDeclarationNode()->getSequence()->empty())
1131 {
1132 mInit = nullptr;
1133 }
1134}
1135
Olli Etuaho923ecef2017-10-11 12:01:38 +03001136TIntermIfElse::TIntermIfElse(TIntermTyped *cond, TIntermBlock *trueB, TIntermBlock *falseB)
1137 : TIntermNode(), mCondition(cond), mTrueBlock(trueB), mFalseBlock(falseB)
1138{
1139 // Prune empty false blocks so that there won't be unnecessary operations done on it.
1140 if (mFalseBlock && mFalseBlock->getSequence()->empty())
1141 {
1142 mFalseBlock = nullptr;
1143 }
1144}
1145
1146TIntermSwitch::TIntermSwitch(TIntermTyped *init, TIntermBlock *statementList)
1147 : TIntermNode(), mInit(init), mStatementList(statementList)
1148{
1149 ASSERT(mStatementList);
1150}
1151
1152void TIntermSwitch::setStatementList(TIntermBlock *statementList)
1153{
1154 ASSERT(statementList);
1155 mStatementList = statementList;
1156}
1157
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001158// static
1159TQualifier TIntermTernary::DetermineQualifier(TIntermTyped *cond,
1160 TIntermTyped *trueExpression,
1161 TIntermTyped *falseExpression)
1162{
1163 if (cond->getQualifier() == EvqConst && trueExpression->getQualifier() == EvqConst &&
1164 falseExpression->getQualifier() == EvqConst)
1165 {
1166 return EvqConst;
1167 }
1168 return EvqTemporary;
1169}
1170
Olli Etuaho765924f2018-01-04 12:48:36 +02001171TIntermTyped *TIntermTernary::fold(TDiagnostics * /* diagnostics */)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001172{
1173 if (mCondition->getAsConstantUnion())
1174 {
1175 if (mCondition->getAsConstantUnion()->getBConst(0))
1176 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001177 return mTrueExpression;
1178 }
1179 else
1180 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001181 return mFalseExpression;
1182 }
1183 }
1184 return this;
1185}
1186
Olli Etuahob6fa0432016-09-28 16:28:05 +01001187void TIntermSwizzle::promote()
1188{
1189 TQualifier resultQualifier = EvqTemporary;
1190 if (mOperand->getQualifier() == EvqConst)
1191 resultQualifier = EvqConst;
1192
1193 auto numFields = mSwizzleOffsets.size();
1194 setType(TType(mOperand->getBasicType(), mOperand->getPrecision(), resultQualifier,
1195 static_cast<unsigned char>(numFields)));
1196}
1197
1198bool TIntermSwizzle::hasDuplicateOffsets() const
1199{
1200 int offsetCount[4] = {0u, 0u, 0u, 0u};
1201 for (const auto offset : mSwizzleOffsets)
1202 {
1203 offsetCount[offset]++;
1204 if (offsetCount[offset] > 1)
1205 {
1206 return true;
1207 }
1208 }
1209 return false;
1210}
1211
Olli Etuaho09b04a22016-12-15 13:30:26 +00001212bool TIntermSwizzle::offsetsMatch(int offset) const
1213{
1214 return mSwizzleOffsets.size() == 1 && mSwizzleOffsets[0] == offset;
1215}
1216
Olli Etuahob6fa0432016-09-28 16:28:05 +01001217void TIntermSwizzle::writeOffsetsAsXYZW(TInfoSinkBase *out) const
1218{
1219 for (const int offset : mSwizzleOffsets)
1220 {
1221 switch (offset)
1222 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001223 case 0:
1224 *out << "x";
1225 break;
1226 case 1:
1227 *out << "y";
1228 break;
1229 case 2:
1230 *out << "z";
1231 break;
1232 case 3:
1233 *out << "w";
1234 break;
1235 default:
1236 UNREACHABLE();
Olli Etuahob6fa0432016-09-28 16:28:05 +01001237 }
1238 }
1239}
1240
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001241TQualifier TIntermBinary::GetCommaQualifier(int shaderVersion,
1242 const TIntermTyped *left,
1243 const TIntermTyped *right)
1244{
1245 // ESSL3.00 section 12.43: The result of a sequence operator is not a constant-expression.
1246 if (shaderVersion >= 300 || left->getQualifier() != EvqConst ||
1247 right->getQualifier() != EvqConst)
1248 {
1249 return EvqTemporary;
1250 }
1251 return EvqConst;
1252}
Olli Etuahob6fa0432016-09-28 16:28:05 +01001253
1254// Establishes the type of the result of the binary operation.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001255void TIntermBinary::promote()
Jamie Madillb1a85f42014-08-19 15:23:24 -04001256{
Olli Etuaho1dded802016-08-18 18:13:13 +03001257 ASSERT(!isMultiplication() ||
1258 mOp == GetMulOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1259
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001260 // Comma is handled as a special case. Note that the comma node qualifier depends on the shader
1261 // version and so is not being set here.
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001262 if (mOp == EOpComma)
1263 {
1264 setType(mRight->getType());
1265 return;
1266 }
1267
Jamie Madillb1a85f42014-08-19 15:23:24 -04001268 // Base assumption: just make the type the same as the left
1269 // operand. Then only deviations from this need be coded.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001270 setType(mLeft->getType());
1271
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001272 TQualifier resultQualifier = EvqConst;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001273 // Binary operations results in temporary variables unless both
1274 // operands are const.
1275 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
1276 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001277 resultQualifier = EvqTemporary;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001278 getTypePointer()->setQualifier(EvqTemporary);
1279 }
1280
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001281 // Handle indexing ops.
1282 switch (mOp)
1283 {
1284 case EOpIndexDirect:
1285 case EOpIndexIndirect:
1286 if (mLeft->isArray())
1287 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001288 mType.toArrayElementType();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001289 }
1290 else if (mLeft->isMatrix())
1291 {
1292 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier,
1293 static_cast<unsigned char>(mLeft->getRows())));
1294 }
1295 else if (mLeft->isVector())
1296 {
1297 setType(TType(mLeft->getBasicType(), mLeft->getPrecision(), resultQualifier));
1298 }
1299 else
1300 {
1301 UNREACHABLE();
1302 }
1303 return;
1304 case EOpIndexDirectStruct:
1305 {
1306 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1307 const int i = mRight->getAsConstantUnion()->getIConst(0);
1308 setType(*fields[i]->type());
1309 getTypePointer()->setQualifier(resultQualifier);
1310 return;
1311 }
1312 case EOpIndexDirectInterfaceBlock:
1313 {
1314 const TFieldList &fields = mLeft->getType().getInterfaceBlock()->fields();
1315 const int i = mRight->getAsConstantUnion()->getIConst(0);
1316 setType(*fields[i]->type());
1317 getTypePointer()->setQualifier(resultQualifier);
1318 return;
1319 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001320 default:
1321 break;
1322 }
1323
1324 ASSERT(mLeft->isArray() == mRight->isArray());
1325
1326 // The result gets promoted to the highest precision.
1327 TPrecision higherPrecision = GetHigherPrecision(mLeft->getPrecision(), mRight->getPrecision());
1328 getTypePointer()->setPrecision(higherPrecision);
1329
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001330 const int nominalSize = std::max(mLeft->getNominalSize(), mRight->getNominalSize());
Jamie Madillb1a85f42014-08-19 15:23:24 -04001331
1332 //
1333 // All scalars or structs. Code after this test assumes this case is removed!
1334 //
1335 if (nominalSize == 1)
1336 {
1337 switch (mOp)
1338 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001339 //
1340 // Promote to conditional
1341 //
1342 case EOpEqual:
1343 case EOpNotEqual:
1344 case EOpLessThan:
1345 case EOpGreaterThan:
1346 case EOpLessThanEqual:
1347 case EOpGreaterThanEqual:
1348 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1349 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001350
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001351 //
1352 // And and Or operate on conditionals
1353 //
1354 case EOpLogicalAnd:
1355 case EOpLogicalXor:
1356 case EOpLogicalOr:
1357 ASSERT(mLeft->getBasicType() == EbtBool && mRight->getBasicType() == EbtBool);
1358 setType(TType(EbtBool, EbpUndefined, resultQualifier));
1359 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001360
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001361 default:
1362 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001363 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001364 return;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001365 }
1366
1367 // If we reach here, at least one of the operands is vector or matrix.
1368 // The other operand could be a scalar, vector, or matrix.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001369 TBasicType basicType = mLeft->getBasicType();
Olli Etuaho1dded802016-08-18 18:13:13 +03001370
Jamie Madillb1a85f42014-08-19 15:23:24 -04001371 switch (mOp)
1372 {
Olli Etuaho1dded802016-08-18 18:13:13 +03001373 case EOpMul:
1374 break;
1375 case EOpMatrixTimesScalar:
1376 if (mRight->isMatrix())
Jamie Madillb1a85f42014-08-19 15:23:24 -04001377 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001378 setType(TType(basicType, higherPrecision, resultQualifier,
1379 static_cast<unsigned char>(mRight->getCols()),
1380 static_cast<unsigned char>(mRight->getRows())));
Jamie Madillb1a85f42014-08-19 15:23:24 -04001381 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001382 break;
1383 case EOpMatrixTimesVector:
1384 setType(TType(basicType, higherPrecision, resultQualifier,
1385 static_cast<unsigned char>(mLeft->getRows()), 1));
1386 break;
1387 case EOpMatrixTimesMatrix:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001388 setType(TType(basicType, higherPrecision, resultQualifier,
1389 static_cast<unsigned char>(mRight->getCols()),
1390 static_cast<unsigned char>(mLeft->getRows())));
Olli Etuaho1dded802016-08-18 18:13:13 +03001391 break;
1392 case EOpVectorTimesScalar:
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001393 setType(TType(basicType, higherPrecision, resultQualifier,
Olli Etuaho1dded802016-08-18 18:13:13 +03001394 static_cast<unsigned char>(nominalSize), 1));
1395 break;
1396 case EOpVectorTimesMatrix:
1397 setType(TType(basicType, higherPrecision, resultQualifier,
1398 static_cast<unsigned char>(mRight->getCols()), 1));
1399 break;
1400 case EOpMulAssign:
1401 case EOpVectorTimesScalarAssign:
1402 case EOpVectorTimesMatrixAssign:
1403 case EOpMatrixTimesScalarAssign:
1404 case EOpMatrixTimesMatrixAssign:
1405 ASSERT(mOp == GetMulAssignOpBasedOnOperands(mLeft->getType(), mRight->getType()));
1406 break;
1407 case EOpAssign:
1408 case EOpInitialize:
Olli Etuaho1dded802016-08-18 18:13:13 +03001409 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1410 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
1411 break;
1412 case EOpAdd:
1413 case EOpSub:
1414 case EOpDiv:
1415 case EOpIMod:
1416 case EOpBitShiftLeft:
1417 case EOpBitShiftRight:
1418 case EOpBitwiseAnd:
1419 case EOpBitwiseXor:
1420 case EOpBitwiseOr:
1421 case EOpAddAssign:
1422 case EOpSubAssign:
1423 case EOpDivAssign:
1424 case EOpIModAssign:
1425 case EOpBitShiftLeftAssign:
1426 case EOpBitShiftRightAssign:
1427 case EOpBitwiseAndAssign:
1428 case EOpBitwiseXorAssign:
1429 case EOpBitwiseOrAssign:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001430 {
1431 const int secondarySize =
1432 std::max(mLeft->getSecondarySize(), mRight->getSecondarySize());
1433 setType(TType(basicType, higherPrecision, resultQualifier,
1434 static_cast<unsigned char>(nominalSize),
1435 static_cast<unsigned char>(secondarySize)));
1436 ASSERT(!mLeft->isArray() && !mRight->isArray());
Olli Etuaho1dded802016-08-18 18:13:13 +03001437 break;
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001438 }
Olli Etuaho1dded802016-08-18 18:13:13 +03001439 case EOpEqual:
1440 case EOpNotEqual:
1441 case EOpLessThan:
1442 case EOpGreaterThan:
1443 case EOpLessThanEqual:
1444 case EOpGreaterThanEqual:
1445 ASSERT((mLeft->getNominalSize() == mRight->getNominalSize()) &&
1446 (mLeft->getSecondarySize() == mRight->getSecondarySize()));
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001447 setType(TType(EbtBool, EbpUndefined, resultQualifier));
Olli Etuaho1dded802016-08-18 18:13:13 +03001448 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001449
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001450 case EOpIndexDirect:
1451 case EOpIndexIndirect:
1452 case EOpIndexDirectInterfaceBlock:
1453 case EOpIndexDirectStruct:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001454 // These ops should be already fully handled.
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001455 UNREACHABLE();
1456 break;
Olli Etuaho1dded802016-08-18 18:13:13 +03001457 default:
Olli Etuaho63e1ec52016-08-18 22:05:12 +03001458 UNREACHABLE();
1459 break;
Jamie Madillb1a85f42014-08-19 15:23:24 -04001460 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04001461}
1462
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001463bool TIntermConstantUnion::hasConstantValue() const
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001464{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001465 return true;
1466}
1467
1468const TConstantUnion *TIntermConstantUnion::getConstantValue() const
1469{
1470 return mUnionArrayPointer;
1471}
1472
1473const TConstantUnion *TIntermConstantUnion::FoldIndexing(const TType &type,
1474 const TConstantUnion *constArray,
1475 int index)
1476{
1477 if (type.isArray())
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001478 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001479 ASSERT(index < static_cast<int>(type.getOutermostArraySize()));
1480 TType arrayElementType(type);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001481 arrayElementType.toArrayElementType();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001482 size_t arrayElementSize = arrayElementType.getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001483 return &constArray[arrayElementSize * index];
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001484 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001485 else if (type.isMatrix())
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001486 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001487 ASSERT(index < type.getCols());
1488 int size = type.getRows();
1489 return &constArray[size * index];
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001490 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001491 else if (type.isVector())
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001492 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001493 ASSERT(index < type.getNominalSize());
1494 return &constArray[index];
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001495 }
1496 else
1497 {
1498 UNREACHABLE();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001499 return nullptr;
1500 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001501}
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001502
Olli Etuaho765924f2018-01-04 12:48:36 +02001503TIntermTyped *TIntermSwizzle::fold(TDiagnostics * /* diagnostics */)
Olli Etuahob6fa0432016-09-28 16:28:05 +01001504{
1505 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1506 if (operandConstant == nullptr)
1507 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001508 return this;
Olli Etuahob6fa0432016-09-28 16:28:05 +01001509 }
1510
1511 TConstantUnion *constArray = new TConstantUnion[mSwizzleOffsets.size()];
1512 for (size_t i = 0; i < mSwizzleOffsets.size(); ++i)
1513 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001514 constArray[i] = *TIntermConstantUnion::FoldIndexing(
1515 operandConstant->getType(), operandConstant->getConstantValue(), mSwizzleOffsets.at(i));
Olli Etuahob6fa0432016-09-28 16:28:05 +01001516 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001517 return CreateFoldedNode(constArray, this);
Olli Etuahob6fa0432016-09-28 16:28:05 +01001518}
1519
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001520TIntermTyped *TIntermBinary::fold(TDiagnostics *diagnostics)
1521{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001522 const TConstantUnion *rightConstant = mRight->getConstantValue();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001523 switch (mOp)
1524 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001525 case EOpComma:
1526 {
1527 if (mLeft->hasSideEffects())
1528 {
1529 return this;
1530 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001531 return mRight;
1532 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001533 case EOpIndexDirect:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001534 case EOpIndexDirectStruct:
1535 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001536 if (rightConstant == nullptr)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001537 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001538 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001539 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001540 size_t index = static_cast<size_t>(rightConstant->getIConst());
1541 TIntermAggregate *leftAggregate = mLeft->getAsAggregate();
1542 if (leftAggregate && leftAggregate->isConstructor() && leftAggregate->isArray() &&
1543 !leftAggregate->hasSideEffects())
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001544 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001545 ASSERT(index < leftAggregate->getSequence()->size());
1546 // This transformation can't add complexity as we're eliminating the constructor
1547 // entirely.
1548 return leftAggregate->getSequence()->at(index)->getAsTyped();
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001549 }
1550
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001551 // If the indexed value is already a constant union, we can't increase duplication of
1552 // data by folding the indexing. Also fold the node in case it's generally beneficial to
1553 // replace this type of node with a constant union even if that would mean duplicating
1554 // data.
1555 if (mLeft->getAsConstantUnion() || getType().canReplaceWithConstantUnion())
1556 {
1557 const TConstantUnion *constantValue = getConstantValue();
1558 if (constantValue == nullptr)
1559 {
1560 return this;
1561 }
1562 return CreateFoldedNode(constantValue, this);
1563 }
1564 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001565 }
1566 case EOpIndexIndirect:
1567 case EOpIndexDirectInterfaceBlock:
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001568 case EOpInitialize:
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001569 // Can never be constant folded.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001570 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001571 default:
1572 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001573 if (rightConstant == nullptr)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001574 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001575 return this;
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001576 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001577 const TConstantUnion *leftConstant = mLeft->getConstantValue();
1578 if (leftConstant == nullptr)
1579 {
1580 return this;
1581 }
1582 const TConstantUnion *constArray =
1583 TIntermConstantUnion::FoldBinary(mOp, leftConstant, mLeft->getType(), rightConstant,
1584 mRight->getType(), diagnostics, mLeft->getLine());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001585 if (!constArray)
1586 {
1587 return this;
1588 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001589 return CreateFoldedNode(constArray, this);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03001590 }
1591 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001592}
1593
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001594bool TIntermBinary::hasConstantValue() const
1595{
1596 switch (mOp)
1597 {
1598 case EOpIndexDirect:
1599 case EOpIndexDirectStruct:
1600 {
1601 if (mLeft->hasConstantValue() && mRight->hasConstantValue())
1602 {
1603 return true;
1604 }
1605 }
1606 default:
1607 break;
1608 }
1609 return false;
1610}
1611
1612const TConstantUnion *TIntermBinary::getConstantValue() const
1613{
1614 if (!hasConstantValue())
1615 {
1616 return nullptr;
1617 }
1618
1619 const TConstantUnion *leftConstantValue = mLeft->getConstantValue();
1620 int index = mRight->getConstantValue()->getIConst();
1621 const TConstantUnion *constIndexingResult = nullptr;
1622 if (mOp == EOpIndexDirect)
1623 {
1624 constIndexingResult =
1625 TIntermConstantUnion::FoldIndexing(mLeft->getType(), leftConstantValue, index);
1626 }
1627 else
1628 {
1629 ASSERT(mOp == EOpIndexDirectStruct);
1630 const TFieldList &fields = mLeft->getType().getStruct()->fields();
1631
1632 size_t previousFieldsSize = 0;
1633 for (int i = 0; i < index; ++i)
1634 {
1635 previousFieldsSize += fields[i]->type()->getObjectSize();
1636 }
1637 constIndexingResult = leftConstantValue + previousFieldsSize;
1638 }
1639 return constIndexingResult;
1640}
1641
Olli Etuahof119a262016-08-19 15:54:22 +03001642TIntermTyped *TIntermUnary::fold(TDiagnostics *diagnostics)
Olli Etuaho95310b02015-06-02 17:43:38 +03001643{
Arun Patoleab2b9a22015-07-06 18:27:56 +05301644 TConstantUnion *constArray = nullptr;
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03001645
1646 if (mOp == EOpArrayLength)
Arun Patoleab2b9a22015-07-06 18:27:56 +05301647 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02001648 // The size of runtime-sized arrays may only be determined at runtime.
1649 if (mOperand->hasSideEffects() || mOperand->getType().isUnsizedArray())
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03001650 {
1651 return this;
1652 }
1653 constArray = new TConstantUnion[1];
1654 constArray->setIConst(mOperand->getOutermostArraySize());
1655 }
1656 else
1657 {
1658 TIntermConstantUnion *operandConstant = mOperand->getAsConstantUnion();
1659 if (operandConstant == nullptr)
1660 {
1661 return this;
1662 }
1663
1664 switch (mOp)
1665 {
1666 case EOpAny:
1667 case EOpAll:
1668 case EOpLength:
1669 case EOpTranspose:
1670 case EOpDeterminant:
1671 case EOpInverse:
1672 case EOpPackSnorm2x16:
1673 case EOpUnpackSnorm2x16:
1674 case EOpPackUnorm2x16:
1675 case EOpUnpackUnorm2x16:
1676 case EOpPackHalf2x16:
1677 case EOpUnpackHalf2x16:
1678 case EOpPackUnorm4x8:
1679 case EOpPackSnorm4x8:
1680 case EOpUnpackUnorm4x8:
1681 case EOpUnpackSnorm4x8:
1682 constArray = operandConstant->foldUnaryNonComponentWise(mOp);
1683 break;
1684 default:
1685 constArray = operandConstant->foldUnaryComponentWise(mOp, diagnostics);
1686 break;
1687 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05301688 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001689 if (constArray == nullptr)
1690 {
1691 return this;
1692 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001693 return CreateFoldedNode(constArray, this);
Olli Etuahob43846e2015-06-02 18:18:57 +03001694}
1695
Olli Etuahof119a262016-08-19 15:54:22 +03001696TIntermTyped *TIntermAggregate::fold(TDiagnostics *diagnostics)
Olli Etuahob43846e2015-06-02 18:18:57 +03001697{
1698 // Make sure that all params are constant before actual constant folding.
1699 for (auto *param : *getSequence())
Olli Etuaho95310b02015-06-02 17:43:38 +03001700 {
Olli Etuahob43846e2015-06-02 18:18:57 +03001701 if (param->getAsConstantUnion() == nullptr)
1702 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001703 return this;
Olli Etuahob43846e2015-06-02 18:18:57 +03001704 }
Olli Etuaho95310b02015-06-02 17:43:38 +03001705 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001706 const TConstantUnion *constArray = nullptr;
Olli Etuaho1d122782015-11-06 15:35:17 +02001707 if (isConstructor())
Olli Etuaho2768bc82017-12-12 11:51:48 +02001708 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001709 if (mType.canReplaceWithConstantUnion())
Olli Etuaho765924f2018-01-04 12:48:36 +02001710 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001711 constArray = getConstantValue();
Olli Etuaho765924f2018-01-04 12:48:36 +02001712 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001713 }
Olli Etuaho765924f2018-01-04 12:48:36 +02001714 else if (CanFoldAggregateBuiltInOp(mOp))
Olli Etuaho2768bc82017-12-12 11:51:48 +02001715 {
Olli Etuahof119a262016-08-19 15:54:22 +03001716 constArray = TIntermConstantUnion::FoldAggregateBuiltIn(this, diagnostics);
Olli Etuaho2768bc82017-12-12 11:51:48 +02001717 }
Olli Etuaho765924f2018-01-04 12:48:36 +02001718 if (constArray == nullptr)
1719 {
1720 return this;
1721 }
Olli Etuaho2768bc82017-12-12 11:51:48 +02001722 return CreateFoldedNode(constArray, this);
Olli Etuaho95310b02015-06-02 17:43:38 +03001723}
1724
Jamie Madillb1a85f42014-08-19 15:23:24 -04001725//
1726// The fold functions see if an operation on a constant can be done in place,
1727// without generating run-time code.
1728//
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001729// Returns the constant value to keep using or nullptr.
Jamie Madillb1a85f42014-08-19 15:23:24 -04001730//
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001731const TConstantUnion *TIntermConstantUnion::FoldBinary(TOperator op,
1732 const TConstantUnion *leftArray,
1733 const TType &leftType,
1734 const TConstantUnion *rightArray,
1735 const TType &rightType,
1736 TDiagnostics *diagnostics,
1737 const TSourceLoc &line)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001738{
Olli Etuahof119a262016-08-19 15:54:22 +03001739 ASSERT(leftArray && rightArray);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001740
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001741 size_t objectSize = leftType.getObjectSize();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001742
1743 // for a case like float f = vec4(2, 3, 4, 5) + 1.2;
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001744 if (rightType.getObjectSize() == 1 && objectSize > 1)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001745 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001746 rightArray = Vectorize(*rightArray, objectSize);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001747 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001748 else if (rightType.getObjectSize() > 1 && objectSize == 1)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001749 {
1750 // for a case like float f = 1.2 + vec4(2, 3, 4, 5);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001751 leftArray = Vectorize(*leftArray, rightType.getObjectSize());
1752 objectSize = rightType.getObjectSize();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001753 }
1754
1755 TConstantUnion *resultArray = nullptr;
1756
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001757 switch (op)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001758 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001759 case EOpAdd:
1760 resultArray = new TConstantUnion[objectSize];
1761 for (size_t i = 0; i < objectSize; i++)
1762 resultArray[i] =
1763 TConstantUnion::add(leftArray[i], rightArray[i], diagnostics, line);
1764 break;
1765 case EOpSub:
1766 resultArray = new TConstantUnion[objectSize];
1767 for (size_t i = 0; i < objectSize; i++)
1768 resultArray[i] =
1769 TConstantUnion::sub(leftArray[i], rightArray[i], diagnostics, line);
1770 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001771
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001772 case EOpMul:
1773 case EOpVectorTimesScalar:
1774 case EOpMatrixTimesScalar:
1775 resultArray = new TConstantUnion[objectSize];
1776 for (size_t i = 0; i < objectSize; i++)
1777 resultArray[i] =
1778 TConstantUnion::mul(leftArray[i], rightArray[i], diagnostics, line);
1779 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001780
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001781 case EOpMatrixTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001782 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001783 // TODO(jmadll): This code should check for overflows.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001784 ASSERT(leftType.getBasicType() == EbtFloat && rightType.getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001785
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001786 const int leftCols = leftType.getCols();
1787 const int leftRows = leftType.getRows();
1788 const int rightCols = rightType.getCols();
1789 const int rightRows = rightType.getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001790 const int resultCols = rightCols;
1791 const int resultRows = leftRows;
1792
1793 resultArray = new TConstantUnion[resultCols * resultRows];
1794 for (int row = 0; row < resultRows; row++)
1795 {
1796 for (int column = 0; column < resultCols; column++)
1797 {
1798 resultArray[resultRows * column + row].setFConst(0.0f);
1799 for (int i = 0; i < leftCols; i++)
1800 {
1801 resultArray[resultRows * column + row].setFConst(
1802 resultArray[resultRows * column + row].getFConst() +
1803 leftArray[i * leftRows + row].getFConst() *
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001804 rightArray[column * rightRows + i].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001805 }
1806 }
1807 }
1808 }
1809 break;
1810
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001811 case EOpDiv:
1812 case EOpIMod:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001813 {
1814 resultArray = new TConstantUnion[objectSize];
1815 for (size_t i = 0; i < objectSize; i++)
1816 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001817 switch (leftType.getBasicType())
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001818 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001819 case EbtFloat:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001820 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001821 ASSERT(op == EOpDiv);
1822 float dividend = leftArray[i].getFConst();
1823 float divisor = rightArray[i].getFConst();
1824 if (divisor == 0.0f)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001825 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001826 if (dividend == 0.0f)
Olli Etuahod4453572016-09-27 13:21:46 +01001827 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001828 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001829 line,
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001830 "Zero divided by zero during constant folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001831 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001832 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
Olli Etuahod4453572016-09-27 13:21:46 +01001833 }
1834 else
1835 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001836 diagnostics->warning(line, "Divide by zero during constant folding",
1837 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001838 bool negativeResult =
1839 std::signbit(dividend) != std::signbit(divisor);
1840 resultArray[i].setFConst(
1841 negativeResult ? -std::numeric_limits<float>::infinity()
1842 : std::numeric_limits<float>::infinity());
Olli Etuahod4453572016-09-27 13:21:46 +01001843 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001844 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001845 else if (gl::isInf(dividend) && gl::isInf(divisor))
1846 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001847 diagnostics->warning(line,
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001848 "Infinity divided by infinity during constant "
1849 "folding generated NaN",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001850 "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001851 resultArray[i].setFConst(std::numeric_limits<float>::quiet_NaN());
1852 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001853 else
1854 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001855 float result = dividend / divisor;
1856 if (!gl::isInf(dividend) && gl::isInf(result))
Olli Etuahod4453572016-09-27 13:21:46 +01001857 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001858 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001859 line, "Constant folded division overflowed to infinity", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001860 }
1861 resultArray[i].setFConst(result);
1862 }
1863 break;
1864 }
1865 case EbtInt:
1866 if (rightArray[i] == 0)
1867 {
1868 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001869 line, "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001870 resultArray[i].setIConst(INT_MAX);
1871 }
1872 else
1873 {
1874 int lhs = leftArray[i].getIConst();
1875 int divisor = rightArray[i].getIConst();
1876 if (op == EOpDiv)
1877 {
1878 // Check for the special case where the minimum representable number
1879 // is
1880 // divided by -1. If left alone this leads to integer overflow in
1881 // C++.
1882 // ESSL 3.00.6 section 4.1.3 Integers:
1883 // "However, for the case where the minimum representable value is
1884 // divided by -1, it is allowed to return either the minimum
1885 // representable value or the maximum representable value."
1886 if (lhs == -0x7fffffff - 1 && divisor == -1)
1887 {
1888 resultArray[i].setIConst(0x7fffffff);
1889 }
1890 else
1891 {
1892 resultArray[i].setIConst(lhs / divisor);
1893 }
Olli Etuahod4453572016-09-27 13:21:46 +01001894 }
1895 else
1896 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001897 ASSERT(op == EOpIMod);
1898 if (lhs < 0 || divisor < 0)
1899 {
1900 // ESSL 3.00.6 section 5.9: Results of modulus are undefined
1901 // when
1902 // either one of the operands is negative.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001903 diagnostics->warning(line,
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001904 "Negative modulus operator operand "
1905 "encountered during constant folding",
Olli Etuaho4de340a2016-12-16 09:32:03 +00001906 "%");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001907 resultArray[i].setIConst(0);
1908 }
1909 else
1910 {
1911 resultArray[i].setIConst(lhs % divisor);
1912 }
Olli Etuahod4453572016-09-27 13:21:46 +01001913 }
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 case EbtUInt:
1918 if (rightArray[i] == 0)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001919 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001920 diagnostics->warning(
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001921 line, "Divide by zero error during constant folding", "/");
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001922 resultArray[i].setUConst(UINT_MAX);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001923 }
1924 else
1925 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001926 if (op == EOpDiv)
1927 {
1928 resultArray[i].setUConst(leftArray[i].getUConst() /
1929 rightArray[i].getUConst());
1930 }
1931 else
1932 {
1933 ASSERT(op == EOpIMod);
1934 resultArray[i].setUConst(leftArray[i].getUConst() %
1935 rightArray[i].getUConst());
1936 }
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001937 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001938 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001939
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001940 default:
1941 UNREACHABLE();
1942 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001943 }
1944 }
1945 }
1946 break;
1947
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001948 case EOpMatrixTimesVector:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001949 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001950 // TODO(jmadll): This code should check for overflows.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001951 ASSERT(rightType.getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001952
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001953 const int matrixCols = leftType.getCols();
1954 const int matrixRows = leftType.getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001955
1956 resultArray = new TConstantUnion[matrixRows];
1957
1958 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1959 {
1960 resultArray[matrixRow].setFConst(0.0f);
1961 for (int col = 0; col < matrixCols; col++)
1962 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001963 resultArray[matrixRow].setFConst(
1964 resultArray[matrixRow].getFConst() +
1965 leftArray[col * matrixRows + matrixRow].getFConst() *
1966 rightArray[col].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001967 }
1968 }
1969 }
1970 break;
1971
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001972 case EOpVectorTimesMatrix:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001973 {
Jamie Madill5db69f52016-09-15 12:47:32 -04001974 // TODO(jmadll): This code should check for overflows.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001975 ASSERT(leftType.getBasicType() == EbtFloat);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001976
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001977 const int matrixCols = rightType.getCols();
1978 const int matrixRows = rightType.getRows();
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001979
1980 resultArray = new TConstantUnion[matrixCols];
1981
1982 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
1983 {
1984 resultArray[matrixCol].setFConst(0.0f);
1985 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
1986 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001987 resultArray[matrixCol].setFConst(
1988 resultArray[matrixCol].getFConst() +
1989 leftArray[matrixRow].getFConst() *
1990 rightArray[matrixCol * matrixRows + matrixRow].getFConst());
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001991 }
1992 }
1993 }
1994 break;
1995
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001996 case EOpLogicalAnd:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03001997 {
1998 resultArray = new TConstantUnion[objectSize];
1999 for (size_t i = 0; i < objectSize; i++)
2000 {
2001 resultArray[i] = leftArray[i] && rightArray[i];
2002 }
2003 }
2004 break;
2005
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002006 case EOpLogicalOr:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002007 {
2008 resultArray = new TConstantUnion[objectSize];
2009 for (size_t i = 0; i < objectSize; i++)
2010 {
2011 resultArray[i] = leftArray[i] || rightArray[i];
2012 }
2013 }
2014 break;
2015
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002016 case EOpLogicalXor:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002017 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002018 ASSERT(leftType.getBasicType() == EbtBool);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002019 resultArray = new TConstantUnion[objectSize];
2020 for (size_t i = 0; i < objectSize; i++)
2021 {
Olli Etuaho3fdec912016-08-18 15:08:06 +03002022 resultArray[i].setBConst(leftArray[i] != rightArray[i]);
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002023 }
2024 }
2025 break;
2026
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002027 case EOpBitwiseAnd:
2028 resultArray = new TConstantUnion[objectSize];
2029 for (size_t i = 0; i < objectSize; i++)
2030 resultArray[i] = leftArray[i] & rightArray[i];
2031 break;
2032 case EOpBitwiseXor:
2033 resultArray = new TConstantUnion[objectSize];
2034 for (size_t i = 0; i < objectSize; i++)
2035 resultArray[i] = leftArray[i] ^ rightArray[i];
2036 break;
2037 case EOpBitwiseOr:
2038 resultArray = new TConstantUnion[objectSize];
2039 for (size_t i = 0; i < objectSize; i++)
2040 resultArray[i] = leftArray[i] | rightArray[i];
2041 break;
2042 case EOpBitShiftLeft:
2043 resultArray = new TConstantUnion[objectSize];
2044 for (size_t i = 0; i < objectSize; i++)
2045 resultArray[i] =
2046 TConstantUnion::lshift(leftArray[i], rightArray[i], diagnostics, line);
2047 break;
2048 case EOpBitShiftRight:
2049 resultArray = new TConstantUnion[objectSize];
2050 for (size_t i = 0; i < objectSize; i++)
2051 resultArray[i] =
2052 TConstantUnion::rshift(leftArray[i], rightArray[i], diagnostics, line);
2053 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002054
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002055 case EOpLessThan:
2056 ASSERT(objectSize == 1);
2057 resultArray = new TConstantUnion[1];
2058 resultArray->setBConst(*leftArray < *rightArray);
2059 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002060
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002061 case EOpGreaterThan:
2062 ASSERT(objectSize == 1);
2063 resultArray = new TConstantUnion[1];
2064 resultArray->setBConst(*leftArray > *rightArray);
2065 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002066
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002067 case EOpLessThanEqual:
2068 ASSERT(objectSize == 1);
2069 resultArray = new TConstantUnion[1];
2070 resultArray->setBConst(!(*leftArray > *rightArray));
2071 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002072
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002073 case EOpGreaterThanEqual:
2074 ASSERT(objectSize == 1);
2075 resultArray = new TConstantUnion[1];
2076 resultArray->setBConst(!(*leftArray < *rightArray));
2077 break;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002078
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002079 case EOpEqual:
2080 case EOpNotEqual:
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002081 {
2082 resultArray = new TConstantUnion[1];
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002083 bool equal = true;
Olli Etuaho40d9edf2015-11-12 17:30:34 +02002084 for (size_t i = 0; i < objectSize; i++)
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002085 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02002086 if (leftArray[i] != rightArray[i])
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002087 {
Olli Etuaho40d9edf2015-11-12 17:30:34 +02002088 equal = false;
2089 break; // break out of for loop
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002090 }
2091 }
2092 if (op == EOpEqual)
2093 {
2094 resultArray->setBConst(equal);
2095 }
2096 else
2097 {
2098 resultArray->setBConst(!equal);
2099 }
2100 }
2101 break;
2102
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002103 default:
2104 UNREACHABLE();
2105 return nullptr;
Olli Etuaho2c4b7462015-06-08 11:30:31 +03002106 }
2107 return resultArray;
2108}
2109
Olli Etuahof119a262016-08-19 15:54:22 +03002110// The fold functions do operations on a constant at GLSL compile time, without generating run-time
2111// code. Returns the constant value to keep using. Nullptr should not be returned.
2112TConstantUnion *TIntermConstantUnion::foldUnaryNonComponentWise(TOperator op)
Jamie Madillb1a85f42014-08-19 15:23:24 -04002113{
Olli Etuahof119a262016-08-19 15:54:22 +03002114 // Do operations where the return type may have a different number of components compared to the
2115 // operand type.
Jamie Madillb1a85f42014-08-19 15:23:24 -04002116
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002117 const TConstantUnion *operandArray = getConstantValue();
Olli Etuahof119a262016-08-19 15:54:22 +03002118 ASSERT(operandArray);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302119
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002120 size_t objectSize = getType().getObjectSize();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302121 TConstantUnion *resultArray = nullptr;
2122 switch (op)
2123 {
Olli Etuahof119a262016-08-19 15:54:22 +03002124 case EOpAny:
2125 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302126 resultArray = new TConstantUnion();
2127 resultArray->setBConst(false);
2128 for (size_t i = 0; i < objectSize; i++)
2129 {
2130 if (operandArray[i].getBConst())
2131 {
2132 resultArray->setBConst(true);
2133 break;
2134 }
2135 }
2136 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302137
Olli Etuahof119a262016-08-19 15:54:22 +03002138 case EOpAll:
2139 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302140 resultArray = new TConstantUnion();
2141 resultArray->setBConst(true);
2142 for (size_t i = 0; i < objectSize; i++)
2143 {
2144 if (!operandArray[i].getBConst())
2145 {
2146 resultArray->setBConst(false);
2147 break;
2148 }
2149 }
2150 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302151
Olli Etuahof119a262016-08-19 15:54:22 +03002152 case EOpLength:
2153 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302154 resultArray = new TConstantUnion();
2155 resultArray->setFConst(VectorLength(operandArray, objectSize));
2156 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302157
Olli Etuahof119a262016-08-19 15:54:22 +03002158 case EOpTranspose:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302159 {
Olli Etuahof119a262016-08-19 15:54:22 +03002160 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302161 resultArray = new TConstantUnion[objectSize];
2162 angle::Matrix<float> result =
Olli Etuahod5da5052016-08-29 13:16:55 +03002163 GetMatrix(operandArray, getType().getRows(), getType().getCols()).transpose();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302164 SetUnionArrayFromMatrix(result, resultArray);
2165 break;
2166 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302167
Olli Etuahof119a262016-08-19 15:54:22 +03002168 case EOpDeterminant:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302169 {
Olli Etuahof119a262016-08-19 15:54:22 +03002170 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302171 unsigned int size = getType().getNominalSize();
2172 ASSERT(size >= 2 && size <= 4);
2173 resultArray = new TConstantUnion();
2174 resultArray->setFConst(GetMatrix(operandArray, size).determinant());
2175 break;
2176 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302177
Olli Etuahof119a262016-08-19 15:54:22 +03002178 case EOpInverse:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302179 {
Olli Etuahof119a262016-08-19 15:54:22 +03002180 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302181 unsigned int size = getType().getNominalSize();
2182 ASSERT(size >= 2 && size <= 4);
Olli Etuahof119a262016-08-19 15:54:22 +03002183 resultArray = new TConstantUnion[objectSize];
Arun Patoleab2b9a22015-07-06 18:27:56 +05302184 angle::Matrix<float> result = GetMatrix(operandArray, size).inverse();
2185 SetUnionArrayFromMatrix(result, resultArray);
2186 break;
2187 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302188
Olli Etuahof119a262016-08-19 15:54:22 +03002189 case EOpPackSnorm2x16:
2190 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302191 ASSERT(getType().getNominalSize() == 2);
2192 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002193 resultArray->setUConst(
2194 gl::packSnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05302195 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302196
Olli Etuahof119a262016-08-19 15:54:22 +03002197 case EOpUnpackSnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302198 {
Olli Etuahof119a262016-08-19 15:54:22 +03002199 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302200 resultArray = new TConstantUnion[2];
2201 float f1, f2;
2202 gl::unpackSnorm2x16(operandArray[0].getUConst(), &f1, &f2);
2203 resultArray[0].setFConst(f1);
2204 resultArray[1].setFConst(f2);
2205 break;
2206 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302207
Olli Etuahof119a262016-08-19 15:54:22 +03002208 case EOpPackUnorm2x16:
2209 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302210 ASSERT(getType().getNominalSize() == 2);
2211 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002212 resultArray->setUConst(
2213 gl::packUnorm2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05302214 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302215
Olli Etuahof119a262016-08-19 15:54:22 +03002216 case EOpUnpackUnorm2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302217 {
Olli Etuahof119a262016-08-19 15:54:22 +03002218 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302219 resultArray = new TConstantUnion[2];
2220 float f1, f2;
2221 gl::unpackUnorm2x16(operandArray[0].getUConst(), &f1, &f2);
2222 resultArray[0].setFConst(f1);
2223 resultArray[1].setFConst(f2);
2224 break;
2225 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302226
Olli Etuahof119a262016-08-19 15:54:22 +03002227 case EOpPackHalf2x16:
2228 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302229 ASSERT(getType().getNominalSize() == 2);
2230 resultArray = new TConstantUnion();
Olli Etuahof119a262016-08-19 15:54:22 +03002231 resultArray->setUConst(
2232 gl::packHalf2x16(operandArray[0].getFConst(), operandArray[1].getFConst()));
Arun Patoleab2b9a22015-07-06 18:27:56 +05302233 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302234
Olli Etuahof119a262016-08-19 15:54:22 +03002235 case EOpUnpackHalf2x16:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302236 {
Olli Etuahof119a262016-08-19 15:54:22 +03002237 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302238 resultArray = new TConstantUnion[2];
2239 float f1, f2;
2240 gl::unpackHalf2x16(operandArray[0].getUConst(), &f1, &f2);
2241 resultArray[0].setFConst(f1);
2242 resultArray[1].setFConst(f2);
2243 break;
2244 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302245
Olli Etuaho25aef452017-01-29 16:15:44 -08002246 case EOpPackUnorm4x8:
2247 {
2248 ASSERT(getType().getBasicType() == EbtFloat);
2249 resultArray = new TConstantUnion();
2250 resultArray->setUConst(
2251 gl::PackUnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2252 operandArray[2].getFConst(), operandArray[3].getFConst()));
2253 break;
2254 }
2255 case EOpPackSnorm4x8:
2256 {
2257 ASSERT(getType().getBasicType() == EbtFloat);
2258 resultArray = new TConstantUnion();
2259 resultArray->setUConst(
2260 gl::PackSnorm4x8(operandArray[0].getFConst(), operandArray[1].getFConst(),
2261 operandArray[2].getFConst(), operandArray[3].getFConst()));
2262 break;
2263 }
2264 case EOpUnpackUnorm4x8:
2265 {
2266 ASSERT(getType().getBasicType() == EbtUInt);
2267 resultArray = new TConstantUnion[4];
2268 float f[4];
2269 gl::UnpackUnorm4x8(operandArray[0].getUConst(), f);
2270 for (size_t i = 0; i < 4; ++i)
2271 {
2272 resultArray[i].setFConst(f[i]);
2273 }
2274 break;
2275 }
2276 case EOpUnpackSnorm4x8:
2277 {
2278 ASSERT(getType().getBasicType() == EbtUInt);
2279 resultArray = new TConstantUnion[4];
2280 float f[4];
2281 gl::UnpackSnorm4x8(operandArray[0].getUConst(), f);
2282 for (size_t i = 0; i < 4; ++i)
2283 {
2284 resultArray[i].setFConst(f[i]);
2285 }
2286 break;
2287 }
2288
Olli Etuahof119a262016-08-19 15:54:22 +03002289 default:
2290 UNREACHABLE();
2291 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302292 }
2293
2294 return resultArray;
2295}
2296
Olli Etuahof119a262016-08-19 15:54:22 +03002297TConstantUnion *TIntermConstantUnion::foldUnaryComponentWise(TOperator op,
2298 TDiagnostics *diagnostics)
Arun Patoleab2b9a22015-07-06 18:27:56 +05302299{
Olli Etuahof119a262016-08-19 15:54:22 +03002300 // Do unary operations where each component of the result is computed based on the corresponding
2301 // component of the operand. Also folds normalize, though the divisor in that case takes all
2302 // components into account.
Arun Patoleab2b9a22015-07-06 18:27:56 +05302303
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002304 const TConstantUnion *operandArray = getConstantValue();
Olli Etuahof119a262016-08-19 15:54:22 +03002305 ASSERT(operandArray);
Jamie Madillb1a85f42014-08-19 15:23:24 -04002306
2307 size_t objectSize = getType().getObjectSize();
2308
Arun Patoleab2b9a22015-07-06 18:27:56 +05302309 TConstantUnion *resultArray = new TConstantUnion[objectSize];
2310 for (size_t i = 0; i < objectSize; i++)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302311 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002312 switch (op)
Arun Patole9d0b1f92015-05-20 14:27:17 +05302313 {
Olli Etuahof119a262016-08-19 15:54:22 +03002314 case EOpNegative:
2315 switch (getType().getBasicType())
2316 {
2317 case EbtFloat:
2318 resultArray[i].setFConst(-operandArray[i].getFConst());
2319 break;
2320 case EbtInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002321 if (operandArray[i] == std::numeric_limits<int>::min())
2322 {
2323 // The minimum representable integer doesn't have a positive
2324 // counterpart, rather the negation overflows and in ESSL is supposed to
2325 // wrap back to the minimum representable integer. Make sure that we
2326 // don't actually let the negation overflow, which has undefined
2327 // behavior in C++.
2328 resultArray[i].setIConst(std::numeric_limits<int>::min());
2329 }
2330 else
2331 {
2332 resultArray[i].setIConst(-operandArray[i].getIConst());
2333 }
Olli Etuahof119a262016-08-19 15:54:22 +03002334 break;
2335 case EbtUInt:
Olli Etuaho42fad762016-09-28 10:06:29 +01002336 if (operandArray[i] == 0x80000000u)
2337 {
2338 resultArray[i].setUConst(0x80000000u);
2339 }
2340 else
2341 {
2342 resultArray[i].setUConst(static_cast<unsigned int>(
2343 -static_cast<int>(operandArray[i].getUConst())));
2344 }
Olli Etuahof119a262016-08-19 15:54:22 +03002345 break;
2346 default:
2347 UNREACHABLE();
2348 return nullptr;
2349 }
Arun Patole1155ddd2015-06-05 18:04:36 +05302350 break;
Arun Patolecdfa8f52015-06-30 17:48:25 +05302351
Olli Etuahof119a262016-08-19 15:54:22 +03002352 case EOpPositive:
2353 switch (getType().getBasicType())
2354 {
2355 case EbtFloat:
2356 resultArray[i].setFConst(operandArray[i].getFConst());
2357 break;
2358 case EbtInt:
2359 resultArray[i].setIConst(operandArray[i].getIConst());
2360 break;
2361 case EbtUInt:
2362 resultArray[i].setUConst(static_cast<unsigned int>(
2363 static_cast<int>(operandArray[i].getUConst())));
2364 break;
2365 default:
2366 UNREACHABLE();
2367 return nullptr;
2368 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302369 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302370
Olli Etuahof119a262016-08-19 15:54:22 +03002371 case EOpLogicalNot:
2372 switch (getType().getBasicType())
2373 {
2374 case EbtBool:
2375 resultArray[i].setBConst(!operandArray[i].getBConst());
2376 break;
2377 default:
2378 UNREACHABLE();
2379 return nullptr;
2380 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302381 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302382
Olli Etuahof119a262016-08-19 15:54:22 +03002383 case EOpBitwiseNot:
2384 switch (getType().getBasicType())
2385 {
2386 case EbtInt:
2387 resultArray[i].setIConst(~operandArray[i].getIConst());
2388 break;
2389 case EbtUInt:
2390 resultArray[i].setUConst(~operandArray[i].getUConst());
2391 break;
2392 default:
2393 UNREACHABLE();
2394 return nullptr;
2395 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302396 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302397
Olli Etuahof119a262016-08-19 15:54:22 +03002398 case EOpRadians:
2399 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302400 resultArray[i].setFConst(kDegreesToRadiansMultiplier * operandArray[i].getFConst());
2401 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302402
Olli Etuahof119a262016-08-19 15:54:22 +03002403 case EOpDegrees:
2404 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302405 resultArray[i].setFConst(kRadiansToDegreesMultiplier * operandArray[i].getFConst());
2406 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302407
Olli Etuahof119a262016-08-19 15:54:22 +03002408 case EOpSin:
2409 foldFloatTypeUnary(operandArray[i], &sinf, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302410 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302411
Olli Etuahof119a262016-08-19 15:54:22 +03002412 case EOpCos:
2413 foldFloatTypeUnary(operandArray[i], &cosf, &resultArray[i]);
2414 break;
2415
2416 case EOpTan:
2417 foldFloatTypeUnary(operandArray[i], &tanf, &resultArray[i]);
2418 break;
2419
2420 case EOpAsin:
2421 // For asin(x), results are undefined if |x| > 1, we are choosing to set result to
2422 // 0.
2423 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2424 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2425 diagnostics, &resultArray[i]);
2426 else
2427 foldFloatTypeUnary(operandArray[i], &asinf, &resultArray[i]);
2428 break;
2429
2430 case EOpAcos:
2431 // For acos(x), results are undefined if |x| > 1, we are choosing to set result to
2432 // 0.
2433 if (fabsf(operandArray[i].getFConst()) > 1.0f)
2434 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2435 diagnostics, &resultArray[i]);
2436 else
2437 foldFloatTypeUnary(operandArray[i], &acosf, &resultArray[i]);
2438 break;
2439
2440 case EOpAtan:
2441 foldFloatTypeUnary(operandArray[i], &atanf, &resultArray[i]);
2442 break;
2443
2444 case EOpSinh:
2445 foldFloatTypeUnary(operandArray[i], &sinhf, &resultArray[i]);
2446 break;
2447
2448 case EOpCosh:
2449 foldFloatTypeUnary(operandArray[i], &coshf, &resultArray[i]);
2450 break;
2451
2452 case EOpTanh:
2453 foldFloatTypeUnary(operandArray[i], &tanhf, &resultArray[i]);
2454 break;
2455
2456 case EOpAsinh:
2457 foldFloatTypeUnary(operandArray[i], &asinhf, &resultArray[i]);
2458 break;
2459
2460 case EOpAcosh:
2461 // For acosh(x), results are undefined if x < 1, we are choosing to set result to 0.
2462 if (operandArray[i].getFConst() < 1.0f)
2463 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2464 diagnostics, &resultArray[i]);
2465 else
2466 foldFloatTypeUnary(operandArray[i], &acoshf, &resultArray[i]);
2467 break;
2468
2469 case EOpAtanh:
2470 // For atanh(x), results are undefined if |x| >= 1, we are choosing to set result to
2471 // 0.
2472 if (fabsf(operandArray[i].getFConst()) >= 1.0f)
2473 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2474 diagnostics, &resultArray[i]);
2475 else
2476 foldFloatTypeUnary(operandArray[i], &atanhf, &resultArray[i]);
2477 break;
2478
2479 case EOpAbs:
2480 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302481 {
Olli Etuahof119a262016-08-19 15:54:22 +03002482 case EbtFloat:
2483 resultArray[i].setFConst(fabsf(operandArray[i].getFConst()));
2484 break;
2485 case EbtInt:
2486 resultArray[i].setIConst(abs(operandArray[i].getIConst()));
2487 break;
2488 default:
2489 UNREACHABLE();
2490 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302491 }
2492 break;
Olli Etuahof119a262016-08-19 15:54:22 +03002493
2494 case EOpSign:
2495 switch (getType().getBasicType())
Arun Patoleab2b9a22015-07-06 18:27:56 +05302496 {
Olli Etuahof119a262016-08-19 15:54:22 +03002497 case EbtFloat:
2498 {
2499 float fConst = operandArray[i].getFConst();
2500 float fResult = 0.0f;
2501 if (fConst > 0.0f)
2502 fResult = 1.0f;
2503 else if (fConst < 0.0f)
2504 fResult = -1.0f;
2505 resultArray[i].setFConst(fResult);
2506 break;
2507 }
2508 case EbtInt:
2509 {
2510 int iConst = operandArray[i].getIConst();
2511 int iResult = 0;
2512 if (iConst > 0)
2513 iResult = 1;
2514 else if (iConst < 0)
2515 iResult = -1;
2516 resultArray[i].setIConst(iResult);
2517 break;
2518 }
2519 default:
2520 UNREACHABLE();
2521 return nullptr;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302522 }
2523 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302524
Olli Etuahof119a262016-08-19 15:54:22 +03002525 case EOpFloor:
2526 foldFloatTypeUnary(operandArray[i], &floorf, &resultArray[i]);
2527 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302528
Olli Etuahof119a262016-08-19 15:54:22 +03002529 case EOpTrunc:
2530 foldFloatTypeUnary(operandArray[i], &truncf, &resultArray[i]);
2531 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302532
Olli Etuahof119a262016-08-19 15:54:22 +03002533 case EOpRound:
2534 foldFloatTypeUnary(operandArray[i], &roundf, &resultArray[i]);
2535 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302536
Olli Etuahof119a262016-08-19 15:54:22 +03002537 case EOpRoundEven:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302538 {
Olli Etuahof119a262016-08-19 15:54:22 +03002539 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302540 float x = operandArray[i].getFConst();
2541 float result;
2542 float fractPart = modff(x, &result);
2543 if (fabsf(fractPart) == 0.5f)
2544 result = 2.0f * roundf(x / 2.0f);
2545 else
2546 result = roundf(x);
2547 resultArray[i].setFConst(result);
2548 break;
2549 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302550
Olli Etuahof119a262016-08-19 15:54:22 +03002551 case EOpCeil:
2552 foldFloatTypeUnary(operandArray[i], &ceilf, &resultArray[i]);
2553 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302554
Olli Etuahof119a262016-08-19 15:54:22 +03002555 case EOpFract:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302556 {
Olli Etuahof119a262016-08-19 15:54:22 +03002557 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302558 float x = operandArray[i].getFConst();
2559 resultArray[i].setFConst(x - floorf(x));
2560 break;
2561 }
Arun Patoleab2b9a22015-07-06 18:27:56 +05302562
Olli Etuahof119a262016-08-19 15:54:22 +03002563 case EOpIsNan:
2564 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302565 resultArray[i].setBConst(gl::isNaN(operandArray[0].getFConst()));
2566 break;
Arun Patole551279e2015-07-07 18:18:23 +05302567
Olli Etuahof119a262016-08-19 15:54:22 +03002568 case EOpIsInf:
2569 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302570 resultArray[i].setBConst(gl::isInf(operandArray[0].getFConst()));
2571 break;
Arun Patole551279e2015-07-07 18:18:23 +05302572
Olli Etuahof119a262016-08-19 15:54:22 +03002573 case EOpFloatBitsToInt:
2574 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302575 resultArray[i].setIConst(gl::bitCast<int32_t>(operandArray[0].getFConst()));
2576 break;
Arun Patole551279e2015-07-07 18:18:23 +05302577
Olli Etuahof119a262016-08-19 15:54:22 +03002578 case EOpFloatBitsToUint:
2579 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole551279e2015-07-07 18:18:23 +05302580 resultArray[i].setUConst(gl::bitCast<uint32_t>(operandArray[0].getFConst()));
2581 break;
Arun Patole551279e2015-07-07 18:18:23 +05302582
Olli Etuahof119a262016-08-19 15:54:22 +03002583 case EOpIntBitsToFloat:
2584 ASSERT(getType().getBasicType() == EbtInt);
Arun Patole551279e2015-07-07 18:18:23 +05302585 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getIConst()));
2586 break;
Arun Patole551279e2015-07-07 18:18:23 +05302587
Olli Etuahof119a262016-08-19 15:54:22 +03002588 case EOpUintBitsToFloat:
2589 ASSERT(getType().getBasicType() == EbtUInt);
Arun Patole551279e2015-07-07 18:18:23 +05302590 resultArray[i].setFConst(gl::bitCast<float>(operandArray[0].getUConst()));
2591 break;
Arun Patole551279e2015-07-07 18:18:23 +05302592
Olli Etuahof119a262016-08-19 15:54:22 +03002593 case EOpExp:
2594 foldFloatTypeUnary(operandArray[i], &expf, &resultArray[i]);
2595 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302596
Olli Etuahof119a262016-08-19 15:54:22 +03002597 case EOpLog:
2598 // For log(x), results are undefined if x <= 0, we are choosing to set result to 0.
2599 if (operandArray[i].getFConst() <= 0.0f)
2600 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2601 diagnostics, &resultArray[i]);
2602 else
2603 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2604 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302605
Olli Etuahof119a262016-08-19 15:54:22 +03002606 case EOpExp2:
2607 foldFloatTypeUnary(operandArray[i], &exp2f, &resultArray[i]);
2608 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302609
Olli Etuahof119a262016-08-19 15:54:22 +03002610 case EOpLog2:
2611 // For log2(x), results are undefined if x <= 0, we are choosing to set result to 0.
2612 // And log2f is not available on some plarforms like old android, so just using
2613 // log(x)/log(2) here.
2614 if (operandArray[i].getFConst() <= 0.0f)
2615 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2616 diagnostics, &resultArray[i]);
2617 else
2618 {
2619 foldFloatTypeUnary(operandArray[i], &logf, &resultArray[i]);
2620 resultArray[i].setFConst(resultArray[i].getFConst() / logf(2.0f));
2621 }
2622 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302623
Olli Etuahof119a262016-08-19 15:54:22 +03002624 case EOpSqrt:
2625 // For sqrt(x), results are undefined if x < 0, we are choosing to set result to 0.
2626 if (operandArray[i].getFConst() < 0.0f)
2627 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2628 diagnostics, &resultArray[i]);
2629 else
2630 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2631 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302632
Olli Etuahof119a262016-08-19 15:54:22 +03002633 case EOpInverseSqrt:
2634 // There is no stdlib built-in function equavalent for GLES built-in inversesqrt(),
2635 // so getting the square root first using builtin function sqrt() and then taking
2636 // its inverse.
2637 // Also, for inversesqrt(x), results are undefined if x <= 0, we are choosing to set
2638 // result to 0.
2639 if (operandArray[i].getFConst() <= 0.0f)
2640 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2641 diagnostics, &resultArray[i]);
2642 else
2643 {
2644 foldFloatTypeUnary(operandArray[i], &sqrtf, &resultArray[i]);
2645 resultArray[i].setFConst(1.0f / resultArray[i].getFConst());
2646 }
2647 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302648
Olli Etuahod68924e2017-01-02 17:34:40 +00002649 case EOpLogicalNotComponentWise:
Olli Etuahof119a262016-08-19 15:54:22 +03002650 ASSERT(getType().getBasicType() == EbtBool);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302651 resultArray[i].setBConst(!operandArray[i].getBConst());
2652 break;
Arun Patoleab2b9a22015-07-06 18:27:56 +05302653
Olli Etuahof119a262016-08-19 15:54:22 +03002654 case EOpNormalize:
Arun Patoleab2b9a22015-07-06 18:27:56 +05302655 {
Olli Etuahof119a262016-08-19 15:54:22 +03002656 ASSERT(getType().getBasicType() == EbtFloat);
2657 float x = operandArray[i].getFConst();
Arun Patoleab2b9a22015-07-06 18:27:56 +05302658 float length = VectorLength(operandArray, objectSize);
2659 if (length)
2660 resultArray[i].setFConst(x / length);
2661 else
Olli Etuahof119a262016-08-19 15:54:22 +03002662 UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(),
2663 diagnostics, &resultArray[i]);
Arun Patoleab2b9a22015-07-06 18:27:56 +05302664 break;
2665 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00002666 case EOpBitfieldReverse:
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 uint32_t result = gl::BitfieldReverse(value);
2679 if (getType().getBasicType() == EbtInt)
2680 {
2681 resultArray[i].setIConst(static_cast<int32_t>(result));
2682 }
2683 else
2684 {
2685 resultArray[i].setUConst(result);
2686 }
2687 break;
2688 }
2689 case EOpBitCount:
2690 {
2691 uint32_t value;
2692 if (getType().getBasicType() == EbtInt)
2693 {
2694 value = static_cast<uint32_t>(operandArray[i].getIConst());
2695 }
2696 else
2697 {
2698 ASSERT(getType().getBasicType() == EbtUInt);
2699 value = operandArray[i].getUConst();
2700 }
2701 int result = gl::BitCount(value);
2702 resultArray[i].setIConst(result);
2703 break;
2704 }
2705 case EOpFindLSB:
2706 {
2707 uint32_t value;
2708 if (getType().getBasicType() == EbtInt)
2709 {
2710 value = static_cast<uint32_t>(operandArray[i].getIConst());
2711 }
2712 else
2713 {
2714 ASSERT(getType().getBasicType() == EbtUInt);
2715 value = operandArray[i].getUConst();
2716 }
2717 resultArray[i].setIConst(gl::FindLSB(value));
2718 break;
2719 }
2720 case EOpFindMSB:
2721 {
2722 uint32_t value;
2723 if (getType().getBasicType() == EbtInt)
2724 {
2725 int intValue = operandArray[i].getIConst();
2726 value = static_cast<uint32_t>(intValue);
2727 if (intValue < 0)
2728 {
2729 // Look for zero instead of one in value. This also handles the intValue ==
2730 // -1 special case, where the return value needs to be -1.
2731 value = ~value;
2732 }
2733 }
2734 else
2735 {
2736 ASSERT(getType().getBasicType() == EbtUInt);
2737 value = operandArray[i].getUConst();
2738 }
2739 resultArray[i].setIConst(gl::FindMSB(value));
2740 break;
2741 }
Olli Etuahof119a262016-08-19 15:54:22 +03002742 case EOpDFdx:
2743 case EOpDFdy:
2744 case EOpFwidth:
2745 ASSERT(getType().getBasicType() == EbtFloat);
Arun Patole0c5409f2015-07-08 15:17:53 +05302746 // Derivatives of constant arguments should be 0.
2747 resultArray[i].setFConst(0.0f);
2748 break;
Arun Patole0c5409f2015-07-08 15:17:53 +05302749
Olli Etuahof119a262016-08-19 15:54:22 +03002750 default:
2751 return nullptr;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302752 }
Arun Patole9d0b1f92015-05-20 14:27:17 +05302753 }
Jamie Madillb1a85f42014-08-19 15:23:24 -04002754
Arun Patoleab2b9a22015-07-06 18:27:56 +05302755 return resultArray;
Jamie Madillb1a85f42014-08-19 15:23:24 -04002756}
2757
Olli Etuahof119a262016-08-19 15:54:22 +03002758void TIntermConstantUnion::foldFloatTypeUnary(const TConstantUnion &parameter,
2759 FloatTypeUnaryFunc builtinFunc,
2760 TConstantUnion *result) const
Arun Patole9dea48f2015-04-02 11:45:09 +05302761{
2762 ASSERT(builtinFunc);
2763
Olli Etuahof119a262016-08-19 15:54:22 +03002764 ASSERT(getType().getBasicType() == EbtFloat);
2765 result->setFConst(builtinFunc(parameter.getFConst()));
Arun Patole9dea48f2015-04-02 11:45:09 +05302766}
2767
Jamie Madillb1a85f42014-08-19 15:23:24 -04002768// static
Olli Etuahof119a262016-08-19 15:54:22 +03002769TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *aggregate,
2770 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +05302771{
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002772 TOperator op = aggregate->getOp();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002773 TIntermSequence *arguments = aggregate->getSequence();
2774 unsigned int argsCount = static_cast<unsigned int>(arguments->size());
2775 std::vector<const TConstantUnion *> unionArrays(argsCount);
2776 std::vector<size_t> objectSizes(argsCount);
Olli Etuahob43846e2015-06-02 18:18:57 +03002777 size_t maxObjectSize = 0;
Arun Patole274f0702015-05-05 13:33:30 +05302778 TBasicType basicType = EbtVoid;
2779 TSourceLoc loc;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002780 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302781 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002782 TIntermConstantUnion *argConstant = (*arguments)[i]->getAsConstantUnion();
2783 ASSERT(argConstant != nullptr); // Should be checked already.
Arun Patole274f0702015-05-05 13:33:30 +05302784
2785 if (i == 0)
2786 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002787 basicType = argConstant->getType().getBasicType();
2788 loc = argConstant->getLine();
Arun Patole274f0702015-05-05 13:33:30 +05302789 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002790 unionArrays[i] = argConstant->getConstantValue();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002791 objectSizes[i] = argConstant->getType().getObjectSize();
Olli Etuahob43846e2015-06-02 18:18:57 +03002792 if (objectSizes[i] > maxObjectSize)
2793 maxObjectSize = objectSizes[i];
Arun Patole274f0702015-05-05 13:33:30 +05302794 }
2795
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002796 if (!(*arguments)[0]->getAsTyped()->isMatrix() && aggregate->getOp() != EOpOuterProduct)
Arun Patole7fa33552015-06-10 15:15:18 +05302797 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002798 for (unsigned int i = 0; i < argsCount; i++)
Arun Patole7fa33552015-06-10 15:15:18 +05302799 if (objectSizes[i] != maxObjectSize)
2800 unionArrays[i] = Vectorize(*unionArrays[i], maxObjectSize);
2801 }
Arun Patole274f0702015-05-05 13:33:30 +05302802
Olli Etuahob43846e2015-06-02 18:18:57 +03002803 TConstantUnion *resultArray = nullptr;
Olli Etuaho51182ab2017-01-22 00:12:29 +00002804
2805 switch (op)
Arun Patole274f0702015-05-05 13:33:30 +05302806 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002807 case EOpAtan:
Arun Patole274f0702015-05-05 13:33:30 +05302808 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002809 ASSERT(basicType == EbtFloat);
2810 resultArray = new TConstantUnion[maxObjectSize];
2811 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302812 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002813 float y = unionArrays[0][i].getFConst();
2814 float x = unionArrays[1][i].getFConst();
2815 // Results are undefined if x and y are both 0.
2816 if (x == 0.0f && y == 0.0f)
2817 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2818 else
2819 resultArray[i].setFConst(atan2f(y, x));
Arun Patolebf790422015-05-18 17:53:04 +05302820 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002821 break;
2822 }
Arun Patolebf790422015-05-18 17:53:04 +05302823
Olli Etuaho51182ab2017-01-22 00:12:29 +00002824 case EOpPow:
2825 {
2826 ASSERT(basicType == EbtFloat);
2827 resultArray = new TConstantUnion[maxObjectSize];
2828 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302829 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002830 float x = unionArrays[0][i].getFConst();
2831 float y = unionArrays[1][i].getFConst();
2832 // Results are undefined if x < 0.
2833 // Results are undefined if x = 0 and y <= 0.
2834 if (x < 0.0f)
2835 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2836 else if (x == 0.0f && y <= 0.0f)
2837 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
2838 else
2839 resultArray[i].setFConst(powf(x, y));
Arun Patolebf790422015-05-18 17:53:04 +05302840 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002841 break;
2842 }
Arun Patolebf790422015-05-18 17:53:04 +05302843
Olli Etuaho51182ab2017-01-22 00:12:29 +00002844 case EOpMod:
2845 {
2846 ASSERT(basicType == EbtFloat);
2847 resultArray = new TConstantUnion[maxObjectSize];
2848 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patolebf790422015-05-18 17:53:04 +05302849 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002850 float x = unionArrays[0][i].getFConst();
2851 float y = unionArrays[1][i].getFConst();
2852 resultArray[i].setFConst(x - y * floorf(x / y));
Arun Patolebf790422015-05-18 17:53:04 +05302853 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002854 break;
2855 }
Arun Patolebf790422015-05-18 17:53:04 +05302856
Olli Etuaho51182ab2017-01-22 00:12:29 +00002857 case EOpMin:
2858 {
2859 resultArray = new TConstantUnion[maxObjectSize];
2860 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302861 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002862 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302863 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002864 case EbtFloat:
2865 resultArray[i].setFConst(
2866 std::min(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2867 break;
2868 case EbtInt:
2869 resultArray[i].setIConst(
2870 std::min(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2871 break;
2872 case EbtUInt:
2873 resultArray[i].setUConst(
2874 std::min(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2875 break;
2876 default:
2877 UNREACHABLE();
2878 break;
Arun Patole9d0b1f92015-05-20 14:27:17 +05302879 }
2880 }
2881 break;
Arun Patole274f0702015-05-05 13:33:30 +05302882 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002883
2884 case EOpMax:
Arun Patole274f0702015-05-05 13:33:30 +05302885 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002886 resultArray = new TConstantUnion[maxObjectSize];
2887 for (size_t i = 0; i < maxObjectSize; i++)
Arun Patole274f0702015-05-05 13:33:30 +05302888 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002889 switch (basicType)
Arun Patole274f0702015-05-05 13:33:30 +05302890 {
Olli Etuaho51182ab2017-01-22 00:12:29 +00002891 case EbtFloat:
2892 resultArray[i].setFConst(
2893 std::max(unionArrays[0][i].getFConst(), unionArrays[1][i].getFConst()));
2894 break;
2895 case EbtInt:
2896 resultArray[i].setIConst(
2897 std::max(unionArrays[0][i].getIConst(), unionArrays[1][i].getIConst()));
2898 break;
2899 case EbtUInt:
2900 resultArray[i].setUConst(
2901 std::max(unionArrays[0][i].getUConst(), unionArrays[1][i].getUConst()));
2902 break;
2903 default:
2904 UNREACHABLE();
2905 break;
Arun Patole274f0702015-05-05 13:33:30 +05302906 }
2907 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002908 break;
Arun Patole274f0702015-05-05 13:33:30 +05302909 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00002910
2911 case EOpStep:
2912 {
2913 ASSERT(basicType == EbtFloat);
2914 resultArray = new TConstantUnion[maxObjectSize];
2915 for (size_t i = 0; i < maxObjectSize; i++)
2916 resultArray[i].setFConst(
2917 unionArrays[1][i].getFConst() < unionArrays[0][i].getFConst() ? 0.0f : 1.0f);
2918 break;
2919 }
2920
2921 case EOpLessThanComponentWise:
2922 {
2923 resultArray = new TConstantUnion[maxObjectSize];
2924 for (size_t i = 0; i < maxObjectSize; i++)
2925 {
2926 switch (basicType)
2927 {
2928 case EbtFloat:
2929 resultArray[i].setBConst(unionArrays[0][i].getFConst() <
2930 unionArrays[1][i].getFConst());
2931 break;
2932 case EbtInt:
2933 resultArray[i].setBConst(unionArrays[0][i].getIConst() <
2934 unionArrays[1][i].getIConst());
2935 break;
2936 case EbtUInt:
2937 resultArray[i].setBConst(unionArrays[0][i].getUConst() <
2938 unionArrays[1][i].getUConst());
2939 break;
2940 default:
2941 UNREACHABLE();
2942 break;
2943 }
2944 }
2945 break;
2946 }
2947
2948 case EOpLessThanEqualComponentWise:
2949 {
2950 resultArray = new TConstantUnion[maxObjectSize];
2951 for (size_t i = 0; i < maxObjectSize; i++)
2952 {
2953 switch (basicType)
2954 {
2955 case EbtFloat:
2956 resultArray[i].setBConst(unionArrays[0][i].getFConst() <=
2957 unionArrays[1][i].getFConst());
2958 break;
2959 case EbtInt:
2960 resultArray[i].setBConst(unionArrays[0][i].getIConst() <=
2961 unionArrays[1][i].getIConst());
2962 break;
2963 case EbtUInt:
2964 resultArray[i].setBConst(unionArrays[0][i].getUConst() <=
2965 unionArrays[1][i].getUConst());
2966 break;
2967 default:
2968 UNREACHABLE();
2969 break;
2970 }
2971 }
2972 break;
2973 }
2974
2975 case EOpGreaterThanComponentWise:
2976 {
2977 resultArray = new TConstantUnion[maxObjectSize];
2978 for (size_t i = 0; i < maxObjectSize; i++)
2979 {
2980 switch (basicType)
2981 {
2982 case EbtFloat:
2983 resultArray[i].setBConst(unionArrays[0][i].getFConst() >
2984 unionArrays[1][i].getFConst());
2985 break;
2986 case EbtInt:
2987 resultArray[i].setBConst(unionArrays[0][i].getIConst() >
2988 unionArrays[1][i].getIConst());
2989 break;
2990 case EbtUInt:
2991 resultArray[i].setBConst(unionArrays[0][i].getUConst() >
2992 unionArrays[1][i].getUConst());
2993 break;
2994 default:
2995 UNREACHABLE();
2996 break;
2997 }
2998 }
2999 break;
3000 }
3001 case EOpGreaterThanEqualComponentWise:
3002 {
3003 resultArray = new TConstantUnion[maxObjectSize];
3004 for (size_t i = 0; i < maxObjectSize; i++)
3005 {
3006 switch (basicType)
3007 {
3008 case EbtFloat:
3009 resultArray[i].setBConst(unionArrays[0][i].getFConst() >=
3010 unionArrays[1][i].getFConst());
3011 break;
3012 case EbtInt:
3013 resultArray[i].setBConst(unionArrays[0][i].getIConst() >=
3014 unionArrays[1][i].getIConst());
3015 break;
3016 case EbtUInt:
3017 resultArray[i].setBConst(unionArrays[0][i].getUConst() >=
3018 unionArrays[1][i].getUConst());
3019 break;
3020 default:
3021 UNREACHABLE();
3022 break;
3023 }
3024 }
3025 }
3026 break;
3027
3028 case EOpEqualComponentWise:
3029 {
3030 resultArray = new TConstantUnion[maxObjectSize];
3031 for (size_t i = 0; i < maxObjectSize; i++)
3032 {
3033 switch (basicType)
3034 {
3035 case EbtFloat:
3036 resultArray[i].setBConst(unionArrays[0][i].getFConst() ==
3037 unionArrays[1][i].getFConst());
3038 break;
3039 case EbtInt:
3040 resultArray[i].setBConst(unionArrays[0][i].getIConst() ==
3041 unionArrays[1][i].getIConst());
3042 break;
3043 case EbtUInt:
3044 resultArray[i].setBConst(unionArrays[0][i].getUConst() ==
3045 unionArrays[1][i].getUConst());
3046 break;
3047 case EbtBool:
3048 resultArray[i].setBConst(unionArrays[0][i].getBConst() ==
3049 unionArrays[1][i].getBConst());
3050 break;
3051 default:
3052 UNREACHABLE();
3053 break;
3054 }
3055 }
3056 break;
3057 }
3058
3059 case EOpNotEqualComponentWise:
3060 {
3061 resultArray = new TConstantUnion[maxObjectSize];
3062 for (size_t i = 0; i < maxObjectSize; i++)
3063 {
3064 switch (basicType)
3065 {
3066 case EbtFloat:
3067 resultArray[i].setBConst(unionArrays[0][i].getFConst() !=
3068 unionArrays[1][i].getFConst());
3069 break;
3070 case EbtInt:
3071 resultArray[i].setBConst(unionArrays[0][i].getIConst() !=
3072 unionArrays[1][i].getIConst());
3073 break;
3074 case EbtUInt:
3075 resultArray[i].setBConst(unionArrays[0][i].getUConst() !=
3076 unionArrays[1][i].getUConst());
3077 break;
3078 case EbtBool:
3079 resultArray[i].setBConst(unionArrays[0][i].getBConst() !=
3080 unionArrays[1][i].getBConst());
3081 break;
3082 default:
3083 UNREACHABLE();
3084 break;
3085 }
3086 }
3087 break;
3088 }
3089
3090 case EOpDistance:
3091 {
3092 ASSERT(basicType == EbtFloat);
3093 TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
3094 resultArray = new TConstantUnion();
3095 for (size_t i = 0; i < maxObjectSize; i++)
3096 {
3097 float x = unionArrays[0][i].getFConst();
3098 float y = unionArrays[1][i].getFConst();
3099 distanceArray[i].setFConst(x - y);
3100 }
3101 resultArray->setFConst(VectorLength(distanceArray, maxObjectSize));
3102 break;
3103 }
3104
3105 case EOpDot:
3106 ASSERT(basicType == EbtFloat);
3107 resultArray = new TConstantUnion();
3108 resultArray->setFConst(VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
3109 break;
3110
3111 case EOpCross:
3112 {
3113 ASSERT(basicType == EbtFloat && maxObjectSize == 3);
3114 resultArray = new TConstantUnion[maxObjectSize];
3115 float x0 = unionArrays[0][0].getFConst();
3116 float x1 = unionArrays[0][1].getFConst();
3117 float x2 = unionArrays[0][2].getFConst();
3118 float y0 = unionArrays[1][0].getFConst();
3119 float y1 = unionArrays[1][1].getFConst();
3120 float y2 = unionArrays[1][2].getFConst();
3121 resultArray[0].setFConst(x1 * y2 - y1 * x2);
3122 resultArray[1].setFConst(x2 * y0 - y2 * x0);
3123 resultArray[2].setFConst(x0 * y1 - y0 * x1);
3124 break;
3125 }
3126
3127 case EOpReflect:
3128 {
3129 ASSERT(basicType == EbtFloat);
3130 // genType reflect (genType I, genType N) :
3131 // For the incident vector I and surface orientation N, returns the reflection
3132 // direction:
3133 // I - 2 * dot(N, I) * N.
3134 resultArray = new TConstantUnion[maxObjectSize];
3135 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
3136 for (size_t i = 0; i < maxObjectSize; i++)
3137 {
3138 float result = unionArrays[0][i].getFConst() -
3139 2.0f * dotProduct * unionArrays[1][i].getFConst();
3140 resultArray[i].setFConst(result);
3141 }
3142 break;
3143 }
3144
3145 case EOpMulMatrixComponentWise:
3146 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003147 ASSERT(basicType == EbtFloat && (*arguments)[0]->getAsTyped()->isMatrix() &&
3148 (*arguments)[1]->getAsTyped()->isMatrix());
Olli Etuaho51182ab2017-01-22 00:12:29 +00003149 // Perform component-wise matrix multiplication.
3150 resultArray = new TConstantUnion[maxObjectSize];
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003151 int size = (*arguments)[0]->getAsTyped()->getNominalSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003152 angle::Matrix<float> result =
3153 GetMatrix(unionArrays[0], size).compMult(GetMatrix(unionArrays[1], size));
3154 SetUnionArrayFromMatrix(result, resultArray);
3155 break;
3156 }
3157
3158 case EOpOuterProduct:
3159 {
3160 ASSERT(basicType == EbtFloat);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003161 size_t numRows = (*arguments)[0]->getAsTyped()->getType().getObjectSize();
3162 size_t numCols = (*arguments)[1]->getAsTyped()->getType().getObjectSize();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003163 resultArray = new TConstantUnion[numRows * numCols];
3164 angle::Matrix<float> result =
3165 GetMatrix(unionArrays[0], static_cast<int>(numRows), 1)
3166 .outerProduct(GetMatrix(unionArrays[1], 1, static_cast<int>(numCols)));
3167 SetUnionArrayFromMatrix(result, resultArray);
3168 break;
3169 }
3170
3171 case EOpClamp:
3172 {
3173 resultArray = new TConstantUnion[maxObjectSize];
3174 for (size_t i = 0; i < maxObjectSize; i++)
3175 {
3176 switch (basicType)
3177 {
3178 case EbtFloat:
3179 {
3180 float x = unionArrays[0][i].getFConst();
3181 float min = unionArrays[1][i].getFConst();
3182 float max = unionArrays[2][i].getFConst();
3183 // Results are undefined if min > max.
3184 if (min > max)
3185 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3186 &resultArray[i]);
3187 else
3188 resultArray[i].setFConst(gl::clamp(x, min, max));
3189 break;
3190 }
3191
3192 case EbtInt:
3193 {
3194 int x = unionArrays[0][i].getIConst();
3195 int min = unionArrays[1][i].getIConst();
3196 int max = unionArrays[2][i].getIConst();
3197 // Results are undefined if min > max.
3198 if (min > max)
3199 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3200 &resultArray[i]);
3201 else
3202 resultArray[i].setIConst(gl::clamp(x, min, max));
3203 break;
3204 }
3205 case EbtUInt:
3206 {
3207 unsigned int x = unionArrays[0][i].getUConst();
3208 unsigned int min = unionArrays[1][i].getUConst();
3209 unsigned int max = unionArrays[2][i].getUConst();
3210 // Results are undefined if min > max.
3211 if (min > max)
3212 UndefinedConstantFoldingError(loc, op, basicType, diagnostics,
3213 &resultArray[i]);
3214 else
3215 resultArray[i].setUConst(gl::clamp(x, min, max));
3216 break;
3217 }
3218 default:
3219 UNREACHABLE();
3220 break;
3221 }
3222 }
3223 break;
3224 }
3225
3226 case EOpMix:
3227 {
3228 ASSERT(basicType == EbtFloat);
3229 resultArray = new TConstantUnion[maxObjectSize];
3230 for (size_t i = 0; i < maxObjectSize; i++)
3231 {
3232 float x = unionArrays[0][i].getFConst();
3233 float y = unionArrays[1][i].getFConst();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003234 TBasicType type = (*arguments)[2]->getAsTyped()->getType().getBasicType();
Olli Etuaho51182ab2017-01-22 00:12:29 +00003235 if (type == EbtFloat)
3236 {
3237 // Returns the linear blend of x and y, i.e., x * (1 - a) + y * a.
3238 float a = unionArrays[2][i].getFConst();
3239 resultArray[i].setFConst(x * (1.0f - a) + y * a);
3240 }
3241 else // 3rd parameter is EbtBool
3242 {
3243 ASSERT(type == EbtBool);
3244 // Selects which vector each returned component comes from.
3245 // For a component of a that is false, the corresponding component of x is
3246 // returned.
3247 // For a component of a that is true, the corresponding component of y is
3248 // returned.
3249 bool a = unionArrays[2][i].getBConst();
3250 resultArray[i].setFConst(a ? y : x);
3251 }
3252 }
3253 break;
3254 }
3255
3256 case EOpSmoothStep:
3257 {
3258 ASSERT(basicType == EbtFloat);
3259 resultArray = new TConstantUnion[maxObjectSize];
3260 for (size_t i = 0; i < maxObjectSize; i++)
3261 {
3262 float edge0 = unionArrays[0][i].getFConst();
3263 float edge1 = unionArrays[1][i].getFConst();
3264 float x = unionArrays[2][i].getFConst();
3265 // Results are undefined if edge0 >= edge1.
3266 if (edge0 >= edge1)
3267 {
3268 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3269 }
3270 else
3271 {
3272 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth
3273 // Hermite interpolation between 0 and 1 when edge0 < x < edge1.
3274 float t = gl::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
3275 resultArray[i].setFConst(t * t * (3.0f - 2.0f * t));
3276 }
3277 }
3278 break;
3279 }
3280
Olli Etuaho74da73f2017-02-01 15:37:48 +00003281 case EOpLdexp:
3282 {
3283 resultArray = new TConstantUnion[maxObjectSize];
3284 for (size_t i = 0; i < maxObjectSize; i++)
3285 {
3286 float x = unionArrays[0][i].getFConst();
3287 int exp = unionArrays[1][i].getIConst();
3288 if (exp > 128)
3289 {
3290 UndefinedConstantFoldingError(loc, op, basicType, diagnostics, &resultArray[i]);
3291 }
3292 else
3293 {
3294 resultArray[i].setFConst(gl::Ldexp(x, exp));
3295 }
3296 }
3297 break;
3298 }
3299
Jamie Madille72595b2017-06-06 15:12:26 -04003300 case EOpFaceforward:
Olli Etuaho51182ab2017-01-22 00:12:29 +00003301 {
3302 ASSERT(basicType == EbtFloat);
3303 // genType faceforward(genType N, genType I, genType Nref) :
3304 // If dot(Nref, I) < 0 return N, otherwise return -N.
3305 resultArray = new TConstantUnion[maxObjectSize];
3306 float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
3307 for (size_t i = 0; i < maxObjectSize; i++)
3308 {
3309 if (dotProduct < 0)
3310 resultArray[i].setFConst(unionArrays[0][i].getFConst());
3311 else
3312 resultArray[i].setFConst(-unionArrays[0][i].getFConst());
3313 }
3314 break;
3315 }
3316
3317 case EOpRefract:
3318 {
3319 ASSERT(basicType == EbtFloat);
3320 // genType refract(genType I, genType N, float eta) :
3321 // For the incident vector I and surface normal N, and the ratio of indices of
3322 // refraction eta,
3323 // return the refraction vector. The result is computed by
3324 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
3325 // if (k < 0.0)
3326 // return genType(0.0)
3327 // else
3328 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
3329 resultArray = new TConstantUnion[maxObjectSize];
3330 float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
3331 for (size_t i = 0; i < maxObjectSize; i++)
3332 {
3333 float eta = unionArrays[2][i].getFConst();
3334 float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
3335 if (k < 0.0f)
3336 resultArray[i].setFConst(0.0f);
3337 else
3338 resultArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
3339 (eta * dotProduct + sqrtf(k)) *
3340 unionArrays[1][i].getFConst());
3341 }
3342 break;
3343 }
Olli Etuaho9250cb22017-01-21 10:51:27 +00003344 case EOpBitfieldExtract:
3345 {
3346 resultArray = new TConstantUnion[maxObjectSize];
3347 for (size_t i = 0; i < maxObjectSize; ++i)
3348 {
3349 int offset = unionArrays[1][0].getIConst();
3350 int bits = unionArrays[2][0].getIConst();
3351 if (bits == 0)
3352 {
3353 if (aggregate->getBasicType() == EbtInt)
3354 {
3355 resultArray[i].setIConst(0);
3356 }
3357 else
3358 {
3359 ASSERT(aggregate->getBasicType() == EbtUInt);
3360 resultArray[i].setUConst(0);
3361 }
3362 }
3363 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3364 {
3365 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3366 &resultArray[i]);
3367 }
3368 else
3369 {
3370 // bits can be 32 here, so we need to avoid bit shift overflow.
3371 uint32_t maskMsb = 1u << (bits - 1);
3372 uint32_t mask = ((maskMsb - 1u) | maskMsb) << offset;
3373 if (aggregate->getBasicType() == EbtInt)
3374 {
3375 uint32_t value = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3376 uint32_t resultUnsigned = (value & mask) >> offset;
3377 if ((resultUnsigned & maskMsb) != 0)
3378 {
3379 // The most significant bits (from bits+1 to the most significant bit)
3380 // should be set to 1.
3381 uint32_t higherBitsMask = ((1u << (32 - bits)) - 1u) << bits;
3382 resultUnsigned |= higherBitsMask;
3383 }
3384 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3385 }
3386 else
3387 {
3388 ASSERT(aggregate->getBasicType() == EbtUInt);
3389 uint32_t value = unionArrays[0][i].getUConst();
3390 resultArray[i].setUConst((value & mask) >> offset);
3391 }
3392 }
3393 }
3394 break;
3395 }
3396 case EOpBitfieldInsert:
3397 {
3398 resultArray = new TConstantUnion[maxObjectSize];
3399 for (size_t i = 0; i < maxObjectSize; ++i)
3400 {
3401 int offset = unionArrays[2][0].getIConst();
3402 int bits = unionArrays[3][0].getIConst();
3403 if (bits == 0)
3404 {
3405 if (aggregate->getBasicType() == EbtInt)
3406 {
3407 int32_t base = unionArrays[0][i].getIConst();
3408 resultArray[i].setIConst(base);
3409 }
3410 else
3411 {
3412 ASSERT(aggregate->getBasicType() == EbtUInt);
3413 uint32_t base = unionArrays[0][i].getUConst();
3414 resultArray[i].setUConst(base);
3415 }
3416 }
3417 else if (offset < 0 || bits < 0 || offset >= 32 || bits > 32 || offset + bits > 32)
3418 {
3419 UndefinedConstantFoldingError(loc, op, aggregate->getBasicType(), diagnostics,
3420 &resultArray[i]);
3421 }
3422 else
3423 {
3424 // bits can be 32 here, so we need to avoid bit shift overflow.
3425 uint32_t maskMsb = 1u << (bits - 1);
3426 uint32_t insertMask = ((maskMsb - 1u) | maskMsb) << offset;
3427 uint32_t baseMask = ~insertMask;
3428 if (aggregate->getBasicType() == EbtInt)
3429 {
3430 uint32_t base = static_cast<uint32_t>(unionArrays[0][i].getIConst());
3431 uint32_t insert = static_cast<uint32_t>(unionArrays[1][i].getIConst());
3432 uint32_t resultUnsigned =
3433 (base & baseMask) | ((insert << offset) & insertMask);
3434 resultArray[i].setIConst(static_cast<int32_t>(resultUnsigned));
3435 }
3436 else
3437 {
3438 ASSERT(aggregate->getBasicType() == EbtUInt);
3439 uint32_t base = unionArrays[0][i].getUConst();
3440 uint32_t insert = unionArrays[1][i].getUConst();
3441 resultArray[i].setUConst((base & baseMask) |
3442 ((insert << offset) & insertMask));
3443 }
3444 }
3445 }
3446 break;
3447 }
Olli Etuaho51182ab2017-01-22 00:12:29 +00003448
3449 default:
3450 UNREACHABLE();
3451 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +05303452 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003453 return resultArray;
Arun Patole274f0702015-05-05 13:33:30 +05303454}
3455
Jamie Madill45bcc782016-11-07 13:58:48 -05003456} // namespace sh