1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.1 Module
3 * -------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Common built-in function tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es31fShaderCommonFunctionTests.hpp"
25 #include "gluContextInfo.hpp"
26 #include "glsShaderExecUtil.hpp"
27 #include "tcuTestLog.hpp"
28 #include "tcuFormatUtil.hpp"
29 #include "tcuFloat.hpp"
30 #include "tcuInterval.hpp"
31 #include "tcuFloatFormat.hpp"
32 #include "deRandom.hpp"
35 #include "deArrayUtil.hpp"
47 using namespace gls::ShaderExecUtil;
58 template<typename T, int Size>
62 VecArrayAccess (const void* ptr) : m_array((tcu::Vector<T, Size>*)ptr) {}
63 ~VecArrayAccess (void) {}
65 const tcu::Vector<T, Size>& operator[] (size_t offset) const { return m_array[offset]; }
66 tcu::Vector<T, Size>& operator[] (size_t offset) { return m_array[offset]; }
69 tcu::Vector<T, Size>* m_array;
72 template<typename T> T randomScalar (de::Random& rnd, T minValue, T maxValue);
73 template<> inline float randomScalar (de::Random& rnd, float minValue, float maxValue) { return rnd.getFloat(minValue, maxValue); }
74 template<> inline deInt32 randomScalar (de::Random& rnd, deInt32 minValue, deInt32 maxValue) { return rnd.getInt(minValue, maxValue); }
75 template<> inline deUint32 randomScalar (de::Random& rnd, deUint32 minValue, deUint32 maxValue) { return minValue + rnd.getUint32() % (maxValue - minValue + 1); }
77 template<typename T, int Size>
78 inline tcu::Vector<T, Size> randomVector (de::Random& rnd, const tcu::Vector<T, Size>& minValue, const tcu::Vector<T, Size>& maxValue)
80 tcu::Vector<T, Size> res;
81 for (int ndx = 0; ndx < Size; ndx++)
82 res[ndx] = randomScalar<T>(rnd, minValue[ndx], maxValue[ndx]);
86 template<typename T, int Size>
87 static void fillRandomVectors (de::Random& rnd, const tcu::Vector<T, Size>& minValue, const tcu::Vector<T, Size>& maxValue, void* dst, int numValues, int offset = 0)
89 VecArrayAccess<T, Size> access(dst);
90 for (int ndx = 0; ndx < numValues; ndx++)
91 access[offset + ndx] = randomVector<T, Size>(rnd, minValue, maxValue);
95 static void fillRandomScalars (de::Random& rnd, T minValue, T maxValue, void* dst, int numValues, int offset = 0)
97 T* typedPtr = (T*)dst;
98 for (int ndx = 0; ndx < numValues; ndx++)
99 typedPtr[offset + ndx] = randomScalar<T>(rnd, minValue, maxValue);
102 inline int numBitsLostInOp (float input, float output)
104 const int inExp = tcu::Float32(input).exponent();
105 const int outExp = tcu::Float32(output).exponent();
107 return de::max(0, inExp-outExp); // Lost due to mantissa shift.
110 inline deUint32 getUlpDiff (float a, float b)
112 const deUint32 aBits = tcu::Float32(a).bits();
113 const deUint32 bBits = tcu::Float32(b).bits();
114 return aBits > bBits ? aBits - bBits : bBits - aBits;
117 inline deUint32 getUlpDiffIgnoreZeroSign (float a, float b)
119 if (tcu::Float32(a).isZero())
120 return getUlpDiff(tcu::Float32::construct(tcu::Float32(b).sign(), 0, 0).asFloat(), b);
121 else if (tcu::Float32(b).isZero())
122 return getUlpDiff(a, tcu::Float32::construct(tcu::Float32(a).sign(), 0, 0).asFloat());
124 return getUlpDiff(a, b);
127 inline bool supportsSignedZero (glu::Precision precision)
129 // \note GLSL ES 3.1 doesn't really require support for -0, but we require it for highp
130 // as it is very widely supported.
131 return precision == glu::PRECISION_HIGHP;
134 inline float getEpsFromMaxUlpDiff (float value, deUint32 ulpDiff)
136 const int exp = tcu::Float32(value).exponent();
137 return tcu::Float32::construct(+1, exp, (1u<<23) | ulpDiff).asFloat() - tcu::Float32::construct(+1, exp, 1u<<23).asFloat();
140 inline deUint32 getMaxUlpDiffFromBits (int numAccurateBits)
142 const int numGarbageBits = 23-numAccurateBits;
143 const deUint32 mask = (1u<<numGarbageBits)-1u;
148 inline float getEpsFromBits (float value, int numAccurateBits)
150 return getEpsFromMaxUlpDiff(value, getMaxUlpDiffFromBits(numAccurateBits));
153 static int getMinMantissaBits (glu::Precision precision)
161 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(bits) == glu::PRECISION_LAST);
162 DE_ASSERT(de::inBounds<int>(precision, 0, DE_LENGTH_OF_ARRAY(bits)));
163 return bits[precision];
166 static int getMaxNormalizedValueExponent (glu::Precision precision)
168 const int exponent[] =
174 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(exponent) == glu::PRECISION_LAST);
175 DE_ASSERT(de::inBounds<int>(precision, 0, DE_LENGTH_OF_ARRAY(exponent)));
176 return exponent[precision];
179 static int getMinNormalizedValueExponent (glu::Precision precision)
181 const int exponent[] =
187 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(exponent) == glu::PRECISION_LAST);
188 DE_ASSERT(de::inBounds<int>(precision, 0, DE_LENGTH_OF_ARRAY(exponent)));
189 return exponent[precision];
192 static float makeFloatRepresentable (float f, glu::Precision precision)
194 if (precision == glu::PRECISION_HIGHP)
196 // \note: assuming f is not extended-precision
201 const int numMantissaBits = getMinMantissaBits(precision);
202 const int maxNormalizedValueExponent = getMaxNormalizedValueExponent(precision);
203 const int minNormalizedValueExponent = getMinNormalizedValueExponent(precision);
204 const deUint32 representableMantissaMask = ((deUint32(1) << numMantissaBits) - 1) << (23 - (deUint32)numMantissaBits);
205 const float largestRepresentableValue = tcu::Float32::constructBits(+1, maxNormalizedValueExponent, ((1u << numMantissaBits) - 1u) << (23u - (deUint32)numMantissaBits)).asFloat();
206 const bool zeroNotRepresentable = (precision == glu::PRECISION_LOWP);
208 // if zero is not required to be representable, use smallest positive non-subnormal value
209 const float zeroValue = (zeroNotRepresentable) ? (tcu::Float32::constructBits(+1, minNormalizedValueExponent, 1).asFloat()) : (0.0f);
211 const tcu::Float32 float32Representation (f);
213 if (float32Representation.exponent() < minNormalizedValueExponent)
215 // flush too small values to zero
218 else if (float32Representation.exponent() > maxNormalizedValueExponent)
220 // clamp too large values
221 return (float32Representation.sign() == +1) ? (largestRepresentableValue) : (-largestRepresentableValue);
225 // remove unrepresentable mantissa bits
226 const tcu::Float32 targetRepresentation(tcu::Float32::constructBits(float32Representation.sign(),
227 float32Representation.exponent(),
228 float32Representation.mantissaBits() & representableMantissaMask));
230 return targetRepresentation.asFloat();
235 // CommonFunctionCase
237 class CommonFunctionCase : public TestCase
240 CommonFunctionCase (Context& context, const char* name, const char* description, glu::ShaderType shaderType);
241 ~CommonFunctionCase (void);
245 IterateResult iterate (void);
248 CommonFunctionCase (const CommonFunctionCase& other);
249 CommonFunctionCase& operator= (const CommonFunctionCase& other);
251 virtual void getInputValues (int numValues, void* const* values) const = 0;
252 virtual bool compare (const void* const* inputs, const void* const* outputs) = 0;
254 glu::ShaderType m_shaderType;
258 std::ostringstream m_failMsg; //!< Comparison failure help message.
261 ShaderExecutor* m_executor;
264 CommonFunctionCase::CommonFunctionCase (Context& context, const char* name, const char* description, glu::ShaderType shaderType)
265 : TestCase (context, name, description)
266 , m_shaderType (shaderType)
268 , m_executor (DE_NULL)
272 CommonFunctionCase::~CommonFunctionCase (void)
274 CommonFunctionCase::deinit();
277 void CommonFunctionCase::init (void)
279 DE_ASSERT(!m_executor);
281 m_spec.version = contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2)) ? glu::GLSL_VERSION_320_ES : glu::GLSL_VERSION_310_ES;
283 m_executor = createExecutor(m_context.getRenderContext(), m_shaderType, m_spec);
284 m_testCtx.getLog() << m_executor;
286 if (!m_executor->isOk())
287 throw tcu::TestError("Compile failed");
290 void CommonFunctionCase::deinit (void)
293 m_executor = DE_NULL;
296 static vector<int> getScalarSizes (const vector<Symbol>& symbols)
298 vector<int> sizes(symbols.size());
299 for (int ndx = 0; ndx < (int)symbols.size(); ++ndx)
300 sizes[ndx] = symbols[ndx].varType.getScalarSize();
304 static int computeTotalScalarSize (const vector<Symbol>& symbols)
307 for (vector<Symbol>::const_iterator sym = symbols.begin(); sym != symbols.end(); ++sym)
308 totalSize += sym->varType.getScalarSize();
312 static vector<void*> getInputOutputPointers (const vector<Symbol>& symbols, vector<deUint32>& data, const int numValues)
314 vector<void*> pointers (symbols.size());
315 int curScalarOffset = 0;
317 for (int varNdx = 0; varNdx < (int)symbols.size(); ++varNdx)
319 const Symbol& var = symbols[varNdx];
320 const int scalarSize = var.varType.getScalarSize();
322 // Uses planar layout as input/output specs do not support strides.
323 pointers[varNdx] = &data[curScalarOffset];
324 curScalarOffset += scalarSize*numValues;
327 DE_ASSERT(curScalarOffset == (int)data.size());
332 // \todo [2013-08-08 pyry] Make generic utility and move to glu?
337 HexFloat (const float value_) : value(value_) {}
340 std::ostream& operator<< (std::ostream& str, const HexFloat& v)
342 return str << v.value << " / " << tcu::toHex(tcu::Float32(v.value).bits());
347 const deUint32 value;
348 HexBool (const deUint32 value_) : value(value_) {}
351 std::ostream& operator<< (std::ostream& str, const HexBool& v)
353 return str << (v.value ? "true" : "false") << " / " << tcu::toHex(v.value);
358 const glu::VarType& type;
361 VarValue (const glu::VarType& type_, const void* value_) : type(type_), value(value_) {}
364 std::ostream& operator<< (std::ostream& str, const VarValue& varValue)
366 DE_ASSERT(varValue.type.isBasicType());
368 const glu::DataType basicType = varValue.type.getBasicType();
369 const glu::DataType scalarType = glu::getDataTypeScalarType(basicType);
370 const int numComponents = glu::getDataTypeScalarSize(basicType);
372 if (numComponents > 1)
373 str << glu::getDataTypeName(basicType) << "(";
375 for (int compNdx = 0; compNdx < numComponents; compNdx++)
382 case glu::TYPE_FLOAT: str << HexFloat(((const float*)varValue.value)[compNdx]); break;
383 case glu::TYPE_INT: str << ((const deInt32*)varValue.value)[compNdx]; break;
384 case glu::TYPE_UINT: str << tcu::toHex(((const deUint32*)varValue.value)[compNdx]); break;
385 case glu::TYPE_BOOL: str << HexBool(((const deUint32*)varValue.value)[compNdx]); break;
392 if (numComponents > 1)
398 CommonFunctionCase::IterateResult CommonFunctionCase::iterate (void)
400 const int numInputScalars = computeTotalScalarSize(m_spec.inputs);
401 const int numOutputScalars = computeTotalScalarSize(m_spec.outputs);
402 vector<deUint32> inputData (numInputScalars * m_numValues);
403 vector<deUint32> outputData (numOutputScalars * m_numValues);
404 const vector<void*> inputPointers = getInputOutputPointers(m_spec.inputs, inputData, m_numValues);
405 const vector<void*> outputPointers = getInputOutputPointers(m_spec.outputs, outputData, m_numValues);
407 // Initialize input data.
408 getInputValues(m_numValues, &inputPointers[0]);
411 m_executor->useProgram();
412 m_executor->execute(m_numValues, &inputPointers[0], &outputPointers[0]);
416 const vector<int> inScalarSizes = getScalarSizes(m_spec.inputs);
417 const vector<int> outScalarSizes = getScalarSizes(m_spec.outputs);
418 vector<void*> curInputPtr (inputPointers.size());
419 vector<void*> curOutputPtr (outputPointers.size());
422 for (int valNdx = 0; valNdx < m_numValues; valNdx++)
424 // Set up pointers for comparison.
425 for (int inNdx = 0; inNdx < (int)curInputPtr.size(); ++inNdx)
426 curInputPtr[inNdx] = (deUint32*)inputPointers[inNdx] + inScalarSizes[inNdx]*valNdx;
428 for (int outNdx = 0; outNdx < (int)curOutputPtr.size(); ++outNdx)
429 curOutputPtr[outNdx] = (deUint32*)outputPointers[outNdx] + outScalarSizes[outNdx]*valNdx;
431 if (!compare(&curInputPtr[0], &curOutputPtr[0]))
433 // \todo [2013-08-08 pyry] We probably want to log reference value as well?
435 m_testCtx.getLog() << TestLog::Message << "ERROR: comparison failed for value " << valNdx << ":\n " << m_failMsg.str() << TestLog::EndMessage;
437 m_testCtx.getLog() << TestLog::Message << " inputs:" << TestLog::EndMessage;
438 for (int inNdx = 0; inNdx < (int)curInputPtr.size(); inNdx++)
439 m_testCtx.getLog() << TestLog::Message << " " << m_spec.inputs[inNdx].name << " = "
440 << VarValue(m_spec.inputs[inNdx].varType, curInputPtr[inNdx])
441 << TestLog::EndMessage;
443 m_testCtx.getLog() << TestLog::Message << " outputs:" << TestLog::EndMessage;
444 for (int outNdx = 0; outNdx < (int)curOutputPtr.size(); outNdx++)
445 m_testCtx.getLog() << TestLog::Message << " " << m_spec.outputs[outNdx].name << " = "
446 << VarValue(m_spec.outputs[outNdx].varType, curOutputPtr[outNdx])
447 << TestLog::EndMessage;
455 m_testCtx.getLog() << TestLog::Message << (m_numValues - numFailed) << " / " << m_numValues << " values passed" << TestLog::EndMessage;
457 m_testCtx.setTestResult(numFailed == 0 ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL,
458 numFailed == 0 ? "Pass" : "Result comparison failed");
464 static const char* getPrecisionPostfix (glu::Precision precision)
466 static const char* s_postfix[] =
472 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_postfix) == glu::PRECISION_LAST);
473 DE_ASSERT(de::inBounds<int>(precision, 0, DE_LENGTH_OF_ARRAY(s_postfix)));
474 return s_postfix[precision];
477 static const char* getShaderTypePostfix (glu::ShaderType shaderType)
479 static const char* s_postfix[] =
488 DE_ASSERT(de::inBounds<int>(shaderType, 0, DE_LENGTH_OF_ARRAY(s_postfix)));
489 return s_postfix[shaderType];
492 static std::string getCommonFuncCaseName (glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
494 return string(glu::getDataTypeName(baseType)) + getPrecisionPostfix(precision) + getShaderTypePostfix(shaderType);
497 class AbsCase : public CommonFunctionCase
500 AbsCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
501 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "abs", shaderType)
503 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
504 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
505 m_spec.source = "out0 = abs(in0);";
508 void getInputValues (int numValues, void* const* values) const
510 const Vec2 floatRanges[] =
512 Vec2(-2.0f, 2.0f), // lowp
513 Vec2(-1e3f, 1e3f), // mediump
514 Vec2(-1e7f, 1e7f) // highp
516 const IVec2 intRanges[] =
518 IVec2(-(1<<7)+1, (1<<7)-1),
519 IVec2(-(1<<15)+1, (1<<15)-1),
520 IVec2(0x80000001, 0x7fffffff)
523 de::Random rnd (deStringHash(getName()) ^ 0x235facu);
524 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
525 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
526 const int scalarSize = glu::getDataTypeScalarSize(type);
528 if (glu::isDataTypeFloatOrVec(type))
529 fillRandomScalars(rnd, floatRanges[precision].x(), floatRanges[precision].y(), values[0], numValues*scalarSize);
531 fillRandomScalars(rnd, intRanges[precision].x(), intRanges[precision].y(), values[0], numValues*scalarSize);
534 bool compare (const void* const* inputs, const void* const* outputs)
536 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
537 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
538 const int scalarSize = glu::getDataTypeScalarSize(type);
540 if (glu::isDataTypeFloatOrVec(type))
542 const int mantissaBits = getMinMantissaBits(precision);
543 const deUint32 maxUlpDiff = (1u<<(23-mantissaBits))-1u;
545 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
547 const float in0 = ((const float*)inputs[0])[compNdx];
548 const float out0 = ((const float*)outputs[0])[compNdx];
549 const float ref0 = de::abs(in0);
550 const deUint32 ulpDiff0 = getUlpDiff(out0, ref0);
552 if (ulpDiff0 > maxUlpDiff)
554 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref0) << " with ULP threshold " << maxUlpDiff << ", got ULP diff " << ulpDiff0;
561 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
563 const int in0 = ((const int*)inputs[0])[compNdx];
564 const int out0 = ((const int*)outputs[0])[compNdx];
565 const int ref0 = de::abs(in0);
569 m_failMsg << "Expected [" << compNdx << "] = " << ref0;
579 class SignCase : public CommonFunctionCase
582 SignCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
583 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "sign", shaderType)
585 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
586 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
587 m_spec.source = "out0 = sign(in0);";
590 void getInputValues (int numValues, void* const* values) const
592 const Vec2 floatRanges[] =
594 Vec2(-2.0f, 2.0f), // lowp
595 Vec2(-1e4f, 1e4f), // mediump - note: may end up as inf
596 Vec2(-1e8f, 1e8f) // highp - note: may end up as inf
598 const IVec2 intRanges[] =
600 IVec2(-(1<<7), (1<<7)-1),
601 IVec2(-(1<<15), (1<<15)-1),
602 IVec2(0x80000000, 0x7fffffff)
605 de::Random rnd (deStringHash(getName()) ^ 0x324u);
606 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
607 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
608 const int scalarSize = glu::getDataTypeScalarSize(type);
610 if (glu::isDataTypeFloatOrVec(type))
613 std::fill((float*)values[0], (float*)values[0] + scalarSize, +1.0f);
614 std::fill((float*)values[0] + scalarSize*1, (float*)values[0] + scalarSize*2, -1.0f);
615 std::fill((float*)values[0] + scalarSize*2, (float*)values[0] + scalarSize*3, 0.0f);
616 fillRandomScalars(rnd, floatRanges[precision].x(), floatRanges[precision].y(), (float*)values[0] + scalarSize*3, (numValues-3)*scalarSize);
620 std::fill((int*)values[0], (int*)values[0] + scalarSize, +1);
621 std::fill((int*)values[0] + scalarSize*1, (int*)values[0] + scalarSize*2, -1);
622 std::fill((int*)values[0] + scalarSize*2, (int*)values[0] + scalarSize*3, 0);
623 fillRandomScalars(rnd, intRanges[precision].x(), intRanges[precision].y(), (int*)values[0] + scalarSize*3, (numValues-3)*scalarSize);
627 bool compare (const void* const* inputs, const void* const* outputs)
629 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
630 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
631 const int scalarSize = glu::getDataTypeScalarSize(type);
633 if (glu::isDataTypeFloatOrVec(type))
635 // Both highp and mediump should be able to represent -1, 0, and +1 exactly
636 const deUint32 maxUlpDiff = precision == glu::PRECISION_LOWP ? getMaxUlpDiffFromBits(getMinMantissaBits(precision)) : 0;
638 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
640 const float in0 = ((const float*)inputs[0])[compNdx];
641 const float out0 = ((const float*)outputs[0])[compNdx];
642 const float ref0 = in0 < 0.0f ? -1.0f :
643 in0 > 0.0f ? +1.0f : 0.0f;
644 const deUint32 ulpDiff0 = getUlpDiff(out0, ref0);
646 if (ulpDiff0 > maxUlpDiff)
648 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref0) << " with ULP threshold " << maxUlpDiff << ", got ULP diff " << ulpDiff0;
655 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
657 const int in0 = ((const int*)inputs[0])[compNdx];
658 const int out0 = ((const int*)outputs[0])[compNdx];
659 const int ref0 = in0 < 0 ? -1 :
664 m_failMsg << "Expected [" << compNdx << "] = " << ref0;
674 static float roundEven (float v)
676 const float q = deFloatFrac(v);
677 const int truncated = int(v-q);
678 const int rounded = (q > 0.5f) ? (truncated + 1) : // Rounded up
679 (q == 0.5f && (truncated % 2 != 0)) ? (truncated + 1) : // Round to nearest even at 0.5
680 truncated; // Rounded down
682 return float(rounded);
685 class RoundEvenCase : public CommonFunctionCase
688 RoundEvenCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
689 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "roundEven", shaderType)
691 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
692 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
693 m_spec.source = "out0 = roundEven(in0);";
696 void getInputValues (int numValues, void* const* values) const
698 const Vec2 ranges[] =
700 Vec2(-2.0f, 2.0f), // lowp
701 Vec2(-1e3f, 1e3f), // mediump
702 Vec2(-1e7f, 1e7f) // highp
705 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
706 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
707 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
708 const int scalarSize = glu::getDataTypeScalarSize(type);
709 int numSpecialCases = 0;
712 if (precision != glu::PRECISION_LOWP)
714 DE_ASSERT(numValues >= 20);
715 for (int ndx = 0; ndx < 20; ndx++)
717 const float v = de::clamp(float(ndx) - 10.5f, ranges[precision].x(), ranges[precision].y());
718 std::fill((float*)values[0], (float*)values[0] + scalarSize, v);
719 numSpecialCases += 1;
724 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[0] + numSpecialCases*scalarSize, (numValues-numSpecialCases)*scalarSize);
726 // If precision is mediump, make sure values can be represented in fp16 exactly
727 if (precision == glu::PRECISION_MEDIUMP)
729 for (int ndx = 0; ndx < numValues*scalarSize; ndx++)
730 ((float*)values[0])[ndx] = tcu::Float16(((float*)values[0])[ndx]).asFloat();
734 bool compare (const void* const* inputs, const void* const* outputs)
736 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
737 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
738 const bool hasSignedZero = supportsSignedZero(precision);
739 const int scalarSize = glu::getDataTypeScalarSize(type);
741 if (precision == glu::PRECISION_HIGHP || precision == glu::PRECISION_MEDIUMP)
743 // Require exact rounding result.
744 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
746 const float in0 = ((const float*)inputs[0])[compNdx];
747 const float out0 = ((const float*)outputs[0])[compNdx];
748 const float ref = roundEven(in0);
750 const deUint32 ulpDiff = hasSignedZero ? getUlpDiff(out0, ref) : getUlpDiffIgnoreZeroSign(out0, ref);
754 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref) << ", got ULP diff " << tcu::toHex(ulpDiff);
761 const int mantissaBits = getMinMantissaBits(precision);
762 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits); // ULP diff for rounded integer value.
763 const float eps = getEpsFromBits(1.0f, mantissaBits); // epsilon for rounding bounds
765 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
767 const float in0 = ((const float*)inputs[0])[compNdx];
768 const float out0 = ((const float*)outputs[0])[compNdx];
769 const int minRes = int(roundEven(in0-eps));
770 const int maxRes = int(roundEven(in0+eps));
773 for (int roundedVal = minRes; roundedVal <= maxRes; roundedVal++)
775 const deUint32 ulpDiff = getUlpDiffIgnoreZeroSign(out0, float(roundedVal));
777 if (ulpDiff <= maxUlpDiff)
786 m_failMsg << "Expected [" << compNdx << "] = [" << minRes << ", " << maxRes << "] with ULP threshold " << tcu::toHex(maxUlpDiff);
796 class ModfCase : public CommonFunctionCase
799 ModfCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
800 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "modf", shaderType)
802 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
803 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
804 m_spec.outputs.push_back(Symbol("out1", glu::VarType(baseType, precision)));
805 m_spec.source = "out0 = modf(in0, out1);";
808 void getInputValues (int numValues, void* const* values) const
810 const Vec2 ranges[] =
812 Vec2(-2.0f, 2.0f), // lowp
813 Vec2(-1e3f, 1e3f), // mediump
814 Vec2(-1e7f, 1e7f) // highp
817 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
818 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
819 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
820 const int scalarSize = glu::getDataTypeScalarSize(type);
822 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), values[0], numValues*scalarSize);
825 bool compare (const void* const* inputs, const void* const* outputs)
827 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
828 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
829 const bool hasZeroSign = supportsSignedZero(precision);
830 const int scalarSize = glu::getDataTypeScalarSize(type);
832 const int mantissaBits = getMinMantissaBits(precision);
834 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
836 const float in0 = ((const float*)inputs[0])[compNdx];
837 const float out0 = ((const float*)outputs[0])[compNdx];
838 const float out1 = ((const float*)outputs[1])[compNdx];
840 const float refOut1 = float(int(in0));
841 const float refOut0 = in0 - refOut1;
843 const int bitsLost = precision != glu::PRECISION_HIGHP ? numBitsLostInOp(in0, refOut0) : 0;
844 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(de::max(mantissaBits - bitsLost, 0));
846 const float resSum = out0 + out1;
848 const deUint32 ulpDiff = hasZeroSign ? getUlpDiff(resSum, in0) : getUlpDiffIgnoreZeroSign(resSum, in0);
850 if (ulpDiff > maxUlpDiff)
852 m_failMsg << "Expected [" << compNdx << "] = (" << HexFloat(refOut0) << ") + (" << HexFloat(refOut1) << ") = " << HexFloat(in0) << " with ULP threshold "
853 << tcu::toHex(maxUlpDiff) << ", got ULP diff " << tcu::toHex(ulpDiff);
862 class IsnanCase : public CommonFunctionCase
865 IsnanCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
866 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "isnan", shaderType)
868 DE_ASSERT(glu::isDataTypeFloatOrVec(baseType));
870 const int vecSize = glu::getDataTypeScalarSize(baseType);
871 const glu::DataType boolType = vecSize > 1 ? glu::getDataTypeBoolVec(vecSize) : glu::TYPE_BOOL;
873 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
874 m_spec.outputs.push_back(Symbol("out0", glu::VarType(boolType, glu::PRECISION_LAST)));
875 m_spec.source = "out0 = isnan(in0);";
878 void getInputValues (int numValues, void* const* values) const
880 de::Random rnd (deStringHash(getName()) ^ 0xc2a39fu);
881 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
882 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
883 const int scalarSize = glu::getDataTypeScalarSize(type);
884 const int mantissaBits = getMinMantissaBits(precision);
885 const deUint32 mantissaMask = ~getMaxUlpDiffFromBits(mantissaBits) & ((1u<<23)-1u);
887 for (int valNdx = 0; valNdx < numValues*scalarSize; valNdx++)
889 const bool isNan = rnd.getFloat() > 0.3f;
890 const bool isInf = !isNan && rnd.getFloat() > 0.4f;
891 const deUint32 mantissa = !isInf ? ((1u<<22) | (rnd.getUint32() & mantissaMask)) : 0;
892 const deUint32 exp = !isNan && !isInf ? (rnd.getUint32() & 0x7fu) : 0xffu;
893 const deUint32 sign = rnd.getUint32() & 0x1u;
894 const deUint32 value = (sign << 31) | (exp << 23) | mantissa;
896 DE_ASSERT(tcu::Float32(value).isInf() == isInf && tcu::Float32(value).isNaN() == isNan);
898 ((deUint32*)values[0])[valNdx] = value;
902 bool compare (const void* const* inputs, const void* const* outputs)
904 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
905 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
906 const int scalarSize = glu::getDataTypeScalarSize(type);
908 if (precision == glu::PRECISION_HIGHP)
910 // Only highp is required to support inf/nan
911 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
913 const float in0 = ((const float*)inputs[0])[compNdx];
914 const bool out0 = ((const deUint32*)outputs[0])[compNdx] != 0;
915 const bool ref = tcu::Float32(in0).isNaN();
919 m_failMsg << "Expected [" << compNdx << "] = " << (ref ? "true" : "false");
924 else if (precision == glu::PRECISION_MEDIUMP || precision == glu::PRECISION_LOWP)
926 // NaN support is optional, check that inputs that are not NaN don't result in true.
927 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
929 const float in0 = ((const float*)inputs[0])[compNdx];
930 const bool out0 = ((const deUint32*)outputs[0])[compNdx] != 0;
931 const bool ref = tcu::Float32(in0).isNaN();
935 m_failMsg << "Expected [" << compNdx << "] = " << (ref ? "true" : "false");
945 class IsinfCase : public CommonFunctionCase
948 IsinfCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
949 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "isinf", shaderType)
951 DE_ASSERT(glu::isDataTypeFloatOrVec(baseType));
953 const int vecSize = glu::getDataTypeScalarSize(baseType);
954 const glu::DataType boolType = vecSize > 1 ? glu::getDataTypeBoolVec(vecSize) : glu::TYPE_BOOL;
956 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
957 m_spec.outputs.push_back(Symbol("out0", glu::VarType(boolType, glu::PRECISION_LAST)));
958 m_spec.source = "out0 = isinf(in0);";
961 void getInputValues (int numValues, void* const* values) const
963 de::Random rnd (deStringHash(getName()) ^ 0xc2a39fu);
964 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
965 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
966 const int scalarSize = glu::getDataTypeScalarSize(type);
967 const int mantissaBits = getMinMantissaBits(precision);
968 const deUint32 mantissaMask = ~getMaxUlpDiffFromBits(mantissaBits) & ((1u<<23)-1u);
970 for (int valNdx = 0; valNdx < numValues*scalarSize; valNdx++)
972 const bool isInf = rnd.getFloat() > 0.3f;
973 const bool isNan = !isInf && rnd.getFloat() > 0.4f;
974 const deUint32 mantissa = !isInf ? ((1u<<22) | (rnd.getUint32() & mantissaMask)) : 0;
975 const deUint32 exp = !isNan && !isInf ? (rnd.getUint32() & 0x7fu) : 0xffu;
976 const deUint32 sign = rnd.getUint32() & 0x1u;
977 const deUint32 value = (sign << 31) | (exp << 23) | mantissa;
979 DE_ASSERT(tcu::Float32(value).isInf() == isInf && tcu::Float32(value).isNaN() == isNan);
981 ((deUint32*)values[0])[valNdx] = value;
985 bool compare (const void* const* inputs, const void* const* outputs)
987 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
988 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
989 const int scalarSize = glu::getDataTypeScalarSize(type);
991 if (precision == glu::PRECISION_HIGHP)
993 // Only highp is required to support inf/nan
994 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
996 const float in0 = ((const float*)inputs[0])[compNdx];
997 const bool out0 = ((const deUint32*)outputs[0])[compNdx] != 0;
998 const bool ref = tcu::Float32(in0).isInf();
1002 m_failMsg << "Expected [" << compNdx << "] = " << HexBool(ref);
1007 else if (precision == glu::PRECISION_MEDIUMP)
1009 // Inf support is optional, check that inputs that are not Inf in mediump don't result in true.
1010 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1012 const float in0 = ((const float*)inputs[0])[compNdx];
1013 const bool out0 = ((const deUint32*)outputs[0])[compNdx] != 0;
1014 const bool ref = tcu::Float16(in0).isInf();
1018 m_failMsg << "Expected [" << compNdx << "] = " << (ref ? "true" : "false");
1023 // else: no verification can be performed
1029 class FloatBitsToUintIntCase : public CommonFunctionCase
1032 FloatBitsToUintIntCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType, bool outIsSigned)
1033 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), outIsSigned ? "floatBitsToInt" : "floatBitsToUint", shaderType)
1035 const int vecSize = glu::getDataTypeScalarSize(baseType);
1036 const glu::DataType intType = outIsSigned ? (vecSize > 1 ? glu::getDataTypeIntVec(vecSize) : glu::TYPE_INT)
1037 : (vecSize > 1 ? glu::getDataTypeUintVec(vecSize) : glu::TYPE_UINT);
1039 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1040 m_spec.outputs.push_back(Symbol("out0", glu::VarType(intType, glu::PRECISION_HIGHP)));
1041 m_spec.source = outIsSigned ? "out0 = floatBitsToInt(in0);" : "out0 = floatBitsToUint(in0);";
1044 void getInputValues (int numValues, void* const* values) const
1046 const Vec2 ranges[] =
1048 Vec2(-2.0f, 2.0f), // lowp
1049 Vec2(-1e3f, 1e3f), // mediump
1050 Vec2(-1e7f, 1e7f) // highp
1053 de::Random rnd (deStringHash(getName()) ^ 0x2790au);
1054 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1055 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1056 const int scalarSize = glu::getDataTypeScalarSize(type);
1058 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), values[0], numValues*scalarSize);
1061 bool compare (const void* const* inputs, const void* const* outputs)
1063 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1064 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1065 const int scalarSize = glu::getDataTypeScalarSize(type);
1067 const int mantissaBits = getMinMantissaBits(precision);
1068 const int maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits);
1070 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1072 const float in0 = ((const float*)inputs[0])[compNdx];
1073 const deUint32 out0 = ((const deUint32*)outputs[0])[compNdx];
1074 const deUint32 refOut0 = tcu::Float32(in0).bits();
1075 const int ulpDiff = de::abs((int)out0 - (int)refOut0);
1077 if (ulpDiff > maxUlpDiff)
1079 m_failMsg << "Expected [" << compNdx << "] = " << tcu::toHex(refOut0) << " with threshold "
1080 << tcu::toHex(maxUlpDiff) << ", got diff " << tcu::toHex(ulpDiff);
1089 class FloatBitsToIntCase : public FloatBitsToUintIntCase
1092 FloatBitsToIntCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1093 : FloatBitsToUintIntCase(context, baseType, precision, shaderType, true)
1098 class FloatBitsToUintCase : public FloatBitsToUintIntCase
1101 FloatBitsToUintCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1102 : FloatBitsToUintIntCase(context, baseType, precision, shaderType, false)
1107 class BitsToFloatCase : public CommonFunctionCase
1110 BitsToFloatCase (Context& context, glu::DataType baseType, glu::ShaderType shaderType)
1111 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, glu::PRECISION_HIGHP, shaderType).c_str(), glu::isDataTypeIntOrIVec(baseType) ? "intBitsToFloat" : "uintBitsToFloat", shaderType)
1113 const bool inIsSigned = glu::isDataTypeIntOrIVec(baseType);
1114 const int vecSize = glu::getDataTypeScalarSize(baseType);
1115 const glu::DataType floatType = vecSize > 1 ? glu::getDataTypeFloatVec(vecSize) : glu::TYPE_FLOAT;
1117 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, glu::PRECISION_HIGHP)));
1118 m_spec.outputs.push_back(Symbol("out0", glu::VarType(floatType, glu::PRECISION_HIGHP)));
1119 m_spec.source = inIsSigned ? "out0 = intBitsToFloat(in0);" : "out0 = uintBitsToFloat(in0);";
1122 void getInputValues (int numValues, void* const* values) const
1124 de::Random rnd (deStringHash(getName()) ^ 0xbbb225u);
1125 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1126 const int scalarSize = glu::getDataTypeScalarSize(type);
1127 const Vec2 range (-1e8f, +1e8f);
1129 // \note Filled as floats.
1130 fillRandomScalars(rnd, range.x(), range.y(), values[0], numValues*scalarSize);
1133 bool compare (const void* const* inputs, const void* const* outputs)
1135 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1136 const int scalarSize = glu::getDataTypeScalarSize(type);
1137 const deUint32 maxUlpDiff = 0;
1139 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1141 const float in0 = ((const float*)inputs[0])[compNdx];
1142 const float out0 = ((const float*)outputs[0])[compNdx];
1143 const deUint32 ulpDiff = getUlpDiff(in0, out0);
1145 if (ulpDiff > maxUlpDiff)
1147 m_failMsg << "Expected [" << compNdx << "] = " << tcu::toHex(tcu::Float32(in0).bits()) << " with ULP threshold "
1148 << tcu::toHex(maxUlpDiff) << ", got ULP diff " << tcu::toHex(ulpDiff);
1157 class FloorCase : public CommonFunctionCase
1160 FloorCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1161 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "floor", shaderType)
1163 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1164 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
1165 m_spec.source = "out0 = floor(in0);";
1168 void getInputValues (int numValues, void* const* values) const
1170 const Vec2 ranges[] =
1172 Vec2(-2.0f, 2.0f), // lowp
1173 Vec2(-1e3f, 1e3f), // mediump
1174 Vec2(-1e7f, 1e7f) // highp
1177 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
1178 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1179 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1180 const int scalarSize = glu::getDataTypeScalarSize(type);
1182 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[0], numValues*scalarSize);
1184 // If precision is mediump, make sure values can be represented in fp16 exactly
1185 if (precision == glu::PRECISION_MEDIUMP)
1187 for (int ndx = 0; ndx < numValues*scalarSize; ndx++)
1188 ((float*)values[0])[ndx] = tcu::Float16(((float*)values[0])[ndx]).asFloat();
1192 bool compare (const void* const* inputs, const void* const* outputs)
1194 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1195 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1196 const int scalarSize = glu::getDataTypeScalarSize(type);
1198 if (precision == glu::PRECISION_HIGHP || precision == glu::PRECISION_MEDIUMP)
1200 // Require exact result.
1201 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1203 const float in0 = ((const float*)inputs[0])[compNdx];
1204 const float out0 = ((const float*)outputs[0])[compNdx];
1205 const float ref = deFloatFloor(in0);
1207 const deUint32 ulpDiff = getUlpDiff(out0, ref);
1211 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref) << ", got ULP diff " << tcu::toHex(ulpDiff);
1218 const int mantissaBits = getMinMantissaBits(precision);
1219 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits); // ULP diff for rounded integer value.
1220 const float eps = getEpsFromBits(1.0f, mantissaBits); // epsilon for rounding bounds
1222 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1224 const float in0 = ((const float*)inputs[0])[compNdx];
1225 const float out0 = ((const float*)outputs[0])[compNdx];
1226 const int minRes = int(deFloatFloor(in0-eps));
1227 const int maxRes = int(deFloatFloor(in0+eps));
1230 for (int roundedVal = minRes; roundedVal <= maxRes; roundedVal++)
1232 const deUint32 ulpDiff = getUlpDiff(out0, float(roundedVal));
1234 if (ulpDiff <= maxUlpDiff)
1243 m_failMsg << "Expected [" << compNdx << "] = [" << minRes << ", " << maxRes << "] with ULP threshold " << tcu::toHex(maxUlpDiff);
1253 class TruncCase : public CommonFunctionCase
1256 TruncCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1257 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "trunc", shaderType)
1259 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1260 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
1261 m_spec.source = "out0 = trunc(in0);";
1264 void getInputValues (int numValues, void* const* values) const
1266 const Vec2 ranges[] =
1268 Vec2(-2.0f, 2.0f), // lowp
1269 Vec2(-1e3f, 1e3f), // mediump
1270 Vec2(-1e7f, 1e7f) // highp
1273 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
1274 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1275 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1276 const int scalarSize = glu::getDataTypeScalarSize(type);
1277 const float specialCases[] = { 0.0f, -0.0f, -0.9f, 0.9f, 1.0f, -1.0f };
1278 const int numSpecialCases = DE_LENGTH_OF_ARRAY(specialCases);
1281 for (int caseNdx = 0; caseNdx < numSpecialCases; caseNdx++)
1283 for (int scalarNdx = 0; scalarNdx < scalarSize; scalarNdx++)
1284 ((float*)values[0])[caseNdx*scalarSize + scalarNdx] = specialCases[caseNdx];
1288 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[0] + scalarSize*numSpecialCases, (numValues-numSpecialCases)*scalarSize);
1290 // If precision is mediump, make sure values can be represented in fp16 exactly
1291 if (precision == glu::PRECISION_MEDIUMP)
1293 for (int ndx = 0; ndx < numValues*scalarSize; ndx++)
1294 ((float*)values[0])[ndx] = tcu::Float16(((float*)values[0])[ndx]).asFloat();
1298 bool compare (const void* const* inputs, const void* const* outputs)
1300 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1301 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1302 const int scalarSize = glu::getDataTypeScalarSize(type);
1304 if (precision == glu::PRECISION_HIGHP || precision == glu::PRECISION_MEDIUMP)
1306 // Require exact result.
1307 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1309 const float in0 = ((const float*)inputs[0])[compNdx];
1310 const float out0 = ((const float*)outputs[0])[compNdx];
1311 const bool isNeg = tcu::Float32(in0).sign() < 0;
1312 const float ref = isNeg ? (-float(int(-in0))) : float(int(in0));
1314 // \note: trunc() function definition is a bit broad on negative zeros. Ignore result sign if zero.
1315 const deUint32 ulpDiff = getUlpDiffIgnoreZeroSign(out0, ref);
1319 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref) << ", got ULP diff " << tcu::toHex(ulpDiff);
1326 const int mantissaBits = getMinMantissaBits(precision);
1327 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits); // ULP diff for rounded integer value.
1328 const float eps = getEpsFromBits(1.0f, mantissaBits); // epsilon for rounding bounds
1330 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1332 const float in0 = ((const float*)inputs[0])[compNdx];
1333 const float out0 = ((const float*)outputs[0])[compNdx];
1334 const int minRes = int(in0-eps);
1335 const int maxRes = int(in0+eps);
1338 for (int roundedVal = minRes; roundedVal <= maxRes; roundedVal++)
1340 const deUint32 ulpDiff = getUlpDiffIgnoreZeroSign(out0, float(roundedVal));
1342 if (ulpDiff <= maxUlpDiff)
1351 m_failMsg << "Expected [" << compNdx << "] = [" << minRes << ", " << maxRes << "] with ULP threshold " << tcu::toHex(maxUlpDiff);
1361 class RoundCase : public CommonFunctionCase
1364 RoundCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1365 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "round", shaderType)
1367 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1368 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
1369 m_spec.source = "out0 = round(in0);";
1372 void getInputValues (int numValues, void* const* values) const
1374 const Vec2 ranges[] =
1376 Vec2(-2.0f, 2.0f), // lowp
1377 Vec2(-1e3f, 1e3f), // mediump
1378 Vec2(-1e7f, 1e7f) // highp
1381 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
1382 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1383 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1384 const int scalarSize = glu::getDataTypeScalarSize(type);
1385 int numSpecialCases = 0;
1388 if (precision != glu::PRECISION_LOWP)
1390 DE_ASSERT(numValues >= 10);
1391 for (int ndx = 0; ndx < 10; ndx++)
1393 const float v = de::clamp(float(ndx) - 5.5f, ranges[precision].x(), ranges[precision].y());
1394 std::fill((float*)values[0], (float*)values[0] + scalarSize, v);
1395 numSpecialCases += 1;
1400 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[0] + numSpecialCases*scalarSize, (numValues-numSpecialCases)*scalarSize);
1402 // If precision is mediump, make sure values can be represented in fp16 exactly
1403 if (precision == glu::PRECISION_MEDIUMP)
1405 for (int ndx = 0; ndx < numValues*scalarSize; ndx++)
1406 ((float*)values[0])[ndx] = tcu::Float16(((float*)values[0])[ndx]).asFloat();
1410 bool compare (const void* const* inputs, const void* const* outputs)
1412 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1413 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1414 const bool hasZeroSign = supportsSignedZero(precision);
1415 const int scalarSize = glu::getDataTypeScalarSize(type);
1417 if (precision == glu::PRECISION_HIGHP || precision == glu::PRECISION_MEDIUMP)
1419 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1421 const float in0 = ((const float*)inputs[0])[compNdx];
1422 const float out0 = ((const float*)outputs[0])[compNdx];
1424 if (deFloatFrac(in0) == 0.5f)
1426 // Allow both ceil(in) and floor(in)
1427 const float ref0 = deFloatFloor(in0);
1428 const float ref1 = deFloatCeil(in0);
1429 const deUint32 ulpDiff0 = hasZeroSign ? getUlpDiff(out0, ref0) : getUlpDiffIgnoreZeroSign(out0, ref0);
1430 const deUint32 ulpDiff1 = hasZeroSign ? getUlpDiff(out0, ref1) : getUlpDiffIgnoreZeroSign(out0, ref1);
1432 if (ulpDiff0 > 0 && ulpDiff1 > 0)
1434 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref0) << " or " << HexFloat(ref1) << ", got ULP diff " << tcu::toHex(de::min(ulpDiff0, ulpDiff1));
1440 // Require exact result
1441 const float ref = roundEven(in0);
1442 const deUint32 ulpDiff = hasZeroSign ? getUlpDiff(out0, ref) : getUlpDiffIgnoreZeroSign(out0, ref);
1446 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref) << ", got ULP diff " << tcu::toHex(ulpDiff);
1454 const int mantissaBits = getMinMantissaBits(precision);
1455 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits); // ULP diff for rounded integer value.
1456 const float eps = getEpsFromBits(1.0f, mantissaBits); // epsilon for rounding bounds
1458 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1460 const float in0 = ((const float*)inputs[0])[compNdx];
1461 const float out0 = ((const float*)outputs[0])[compNdx];
1462 const int minRes = int(roundEven(in0-eps));
1463 const int maxRes = int(roundEven(in0+eps));
1466 for (int roundedVal = minRes; roundedVal <= maxRes; roundedVal++)
1468 const deUint32 ulpDiff = getUlpDiffIgnoreZeroSign(out0, float(roundedVal));
1470 if (ulpDiff <= maxUlpDiff)
1479 m_failMsg << "Expected [" << compNdx << "] = [" << minRes << ", " << maxRes << "] with ULP threshold " << tcu::toHex(maxUlpDiff);
1489 class CeilCase : public CommonFunctionCase
1492 CeilCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1493 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "ceil", shaderType)
1495 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1496 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
1497 m_spec.source = "out0 = ceil(in0);";
1500 void getInputValues (int numValues, void* const* values) const
1502 const Vec2 ranges[] =
1504 Vec2(-2.0f, 2.0f), // lowp
1505 Vec2(-1e3f, 1e3f), // mediump
1506 Vec2(-1e7f, 1e7f) // highp
1509 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
1510 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1511 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1512 const int scalarSize = glu::getDataTypeScalarSize(type);
1515 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[0], numValues*scalarSize);
1517 // If precision is mediump, make sure values can be represented in fp16 exactly
1518 if (precision == glu::PRECISION_MEDIUMP)
1520 for (int ndx = 0; ndx < numValues*scalarSize; ndx++)
1521 ((float*)values[0])[ndx] = tcu::Float16(((float*)values[0])[ndx]).asFloat();
1525 bool compare (const void* const* inputs, const void* const* outputs)
1527 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1528 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1529 const bool hasZeroSign = supportsSignedZero(precision);
1530 const int scalarSize = glu::getDataTypeScalarSize(type);
1532 if (precision == glu::PRECISION_HIGHP || precision == glu::PRECISION_MEDIUMP)
1534 // Require exact result.
1535 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1537 const float in0 = ((const float*)inputs[0])[compNdx];
1538 const float out0 = ((const float*)outputs[0])[compNdx];
1539 const float ref = deFloatCeil(in0);
1541 const deUint32 ulpDiff = hasZeroSign ? getUlpDiff(out0, ref) : getUlpDiffIgnoreZeroSign(out0, ref);
1545 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref) << ", got ULP diff " << tcu::toHex(ulpDiff);
1552 const int mantissaBits = getMinMantissaBits(precision);
1553 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits); // ULP diff for rounded integer value.
1554 const float eps = getEpsFromBits(1.0f, mantissaBits); // epsilon for rounding bounds
1556 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1558 const float in0 = ((const float*)inputs[0])[compNdx];
1559 const float out0 = ((const float*)outputs[0])[compNdx];
1560 const int minRes = int(deFloatCeil(in0-eps));
1561 const int maxRes = int(deFloatCeil(in0+eps));
1564 for (int roundedVal = minRes; roundedVal <= maxRes; roundedVal++)
1566 const deUint32 ulpDiff = getUlpDiffIgnoreZeroSign(out0, float(roundedVal));
1568 if (ulpDiff <= maxUlpDiff)
1575 if (!anyOk && de::inRange(0, minRes, maxRes))
1577 // Allow -0 as well.
1578 const int ulpDiff = de::abs((int)tcu::Float32(out0).bits() - (int)0x80000000u);
1579 anyOk = ((deUint32)ulpDiff <= maxUlpDiff);
1584 m_failMsg << "Expected [" << compNdx << "] = [" << minRes << ", " << maxRes << "] with ULP threshold " << tcu::toHex(maxUlpDiff);
1594 class FractCase : public CommonFunctionCase
1597 FractCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1598 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "fract", shaderType)
1600 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1601 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, precision)));
1602 m_spec.source = "out0 = fract(in0);";
1605 void getInputValues (int numValues, void* const* values) const
1607 const Vec2 ranges[] =
1609 Vec2(-2.0f, 2.0f), // lowp
1610 Vec2(-1e3f, 1e3f), // mediump
1611 Vec2(-1e7f, 1e7f) // highp
1614 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
1615 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1616 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1617 const int scalarSize = glu::getDataTypeScalarSize(type);
1618 int numSpecialCases = 0;
1621 if (precision != glu::PRECISION_LOWP)
1623 DE_ASSERT(numValues >= 10);
1624 for (int ndx = 0; ndx < 10; ndx++)
1626 const float v = de::clamp(float(ndx) - 5.5f, ranges[precision].x(), ranges[precision].y());
1627 std::fill((float*)values[0], (float*)values[0] + scalarSize, v);
1628 numSpecialCases += 1;
1633 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[0] + numSpecialCases*scalarSize, (numValues-numSpecialCases)*scalarSize);
1635 // If precision is mediump, make sure values can be represented in fp16 exactly
1636 if (precision == glu::PRECISION_MEDIUMP)
1638 for (int ndx = 0; ndx < numValues*scalarSize; ndx++)
1639 ((float*)values[0])[ndx] = tcu::Float16(((float*)values[0])[ndx]).asFloat();
1643 bool compare (const void* const* inputs, const void* const* outputs)
1645 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1646 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1647 const bool hasZeroSign = supportsSignedZero(precision);
1648 const int scalarSize = glu::getDataTypeScalarSize(type);
1650 if (precision == glu::PRECISION_HIGHP || precision == glu::PRECISION_MEDIUMP)
1652 // Require exact result.
1653 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1655 const float in0 = ((const float*)inputs[0])[compNdx];
1656 const float out0 = ((const float*)outputs[0])[compNdx];
1657 const float ref = deFloatFrac(in0);
1659 const deUint32 ulpDiff = hasZeroSign ? getUlpDiff(out0, ref) : getUlpDiffIgnoreZeroSign(out0, ref);
1663 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref) << ", got ULP diff " << tcu::toHex(ulpDiff);
1670 const int mantissaBits = getMinMantissaBits(precision);
1671 const float eps = getEpsFromBits(1.0f, mantissaBits); // epsilon for rounding bounds
1673 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1675 const float in0 = ((const float*)inputs[0])[compNdx];
1676 const float out0 = ((const float*)outputs[0])[compNdx];
1678 if (int(deFloatFloor(in0-eps)) == int(deFloatFloor(in0+eps)))
1680 const float ref = deFloatFrac(in0);
1681 const int bitsLost = numBitsLostInOp(in0, ref);
1682 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(de::max(0, mantissaBits-bitsLost)); // ULP diff for rounded integer value.
1683 const deUint32 ulpDiff = getUlpDiffIgnoreZeroSign(out0, ref);
1685 if (ulpDiff > maxUlpDiff)
1687 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(ref) << " with ULP threshold " << tcu::toHex(maxUlpDiff) << ", got diff " << tcu::toHex(ulpDiff);
1695 m_failMsg << "Expected [" << compNdx << "] < 1.0";
1706 static inline void frexp (float in, float* significand, int* exponent)
1708 const tcu::Float32 fpValue(in);
1710 if (!fpValue.isZero())
1712 // Construct float that has exactly the mantissa, and exponent of -1.
1713 *significand = tcu::Float32::construct(fpValue.sign(), -1, fpValue.mantissa()).asFloat();
1714 *exponent = fpValue.exponent()+1;
1718 *significand = fpValue.sign() < 0 ? -0.0f : 0.0f;
1723 static inline float ldexp (float significand, int exponent)
1725 const tcu::Float32 mant(significand);
1727 if (exponent == 0 && mant.isZero())
1729 return mant.sign() < 0 ? -0.0f : 0.0f;
1733 return tcu::Float32::construct(mant.sign(), exponent+mant.exponent(), mant.mantissa()).asFloat();
1737 class FrexpCase : public CommonFunctionCase
1740 FrexpCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1741 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "frexp", shaderType)
1743 const int vecSize = glu::getDataTypeScalarSize(baseType);
1744 const glu::DataType intType = vecSize > 1 ? glu::getDataTypeIntVec(vecSize) : glu::TYPE_INT;
1746 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1747 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, glu::PRECISION_HIGHP)));
1748 m_spec.outputs.push_back(Symbol("out1", glu::VarType(intType, glu::PRECISION_HIGHP)));
1749 m_spec.source = "out0 = frexp(in0, out1);";
1752 void getInputValues (int numValues, void* const* values) const
1754 const Vec2 ranges[] =
1756 Vec2(-2.0f, 2.0f), // lowp
1757 Vec2(-1e3f, 1e3f), // mediump
1758 Vec2(-1e7f, 1e7f) // highp
1761 de::Random rnd (deStringHash(getName()) ^ 0x2790au);
1762 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1763 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1764 const int scalarSize = glu::getDataTypeScalarSize(type);
1767 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1769 ((float*)values[0])[scalarSize*0 + compNdx] = 0.0f;
1770 ((float*)values[0])[scalarSize*1 + compNdx] = -0.0f;
1771 ((float*)values[0])[scalarSize*2 + compNdx] = 0.5f;
1772 ((float*)values[0])[scalarSize*3 + compNdx] = -0.5f;
1773 ((float*)values[0])[scalarSize*4 + compNdx] = 1.0f;
1774 ((float*)values[0])[scalarSize*5 + compNdx] = -1.0f;
1775 ((float*)values[0])[scalarSize*6 + compNdx] = 2.0f;
1776 ((float*)values[0])[scalarSize*7 + compNdx] = -2.0f;
1779 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[0] + 8*scalarSize, (numValues-8)*scalarSize);
1781 // Make sure the values are representable in the target format
1782 for (int caseNdx = 0; caseNdx < numValues; ++caseNdx)
1784 for (int scalarNdx = 0; scalarNdx < scalarSize; scalarNdx++)
1786 float* const valuePtr = &((float*)values[0])[caseNdx * scalarSize + scalarNdx];
1788 *valuePtr = makeFloatRepresentable(*valuePtr, precision);
1793 bool compare (const void* const* inputs, const void* const* outputs)
1795 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1796 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1797 const int scalarSize = glu::getDataTypeScalarSize(type);
1798 const bool signedZero = false;
1800 const int mantissaBits = getMinMantissaBits(precision);
1801 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits);
1803 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1805 const float in0 = ((const float*)inputs[0])[compNdx];
1806 const float out0 = ((const float*)outputs[0])[compNdx];
1807 const int out1 = ((const int*)outputs[1])[compNdx];
1812 frexp(in0, &refOut0, &refOut1);
1814 const deUint32 ulpDiff0 = signedZero ? getUlpDiff(out0, refOut0) : getUlpDiffIgnoreZeroSign(out0, refOut0);
1816 if (ulpDiff0 > maxUlpDiff || out1 != refOut1)
1818 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(refOut0) << ", " << refOut1 << " with ULP threshold "
1819 << tcu::toHex(maxUlpDiff) << ", got ULP diff " << tcu::toHex(ulpDiff0);
1828 class LdexpCase : public CommonFunctionCase
1831 LdexpCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1832 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "ldexp", shaderType)
1834 const int vecSize = glu::getDataTypeScalarSize(baseType);
1835 const glu::DataType intType = vecSize > 1 ? glu::getDataTypeIntVec(vecSize) : glu::TYPE_INT;
1837 m_spec.inputs.push_back(Symbol("in0", glu::VarType(baseType, precision)));
1838 m_spec.inputs.push_back(Symbol("in1", glu::VarType(intType, glu::PRECISION_HIGHP)));
1839 m_spec.outputs.push_back(Symbol("out0", glu::VarType(baseType, glu::PRECISION_HIGHP)));
1840 m_spec.source = "out0 = ldexp(in0, in1);";
1843 void getInputValues (int numValues, void* const* values) const
1845 const Vec2 ranges[] =
1847 Vec2(-2.0f, 2.0f), // lowp
1848 Vec2(-1e3f, 1e3f), // mediump
1849 Vec2(-1e7f, 1e7f) // highp
1852 de::Random rnd (deStringHash(getName()) ^ 0x2790au);
1853 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1854 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1855 const int scalarSize = glu::getDataTypeScalarSize(type);
1859 const float easySpecialCases[] = { 0.0f, -0.0f, 0.5f, -0.5f, 1.0f, -1.0f, 2.0f, -2.0f };
1861 DE_ASSERT(valueNdx + DE_LENGTH_OF_ARRAY(easySpecialCases) <= numValues);
1862 for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(easySpecialCases); caseNdx++)
1867 frexp(easySpecialCases[caseNdx], &in0, &in1);
1869 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1871 ((float*)values[0])[valueNdx*scalarSize + compNdx] = in0;
1872 ((int*)values[1])[valueNdx*scalarSize + compNdx] = in1;
1880 // \note lowp and mediump can not necessarily fit the values in hard cases, so we'll use only easy ones.
1881 const int numEasyRandomCases = precision == glu::PRECISION_HIGHP ? 50 : (numValues-valueNdx);
1883 DE_ASSERT(valueNdx + numEasyRandomCases <= numValues);
1884 for (int caseNdx = 0; caseNdx < numEasyRandomCases; caseNdx++)
1886 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1888 const float in = rnd.getFloat(ranges[precision].x(), ranges[precision].y());
1892 frexp(in, &in0, &in1);
1894 ((float*)values[0])[valueNdx*scalarSize + compNdx] = in0;
1895 ((int*)values[1])[valueNdx*scalarSize + compNdx] = in1;
1903 const int numHardRandomCases = numValues-valueNdx;
1904 DE_ASSERT(numHardRandomCases >= 0 && valueNdx + numHardRandomCases <= numValues);
1906 for (int caseNdx = 0; caseNdx < numHardRandomCases; caseNdx++)
1908 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1910 const int fpExp = rnd.getInt(-126, 127);
1911 const int sign = rnd.getBool() ? -1 : +1;
1912 const deUint32 mantissa = (1u<<23) | (rnd.getUint32() & ((1u<<23)-1));
1913 const int in1 = rnd.getInt(de::max(-126, -126-fpExp), de::min(127, 127-fpExp));
1914 const float in0 = tcu::Float32::construct(sign, fpExp, mantissa).asFloat();
1916 DE_ASSERT(de::inRange(in1, -126, 127)); // See Khronos bug 11180
1917 DE_ASSERT(de::inRange(in1+fpExp, -126, 127));
1919 const float out = ldexp(in0, in1);
1921 DE_ASSERT(!tcu::Float32(out).isInf() && !tcu::Float32(out).isDenorm());
1924 ((float*)values[0])[valueNdx*scalarSize + compNdx] = in0;
1925 ((int*)values[1])[valueNdx*scalarSize + compNdx] = in1;
1933 bool compare (const void* const* inputs, const void* const* outputs)
1935 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
1936 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
1937 const int scalarSize = glu::getDataTypeScalarSize(type);
1939 const int mantissaBits = getMinMantissaBits(precision);
1940 const deUint32 maxUlpDiff = getMaxUlpDiffFromBits(mantissaBits);
1942 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
1944 const float in0 = ((const float*)inputs[0])[compNdx];
1945 const int in1 = ((const int*)inputs[1])[compNdx];
1946 const float out0 = ((const float*)outputs[0])[compNdx];
1947 const float refOut0 = ldexp(in0, in1);
1948 const deUint32 ulpDiff = getUlpDiffIgnoreZeroSign(out0, refOut0);
1950 const int inExp = tcu::Float32(in0).exponent();
1952 if (ulpDiff > maxUlpDiff)
1954 m_failMsg << "Expected [" << compNdx << "] = " << HexFloat(refOut0) << ", (exp = " << inExp << ") with ULP threshold "
1955 << tcu::toHex(maxUlpDiff) << ", got ULP diff " << tcu::toHex(ulpDiff);
1964 class FmaCase : public CommonFunctionCase
1967 FmaCase (Context& context, glu::DataType baseType, glu::Precision precision, glu::ShaderType shaderType)
1968 : CommonFunctionCase(context, getCommonFuncCaseName(baseType, precision, shaderType).c_str(), "fma", shaderType)
1970 m_spec.inputs.push_back(Symbol("a", glu::VarType(baseType, precision)));
1971 m_spec.inputs.push_back(Symbol("b", glu::VarType(baseType, precision)));
1972 m_spec.inputs.push_back(Symbol("c", glu::VarType(baseType, precision)));
1973 m_spec.outputs.push_back(Symbol("res", glu::VarType(baseType, precision)));
1974 m_spec.source = "res = fma(a, b, c);";
1976 if (!glu::contextSupports(context.getRenderContext().getType(), glu::ApiType::es(3, 2)))
1977 m_spec.globalDeclarations = "#extension GL_EXT_gpu_shader5 : require\n";
1982 if (!glu::contextSupports(m_context.getRenderContext().getType(), glu::ApiType::es(3, 2))
1983 && !m_context.getContextInfo().isExtensionSupported("GL_EXT_gpu_shader5"))
1984 throw tcu::NotSupportedError("GL_EXT_gpu_shader5 not supported");
1986 CommonFunctionCase::init();
1989 void getInputValues (int numValues, void* const* values) const
1991 const Vec2 ranges[] =
1993 Vec2(-2.0f, 2.0f), // lowp
1994 Vec2(-127.f, 127.f), // mediump
1995 Vec2(-1e7f, 1e7f) // highp
1998 de::Random rnd (deStringHash(getName()) ^ 0xac23fu);
1999 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
2000 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
2001 const int scalarSize = glu::getDataTypeScalarSize(type);
2002 const float specialCases[][3] =
2005 { 0.0f, 0.0f, 0.0f },
2006 { 0.0f, 1.0f, 0.0f },
2007 { 0.0f, 0.0f, -1.0f },
2008 { 1.0f, 1.0f, 0.0f },
2009 { 1.0f, 1.0f, 1.0f },
2010 { -1.0f, 1.0f, 0.0f },
2011 { 1.0f, -1.0f, 0.0f },
2012 { -1.0f, -1.0f, 0.0f },
2013 { -0.0f, 1.0f, 0.0f },
2014 { 1.0f, -0.0f, 0.0f }
2016 const int numSpecialCases = DE_LENGTH_OF_ARRAY(specialCases);
2019 for (int caseNdx = 0; caseNdx < numSpecialCases; caseNdx++)
2021 for (int inputNdx = 0; inputNdx < 3; inputNdx++)
2023 for (int scalarNdx = 0; scalarNdx < scalarSize; scalarNdx++)
2024 ((float*)values[inputNdx])[caseNdx*scalarSize + scalarNdx] = specialCases[caseNdx][inputNdx];
2030 const int numScalars = (numValues-numSpecialCases)*scalarSize;
2031 const int offs = scalarSize*numSpecialCases;
2033 for (int inputNdx = 0; inputNdx < 3; inputNdx++)
2034 fillRandomScalars(rnd, ranges[precision].x(), ranges[precision].y(), (float*)values[inputNdx] + offs, numScalars);
2037 // Make sure the values are representable in the target format
2038 for (int inputNdx = 0; inputNdx < 3; inputNdx++)
2040 for (int caseNdx = 0; caseNdx < numValues; ++caseNdx)
2042 for (int scalarNdx = 0; scalarNdx < scalarSize; scalarNdx++)
2044 float* const valuePtr = &((float*)values[inputNdx])[caseNdx * scalarSize + scalarNdx];
2046 *valuePtr = makeFloatRepresentable(*valuePtr, precision);
2052 static tcu::Interval fma (glu::Precision precision, float a, float b, float c)
2054 const tcu::FloatFormat formats[] =
2056 // minExp maxExp mantissa exact, subnormals infinities NaN
2057 tcu::FloatFormat(0, 0, 7, false, tcu::YES, tcu::MAYBE, tcu::MAYBE),
2058 tcu::FloatFormat(-13, 13, 9, false, tcu::MAYBE, tcu::MAYBE, tcu::MAYBE),
2059 tcu::FloatFormat(-126, 127, 23, true, tcu::MAYBE, tcu::YES, tcu::MAYBE)
2061 const tcu::FloatFormat& format = de::getSizedArrayElement<glu::PRECISION_LAST>(formats, precision);
2062 const tcu::Interval ia = format.convert(a);
2063 const tcu::Interval ib = format.convert(b);
2064 const tcu::Interval ic = format.convert(c);
2065 tcu::Interval prod0;
2066 tcu::Interval prod1;
2067 tcu::Interval prod2;
2068 tcu::Interval prod3;
2072 TCU_SET_INTERVAL(prod0, tmp, tmp = ia.lo() * ib.lo());
2073 TCU_SET_INTERVAL(prod1, tmp, tmp = ia.lo() * ib.hi());
2074 TCU_SET_INTERVAL(prod2, tmp, tmp = ia.hi() * ib.lo());
2075 TCU_SET_INTERVAL(prod3, tmp, tmp = ia.hi() * ib.hi());
2077 prod = format.convert(format.roundOut(prod0 | prod1 | prod2 | prod3, ia.isFinite() && ib.isFinite()));
2079 TCU_SET_INTERVAL_BOUNDS(res, tmp,
2080 tmp = prod.lo() + ic.lo(),
2081 tmp = prod.hi() + ic.hi());
2083 return format.convert(format.roundOut(res, prod.isFinite() && ic.isFinite()));
2086 bool compare (const void* const* inputs, const void* const* outputs)
2088 const glu::DataType type = m_spec.inputs[0].varType.getBasicType();
2089 const glu::Precision precision = m_spec.inputs[0].varType.getPrecision();
2090 const int scalarSize = glu::getDataTypeScalarSize(type);
2092 for (int compNdx = 0; compNdx < scalarSize; compNdx++)
2094 const float a = ((const float*)inputs[0])[compNdx];
2095 const float b = ((const float*)inputs[1])[compNdx];
2096 const float c = ((const float*)inputs[2])[compNdx];
2097 const float res = ((const float*)outputs[0])[compNdx];
2098 const tcu::Interval ref = fma(precision, a, b, c);
2100 if (!ref.contains(res))
2102 m_failMsg << "Expected [" << compNdx << "] = " << ref;
2111 ShaderCommonFunctionTests::ShaderCommonFunctionTests (Context& context)
2112 : TestCaseGroup(context, "common", "Common function tests")
2116 ShaderCommonFunctionTests::~ShaderCommonFunctionTests (void)
2120 template<class TestClass>
2121 static void addFunctionCases (TestCaseGroup* parent, const char* functionName, bool floatTypes, bool intTypes, bool uintTypes, deUint32 shaderBits)
2123 tcu::TestCaseGroup* group = new tcu::TestCaseGroup(parent->getTestContext(), functionName, functionName);
2124 parent->addChild(group);
2126 const glu::DataType scalarTypes[] =
2133 for (int scalarTypeNdx = 0; scalarTypeNdx < DE_LENGTH_OF_ARRAY(scalarTypes); scalarTypeNdx++)
2135 const glu::DataType scalarType = scalarTypes[scalarTypeNdx];
2137 if ((!floatTypes && scalarType == glu::TYPE_FLOAT) ||
2138 (!intTypes && scalarType == glu::TYPE_INT) ||
2139 (!uintTypes && scalarType == glu::TYPE_UINT))
2142 for (int vecSize = 1; vecSize <= 4; vecSize++)
2144 for (int prec = glu::PRECISION_LOWP; prec <= glu::PRECISION_HIGHP; prec++)
2146 for (int shaderTypeNdx = 0; shaderTypeNdx < glu::SHADERTYPE_LAST; shaderTypeNdx++)
2148 if (shaderBits & (1<<shaderTypeNdx))
2149 group->addChild(new TestClass(parent->getContext(), glu::DataType(scalarType + vecSize - 1), glu::Precision(prec), glu::ShaderType(shaderTypeNdx)));
2156 void ShaderCommonFunctionTests::init (void)
2160 VS = (1<<glu::SHADERTYPE_VERTEX),
2161 TC = (1<<glu::SHADERTYPE_TESSELLATION_CONTROL),
2162 TE = (1<<glu::SHADERTYPE_TESSELLATION_EVALUATION),
2163 GS = (1<<glu::SHADERTYPE_GEOMETRY),
2164 FS = (1<<glu::SHADERTYPE_FRAGMENT),
2165 CS = (1<<glu::SHADERTYPE_COMPUTE),
2167 ALL_SHADERS = VS|TC|TE|GS|FS|CS,
2168 NEW_SHADERS = TC|TE|GS|CS,
2171 // Float? Int? Uint? Shaders
2172 addFunctionCases<AbsCase> (this, "abs", true, true, false, NEW_SHADERS);
2173 addFunctionCases<SignCase> (this, "sign", true, true, false, NEW_SHADERS);
2174 addFunctionCases<FloorCase> (this, "floor", true, false, false, NEW_SHADERS);
2175 addFunctionCases<TruncCase> (this, "trunc", true, false, false, NEW_SHADERS);
2176 addFunctionCases<RoundCase> (this, "round", true, false, false, NEW_SHADERS);
2177 addFunctionCases<RoundEvenCase> (this, "roundeven", true, false, false, NEW_SHADERS);
2178 addFunctionCases<CeilCase> (this, "ceil", true, false, false, NEW_SHADERS);
2179 addFunctionCases<FractCase> (this, "fract", true, false, false, NEW_SHADERS);
2181 addFunctionCases<ModfCase> (this, "modf", true, false, false, NEW_SHADERS);
2188 addFunctionCases<IsnanCase> (this, "isnan", true, false, false, NEW_SHADERS);
2189 addFunctionCases<IsinfCase> (this, "isinf", true, false, false, NEW_SHADERS);
2190 addFunctionCases<FloatBitsToIntCase> (this, "floatbitstoint", true, false, false, NEW_SHADERS);
2191 addFunctionCases<FloatBitsToUintCase> (this, "floatbitstouint", true, false, false, NEW_SHADERS);
2193 addFunctionCases<FrexpCase> (this, "frexp", true, false, false, ALL_SHADERS);
2194 addFunctionCases<LdexpCase> (this, "ldexp", true, false, false, ALL_SHADERS);
2195 addFunctionCases<FmaCase> (this, "fma", true, false, false, ALL_SHADERS);
2197 // (u)intBitsToFloat()
2199 const deUint32 shaderBits = NEW_SHADERS;
2200 tcu::TestCaseGroup* intGroup = new tcu::TestCaseGroup(m_testCtx, "intbitstofloat", "intBitsToFloat() Tests");
2201 tcu::TestCaseGroup* uintGroup = new tcu::TestCaseGroup(m_testCtx, "uintbitstofloat", "uintBitsToFloat() Tests");
2204 addChild(uintGroup);
2206 for (int vecSize = 1; vecSize < 4; vecSize++)
2208 const glu::DataType intType = vecSize > 1 ? glu::getDataTypeIntVec(vecSize) : glu::TYPE_INT;
2209 const glu::DataType uintType = vecSize > 1 ? glu::getDataTypeUintVec(vecSize) : glu::TYPE_UINT;
2211 for (int shaderType = 0; shaderType < glu::SHADERTYPE_LAST; shaderType++)
2213 if (shaderBits & (1<<shaderType))
2215 intGroup->addChild(new BitsToFloatCase(m_context, intType, glu::ShaderType(shaderType)));
2216 uintGroup->addChild(new BitsToFloatCase(m_context, uintType, glu::ShaderType(shaderType)));