1e5c31af7Sopenharmony_ci#ifndef _DEMATH_H
2e5c31af7Sopenharmony_ci#define _DEMATH_H
3e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
4e5c31af7Sopenharmony_ci * drawElements Base Portability Library
5e5c31af7Sopenharmony_ci * -------------------------------------
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
8e5c31af7Sopenharmony_ci *
9e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
10e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
11e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
14e5c31af7Sopenharmony_ci *
15e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
16e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
17e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
19e5c31af7Sopenharmony_ci * limitations under the License.
20e5c31af7Sopenharmony_ci *
21e5c31af7Sopenharmony_ci *//*!
22e5c31af7Sopenharmony_ci * \file
23e5c31af7Sopenharmony_ci * \brief Basic mathematical operations.
24e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
25e5c31af7Sopenharmony_ci
26e5c31af7Sopenharmony_ci#include "deDefs.h"
27e5c31af7Sopenharmony_ci#include "deMemory.h"
28e5c31af7Sopenharmony_ci
29e5c31af7Sopenharmony_ci#include <math.h>
30e5c31af7Sopenharmony_ci#include <float.h>
31e5c31af7Sopenharmony_ci
32e5c31af7Sopenharmony_ciDE_BEGIN_EXTERN_C
33e5c31af7Sopenharmony_ci
34e5c31af7Sopenharmony_ci/* Mathematical constants. */
35e5c31af7Sopenharmony_ci
36e5c31af7Sopenharmony_ci#define DE_PI			3.14159265358979324f	/*!< Pi.					*/
37e5c31af7Sopenharmony_ci#define DE_LOG_2		0.69314718056f			/*!< log_e(2.0)				*/
38e5c31af7Sopenharmony_ci#define DE_INV_LOG_2	1.44269504089f			/*!< 1.0 / log_e(2.0)		*/
39e5c31af7Sopenharmony_ci#define DE_E			2.71828182845904523536f	/*!< e.						*/
40e5c31af7Sopenharmony_ci#define DE_LOG2_E		1.44269504088896340736f	/*!< log_2(e).				*/
41e5c31af7Sopenharmony_ci#define DE_INV_LOG2_E	0.69314718055994530942f	/*!< 1.0 / log_2(e).		*/
42e5c31af7Sopenharmony_ci
43e5c31af7Sopenharmony_ci#define DE_PI_DOUBLE	3.14159265358979323846	/*!< Pi as a double.		*/
44e5c31af7Sopenharmony_ci#define DE_PI_16BIT		0x4248					/*!< Pi. as a float16b		*/
45e5c31af7Sopenharmony_ci
46e5c31af7Sopenharmony_ci/* Rounding mode control. */
47e5c31af7Sopenharmony_ci
48e5c31af7Sopenharmony_citypedef enum deRoundingMode_e
49e5c31af7Sopenharmony_ci{
50e5c31af7Sopenharmony_ci	DE_ROUNDINGMODE_TO_NEAREST_EVEN = 0,
51e5c31af7Sopenharmony_ci	DE_ROUNDINGMODE_TO_ZERO,
52e5c31af7Sopenharmony_ci	DE_ROUNDINGMODE_TO_POSITIVE_INF,
53e5c31af7Sopenharmony_ci	DE_ROUNDINGMODE_TO_NEGATIVE_INF,
54e5c31af7Sopenharmony_ci
55e5c31af7Sopenharmony_ci	DE_ROUNDINGMODE_LAST
56e5c31af7Sopenharmony_ci} deRoundingMode;
57e5c31af7Sopenharmony_ci
58e5c31af7Sopenharmony_cideRoundingMode		deGetRoundingMode	(void);
59e5c31af7Sopenharmony_cideBool				deSetRoundingMode	(deRoundingMode mode);
60e5c31af7Sopenharmony_ci
61e5c31af7Sopenharmony_civoid				deMath_selfTest		(void);
62e5c31af7Sopenharmony_ci
63e5c31af7Sopenharmony_ci/* Float properties */
64e5c31af7Sopenharmony_ci
65e5c31af7Sopenharmony_ci/* \note The NaN test probably won't work with -ffast-math */
66e5c31af7Sopenharmony_ci
67e5c31af7Sopenharmony_ciDE_INLINE int		deFloatIsInf		(float x)		{ return (x > FLT_MAX) - (x < -FLT_MAX); }
68e5c31af7Sopenharmony_ciDE_INLINE deBool	deFloatIsNaN		(float x)		{ return (x != x); }
69e5c31af7Sopenharmony_ci
70e5c31af7Sopenharmony_ciDE_INLINE int		deIsInf				(double x)		{ return (x > DBL_MAX) - (x < -DBL_MAX); }
71e5c31af7Sopenharmony_ciDE_INLINE deBool	deIsNaN				(double x)		{ return (x != x); }
72e5c31af7Sopenharmony_ci
73e5c31af7Sopenharmony_ciDE_INLINE deUint32 deFloatBitsToUint32(float x)
74e5c31af7Sopenharmony_ci{
75e5c31af7Sopenharmony_ci	deUint32 bits;
76e5c31af7Sopenharmony_ci	deMemcpy((void *)&bits, (void *)&x, 4);
77e5c31af7Sopenharmony_ci	return bits;
78e5c31af7Sopenharmony_ci}
79e5c31af7Sopenharmony_ci
80e5c31af7Sopenharmony_ciDE_INLINE deUint64 deDoubleBitsToUint64(double x)
81e5c31af7Sopenharmony_ci{
82e5c31af7Sopenharmony_ci	deUint64 bits;
83e5c31af7Sopenharmony_ci	deMemcpy((void *)&bits, (void *)&x, 8);
84e5c31af7Sopenharmony_ci	return bits;
85e5c31af7Sopenharmony_ci}
86e5c31af7Sopenharmony_ci
87e5c31af7Sopenharmony_ciDE_INLINE deBool deFloatIsPositiveZero(float x)
88e5c31af7Sopenharmony_ci{
89e5c31af7Sopenharmony_ci	return x == 0 && (deFloatBitsToUint32(x) >> 31) == 0;
90e5c31af7Sopenharmony_ci}
91e5c31af7Sopenharmony_ci
92e5c31af7Sopenharmony_ciDE_INLINE deBool deDoubleIsPositiveZero(double x)
93e5c31af7Sopenharmony_ci{
94e5c31af7Sopenharmony_ci	return x == 0 && (deDoubleBitsToUint64(x) >> 63) == 0;
95e5c31af7Sopenharmony_ci}
96e5c31af7Sopenharmony_ci
97e5c31af7Sopenharmony_ciDE_INLINE deBool deFloatIsNegativeZero(float x)
98e5c31af7Sopenharmony_ci{
99e5c31af7Sopenharmony_ci	return x == 0 && (deFloatBitsToUint32(x) >> 31) != 0;
100e5c31af7Sopenharmony_ci}
101e5c31af7Sopenharmony_ci
102e5c31af7Sopenharmony_ciDE_INLINE deBool deDoubleIsNegativeZero(double x)
103e5c31af7Sopenharmony_ci{
104e5c31af7Sopenharmony_ci	return x == 0 && (deDoubleBitsToUint64(x) >> 63) != 0;
105e5c31af7Sopenharmony_ci}
106e5c31af7Sopenharmony_ci
107e5c31af7Sopenharmony_ciDE_INLINE deBool deFloatIsIEEENaN(float x)
108e5c31af7Sopenharmony_ci{
109e5c31af7Sopenharmony_ci	deUint32 e = (deFloatBitsToUint32(x) & 0x7f800000u) >> 23;
110e5c31af7Sopenharmony_ci	deUint32 m = (deFloatBitsToUint32(x) & 0x007fffffu);
111e5c31af7Sopenharmony_ci	return e == 0xff && m != 0;
112e5c31af7Sopenharmony_ci}
113e5c31af7Sopenharmony_ci
114e5c31af7Sopenharmony_ciDE_INLINE deBool deDoubleIsIEEENaN(double x)
115e5c31af7Sopenharmony_ci{
116e5c31af7Sopenharmony_ci	deUint64 e = (deDoubleBitsToUint64(x) & 0x7ff0000000000000ull) >> 52;
117e5c31af7Sopenharmony_ci	deUint64 m = (deDoubleBitsToUint64(x) & 0x000fffffffffffffull);
118e5c31af7Sopenharmony_ci	return e == 0x7ff && m != 0;
119e5c31af7Sopenharmony_ci}
120e5c31af7Sopenharmony_ci
121e5c31af7Sopenharmony_ci/* \note The definition used for signaling NaN here is valid for ARM and
122e5c31af7Sopenharmony_ci * x86 but possibly not for other platforms.
123e5c31af7Sopenharmony_ci *
124e5c31af7Sopenharmony_ci * These are defined as overloads so that they can be used in templated
125e5c31af7Sopenharmony_ci * code without risking a type conversion which would triggern an exception
126e5c31af7Sopenharmony_ci * on a signaling NaN.  We don't use deIsNan in these helpers because they
127e5c31af7Sopenharmony_ci * do a comparison operation which may also trigger exceptions.
128e5c31af7Sopenharmony_ci */
129e5c31af7Sopenharmony_ciDE_INLINE deBool deFloatIsSignalingNaN(float x)
130e5c31af7Sopenharmony_ci{
131e5c31af7Sopenharmony_ci	return deFloatIsIEEENaN(x) && (deFloatBitsToUint32(x) & (1u << 22)) == 0;
132e5c31af7Sopenharmony_ci}
133e5c31af7Sopenharmony_ci
134e5c31af7Sopenharmony_ciDE_INLINE deBool deDoubleIsSignalingNaN(double x)
135e5c31af7Sopenharmony_ci{
136e5c31af7Sopenharmony_ci	return deDoubleIsIEEENaN(x) && (deDoubleBitsToUint64(x) & (1ull << 51)) == 0;
137e5c31af7Sopenharmony_ci}
138e5c31af7Sopenharmony_ci
139e5c31af7Sopenharmony_ciDE_INLINE deBool deFloatIsQuietNaN(float x)
140e5c31af7Sopenharmony_ci{
141e5c31af7Sopenharmony_ci	return deFloatIsIEEENaN(x) && (deFloatBitsToUint32(x) & (1u << 22)) != 0;
142e5c31af7Sopenharmony_ci}
143e5c31af7Sopenharmony_ci
144e5c31af7Sopenharmony_ciDE_INLINE deBool deDoubleIsQuietNaN(double x)
145e5c31af7Sopenharmony_ci{
146e5c31af7Sopenharmony_ci	return deDoubleIsIEEENaN(x) && (deDoubleBitsToUint64(x) & (1ull << 51)) != 0;
147e5c31af7Sopenharmony_ci}
148e5c31af7Sopenharmony_ci
149e5c31af7Sopenharmony_ci/* Basic utilities. */
150e5c31af7Sopenharmony_ci
151e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAbs			(float x)						{ return (x >= 0.0f) ? x : -x; }
152e5c31af7Sopenharmony_ciDE_INLINE float		deFloatMin			(float a, float b)				{ return (a <= b) ? a : b; }
153e5c31af7Sopenharmony_ciDE_INLINE float		deFloatMax			(float a, float b)				{ return (a >= b) ? a : b; }
154e5c31af7Sopenharmony_ciDE_INLINE float		deFloatClamp		(float x, float mn, float mx)	{ return (x <= mn) ? mn : ((x >= mx) ? mx : x); }
155e5c31af7Sopenharmony_ci
156e5c31af7Sopenharmony_ciDE_INLINE double	deAbs				(double x)							{ return (x >= 0.0) ? x : -x; }
157e5c31af7Sopenharmony_ciDE_INLINE double	deMin				(double a, double b)				{ return (a <= b) ? a : b; }
158e5c31af7Sopenharmony_ciDE_INLINE double	deMax				(double a, double b)				{ return (a >= b) ? a : b; }
159e5c31af7Sopenharmony_ciDE_INLINE double	deClamp				(double x, double mn, double mx)	{ return (x <= mn) ? mn : ((x >= mx) ? mx : x); }
160e5c31af7Sopenharmony_ci
161e5c31af7Sopenharmony_ci/* Utility functions. */
162e5c31af7Sopenharmony_ci
163e5c31af7Sopenharmony_ciDE_INLINE float		deFloatSign			(float a)					{ return (a == 0.0f) ? 0.0f : ((a > 0.0f) ? +1.0f : -1.0f); }
164e5c31af7Sopenharmony_ciDE_INLINE int		deFloatIntSign		(float a)					{ return (a == 0.0f) ? 0 : ((a > 0.0f) ? +1 : -1); }
165e5c31af7Sopenharmony_ciDE_INLINE float		deFloatFloor		(float a)					{ return (float)floor(a); }
166e5c31af7Sopenharmony_ciDE_INLINE float		deFloatCeil			(float a)					{ return (float)ceil(a); }
167e5c31af7Sopenharmony_ciDE_INLINE float		deFloatRound		(float a)					{ return deFloatFloor(a + 0.5f); }
168e5c31af7Sopenharmony_ciDE_INLINE float		deFloatFrac			(float a)					{ return a - deFloatFloor(a); }
169e5c31af7Sopenharmony_ciDE_INLINE float		deFloatMod			(float a, float b)			{ return (float)fmod(a, b); }
170e5c31af7Sopenharmony_ciDE_INLINE float		deFloatModf			(float x, float* i)			{ double j = 0; double ret = modf(x, &j); *i = (float)j; return (float)ret; }
171e5c31af7Sopenharmony_ciDE_INLINE float		deFloatMadd			(float a, float b, float c)	{ return (a*b) + c; }
172e5c31af7Sopenharmony_ciDE_INLINE float		deFloatTrunc		(float a)					{ return deFloatSign(a) * deFloatFloor(deFloatAbs(a)); }
173e5c31af7Sopenharmony_ciDE_INLINE float		deFloatLdExp		(float a, int exponent)		{ return (float)ldexp(a, exponent); }
174e5c31af7Sopenharmony_ciDE_INLINE float		deFloatFrExp		(float x, int* exponent)	{ return (float)frexp(x, exponent); }
175e5c31af7Sopenharmony_cifloat				deFloatFractExp		(float x, int* exponent);
176e5c31af7Sopenharmony_ci
177e5c31af7Sopenharmony_ciDE_INLINE double	deSign				(double x)						{ return deIsNaN(x) ? x : (double)((x > 0.0) - (x < 0.0)); }
178e5c31af7Sopenharmony_ciDE_INLINE int		deIntSign			(double x)						{ return (x > 0.0) - (x < 0.0); }
179e5c31af7Sopenharmony_ciDE_INLINE double	deFloor				(double a)						{ return floor(a); }
180e5c31af7Sopenharmony_ciDE_INLINE double	deCeil				(double a)						{ return ceil(a); }
181e5c31af7Sopenharmony_ciDE_INLINE double	deRound				(double a)						{ return floor(a + 0.5); }
182e5c31af7Sopenharmony_ciDE_INLINE double	deFrac				(double a)						{ return a - deFloor(a); }
183e5c31af7Sopenharmony_ciDE_INLINE double	deMod				(double a, double b)			{ return fmod(a, b); }
184e5c31af7Sopenharmony_ciDE_INLINE double	deModf				(double x, double* i)			{ return modf(x, i); }
185e5c31af7Sopenharmony_ciDE_INLINE double	deMadd				(double a, double b, double c)	{ return (a*b) + c; }
186e5c31af7Sopenharmony_ciDE_INLINE double	deTrunc				(double a)						{ return deSign(a) * floor(fabs(a)); }
187e5c31af7Sopenharmony_ciDE_INLINE double	deLdExp				(double a, int exponent)		{ return ldexp(a, exponent); }
188e5c31af7Sopenharmony_cidouble				deRoundEven			(double a);
189e5c31af7Sopenharmony_ciDE_INLINE double	deFrExp				(double x, int* exponent)		{ return frexp(x, exponent); }
190e5c31af7Sopenharmony_ci/* Like frexp, except the returned fraction is in range [1.0, 2.0) */
191e5c31af7Sopenharmony_cidouble				deFractExp			(double x, int* exponent);
192e5c31af7Sopenharmony_ci
193e5c31af7Sopenharmony_ci/* Exponential functions. */
194e5c31af7Sopenharmony_ci
195e5c31af7Sopenharmony_ciDE_INLINE float		deFloatPow			(float a, float b)			{ return (float)pow(a, b); }
196e5c31af7Sopenharmony_ciDE_INLINE float		deFloatExp			(float a)					{ return (float)exp(a); }
197e5c31af7Sopenharmony_ciDE_INLINE float		deFloatLog			(float a)					{ return (float)log(a); }
198e5c31af7Sopenharmony_ciDE_INLINE float		deFloatExp2			(float a)					{ return (float)exp(a * DE_LOG_2); }
199e5c31af7Sopenharmony_ciDE_INLINE float		deFloatLog2			(float a)					{ return (float)log(a) * DE_INV_LOG_2; }
200e5c31af7Sopenharmony_ciDE_INLINE float		deFloatSqrt			(float a)					{ return (float)sqrt(a); }
201e5c31af7Sopenharmony_ciDE_INLINE float		deFloatRcp			(float a)					{ return (1.0f / a); }
202e5c31af7Sopenharmony_ciDE_INLINE float		deFloatRsq			(float a)					{ float s = (float)sqrt(a); return (s == 0.0f) ? 0.0f : (1.0f / s); }
203e5c31af7Sopenharmony_ci
204e5c31af7Sopenharmony_ciDE_INLINE double	dePow				(double a, double b)		{ return pow(a, b); }
205e5c31af7Sopenharmony_ciDE_INLINE double	deExp				(double a)					{ return exp(a); }
206e5c31af7Sopenharmony_ciDE_INLINE double	deLog				(double a)					{ return log(a); }
207e5c31af7Sopenharmony_ciDE_INLINE double	deExp2				(double a)					{ return exp(a * log(2.0)); }
208e5c31af7Sopenharmony_ciDE_INLINE double	deLog2				(double a)					{ return log(a) / log(2.0); }
209e5c31af7Sopenharmony_ciDE_INLINE double	deSqrt				(double a)					{ return sqrt(a); }
210e5c31af7Sopenharmony_ciDE_INLINE double	deCbrt				(double a)					{ return deSign(a) * dePow(deAbs(a), 1.0 / 3.0); }
211e5c31af7Sopenharmony_ci
212e5c31af7Sopenharmony_ci/* Geometric functions. */
213e5c31af7Sopenharmony_ci
214e5c31af7Sopenharmony_ciDE_INLINE float		deFloatRadians		(float a)					{ return a * (DE_PI / 180.0f); }
215e5c31af7Sopenharmony_ciDE_INLINE float		deFloatDegrees		(float a)					{ return a * (180.0f / DE_PI); }
216e5c31af7Sopenharmony_ciDE_INLINE float		deFloatSin			(float a)					{ return (float)sin(a); }
217e5c31af7Sopenharmony_ciDE_INLINE float		deFloatCos			(float a)					{ return (float)cos(a); }
218e5c31af7Sopenharmony_ciDE_INLINE float		deFloatTan			(float a)					{ return (float)tan(a); }
219e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAsin			(float a)					{ return (float)asin(a); }
220e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAcos			(float a)					{ return (float)acos(a); }
221e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAtan2		(float y, float x)			{ return (float)atan2(y, x); }
222e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAtanOver		(float yOverX)				{ return (float)atan(yOverX); }
223e5c31af7Sopenharmony_ciDE_INLINE float		deFloatSinh			(float a)					{ return (float)sinh(a); }
224e5c31af7Sopenharmony_ciDE_INLINE float		deFloatCosh			(float a)					{ return (float)cosh(a); }
225e5c31af7Sopenharmony_ciDE_INLINE float		deFloatTanh			(float a)					{ return (float)tanh(a); }
226e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAsinh		(float a)					{ return deFloatLog(a + deFloatSqrt(a*a + 1)); }
227e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAcosh		(float a)					{ return deFloatLog(a + deFloatSqrt(a*a - 1)); }
228e5c31af7Sopenharmony_ciDE_INLINE float		deFloatAtanh		(float a)					{ return 0.5f*deFloatLog((1.0f+a)/(1.0f-a)); }
229e5c31af7Sopenharmony_ci
230e5c31af7Sopenharmony_ciDE_INLINE double	deSin			(double a)						{ return sin(a); }
231e5c31af7Sopenharmony_ciDE_INLINE double	deCos			(double a)						{ return cos(a); }
232e5c31af7Sopenharmony_ciDE_INLINE double	deTan			(double a)						{ return tan(a); }
233e5c31af7Sopenharmony_ciDE_INLINE double	deAsin			(double a)						{ return asin(a); }
234e5c31af7Sopenharmony_ciDE_INLINE double	deAcos			(double a)						{ return acos(a); }
235e5c31af7Sopenharmony_ciDE_INLINE double	deAtan2			(double y, double x)			{ return atan2(y, x); }
236e5c31af7Sopenharmony_ciDE_INLINE double	deAtanOver		(double yOverX)					{ return atan(yOverX); }
237e5c31af7Sopenharmony_ciDE_INLINE double	deSinh			(double a)						{ return sinh(a); }
238e5c31af7Sopenharmony_ciDE_INLINE double	deCosh			(double a)						{ return cosh(a); }
239e5c31af7Sopenharmony_ciDE_INLINE double	deTanh			(double a)						{ return tanh(a); }
240e5c31af7Sopenharmony_ciDE_INLINE double	deAsinh			(double a)						{ return deLog(a + deSqrt(a*a + 1)); }
241e5c31af7Sopenharmony_ciDE_INLINE double	deAcosh			(double a)						{ return deLog(a + deSqrt(a*a - 1)); }
242e5c31af7Sopenharmony_ciDE_INLINE double	deAtanh			(double a)						{ return 0.5*deLog((1.0+a)/(1.0-a)); }
243e5c31af7Sopenharmony_ci
244e5c31af7Sopenharmony_ci/* Interpolation. */
245e5c31af7Sopenharmony_ci
246e5c31af7Sopenharmony_ciDE_INLINE float		deFloatMix			(float a, float b, float t)	{ return a*(1.0f-t) + b*t; }
247e5c31af7Sopenharmony_ciDE_INLINE float		deFloatStep			(float limit, float val)	{ return (val < limit) ? 0.0f : 1.0f; }
248e5c31af7Sopenharmony_ciDE_INLINE float		deFloatSmoothStep	(float e0, float e1, float v)
249e5c31af7Sopenharmony_ci{
250e5c31af7Sopenharmony_ci	float t;
251e5c31af7Sopenharmony_ci	if (v <= e0) return 0.0f;
252e5c31af7Sopenharmony_ci	if (v >= e1) return 1.0f;
253e5c31af7Sopenharmony_ci	t = (v - e0) / (e1 - e0);
254e5c31af7Sopenharmony_ci	return t * t * (3.0f - 2.0f * t);
255e5c31af7Sopenharmony_ci}
256e5c31af7Sopenharmony_ci
257e5c31af7Sopenharmony_ciDE_INLINE double	deMix				(double a, double b, double t)	{ return a*(1.0-t) + b*t; }
258e5c31af7Sopenharmony_ciDE_INLINE double	deStep				(double limit, double val)		{ return (val < limit) ? 0.0 : 1.0; }
259e5c31af7Sopenharmony_ci
260e5c31af7Sopenharmony_ci/* Convert int to float. If the value cannot be represented exactly in native single precision format, return
261e5c31af7Sopenharmony_ci * either the nearest lower or the nearest higher representable value, chosen in an implementation-defined manner.
262e5c31af7Sopenharmony_ci *
263e5c31af7Sopenharmony_ci * \note Choosing either nearest lower or nearest higher means that implementation could for example consistently
264e5c31af7Sopenharmony_ci *       choose the lower value, i.e. this function does not round towards nearest.
265e5c31af7Sopenharmony_ci * \note Value returned is in native single precision format. For example with x86 extended precision, the value
266e5c31af7Sopenharmony_ci *       returned might not be representable in IEEE single precision float.
267e5c31af7Sopenharmony_ci */
268e5c31af7Sopenharmony_ciDE_INLINE float		deInt32ToFloat				(deInt32 x)					{ return (float)x; }
269e5c31af7Sopenharmony_ci
270e5c31af7Sopenharmony_ci/* Convert to float. If the value cannot be represented exactly in IEEE single precision floating point format,
271e5c31af7Sopenharmony_ci * return the nearest lower (round towards negative inf). */
272e5c31af7Sopenharmony_cifloat				deInt32ToFloatRoundToNegInf	(deInt32 x);
273e5c31af7Sopenharmony_ci
274e5c31af7Sopenharmony_ci/* Convert to float. If the value cannot be represented exactly IEEE single precision floating point format,
275e5c31af7Sopenharmony_ci * return the nearest higher (round towards positive inf). */
276e5c31af7Sopenharmony_cifloat				deInt32ToFloatRoundToPosInf	(deInt32 x);
277e5c31af7Sopenharmony_ci
278e5c31af7Sopenharmony_ci/* Conversion to integer. */
279e5c31af7Sopenharmony_ci
280e5c31af7Sopenharmony_ciDE_INLINE deInt32	deChopFloatToInt32		(float x)				{ return (deInt32)x; }
281e5c31af7Sopenharmony_ciDE_INLINE deInt32	deFloorFloatToInt32		(float x)				{ return (deInt32)(deFloatFloor(x)); }
282e5c31af7Sopenharmony_ciDE_INLINE deInt32	deCeilFloatToInt32		(float x)				{ return (deInt32)(deFloatCeil(x)); }
283e5c31af7Sopenharmony_ci
284e5c31af7Sopenharmony_ciDE_INLINE deInt32	deChopToInt32			(double x)				{ return (deInt32)x; }
285e5c31af7Sopenharmony_ciDE_INLINE deInt32	deFloorToInt32			(double x)				{ return (deInt32)(deFloor(x)); }
286e5c31af7Sopenharmony_ciDE_INLINE deInt32	deCeilToInt32			(double x)				{ return (deInt32)(deCeil(x)); }
287e5c31af7Sopenharmony_ci
288e5c31af7Sopenharmony_ci/* Arithmetic round */
289e5c31af7Sopenharmony_ciDE_INLINE deInt16	deRoundFloatToInt16		(float x)				{ if(x >= 0.0f) return (deInt16)(x + 0.5f); else return (deInt16)(x - 0.5f); }
290e5c31af7Sopenharmony_ciDE_INLINE deInt32	deRoundFloatToInt32		(float x)				{ if(x >= 0.0f) return (deInt32)(x + 0.5f); else return (deInt32)(x - 0.5f); }
291e5c31af7Sopenharmony_ciDE_INLINE deInt64	deRoundFloatToInt64		(float x)				{ if(x >= 0.0f) return (deInt64)(x + 0.5f); else return (deInt64)(x - 0.5f); }
292e5c31af7Sopenharmony_ci
293e5c31af7Sopenharmony_ciDE_INLINE deInt16	deRoundToInt16			(double x)				{ if(x >= 0.0) return (deInt16)(x + 0.5); else return (deInt16)(x - 0.5); }
294e5c31af7Sopenharmony_ciDE_INLINE deInt32	deRoundToInt32			(double x)				{ if(x >= 0.0) return (deInt32)(x + 0.5); else return (deInt32)(x - 0.5); }
295e5c31af7Sopenharmony_ciDE_INLINE deInt64	deRoundToInt64			(double x)				{ if(x >= 0.0) return (deInt64)(x + 0.5); else return (deInt64)(x - 0.5); }
296e5c31af7Sopenharmony_ci
297e5c31af7Sopenharmony_ciDE_END_EXTERN_C
298e5c31af7Sopenharmony_ci
299e5c31af7Sopenharmony_ci#endif /* _DEMATH_H */
300