Make random number usage platform independent
[platform/upstream/VK-GL-CTS.git] / framework / common / tcuVectorUtil.hpp
index de17642..0797463 100644 (file)
@@ -25,6 +25,8 @@
 
 #include "tcuDefs.hpp"
 #include "tcuVector.hpp"
+#include "deRandom.hpp"
+#include "deMeta.hpp"
 #include "deMath.h"
 #include "deInt32.h"
 
@@ -36,7 +38,7 @@ namespace tcu
 
 static const float PI = 3.141592653589793238f;
 
-#if (DE_OS == DE_OS_ANDROID) || (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_WIN32 && DE_COMPILER == DE_COMPILER_CLANG)
+#if (DE_OS == DE_OS_ANDROID) || (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_QNX) || (DE_OS == DE_OS_WIN32 && DE_COMPILER == DE_COMPILER_CLANG)
 inline float abs                       (float f) { return deFloatAbs(f); }
 #endif
 
@@ -55,15 +57,13 @@ template<typename T> inline T logicalAnd    (T a, T b)      { return a && b; }
 template<typename T> inline T logicalOr                (T a, T b)      { return a || b; }
 
 template<typename T>   inline T                mod             (T a, T b)                      { return a % b; }
-template<>                             inline float    mod             (float x, float y)      { return x - y * floor(x / y); }
+template<>                             inline float    mod             (float x, float y)      { return x - y * deFloatFloor(x / y); }
 
 template<typename T>   inline  T                       negate                          (T f)                   { return -f; }
 template<>                             inline  deUint32        negate<deUint32>        (deUint32 f)    { return (deUint32)-(int)f;     }
 
 inline float radians           (float f) { return deFloatRadians(f); }
 inline float degrees           (float f) { return deFloatDegrees(f); }
-inline float exp2                      (float f) { return deFloatExp2(f); }
-inline float log2                      (float f) { return deFloatLog2(f); }
 inline float inverseSqrt       (float f) { return deFloatRsq(f); }
 inline float sign                      (float f) { return (f < 0.0f) ? -1.0f : ((f > 0.0f) ? +1.0f : 0.0f); }
 inline float fract                     (float f) { return f - deFloatFloor(f); }
@@ -94,13 +94,9 @@ inline float refract         (float i, float n, float eta)
        if (k < 0.0f)
                return 0.0f;
        else
-               return eta * i - (eta * cosAngle + ::sqrt(k)) * n;
+               return eta * i - (eta * cosAngle + deFloatSqrt(k)) * n;
 }
 
-inline float asinh                     (float a) { return deFloatAsinh(a); }
-inline float acosh                     (float a) { return deFloatAcosh(a); }
-inline float atanh                     (float a) { return deFloatAtanh(a); }
-
 template<typename T> inline bool       lessThan                        (T a, T b) { return (a < b); }
 template<typename T> inline bool       lessThanEqual           (T a, T b) { return (a <= b); }
 template<typename T> inline bool       greaterThan                     (T a, T b) { return (a > b); }
@@ -114,8 +110,6 @@ inline bool boolNot                         (bool a) { return !a; }
 
 inline int chopToInt                   (float a) { return deChopFloatToInt32(a); }
 
-inline float trunc                             (float a) { return (float)chopToInt(a); }
-
 inline float roundToEven (float a)
 {
        float q = deFloatFrac(a);
@@ -148,12 +142,18 @@ inline T lengthSquared (const Vector<T, Size>& a)
 }
 
 template <typename T, int Size>
-inline T length (const Vector<T, Size>& a)
+inline typename de::meta::EnableIf<T, de::meta::TypesSame<T, double>::Value>::Type length (const Vector<T, Size>& a)
 {
        return ::sqrt(lengthSquared(a));
 }
 
 template <typename T, int Size>
+inline typename de::meta::EnableIf<T, de::meta::TypesSame<T, float>::Value>::Type length (const Vector<T, Size>& a)
+{
+       return deFloatSqrt(lengthSquared(a));
+}
+
+template <typename T, int Size>
 inline T distance (const Vector<T, Size>& a, const Vector<T, Size>& b)
 {
        return length(a - b);
@@ -369,6 +369,32 @@ static inline Vector<T, Size> absDiff (const Vector<T, Size>& a, const Vector<T,
        return res;
 }
 
+template<typename T, int Size>
+inline tcu::Vector<T, Size> randomVector (de::Random& rnd, const tcu::Vector<T, Size>& minValue, const tcu::Vector<T, Size>& maxValue)
+{
+       tcu::Vector<T, Size> res;
+
+       for (int ndx = 0; ndx < Size; ndx++)
+               res[ndx] = de::randomScalar<T>(rnd, minValue[ndx], maxValue[ndx]);
+
+       return res;
+}
+
+inline Vector<float, 2> randomVec2 (de::Random& rnd)
+{
+       return randomVector<float, 2>(rnd, tcu::Vector<float, 2>(0.0f), tcu::Vector<float, 2>(1.0f));
+}
+
+inline Vector<float, 3> randomVec3 (de::Random& rnd)
+{
+       return randomVector<float, 3>(rnd, tcu::Vector<float, 3>(0.0f), tcu::Vector<float, 3>(1.0f));
+}
+
+inline Vector<float, 4> randomVec4 (de::Random& rnd)
+{
+       return randomVector<float, 4>(rnd, tcu::Vector<float, 4>(0.0f), tcu::Vector<float, 4>(1.0f));
+}
+
 // Macros for component-wise ops.
 
 #define TCU_DECLARE_VECTOR_UNARY_FUNC(FUNC_NAME, OP_NAME)      \
@@ -450,7 +476,7 @@ TCU_DECLARE_VECTOR_UNARY_FUNC(inverseSqrt, deFloatRsq)
 TCU_DECLARE_VECTOR_UNARY_FUNC(abs, de::abs)
 TCU_DECLARE_VECTOR_UNARY_FUNC(sign, deFloatSign)
 TCU_DECLARE_VECTOR_UNARY_FUNC(floor, deFloatFloor)
-TCU_DECLARE_VECTOR_UNARY_FUNC(trunc, trunc)
+TCU_DECLARE_VECTOR_UNARY_FUNC(trunc, deFloatTrunc)
 TCU_DECLARE_VECTOR_UNARY_FUNC(roundToEven, roundToEven)
 TCU_DECLARE_VECTOR_UNARY_FUNC(ceil, deFloatCeil)
 TCU_DECLARE_VECTOR_UNARY_FUNC(fract, deFloatFrac)