2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-2013 LunarG, Inc.
4 // Copyright (C) 2017 ARM Limited.
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
15 // Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
38 #include "localintermediate.h"
46 using namespace glslang;
53 // Some helper functions
58 // tough to find a platform independent library function, do it directly
60 int bitPatternL = u.i[0];
61 int bitPatternH = u.i[1];
62 return (bitPatternH & 0x7ff80000) == 0x7ff80000 &&
63 ((bitPatternH & 0xFFFFF) != 0 || bitPatternL != 0);
69 // tough to find a platform independent library function, do it directly
71 int bitPatternL = u.i[0];
72 int bitPatternH = u.i[1];
73 return (bitPatternH & 0x7ff00000) == 0x7ff00000 &&
74 (bitPatternH & 0xFFFFF) == 0 && bitPatternL == 0;
77 const double pi = 3.1415926535897932384626433832795;
79 } // end anonymous namespace
85 // The fold functions see if an operation on a constant can be done in place,
86 // without generating run-time code.
88 // Returns the node to keep using, which may or may not be the node passed in.
90 // Note: As of version 1.2, all constant operations must be folded. It is
91 // not opportunistic, but rather a semantic requirement.
95 // Do folding between a pair of nodes.
96 // 'this' is the left-hand operand and 'rightConstantNode' is the right-hand operand.
98 // Returns a new node representing the result.
100 TIntermTyped* TIntermConstantUnion::fold(TOperator op, const TIntermTyped* rightConstantNode) const
102 // For most cases, the return type matches the argument type, so set that
103 // up and just code to exceptions below.
105 returnType.shallowCopy(getType());
108 // A pair of nodes is to be folded together
111 const TIntermConstantUnion *rightNode = rightConstantNode->getAsConstantUnion();
112 TConstUnionArray leftUnionArray = getConstArray();
113 TConstUnionArray rightUnionArray = rightNode->getConstArray();
115 // Figure out the size of the result
119 case EOpMatrixTimesMatrix:
120 newComps = rightNode->getMatrixCols() * getMatrixRows();
122 case EOpMatrixTimesVector:
123 newComps = getMatrixRows();
125 case EOpVectorTimesMatrix:
126 newComps = rightNode->getMatrixCols();
129 newComps = getType().computeNumComponents();
130 constComps = rightConstantNode->getType().computeNumComponents();
131 if (constComps == 1 && newComps > 1) {
132 // for a case like vec4 f = vec4(2,3,4,5) + 1.2;
133 TConstUnionArray smearedArray(newComps, rightNode->getConstArray()[0]);
134 rightUnionArray = smearedArray;
135 } else if (constComps > 1 && newComps == 1) {
136 // for a case like vec4 f = 1.2 + vec4(2,3,4,5);
137 newComps = constComps;
138 rightUnionArray = rightNode->getConstArray();
139 TConstUnionArray smearedArray(newComps, getConstArray()[0]);
140 leftUnionArray = smearedArray;
141 returnType.shallowCopy(rightNode->getType());
146 TConstUnionArray newConstArray(newComps);
147 TType constBool(EbtBool, EvqConst);
151 for (int i = 0; i < newComps; i++)
152 newConstArray[i] = leftUnionArray[i] + rightUnionArray[i];
155 for (int i = 0; i < newComps; i++)
156 newConstArray[i] = leftUnionArray[i] - rightUnionArray[i];
160 case EOpVectorTimesScalar:
161 case EOpMatrixTimesScalar:
162 for (int i = 0; i < newComps; i++)
163 newConstArray[i] = leftUnionArray[i] * rightUnionArray[i];
165 case EOpMatrixTimesMatrix:
166 for (int row = 0; row < getMatrixRows(); row++) {
167 for (int column = 0; column < rightNode->getMatrixCols(); column++) {
169 for (int i = 0; i < rightNode->getMatrixRows(); i++)
170 sum += leftUnionArray[i * getMatrixRows() + row].getDConst() * rightUnionArray[column * rightNode->getMatrixRows() + i].getDConst();
171 newConstArray[column * getMatrixRows() + row].setDConst(sum);
174 returnType.shallowCopy(TType(getType().getBasicType(), EvqConst, 0, rightNode->getMatrixCols(), getMatrixRows()));
177 for (int i = 0; i < newComps; i++) {
178 switch (getType().getBasicType()) {
183 auto right = rightUnionArray[i].getDConst();
184 auto left = leftUnionArray[i].getDConst();
188 newConstArray[i].setDConst(left / right);
192 newConstArray[i].setDConst((double)INFINITY);
196 newConstArray[i].setDConst((double)-INFINITY);
200 newConstArray[i].setDConst((double)NAN);
205 if (rightUnionArray[i] == (signed char)0)
206 newConstArray[i].setI8Const((signed char)0x7F);
207 else if (rightUnionArray[i].getI8Const() == (signed char)-1 && leftUnionArray[i].getI8Const() == (signed char)-0x80)
208 newConstArray[i].setI8Const((signed char)-0x80);
210 newConstArray[i].setI8Const(leftUnionArray[i].getI8Const() / rightUnionArray[i].getI8Const());
214 if (rightUnionArray[i] == (unsigned char)0u)
215 newConstArray[i].setU8Const((unsigned char)0xFFu);
217 newConstArray[i].setU8Const(leftUnionArray[i].getU8Const() / rightUnionArray[i].getU8Const());
221 if (rightUnionArray[i] == (signed short)0)
222 newConstArray[i].setI16Const((signed short)0x7FFF);
223 else if (rightUnionArray[i].getI16Const() == (signed short)-1 && leftUnionArray[i].getI16Const() == (signed short)-0x8000)
224 newConstArray[i].setI16Const((signed short)-0x8000);
226 newConstArray[i].setI16Const(leftUnionArray[i].getI16Const() / rightUnionArray[i].getI16Const());
230 if (rightUnionArray[i] == (unsigned short)0u)
231 newConstArray[i].setU16Const((unsigned short)0xFFFFu);
233 newConstArray[i].setU16Const(leftUnionArray[i].getU16Const() / rightUnionArray[i].getU16Const());
237 if (rightUnionArray[i] == 0)
238 newConstArray[i].setIConst(0x7FFFFFFF);
239 else if (rightUnionArray[i].getIConst() == -1 && leftUnionArray[i].getIConst() == -(int)0x80000000)
240 newConstArray[i].setIConst(-(int)0x80000000);
242 newConstArray[i].setIConst(leftUnionArray[i].getIConst() / rightUnionArray[i].getIConst());
246 if (rightUnionArray[i] == 0u)
247 newConstArray[i].setUConst(0xFFFFFFFFu);
249 newConstArray[i].setUConst(leftUnionArray[i].getUConst() / rightUnionArray[i].getUConst());
253 if (rightUnionArray[i] == 0ll)
254 newConstArray[i].setI64Const(0x7FFFFFFFFFFFFFFFll);
255 else if (rightUnionArray[i].getI64Const() == -1 && leftUnionArray[i].getI64Const() == -0x8000000000000000ll)
256 newConstArray[i].setI64Const(-0x8000000000000000ll);
258 newConstArray[i].setI64Const(leftUnionArray[i].getI64Const() / rightUnionArray[i].getI64Const());
262 if (rightUnionArray[i] == 0ull)
263 newConstArray[i].setU64Const(0xFFFFFFFFFFFFFFFFull);
265 newConstArray[i].setU64Const(leftUnionArray[i].getU64Const() / rightUnionArray[i].getU64Const());
273 case EOpMatrixTimesVector:
274 for (int i = 0; i < getMatrixRows(); i++) {
276 for (int j = 0; j < rightNode->getVectorSize(); j++) {
277 sum += leftUnionArray[j*getMatrixRows() + i].getDConst() * rightUnionArray[j].getDConst();
279 newConstArray[i].setDConst(sum);
282 returnType.shallowCopy(TType(getBasicType(), EvqConst, getMatrixRows()));
285 case EOpVectorTimesMatrix:
286 for (int i = 0; i < rightNode->getMatrixCols(); i++) {
288 for (int j = 0; j < getVectorSize(); j++)
289 sum += leftUnionArray[j].getDConst() * rightUnionArray[i*rightNode->getMatrixRows() + j].getDConst();
290 newConstArray[i].setDConst(sum);
293 returnType.shallowCopy(TType(getBasicType(), EvqConst, rightNode->getMatrixCols()));
297 for (int i = 0; i < newComps; i++) {
298 if (rightUnionArray[i] == 0)
299 newConstArray[i] = leftUnionArray[i];
301 switch (getType().getBasicType()) {
303 if (rightUnionArray[i].getIConst() == -1 && leftUnionArray[i].getIConst() == INT_MIN) {
304 newConstArray[i].setIConst(0);
306 } else goto modulo_default;
309 if (rightUnionArray[i].getI64Const() == -1 && leftUnionArray[i].getI64Const() == LLONG_MIN) {
310 newConstArray[i].setI64Const(0);
312 } else goto modulo_default;
313 #ifdef AMD_EXTENSIONS
315 if (rightUnionArray[i].getIConst() == -1 && leftUnionArray[i].getIConst() == SHRT_MIN) {
316 newConstArray[i].setIConst(0);
318 } else goto modulo_default;
322 newConstArray[i] = leftUnionArray[i] % rightUnionArray[i];
329 for (int i = 0; i < newComps; i++)
330 newConstArray[i] = leftUnionArray[i] >> rightUnionArray[i];
334 for (int i = 0; i < newComps; i++)
335 newConstArray[i] = leftUnionArray[i] << rightUnionArray[i];
339 for (int i = 0; i < newComps; i++)
340 newConstArray[i] = leftUnionArray[i] & rightUnionArray[i];
343 for (int i = 0; i < newComps; i++)
344 newConstArray[i] = leftUnionArray[i] | rightUnionArray[i];
347 for (int i = 0; i < newComps; i++)
348 newConstArray[i] = leftUnionArray[i] ^ rightUnionArray[i];
351 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
352 for (int i = 0; i < newComps; i++)
353 newConstArray[i] = leftUnionArray[i] && rightUnionArray[i];
356 case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
357 for (int i = 0; i < newComps; i++)
358 newConstArray[i] = leftUnionArray[i] || rightUnionArray[i];
362 for (int i = 0; i < newComps; i++) {
363 switch (getType().getBasicType()) {
364 case EbtBool: newConstArray[i].setBConst((leftUnionArray[i] == rightUnionArray[i]) ? false : true); break;
365 default: assert(false && "Default missing");
371 newConstArray[0].setBConst(leftUnionArray[0] < rightUnionArray[0]);
372 returnType.shallowCopy(constBool);
375 newConstArray[0].setBConst(leftUnionArray[0] > rightUnionArray[0]);
376 returnType.shallowCopy(constBool);
378 case EOpLessThanEqual:
379 newConstArray[0].setBConst(! (leftUnionArray[0] > rightUnionArray[0]));
380 returnType.shallowCopy(constBool);
382 case EOpGreaterThanEqual:
383 newConstArray[0].setBConst(! (leftUnionArray[0] < rightUnionArray[0]));
384 returnType.shallowCopy(constBool);
387 newConstArray[0].setBConst(rightNode->getConstArray() == leftUnionArray);
388 returnType.shallowCopy(constBool);
391 newConstArray[0].setBConst(rightNode->getConstArray() != leftUnionArray);
392 returnType.shallowCopy(constBool);
399 TIntermConstantUnion *newNode = new TIntermConstantUnion(newConstArray, returnType);
400 newNode->setLoc(getLoc());
406 // Do single unary node folding
408 // Returns a new node representing the result.
410 TIntermTyped* TIntermConstantUnion::fold(TOperator op, const TType& returnType) const
412 // First, size the result, which is mostly the same as the argument's size,
413 // but not always, and classify what is componentwise.
414 // Also, eliminate cases that can't be compile-time constant.
416 bool componentWise = true;
418 int objectSize = getType().computeNumComponents();
424 componentWise = false;
428 case EOpEmitStreamVertex:
429 case EOpEndStreamPrimitive:
430 // These don't actually fold
433 case EOpPackSnorm2x16:
434 case EOpPackUnorm2x16:
435 case EOpPackHalf2x16:
436 componentWise = false;
440 case EOpUnpackSnorm2x16:
441 case EOpUnpackUnorm2x16:
442 case EOpUnpackHalf2x16:
443 componentWise = false;
454 componentWise = false;
455 resultSize = objectSize;
459 resultSize = objectSize;
463 // Set up for processing
464 TConstUnionArray newConstArray(resultSize);
465 const TConstUnionArray& unionArray = getConstArray();
467 // Process non-component-wise operations
473 for (int i = 0; i < objectSize; i++)
474 sum += unionArray[i].getDConst() * unionArray[i].getDConst();
475 double length = sqrt(sum);
477 newConstArray[0].setDConst(length);
479 for (int i = 0; i < objectSize; i++)
480 newConstArray[i].setDConst(unionArray[i].getDConst() / length);
488 for (int i = 0; i < objectSize; i++) {
489 if (unionArray[i].getBConst())
492 newConstArray[0].setBConst(result);
498 for (int i = 0; i < objectSize; i++) {
499 if (! unionArray[i].getBConst())
502 newConstArray[0].setBConst(result);
506 // TODO: 3.0 Functionality: unary constant folding: the rest of the ops have to be fleshed out
508 case EOpPackSnorm2x16:
509 case EOpPackUnorm2x16:
510 case EOpPackHalf2x16:
518 case EOpUnpackSnorm2x16:
519 case EOpUnpackUnorm2x16:
520 case EOpUnpackHalf2x16:
523 case EOpMatrixInverse:
528 assert(componentWise);
532 // Turn off the componentwise loop
536 // Process component-wise operations
537 for (int i = 0; i < objectSize; i++) {
540 switch (getType().getBasicType()) {
543 case EbtFloat: newConstArray[i].setDConst(-unionArray[i].getDConst()); break;
544 case EbtInt8: newConstArray[i].setI8Const(-unionArray[i].getI8Const()); break;
545 case EbtUint8: newConstArray[i].setU8Const(static_cast<unsigned int>(-static_cast<signed int>(unionArray[i].getU8Const()))); break;
546 case EbtInt16: newConstArray[i].setI16Const(-unionArray[i].getI16Const()); break;
547 case EbtUint16:newConstArray[i].setU16Const(static_cast<unsigned int>(-static_cast<signed int>(unionArray[i].getU16Const()))); break;
548 case EbtInt: newConstArray[i].setIConst(-unionArray[i].getIConst()); break;
549 case EbtUint: newConstArray[i].setUConst(static_cast<unsigned int>(-static_cast<int>(unionArray[i].getUConst()))); break;
550 case EbtInt64: newConstArray[i].setI64Const(-unionArray[i].getI64Const()); break;
551 case EbtUint64: newConstArray[i].setU64Const(static_cast<unsigned long long>(-static_cast<long long>(unionArray[i].getU64Const()))); break;
557 case EOpVectorLogicalNot:
558 switch (getType().getBasicType()) {
559 case EbtBool: newConstArray[i].setBConst(!unionArray[i].getBConst()); break;
565 newConstArray[i] = ~unionArray[i];
568 newConstArray[i].setDConst(unionArray[i].getDConst() * pi / 180.0);
571 newConstArray[i].setDConst(unionArray[i].getDConst() * 180.0 / pi);
574 newConstArray[i].setDConst(sin(unionArray[i].getDConst()));
577 newConstArray[i].setDConst(cos(unionArray[i].getDConst()));
580 newConstArray[i].setDConst(tan(unionArray[i].getDConst()));
583 newConstArray[i].setDConst(asin(unionArray[i].getDConst()));
586 newConstArray[i].setDConst(acos(unionArray[i].getDConst()));
589 newConstArray[i].setDConst(atan(unionArray[i].getDConst()));
600 case EOpFwidthCoarse:
601 // The derivatives are all mandated to create a constant 0.
602 newConstArray[i].setDConst(0.0);
606 newConstArray[i].setDConst(exp(unionArray[i].getDConst()));
609 newConstArray[i].setDConst(log(unionArray[i].getDConst()));
613 const double inv_log2_e = 0.69314718055994530941723212145818;
614 newConstArray[i].setDConst(exp(unionArray[i].getDConst() * inv_log2_e));
619 const double log2_e = 1.4426950408889634073599246810019;
620 newConstArray[i].setDConst(log2_e * log(unionArray[i].getDConst()));
624 newConstArray[i].setDConst(sqrt(unionArray[i].getDConst()));
627 newConstArray[i].setDConst(1.0 / sqrt(unionArray[i].getDConst()));
631 if (unionArray[i].getType() == EbtDouble)
632 newConstArray[i].setDConst(fabs(unionArray[i].getDConst()));
633 else if (unionArray[i].getType() == EbtInt)
634 newConstArray[i].setIConst(abs(unionArray[i].getIConst()));
636 newConstArray[i] = unionArray[i];
639 #define SIGN(X) (X == 0 ? 0 : (X < 0 ? -1 : 1))
640 if (unionArray[i].getType() == EbtDouble)
641 newConstArray[i].setDConst(SIGN(unionArray[i].getDConst()));
643 newConstArray[i].setIConst(SIGN(unionArray[i].getIConst()));
646 newConstArray[i].setDConst(floor(unionArray[i].getDConst()));
649 if (unionArray[i].getDConst() > 0)
650 newConstArray[i].setDConst(floor(unionArray[i].getDConst()));
652 newConstArray[i].setDConst(ceil(unionArray[i].getDConst()));
655 newConstArray[i].setDConst(floor(0.5 + unionArray[i].getDConst()));
659 double flr = floor(unionArray[i].getDConst());
660 bool even = flr / 2.0 == floor(flr / 2.0);
661 double rounded = even ? ceil(unionArray[i].getDConst() - 0.5) : floor(unionArray[i].getDConst() + 0.5);
662 newConstArray[i].setDConst(rounded);
666 newConstArray[i].setDConst(ceil(unionArray[i].getDConst()));
670 double x = unionArray[i].getDConst();
671 newConstArray[i].setDConst(x - floor(x));
677 newConstArray[i].setBConst(isNan(unionArray[i].getDConst()));
682 newConstArray[i].setBConst(isInf(unionArray[i].getDConst()));
686 // TODO: 3.0 Functionality: unary constant folding: the rest of the ops have to be fleshed out
695 case EOpFloatBitsToInt:
696 case EOpFloatBitsToUint:
697 case EOpIntBitsToFloat:
698 case EOpUintBitsToFloat:
699 case EOpDoubleBitsToInt64:
700 case EOpDoubleBitsToUint64:
701 case EOpInt64BitsToDouble:
702 case EOpUint64BitsToDouble:
703 case EOpFloat16BitsToInt16:
704 case EOpFloat16BitsToUint16:
705 case EOpInt16BitsToFloat16:
706 case EOpUint16BitsToFloat16:
712 TIntermConstantUnion *newNode = new TIntermConstantUnion(newConstArray, returnType);
713 newNode->getWritableType().getQualifier().storage = EvqConst;
714 newNode->setLoc(getLoc());
720 // Do constant folding for an aggregate node that has all its children
721 // as constants and an operator that requires constant folding.
723 TIntermTyped* TIntermediate::fold(TIntermAggregate* aggrNode)
725 if (aggrNode == nullptr)
728 if (! areAllChildConst(aggrNode))
731 if (aggrNode->isConstructor())
732 return foldConstructor(aggrNode);
734 TIntermSequence& children = aggrNode->getSequence();
736 // First, see if this is an operation to constant fold, kick out if not,
737 // see what size the result is if so.
739 bool componentwise = false; // will also say componentwise if a scalar argument gets repeated to make per-component results
741 switch (aggrNode->getOp()) {
750 case EOpLessThanEqual:
751 case EOpGreaterThanEqual:
753 case EOpVectorNotEqual:
754 componentwise = true;
755 objectSize = children[0]->getAsConstantUnion()->getType().computeNumComponents();
761 objectSize = children[0]->getAsConstantUnion()->getType().computeNumComponents();
767 case EOpOuterProduct:
768 objectSize = children[0]->getAsTyped()->getType().getVectorSize() *
769 children[1]->getAsTyped()->getType().getVectorSize();
772 componentwise = true;
773 objectSize = std::max(children[0]->getAsTyped()->getType().getVectorSize(),
774 children[1]->getAsTyped()->getType().getVectorSize());
777 componentwise = true;
778 objectSize = std::max(children[0]->getAsTyped()->getType().getVectorSize(),
779 children[2]->getAsTyped()->getType().getVectorSize());
784 TConstUnionArray newConstArray(objectSize);
786 TVector<TConstUnionArray> childConstUnions;
787 for (unsigned int arg = 0; arg < children.size(); ++arg)
788 childConstUnions.push_back(children[arg]->getAsConstantUnion()->getConstArray());
791 for (int comp = 0; comp < objectSize; comp++) {
793 // some arguments are scalars instead of matching vectors; simulate a smear
794 int arg0comp = std::min(comp, children[0]->getAsTyped()->getType().getVectorSize() - 1);
796 if (children.size() > 1)
797 arg1comp = std::min(comp, children[1]->getAsTyped()->getType().getVectorSize() - 1);
799 if (children.size() > 2)
800 arg2comp = std::min(comp, children[2]->getAsTyped()->getType().getVectorSize() - 1);
802 switch (aggrNode->getOp()) {
804 newConstArray[comp].setDConst(atan2(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst()));
807 newConstArray[comp].setDConst(pow(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst()));
810 switch(children[0]->getAsTyped()->getBasicType()) {
814 newConstArray[comp].setDConst(std::min(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst()));
817 newConstArray[comp].setI8Const(std::min(childConstUnions[0][arg0comp].getI8Const(), childConstUnions[1][arg1comp].getI8Const()));
820 newConstArray[comp].setU8Const(std::min(childConstUnions[0][arg0comp].getU8Const(), childConstUnions[1][arg1comp].getU8Const()));
823 newConstArray[comp].setI16Const(std::min(childConstUnions[0][arg0comp].getI16Const(), childConstUnions[1][arg1comp].getI16Const()));
826 newConstArray[comp].setU16Const(std::min(childConstUnions[0][arg0comp].getU16Const(), childConstUnions[1][arg1comp].getU16Const()));
829 newConstArray[comp].setIConst(std::min(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst()));
832 newConstArray[comp].setUConst(std::min(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst()));
835 newConstArray[comp].setI64Const(std::min(childConstUnions[0][arg0comp].getI64Const(), childConstUnions[1][arg1comp].getI64Const()));
838 newConstArray[comp].setU64Const(std::min(childConstUnions[0][arg0comp].getU64Const(), childConstUnions[1][arg1comp].getU64Const()));
840 default: assert(false && "Default missing");
844 switch(children[0]->getAsTyped()->getBasicType()) {
848 newConstArray[comp].setDConst(std::max(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst()));
851 newConstArray[comp].setI8Const(std::max(childConstUnions[0][arg0comp].getI8Const(), childConstUnions[1][arg1comp].getI8Const()));
854 newConstArray[comp].setU8Const(std::max(childConstUnions[0][arg0comp].getU8Const(), childConstUnions[1][arg1comp].getU8Const()));
857 newConstArray[comp].setI16Const(std::max(childConstUnions[0][arg0comp].getI16Const(), childConstUnions[1][arg1comp].getI16Const()));
860 newConstArray[comp].setU16Const(std::max(childConstUnions[0][arg0comp].getU16Const(), childConstUnions[1][arg1comp].getU16Const()));
863 newConstArray[comp].setIConst(std::max(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst()));
866 newConstArray[comp].setUConst(std::max(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst()));
869 newConstArray[comp].setI64Const(std::max(childConstUnions[0][arg0comp].getI64Const(), childConstUnions[1][arg1comp].getI64Const()));
872 newConstArray[comp].setU64Const(std::max(childConstUnions[0][arg0comp].getU64Const(), childConstUnions[1][arg1comp].getU64Const()));
874 default: assert(false && "Default missing");
878 switch(children[0]->getAsTyped()->getBasicType()) {
882 newConstArray[comp].setDConst(std::min(std::max(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst()),
883 childConstUnions[2][arg2comp].getDConst()));
886 newConstArray[comp].setI8Const(std::min(std::max(childConstUnions[0][arg0comp].getI8Const(), childConstUnions[1][arg1comp].getI8Const()),
887 childConstUnions[2][arg2comp].getI8Const()));
890 newConstArray[comp].setU8Const(std::min(std::max(childConstUnions[0][arg0comp].getU8Const(), childConstUnions[1][arg1comp].getU8Const()),
891 childConstUnions[2][arg2comp].getU8Const()));
894 newConstArray[comp].setI16Const(std::min(std::max(childConstUnions[0][arg0comp].getI16Const(), childConstUnions[1][arg1comp].getI16Const()),
895 childConstUnions[2][arg2comp].getI16Const()));
898 newConstArray[comp].setU16Const(std::min(std::max(childConstUnions[0][arg0comp].getU16Const(), childConstUnions[1][arg1comp].getU16Const()),
899 childConstUnions[2][arg2comp].getU16Const()));
902 newConstArray[comp].setIConst(std::min(std::max(childConstUnions[0][arg0comp].getIConst(), childConstUnions[1][arg1comp].getIConst()),
903 childConstUnions[2][arg2comp].getIConst()));
906 newConstArray[comp].setUConst(std::min(std::max(childConstUnions[0][arg0comp].getUConst(), childConstUnions[1][arg1comp].getUConst()),
907 childConstUnions[2][arg2comp].getUConst()));
910 newConstArray[comp].setI64Const(std::min(std::max(childConstUnions[0][arg0comp].getI64Const(), childConstUnions[1][arg1comp].getI64Const()),
911 childConstUnions[2][arg2comp].getI64Const()));
914 newConstArray[comp].setU64Const(std::min(std::max(childConstUnions[0][arg0comp].getU64Const(), childConstUnions[1][arg1comp].getU64Const()),
915 childConstUnions[2][arg2comp].getU64Const()));
917 default: assert(false && "Default missing");
921 newConstArray[comp].setBConst(childConstUnions[0][arg0comp] < childConstUnions[1][arg1comp]);
924 newConstArray[comp].setBConst(childConstUnions[0][arg0comp] > childConstUnions[1][arg1comp]);
926 case EOpLessThanEqual:
927 newConstArray[comp].setBConst(! (childConstUnions[0][arg0comp] > childConstUnions[1][arg1comp]));
929 case EOpGreaterThanEqual:
930 newConstArray[comp].setBConst(! (childConstUnions[0][arg0comp] < childConstUnions[1][arg1comp]));
933 newConstArray[comp].setBConst(childConstUnions[0][arg0comp] == childConstUnions[1][arg1comp]);
935 case EOpVectorNotEqual:
936 newConstArray[comp].setBConst(childConstUnions[0][arg0comp] != childConstUnions[1][arg1comp]);
939 if (children[2]->getAsTyped()->getBasicType() == EbtBool)
940 newConstArray[comp].setDConst(childConstUnions[2][arg2comp].getBConst() ? childConstUnions[1][arg1comp].getDConst() :
941 childConstUnions[0][arg0comp].getDConst());
943 newConstArray[comp].setDConst(childConstUnions[0][arg0comp].getDConst() * (1.0 - childConstUnions[2][arg2comp].getDConst()) +
944 childConstUnions[1][arg1comp].getDConst() * childConstUnions[2][arg2comp].getDConst());
947 newConstArray[comp].setDConst(childConstUnions[1][arg1comp].getDConst() < childConstUnions[0][arg0comp].getDConst() ? 0.0 : 1.0);
951 double t = (childConstUnions[2][arg2comp].getDConst() - childConstUnions[0][arg0comp].getDConst()) /
952 (childConstUnions[1][arg1comp].getDConst() - childConstUnions[0][arg0comp].getDConst());
957 newConstArray[comp].setDConst(t * t * (3.0 - 2.0 * t));
965 // Non-componentwise...
967 int numComps = children[0]->getAsConstantUnion()->getType().computeNumComponents();
970 switch (aggrNode->getOp()) {
974 for (int comp = 0; comp < numComps; ++comp) {
975 double diff = childConstUnions[1][comp].getDConst() - childConstUnions[0][comp].getDConst();
978 newConstArray[0].setDConst(sqrt(sum));
982 newConstArray[0].setDConst(childConstUnions[0].dot(childConstUnions[1]));
985 newConstArray[0] = childConstUnions[0][1] * childConstUnions[1][2] - childConstUnions[0][2] * childConstUnions[1][1];
986 newConstArray[1] = childConstUnions[0][2] * childConstUnions[1][0] - childConstUnions[0][0] * childConstUnions[1][2];
987 newConstArray[2] = childConstUnions[0][0] * childConstUnions[1][1] - childConstUnions[0][1] * childConstUnions[1][0];
990 // If dot(Nref, I) < 0 return N, otherwise return -N: Arguments are (N, I, Nref).
991 dot = childConstUnions[1].dot(childConstUnions[2]);
992 for (int comp = 0; comp < numComps; ++comp) {
994 newConstArray[comp] = childConstUnions[0][comp];
996 newConstArray[comp].setDConst(-childConstUnions[0][comp].getDConst());
1000 // I - 2 * dot(N, I) * N: Arguments are (I, N).
1001 dot = childConstUnions[0].dot(childConstUnions[1]);
1003 for (int comp = 0; comp < numComps; ++comp)
1004 newConstArray[comp].setDConst(childConstUnions[0][comp].getDConst() - dot * childConstUnions[1][comp].getDConst());
1008 // Arguments are (I, N, eta).
1009 // k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
1013 // return eta * I - (eta * dot(N, I) + sqrt(k)) * N
1014 dot = childConstUnions[0].dot(childConstUnions[1]);
1015 double eta = childConstUnions[2][0].getDConst();
1016 double k = 1.0 - eta * eta * (1.0 - dot * dot);
1018 for (int comp = 0; comp < numComps; ++comp)
1019 newConstArray[comp].setDConst(0.0);
1021 for (int comp = 0; comp < numComps; ++comp)
1022 newConstArray[comp].setDConst(eta * childConstUnions[0][comp].getDConst() - (eta * dot + sqrt(k)) * childConstUnions[1][comp].getDConst());
1026 case EOpOuterProduct:
1028 int numRows = numComps;
1029 int numCols = children[1]->getAsConstantUnion()->getType().computeNumComponents();
1030 for (int row = 0; row < numRows; ++row)
1031 for (int col = 0; col < numCols; ++col)
1032 newConstArray[col * numRows + row] = childConstUnions[0][row] * childConstUnions[1][col];
1040 TIntermConstantUnion *newNode = new TIntermConstantUnion(newConstArray, aggrNode->getType());
1041 newNode->getWritableType().getQualifier().storage = EvqConst;
1042 newNode->setLoc(aggrNode->getLoc());
1047 bool TIntermediate::areAllChildConst(TIntermAggregate* aggrNode)
1049 bool allConstant = true;
1051 // check if all the child nodes are constants so that they can be inserted into
1054 TIntermSequence& childSequenceVector = aggrNode->getSequence();
1055 for (TIntermSequence::iterator p = childSequenceVector.begin();
1056 p != childSequenceVector.end(); p++) {
1057 if (!(*p)->getAsTyped()->getAsConstantUnion())
1065 TIntermTyped* TIntermediate::foldConstructor(TIntermAggregate* aggrNode)
1069 TConstUnionArray unionArray(aggrNode->getType().computeNumComponents());
1070 if (aggrNode->getSequence().size() == 1)
1071 error = parseConstTree(aggrNode, unionArray, aggrNode->getOp(), aggrNode->getType(), true);
1073 error = parseConstTree(aggrNode, unionArray, aggrNode->getOp(), aggrNode->getType());
1078 return addConstantUnion(unionArray, aggrNode->getType(), aggrNode->getLoc());
1082 // Constant folding of a bracket (array-style) dereference or struct-like dot
1083 // dereference. Can handle anything except a multi-character swizzle, though
1084 // all swizzles may go to foldSwizzle().
1086 TIntermTyped* TIntermediate::foldDereference(TIntermTyped* node, int index, const TSourceLoc& loc)
1088 TType dereferencedType(node->getType(), index);
1089 dereferencedType.getQualifier().storage = EvqConst;
1090 TIntermTyped* result = 0;
1091 int size = dereferencedType.computeNumComponents();
1093 // arrays, vectors, matrices, all use simple multiplicative math
1094 // while structures need to add up heterogeneous members
1096 if (node->isArray() || ! node->isStruct())
1097 start = size * index;
1099 // it is a structure
1100 assert(node->isStruct());
1102 for (int i = 0; i < index; ++i)
1103 start += (*node->getType().getStruct())[i].type->computeNumComponents();
1106 result = addConstantUnion(TConstUnionArray(node->getAsConstantUnion()->getConstArray(), start, size), node->getType(), loc);
1111 result->setType(dereferencedType);
1117 // Make a constant vector node or constant scalar node, representing a given
1118 // constant vector and constant swizzle into it.
1120 TIntermTyped* TIntermediate::foldSwizzle(TIntermTyped* node, TSwizzleSelectors<TVectorSelector>& selectors, const TSourceLoc& loc)
1122 const TConstUnionArray& unionArray = node->getAsConstantUnion()->getConstArray();
1123 TConstUnionArray constArray(selectors.size());
1125 for (int i = 0; i < selectors.size(); i++)
1126 constArray[i] = unionArray[selectors[i]];
1128 TIntermTyped* result = addConstantUnion(constArray, node->getType(), loc);
1133 result->setType(TType(node->getBasicType(), EvqConst, selectors.size()));
1138 } // end namespace glslang