Add deInt32ToFloatRoundToNegInf and deInt32ToFloatRoundToPosInf.

Bug: 21326686
Change-Id: Iace59b3e8ffd7fe88b75bc1801f57207807304c8
diff --git a/Android.mk b/Android.mk
index 9c27830..3a6e7a0 100644
--- a/Android.mk
+++ b/Android.mk
@@ -76,6 +76,7 @@
 	framework/delibs/debase/deInt32.c \
 	framework/delibs/debase/deInt32Test.c \
 	framework/delibs/debase/deMath.c \
+	framework/delibs/debase/deMathTest.c \
 	framework/delibs/debase/deMemory.c \
 	framework/delibs/debase/deRandom.c \
 	framework/delibs/debase/deString.c \
diff --git a/framework/delibs/debase/CMakeLists.txt b/framework/delibs/debase/CMakeLists.txt
index 6f18c88..b960bbc 100644
--- a/framework/delibs/debase/CMakeLists.txt
+++ b/framework/delibs/debase/CMakeLists.txt
@@ -14,6 +14,7 @@
 	deInt32Test.c
 	deMath.c
 	deMath.h
+	deMathTest.c
 	deMemory.c
 	deMemory.h
 	deRandom.c
diff --git a/framework/delibs/debase/deMath.c b/framework/delibs/debase/deMath.c
index 259286c..26e2aef 100644
--- a/framework/delibs/debase/deMath.c
+++ b/framework/delibs/debase/deMath.c
@@ -22,6 +22,7 @@
  *//*--------------------------------------------------------------------*/
 
 #include "deMath.h"
+#include "deInt32.h"
 
 #if (DE_COMPILER == DE_COMPILER_MSC)
 #	include <float.h>
@@ -133,3 +134,56 @@
 		return 2.0 * deRound(a / 2.0);
 	return deRound(a);
 }
+
+float deInt32ToFloatRoundToNegInf (deInt32 x)
+{
+	/* \note Sign bit is separate so the range is symmetric */
+	if (x >= -0xFFFFFF && x <= 0xFFFFFF)
+	{
+		/* 24 bits are representable (23 mantissa + 1 implicit). */
+		return (float)x;
+	}
+	else if (x != -0x7FFFFFFF - 1)
+	{
+		/* we are losing bits */
+		const int		exponent	= 31 - deClz32((deUint32)deAbs32(x));
+		const int		numLostBits	= exponent - 23;
+		const deUint32	lostMask	= deBitMask32(0, numLostBits);
+
+		DE_ASSERT(numLostBits > 0);
+
+		if (x > 0)
+		{
+			/* Mask out lost bits to floor to a representable value */
+			return (float)(deInt32)(~lostMask & (deUint32)x);
+		}
+		else if ((lostMask & (deUint32)-x) == 0u)
+		{
+			/* this was a representable value */
+			DE_ASSERT( (deInt32)(float)x == x );
+			return (float)x;
+		}
+		else
+		{
+			/* not representable, choose the next lower */
+			const float nearestHigher	= (float)-(deInt32)(~lostMask & (deUint32)-x);
+			const float oneUlp			= (float)(1u << (deUint32)numLostBits);
+			const float nearestLower	= nearestHigher - oneUlp;
+
+			/* check sanity */
+			DE_ASSERT((deInt32)(float)nearestHigher > (deInt32)(float)nearestLower);
+
+			return nearestLower;
+		}
+	}
+	else
+		return -(float)0x80000000u;
+}
+
+float deInt32ToFloatRoundToPosInf (deInt32 x)
+{
+	if (x == -0x7FFFFFFF - 1)
+		return -(float)0x80000000u;
+	else
+		return -deInt32ToFloatRoundToNegInf(-x);
+}
diff --git a/framework/delibs/debase/deMath.h b/framework/delibs/debase/deMath.h
index b6f5487..12b25a5 100644
--- a/framework/delibs/debase/deMath.h
+++ b/framework/delibs/debase/deMath.h
@@ -56,6 +56,8 @@
 deRoundingMode		deGetRoundingMode	(void);
 deBool				deSetRoundingMode	(deRoundingMode mode);
 
+void				deMath_selfTest		(void);
+
 /* Float properties */
 
 /* \note The NaN test probably won't work with -ffast-math */
@@ -186,11 +188,27 @@
 DE_INLINE deBool	deFloatCmpGT		(float a, float b)			{ return (a > b);  }
 DE_INLINE deBool	deFloatCmpGE		(float a, float b)			{ return (a >= b); }
 
+/* Convert int to float. If the value cannot be represented exactly in native single precision format, return
+ * either the nearest lower or the nearest higher representable value, chosen in an implementation-defined manner.
+ *
+ * \note Choosing either nearest lower or nearest higher means that implementation could for example consistently
+ *       choose the lower value, i.e. this function does not round towards nearest.
+ * \note Value returned is in native single precision format. For example with x86 extended precision, the value
+ *       returned might not be representable in IEEE single precision float.
+ */
+DE_INLINE float		deInt32ToFloat				(deInt32 x)					{ return (float)x; }
+
+/* Convert to float. If the value cannot be represented exactly in IEEE single precision floating point format,
+ * return the nearest lower (round towards negative inf). */
+float				deInt32ToFloatRoundToNegInf	(deInt32 x);
+
+/* Convert to float. If the value cannot be represented exactly IEEE single precision floating point format,
+ * return the nearest higher (round towards positive inf). */
+float				deInt32ToFloatRoundToPosInf	(deInt32 x);
+
 /* Conversion to integer. */
 
