Hash :
2d73665d
Author :
Date :
2016-11-30T10:37:49
Handle constant folding arithmetic involving infinity Constant folding arithmetic operations that involve infinity are now handled correctly in the cases where the result is infinity or zero. The implementation mostly relies on C++ to implement IEEE float arithmetic correctly so that unnecessary overhead is avoided. Constant folding arithmetic operations that result in overflow now issue a warning but result in infinity. This is not mandated by the spec but is a reasonable choice since it is the behavior of the default IEEE rounding mode. Constant folding arithmetic operations that result in NaN in IEEE will generate a warning but the NaN is kept. This is also not mandated by the spec, but is among the allowed behaviors. There's no special handling for ESSL 1.00. ESSL 1.00 doesn't really have the concept of NaN, but since it is not feasible to control generating NaNs at shader run time either way, it should not be a big issue if constant folding may generate them as well. TEST=angle_unittests BUG=chromium:661857 Change-Id: I06116c6fdd02f224939d4a651e4e62f2fd4c98a8 Reviewed-on: https://chromium-review.googlesource.com/414911 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
//
// Copyright (c) 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ConstantFoldingTest.h:
// Utilities for constant folding tests.
//
#ifndef TESTS_TEST_UTILS_CONSTANTFOLDINGTEST_H_
#define TESTS_TEST_UTILS_CONSTANTFOLDINGTEST_H_
#include <vector>
#include "common/mathutil.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/PoolAlloc.h"
#include "gtest/gtest.h"
namespace sh
{
class TranslatorESSL;
template <typename T>
class ConstantFinder : public TIntermTraverser
{
public:
ConstantFinder(const std::vector<T> &constantVector)
: TIntermTraverser(true, false, false),
mConstantVector(constantVector),
mFaultTolerance(T()),
mFound(false)
{
}
ConstantFinder(const std::vector<T> &constantVector, const T &faultTolerance)
: TIntermTraverser(true, false, false),
mConstantVector(constantVector),
mFaultTolerance(faultTolerance),
mFound(false)
{
}
ConstantFinder(const T &value)
: TIntermTraverser(true, false, false), mFaultTolerance(T()), mFound(false)
{
mConstantVector.push_back(value);
}
void visitConstantUnion(TIntermConstantUnion *node)
{
if (node->getType().getObjectSize() == mConstantVector.size())
{
bool found = true;
for (size_t i = 0; i < mConstantVector.size(); i++)
{
if (!isEqual(node->getUnionArrayPointer()[i], mConstantVector[i]))
{
found = false;
break;
}
}
if (found)
{
mFound = found;
}
}
}
bool found() const { return mFound; }
private:
bool isEqual(const TConstantUnion &node, const float &value) const
{
if (node.getType() != EbtFloat)
{
return false;
}
if (value == std::numeric_limits<float>::infinity())
{
return gl::isInf(node.getFConst()) && node.getFConst() > 0;
}
else if (value == -std::numeric_limits<float>::infinity())
{
return gl::isInf(node.getFConst()) && node.getFConst() < 0;
}
else if (gl::isNaN(value))
{
// All NaNs are treated as equal.
return gl::isNaN(node.getFConst());
}
return mFaultTolerance >= fabsf(node.getFConst() - value);
}
bool isEqual(const TConstantUnion &node, const int &value) const
{
if (node.getType() != EbtInt)
{
return false;
}
ASSERT(mFaultTolerance < std::numeric_limits<int>::max());
// abs() returns 0 at least on some platforms when the minimum int value is passed in (it
// doesn't have a positive counterpart).
return mFaultTolerance >= abs(node.getIConst() - value) &&
(node.getIConst() - value) != std::numeric_limits<int>::min();
}
bool isEqual(const TConstantUnion &node, const unsigned int &value) const
{
if (node.getType() != EbtUInt)
{
return false;
}
ASSERT(mFaultTolerance < static_cast<unsigned int>(std::numeric_limits<int>::max()));
return static_cast<int>(mFaultTolerance) >=
abs(static_cast<int>(node.getUConst() - value)) &&
static_cast<int>(node.getUConst() - value) != std::numeric_limits<int>::min();
}
bool isEqual(const TConstantUnion &node, const bool &value) const
{
if (node.getType() != EbtBool)
{
return false;
}
return node.getBConst() == value;
}
std::vector<T> mConstantVector;
T mFaultTolerance;
bool mFound;
};
class ConstantFoldingTest : public testing::Test
{
public:
ConstantFoldingTest() {}
protected:
void SetUp() override;
void TearDown() override;
void compile(const std::string &shaderString);
// Must be called after compile()
bool hasWarning();
template <typename T>
bool constantFoundInAST(T constant)
{
ConstantFinder<T> finder(constant);
mASTRoot->traverse(&finder);
return finder.found();
}
template <typename T>
bool constantVectorFoundInAST(const std::vector<T> &constantVector)
{
ConstantFinder<T> finder(constantVector);
mASTRoot->traverse(&finder);
return finder.found();
}
template <typename T>
bool constantColumnMajorMatrixFoundInAST(const std::vector<T> &constantMatrix)
{
return constantVectorFoundInAST(constantMatrix);
}
template <typename T>
bool constantVectorNearFoundInAST(const std::vector<T> &constantVector, const T &faultTolerance)
{
ConstantFinder<T> finder(constantVector, faultTolerance);
mASTRoot->traverse(&finder);
return finder.found();
}
private:
TranslatorESSL *mTranslatorESSL;
TIntermNode *mASTRoot;
TPoolAllocator allocator;
};
class ConstantFoldingExpressionTest : public ConstantFoldingTest
{
public:
ConstantFoldingExpressionTest() {}
void evaluateFloat(const std::string &floatExpression);
};
} // namespace sh
#endif // TESTS_TEST_UTILS_CONSTANTFOLDINGTEST_H_