blob: f17dd73657bfa26751dc25834e5d1d77fdfeb73a [file] [log] [blame]
Jamie Madill1048e432016-07-23 18:51:28 -04001//
2// Copyright (c) 2016 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// Implementation of the integer pow expressions HLSL bug workaround.
7// See header for more info.
8
9#include "compiler/translator/ExpandIntegerPowExpressions.h"
10
11#include <cmath>
12#include <cstdlib>
13
Olli Etuahocccf2b02017-07-05 14:50:54 +030014#include "compiler/translator/IntermTraverse.h"
Jamie Madill1048e432016-07-23 18:51:28 -040015
16namespace sh
17{
18
19namespace
20{
21
22class Traverser : public TIntermTraverser
23{
24 public:
Olli Etuahoa5e693a2017-07-13 16:07:26 +030025 static void Apply(TIntermNode *root, TSymbolTable *symbolTable);
Jamie Madill1048e432016-07-23 18:51:28 -040026
27 private:
Olli Etuahoa5e693a2017-07-13 16:07:26 +030028 Traverser(TSymbolTable *symbolTable);
Jamie Madill1048e432016-07-23 18:51:28 -040029 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
Jamie Madill5655b842016-08-02 11:00:07 -040030 void nextIteration();
31
32 bool mFound = false;
Jamie Madill1048e432016-07-23 18:51:28 -040033};
34
35// static
Olli Etuahoa5e693a2017-07-13 16:07:26 +030036void Traverser::Apply(TIntermNode *root, TSymbolTable *symbolTable)
Jamie Madill1048e432016-07-23 18:51:28 -040037{
Olli Etuahoa5e693a2017-07-13 16:07:26 +030038 Traverser traverser(symbolTable);
Jamie Madill5655b842016-08-02 11:00:07 -040039 do
40 {
41 traverser.nextIteration();
42 root->traverse(&traverser);
43 if (traverser.mFound)
44 {
45 traverser.updateTree();
46 }
47 } while (traverser.mFound);
Jamie Madill1048e432016-07-23 18:51:28 -040048}
49
Olli Etuahoa5e693a2017-07-13 16:07:26 +030050Traverser::Traverser(TSymbolTable *symbolTable) : TIntermTraverser(true, false, false, symbolTable)
Jamie Madill1048e432016-07-23 18:51:28 -040051{
52}
53
Jamie Madill5655b842016-08-02 11:00:07 -040054void Traverser::nextIteration()
55{
56 mFound = false;
Olli Etuaho4dd06d52017-07-05 12:41:06 +030057 nextTemporaryId();
Jamie Madill5655b842016-08-02 11:00:07 -040058}
59
Jamie Madill1048e432016-07-23 18:51:28 -040060bool Traverser::visitAggregate(Visit visit, TIntermAggregate *node)
61{
Jamie Madill5655b842016-08-02 11:00:07 -040062 if (mFound)
63 {
64 return false;
65 }
66
Jamie Madill1048e432016-07-23 18:51:28 -040067 // Test 0: skip non-pow operators.
68 if (node->getOp() != EOpPow)
69 {
70 return true;
71 }
72
73 const TIntermSequence *sequence = node->getSequence();
74 ASSERT(sequence->size() == 2u);
75 const TIntermConstantUnion *constantNode = sequence->at(1)->getAsConstantUnion();
76
77 // Test 1: check for a single constant.
78 if (!constantNode || constantNode->getNominalSize() != 1)
79 {
80 return true;
81 }
82
83 const TConstantUnion *constant = constantNode->getUnionArrayPointer();
84
85 TConstantUnion asFloat;
86 asFloat.cast(EbtFloat, *constant);
87
88 float value = asFloat.getFConst();
89
90 // Test 2: value is in the problematic range.
91 if (value < -5.0f || value > 9.0f)
92 {
93 return true;
94 }
95
96 // Test 3: value is integer or pretty close to an integer.
Jamie Madill6c9503e2016-08-16 14:06:32 -040097 float absval = std::abs(value);
98 float frac = absval - std::round(absval);
Jamie Madill1048e432016-07-23 18:51:28 -040099 if (frac > 0.0001f)
100 {
101 return true;
102 }
103
104 // Test 4: skip -1, 0, and 1
105 int exponent = static_cast<int>(value);
106 int n = std::abs(exponent);
107 if (n < 2)
108 {
109 return true;
110 }
111
112 // Potential problem case detected, apply workaround.
Olli Etuaho4dd06d52017-07-05 12:41:06 +0300113 nextTemporaryId();
Jamie Madill1048e432016-07-23 18:51:28 -0400114
115 TIntermTyped *lhs = sequence->at(0)->getAsTyped();
116 ASSERT(lhs);
117
Olli Etuaho13389b62016-10-16 11:48:18 +0100118 TIntermDeclaration *init = createTempInitDeclaration(lhs);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500119 TIntermTyped *current = createTempSymbol(lhs->getType());
Jamie Madill1048e432016-07-23 18:51:28 -0400120
121 insertStatementInParentBlock(init);
122
123 // Create a chain of n-1 multiples.
124 for (int i = 1; i < n; ++i)
125 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300126 TIntermBinary *mul = new TIntermBinary(EOpMul, current, createTempSymbol(lhs->getType()));
Jamie Madill1048e432016-07-23 18:51:28 -0400127 mul->setLine(node->getLine());
128 current = mul;
129 }
130
131 // For negative pow, compute the reciprocal of the positive pow.
132 if (exponent < 0)
133 {
134 TConstantUnion *oneVal = new TConstantUnion();
135 oneVal->setFConst(1.0f);
136 TIntermConstantUnion *oneNode = new TIntermConstantUnion(oneVal, node->getType());
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300137 TIntermBinary *div = new TIntermBinary(EOpDiv, oneNode, current);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500138 current = div;
Jamie Madill1048e432016-07-23 18:51:28 -0400139 }
140
Olli Etuahoea39a222017-07-06 12:47:59 +0300141 queueReplacement(current, OriginalNode::IS_DROPPED);
Jamie Madill5655b842016-08-02 11:00:07 -0400142 mFound = true;
143 return false;
Jamie Madill1048e432016-07-23 18:51:28 -0400144}
145
146} // anonymous namespace
147
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300148void ExpandIntegerPowExpressions(TIntermNode *root, TSymbolTable *symbolTable)
Jamie Madill1048e432016-07-23 18:51:28 -0400149{
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300150 Traverser::Apply(root, symbolTable);
Jamie Madill1048e432016-07-23 18:51:28 -0400151}
152
153} // namespace sh