-DE_INLINE float		deInt32ToFloat		(deInt32 x)					{ return (float)x; }
 DE_INLINE deInt32	deChopFloatToInt32	(float x)					{ return (deInt32)x; }
-
 DE_INLINE deInt32	deFloorFloatToInt32	(float x)					{ return (deInt32)(deFloatFloor(x)); }
 DE_INLINE deInt32	deCeilFloatToInt32	(float x)					{ return (deInt32)(deFloatCeil(x)); }
 
diff --git a/framework/delibs/debase/deMathTest.c b/framework/delibs/debase/deMathTest.c
new file mode 100644
index 0000000..d8e73a7
--- /dev/null
+++ b/framework/delibs/debase/deMathTest.c
@@ -0,0 +1,107 @@
+/*-------------------------------------------------------------------------
+ * drawElements Base Portability Library
+ * -------------------------------------
+ *
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *//*!
+ * \file
+ * \brief Testing of deMath functions.
+ *//*--------------------------------------------------------------------*/
+
+#include "deMath.h"
+#include "deRandom.h"
+
+DE_BEGIN_EXTERN_C
+
+static deBool conversionToFloatLosesPrecision (deInt32 x)
+{
+	if (x == -0x7FFFFFFF - 1)
+		return DE_FALSE;
+	else if (x < 0)
+		return conversionToFloatLosesPrecision(-x);
+	else if (x == 0)
+		return DE_FALSE;
+	else if (((deUint32)x & 0x1) == 0)
+		return conversionToFloatLosesPrecision(x >> 1); /* remove trailing zeros */
+	else
+		return x > ((1 << 24) - 1); /* remaining part does not fit in the mantissa? */
+}
+
+static void testSingleInt32ToFloat (deInt32 x)
+{
+	/* roundTowardsToNegInf(x) <= round(x) <= roundTowardsPosInf(x). */
+	/* \note: Need to use inequalities since round(x) returns arbitrary precision floats. */
+	DE_TEST_ASSERT(deInt32ToFloatRoundToNegInf(x) <= deInt32ToFloat(x));
+	DE_TEST_ASSERT(deInt32ToFloat(x) <= deInt32ToFloatRoundToPosInf(x));
+
+	/* if precision is lost, floor(x) < ceil(x). Else floor(x) == ceil(x) */
+	if (conversionToFloatLosesPrecision(x))
+		DE_TEST_ASSERT(deInt32ToFloatRoundToNegInf(x) < deInt32ToFloatRoundToPosInf(x));
+	else
+		DE_TEST_ASSERT(deInt32ToFloatRoundToNegInf(x) == deInt32ToFloatRoundToPosInf(x));
+
+	/* max one ulp from each other */
+	if (deInt32ToFloatRoundToNegInf(x) < deInt32ToFloatRoundToPosInf(x))
+	{
+		union
+		{
+			float f;
+			deInt32 u;
+		} v0, v1;
+
+		v0.f = deInt32ToFloatRoundToNegInf(x);
+		v1.f = deInt32ToFloatRoundToPosInf(x);
+
+		DE_TEST_ASSERT(v0.u + 1 == v1.u || v0.u == v1.u + 1);
+	}
+}
+
+static void testInt32ToFloat (void)
+{
+	const int	numIterations = 2500000;
+
+	int			sign;
+	int			numBits;
+	int			delta;
+	int			ndx;
+	deRandom	rnd;
+
+	deRandom_init(&rnd, 0xdeadbeefu-1);
+
+	for (sign = -1; sign < 1; ++sign)
+	for (numBits = 0; numBits < 32; ++numBits)
+	for (delta = -2; delta < 3; ++delta)
+	{
+		const deInt64 x = (deInt64)(sign == -1 ? (-1) : (+1)) * (1LL << (deInt64)numBits) + (deInt64)delta;
+
+		/* would overflow */
+		if (x > 0x7FFFFFFF || x < -0x7FFFFFFF - 1)
+			continue;
+
+		testSingleInt32ToFloat((deInt32)x);
+	}
+
+	for (ndx = 0; ndx < numIterations; ++ndx)
+		testSingleInt32ToFloat((deInt32)deRandom_getUint32(&rnd));
+}
+
+void deMath_selfTest (void)
+{
+	/* Test Int32ToFloat*(). */
+	testInt32ToFloat();
+}
+
+DE_END_EXTERN_C
diff --git a/modules/internal/ditDelibsTests.cpp b/modules/internal/ditDelibsTests.cpp
index f2bc06c..5f58c00 100644
--- a/modules/internal/ditDelibsTests.cpp
+++ b/modules/internal/ditDelibsTests.cpp
@@ -43,6 +43,7 @@
 
 // debase
 #include "deInt32.h"
+#include "deMath.h"
 
 // decpp
 #include "deBlockBuffer.hpp"
@@ -156,6 +157,7 @@
 	void init (void)
 	{
 		addChild(new SelfCheckCase(m_testCtx, "int32",	"deInt32_selfTest()",	deInt32_selfTest));
+		addChild(new SelfCheckCase(m_testCtx, "math",	"deMath_selfTest()",	deMath_selfTest));
 	}
 };