1 /*
2   (C) Copyright 2001,2006,
3   International Business Machines Corporation,
4   Sony Computer Entertainment, Incorporated,
5   Toshiba Corporation,
6 
7   All rights reserved.
8 
9   Redistribution and use in source and binary forms, with or without
10   modification, are permitted provided that the following conditions are met:
11 
12     * Redistributions of source code must retain the above copyright notice,
13   this list of conditions and the following disclaimer.
14     * Redistributions in binary form must reproduce the above copyright
15   notice, this list of conditions and the following disclaimer in the
16   documentation and/or other materials provided with the distribution.
17     * Neither the names of the copyright holders nor the names of their
18   contributors may be used to endorse or promote products derived from this
19   software without specific prior written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 #ifndef _EXP2F_H_
34 #define _EXP2F_H_	1
35 
36 #ifndef M_LN2
37 #define M_LN2	0.69314718055994530942	/* ln(2) */
38 #endif /* M_LN2 */
39 
40 /*
41  * FUNCTION
42  *	float _exp2f(float x)
43  *
44  * DESCRIPTION
45  *	_exp2f computes 2 raised to the input x. Computation is
46  *	performed by observing the 2^(a+b) = 2^a * 2^b.
47  *	We decompose x into a and b (above) by letting.
48  *	a = ceil(x), b = x - a;
49  *
50  *	2^a is easilty computed by placing a into the exponent
51  *	or a floating point number whose mantissa is all zeros.
52  *
53  *	2^b is computed using the following polynomial approximation.
54  *	(C. Hastings, Jr, 1955).
55  *
56  *             __7__
57  *	       \
58  *		\
59  *	2^x =   /     Ci*x^i
60  *             /____
61  *              i=0
62  *
63  *	for x in the range 0.0 to 1.0
64  *
65  *	C0 =  1.0
66  *	C1 = -0.9999999995
67  *	C2 =  0.4999999206
68  *	C3 = -0.1666653019
69  *	C4 =  0.0416573475
70  *	C5 = -0.0083013598
71  *	C6 =  0.0013298820
72  *	C7 = -0.0001413161
73  *
74  */
_exp2f(float x)75 static __inline float _exp2f(float x)
76 {
77   union {
78     float f;
79     unsigned int ui;
80   } bias, exp_int, exp_frac;
81   unsigned int overflow, underflow;
82   int ix;
83   float frac, frac2, frac4;
84   float hi, lo;
85 
86   /* Break in the input x into two parts ceil(x), x - ceil(x).
87    */
88   bias.f = x;
89   bias.ui = ~(unsigned int)((signed)(bias.ui) >> 31) & 0x3F7FFFFF;
90   ix = (int)(x + bias.f);
91   frac = (float)ix - x;
92   frac *= (float)(M_LN2);
93 
94   exp_int.ui  = (ix + 127) << 23;
95 
96   overflow  = (ix > 128)  ? 0x7FFFFFFF : 0x0;
97   underflow = (ix < -127) ? 0xFFFFFFFF : 0x0;
98 
99   /* Instruction counts can be reduced if the polynomial was
100    * computed entirely from nested (dependent) fma's. However,
101    * to reduce the number of pipeline stalls, the polygon is evaluated
102    * in two halves (hi amd lo).
103    */
104   frac2 = frac  * frac;
105   frac4 = frac2 * frac2;
106   hi = -0.0001413161f * frac + 0.0013298820f;
107   hi =             hi * frac - 0.0083013598f;
108   hi =             hi * frac + 0.0416573475f;
109   lo = -0.1666653019f * frac + 0.4999999206f;
110   lo =             lo * frac - 0.9999999995f;
111   lo =             lo * frac + 1.0f;
112   exp_frac.f =     hi * frac4 + lo;
113 
114   ix += exp_frac.ui >> 23;
115   exp_frac.f *= exp_int.f;
116 
117   exp_frac.ui = (exp_frac.ui | overflow) & ~underflow;
118 
119   return (exp_frac.f);
120 }
121 
122 #endif /* _EXP2F_H_ */
123 
124 
125