Home | History | Annotate | Download | only in debase
      1 /*-------------------------------------------------------------------------
      2  * drawElements Base Portability Library
      3  * -------------------------------------
      4  *
      5  * Copyright 2015 The Android Open Source Project
      6  *
      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
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     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.
     18  *
     19  *//*!
     20  * \file
     21  * \brief Testing of deMath functions.
     22  *//*--------------------------------------------------------------------*/
     23 
     24 #include "deMath.h"
     25 #include "deRandom.h"
     26 
     27 DE_BEGIN_EXTERN_C
     28 
     29 static deBool conversionToFloatLosesPrecision (deInt32 x)
     30 {
     31 	if (x == -0x7FFFFFFF - 1)
     32 		return DE_FALSE;
     33 	else if (x < 0)
     34 		return conversionToFloatLosesPrecision(-x);
     35 	else if (x == 0)
     36 		return DE_FALSE;
     37 	else if (((deUint32)x & 0x1) == 0)
     38 		return conversionToFloatLosesPrecision(x >> 1); /* remove trailing zeros */
     39 	else
     40 		return x > ((1 << 24) - 1); /* remaining part does not fit in the mantissa? */
     41 }
     42 
     43 static void testSingleInt32ToFloat (deInt32 x)
     44 {
     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));
     49 
     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));
     53 	else
     54 		DE_TEST_ASSERT(deInt32ToFloatRoundToNegInf(x) == deInt32ToFloatRoundToPosInf(x));
     55 
     56 	/* max one ulp from each other */
     57 	if (deInt32ToFloatRoundToNegInf(x) < deInt32ToFloatRoundToPosInf(x))
     58 	{
     59 		union
     60 		{
     61 			float f;
     62 			deInt32 u;
     63 		} v0, v1;
     64 
     65 		v0.f = deInt32ToFloatRoundToNegInf(x);
     66 		v1.f = deInt32ToFloatRoundToPosInf(x);
     67 
     68 		DE_TEST_ASSERT(v0.u + 1 == v1.u || v0.u == v1.u + 1);
     69 	}
     70 }
     71 
     72 static void testInt32ToFloat (void)
     73 {
     74 	const int	numIterations = 2500000;
     75 
     76 	int			sign;
     77 	int			numBits;
     78 	int			delta;
     79 	int			ndx;
     80 	deRandom	rnd;
     81 
     82 	deRandom_init(&rnd, 0xdeadbeefu-1);
     83 
     84 	for (sign = -1; sign < 1; ++sign)
     85 	for (numBits = 0; numBits < 32; ++numBits)
     86 	for (delta = -2; delta < 3; ++delta)
     87 	{
     88 		const deInt64 x = (deInt64)(sign == -1 ? (-1) : (+1)) * (1LL << (deInt64)numBits) + (deInt64)delta;
     89 
     90 		/* would overflow */
     91 		if (x > 0x7FFFFFFF || x < -0x7FFFFFFF - 1)
     92 			continue;
     93 
     94 		testSingleInt32ToFloat((deInt32)x);
     95 	}
     96 
     97 	for (ndx = 0; ndx < numIterations; ++ndx)
     98 		testSingleInt32ToFloat((deInt32)deRandom_getUint32(&rnd));
     99 }
    100 
    101 void deMath_selfTest (void)
    102 {
    103 	/* Test Int32ToFloat*(). */
    104 	testInt32ToFloat();
    105 }
    106 
    107 DE_END_EXTERN_C
    108