1 #ifndef _RSGBINARYOPS_HPP
2 #define _RSGBINARYOPS_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Random Shader Generator
5 * ----------------------------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Binary operators.
24 *//*--------------------------------------------------------------------*/
26 #include "rsgDefs.hpp"
27 #include "rsgExpression.hpp"
34 ASSOCIATIVITY_LEFT = 0,
40 template <int Precedence, Associativity Assoc>
41 class BinaryOp : public Expression
44 BinaryOp (Token::Type operatorToken);
45 virtual ~BinaryOp (void);
47 Expression* createNextChild (GeneratorState& state);
48 void tokenize (GeneratorState& state, TokenStream& str) const;
49 void evaluate (ExecutionContext& execCtx);
50 ExecConstValueAccess getValue (void) const { return m_value.getValue(m_type); }
52 virtual void evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b) = DE_NULL;
55 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
57 Token::Type m_operator;
59 ExecValueStorage m_value;
61 ValueRange m_leftValueRange;
62 ValueRange m_rightValueRange;
64 Expression* m_leftValueExpr;
65 Expression* m_rightValueExpr;
68 template <int Precedence, bool Float, bool Int, bool Bool, class ComputeValueRange, class EvaluateComp>
69 class BinaryVecOp : public BinaryOp<Precedence, ASSOCIATIVITY_LEFT>
72 BinaryVecOp (GeneratorState& state, Token::Type operatorToken, ConstValueRangeAccess valueRange);
73 virtual ~BinaryVecOp (void);
75 void evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b);
78 struct ComputeMulRange
80 void operator() (de::Random& rnd, float dstMin, float dstMax, float& aMin, float& aMax, float& bMin, float& bMax) const;
81 void operator() (de::Random& rnd, int dstMin, int dstMax, int& aMin, int& aMax, int& bMin, int& bMax) const;
82 void operator() (de::Random&, bool, bool, bool&, bool&, bool&, bool&) const { DE_ASSERT(DE_FALSE); }
87 template <typename T> inline T operator() (T a, T b) const { return a*b; }
90 typedef BinaryVecOp<4, true, true, false, ComputeMulRange, EvaluateMul> MulBase;
92 class MulOp : public MulBase
95 MulOp (GeneratorState& state, ConstValueRangeAccess valueRange);
96 virtual ~MulOp (void) {}
98 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
101 struct ComputeAddRange
103 template <typename T>
104 void operator() (de::Random& rnd, T dstMin, T dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const;
109 template <typename T> inline T operator() (T a, T b) const { return a+b; }
112 typedef BinaryVecOp<5, true, true, false, ComputeAddRange, EvaluateAdd> AddBase;
114 class AddOp : public AddBase
117 AddOp (GeneratorState& state, ConstValueRangeAccess valueRange);
118 virtual ~AddOp (void) {}
120 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
123 struct ComputeSubRange
125 template <typename T>
126 void operator() (de::Random& rnd, T dstMin, T dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const;
131 template <typename T> inline T operator() (T a, T b) const { return a-b; }
134 typedef BinaryVecOp<5, true, true, false, ComputeSubRange, EvaluateSub> SubBase;
136 class SubOp : public SubBase
139 SubOp (GeneratorState& state, ConstValueRangeAccess valueRange);
140 virtual ~SubOp (void) {}
142 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
145 /* Template for Relational Operators. */
147 template <class ComputeValueRange, class EvaluateComp>
148 class RelationalOp : public BinaryOp<7, ASSOCIATIVITY_LEFT>
151 RelationalOp (GeneratorState& state, Token::Type operatorToken, ConstValueRangeAccess valueRange);
152 virtual ~RelationalOp (void);
154 void evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b);
156 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
161 struct ComputeLessThanRange
163 template <typename T>
164 void operator() (de::Random& rnd, bool dstMin, bool dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const;
167 struct EvaluateLessThan
169 template <typename T> inline bool operator() (T a, T b) const { return a < b; }
172 typedef RelationalOp<ComputeLessThanRange, EvaluateLessThan> LessThanBase;
174 class LessThanOp : public LessThanBase
177 LessThanOp (GeneratorState& state, ConstValueRangeAccess valueRange);
178 virtual ~LessThanOp (void) {}
180 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
185 struct ComputeLessOrEqualRange
187 template <typename T>
188 void operator() (de::Random& rnd, bool dstMin, bool dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const;
191 struct EvaluateLessOrEqual
193 template <typename T> inline bool operator() (T a, T b) const { return a <= b; }
196 typedef RelationalOp<ComputeLessOrEqualRange, EvaluateLessOrEqual> LessOrEqualBase;
198 class LessOrEqualOp : public LessOrEqualBase
201 LessOrEqualOp (GeneratorState& state, ConstValueRangeAccess valueRange);
202 virtual ~LessOrEqualOp (void) {};
204 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
209 struct ComputeGreaterThanRange
211 template <typename T>
212 void operator() (de::Random& rnd, bool dstMin, bool dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const
214 ComputeLessThanRange()(rnd, dstMin, dstMax, bMin, bMax, aMin, aMax);
218 struct EvaluateGreaterThan
220 template <typename T> inline bool operator() (T a, T b) const { return a > b; }
223 typedef RelationalOp<ComputeGreaterThanRange, EvaluateGreaterThan> GreaterThanBase;
225 class GreaterThanOp : public GreaterThanBase
228 GreaterThanOp (GeneratorState& state, ConstValueRangeAccess valueRange);
229 virtual ~GreaterThanOp (void) {}
231 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
234 /* Greater or Equal. */
236 struct ComputeGreaterOrEqualRange
238 template <typename T>
239 void operator() (de::Random& rnd, bool dstMin, bool dstMax, T& aMin, T& aMax, T& bMin, T& bMax) const
241 ComputeLessOrEqualRange()(rnd, dstMin, dstMax, bMin, bMax, aMin, aMax);
245 struct EvaluateGreaterOrEqual
247 template <typename T> inline bool operator() (T a, T b) const { return a >= b; }
250 typedef RelationalOp<ComputeGreaterOrEqualRange, EvaluateGreaterOrEqual> GreaterOrEqualBase;
252 class GreaterOrEqualOp : public GreaterOrEqualBase
255 GreaterOrEqualOp (GeneratorState& state, ConstValueRangeAccess valueRange);
256 virtual ~GreaterOrEqualOp (void) {};
258 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
261 /* Equality comparison. */
263 template <bool IsEqual>
264 class EqualityComparisonOp : public BinaryOp<8, ASSOCIATIVITY_LEFT>
267 EqualityComparisonOp (GeneratorState& state, ConstValueRangeAccess valueRange);
268 virtual ~EqualityComparisonOp (void) {}
270 void evaluate (ExecValueAccess dst, ExecConstValueAccess a, ExecConstValueAccess b);
272 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
275 // \note Since template implementation is in .cpp we have to reference specialized constructor and static functions from there.
276 class EqualOp : public EqualityComparisonOp<true>
279 EqualOp (GeneratorState& state, ConstValueRangeAccess valueRange);
280 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
283 class NotEqualOp : public EqualityComparisonOp<false>
286 NotEqualOp (GeneratorState& state, ConstValueRangeAccess valueRange);
287 static float getWeight (const GeneratorState& state, ConstValueRangeAccess valueRange);
292 #endif // _RSGBINARYOPS_HPP