1 /*-------------------------------------------------------------------------
2 * drawElements Base Portability Library
3 * -------------------------------------
5 * Copyright 2015 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 Testing of deMath functions.
22 *//*--------------------------------------------------------------------*/
29 static deBool conversionToFloatLosesPrecision (deInt32 x)
31 if (x == -0x7FFFFFFF - 1)
34 return conversionToFloatLosesPrecision(-x);
37 else if (((deUint32)x & 0x1) == 0)
38 return conversionToFloatLosesPrecision(x >> 1); /* remove trailing zeros */
40 return x > ((1 << 24) - 1); /* remaining part does not fit in the mantissa? */
43 static void testSingleInt32ToFloat (deInt32 x)
45 /* roundTowardsToNegInf(x) <= round(x) <= roundTowardsPosInf(x). */
46 /* \note: Need to use inequalities since round(x) returns arbitrary precision floats. */
47 DE_TEST_ASSERT(deInt32ToFloatRoundToNegInf(x) <= deInt32ToFloat(x));
48 DE_TEST_ASSERT(deInt32ToFloat(x) <= deInt32ToFloatRoundToPosInf(x));
50 /* if precision is lost, floor(x) < ceil(x). Else floor(x) == ceil(x) */
51 if (conversionToFloatLosesPrecision(x))
52 DE_TEST_ASSERT(deInt32ToFloatRoundToNegInf(x) < deInt32ToFloatRoundToPosInf(x));
54 DE_TEST_ASSERT(deInt32ToFloatRoundToNegInf(x) == deInt32ToFloatRoundToPosInf(x));
56 /* max one ulp from each other */
57 if (deInt32ToFloatRoundToNegInf(x) < deInt32ToFloatRoundToPosInf(x))
65 v0.f = deInt32ToFloatRoundToNegInf(x);
66 v1.f = deInt32ToFloatRoundToPosInf(x);
68 DE_TEST_ASSERT(v0.u + 1 == v1.u || v0.u == v1.u + 1);
72 static void testInt32ToFloat (void)
74 const int numIterations = 2500000;
82 deRandom_init(&rnd, 0xdeadbeefu-1);
84 for (sign = -1; sign < 1; ++sign)
85 for (numBits = 0; numBits < 32; ++numBits)
86 for (delta = -2; delta < 3; ++delta)
88 const deInt64 x = (deInt64)(sign == -1 ? (-1) : (+1)) * (1LL << (deInt64)numBits) + (deInt64)delta;
91 if (x > 0x7FFFFFFF || x < -0x7FFFFFFF - 1)
94 testSingleInt32ToFloat((deInt32)x);
97 for (ndx = 0; ndx < numIterations; ++ndx)
98 testSingleInt32ToFloat((deInt32)deRandom_getUint32(&rnd));
101 void deMath_selfTest (void)
103 /* Test Int32ToFloat*(). */