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 _LOG2F_H_
34 #define _LOG2F_H_ 1
35
36 #include <spu_intrinsics.h>
37 #include "headers/dom_chkf_less_than.h"
38
39 /*
40 * FUNCTION
41 * float _log2f(float x)
42 *
43 * DESCRIPTION
44 * _log2f computes log (base 2) of the input value x. The log2f
45 * function is approximated as a polynomial of order 8
46 * (C. Hastings, Jr, 1955).
47 *
48 * __8__
49 * \
50 * \
51 * log2f(1+x) = / Ci*x^i
52 * /____
53 * i=1
54 *
55 * for x in the range 0.0 to 1.0
56 *
57 * C1 = 1.4426898816672
58 * C2 = -0.72116591947498
59 * C3 = 0.47868480909345
60 * C4 = -0.34730547155299
61 * C5 = 0.24187369696082
62 * C6 = -0.13753123777116
63 * C7 = 0.052064690894143
64 * C8 = -0.0093104962134977
65 *
66 * This function assumes that x is a non-zero positive value.
67 */
68
_log2f(float x)69 static __inline float _log2f(float x)
70 {
71 union {
72 unsigned int ui;
73 float f;
74 } in;
75 int exponent;
76 float result;
77 float x2, x4;
78 float hi, lo;
79 vector float vx;
80 vector float vc = { 0.0, 0.0, 0.0, 0.0 };
81
82 in.f = x;
83
84 /* Extract the exponent from the input X.
85 */
86 exponent = (signed)((in.ui >> 23) & 0xFF) - 127;
87
88 /* Compute the remainder after removing the exponent.
89 */
90 in.ui -= exponent << 23;
91
92 /* Calculate the log2 of the remainder using the polynomial
93 * approximation.
94 */
95 x = in.f - 1.0f;
96
97 /* Instruction counts can be reduced if the polynomial was
98 * computed entirely from nested (dependent) fma's. However,
99 * to reduce the number of pipeline stalls, the polygon is evaluated
100 * in two halves (hi amd lo).
101 */
102 x2 = x * x;
103 x4 = x2 * x2;
104 hi = -0.0093104962134977f*x + 0.052064690894143f;
105 hi = hi*x - 0.13753123777116f;
106 hi = hi*x + 0.24187369696082f;
107 hi = hi*x - 0.34730547155299f;
108 lo = 0.47868480909345f *x - 0.72116591947498f;
109 lo = lo*x + 1.4426898816672f;
110 lo = lo*x;
111 result = hi*x4 + lo;
112
113 /* Add the exponent back into the result.
114 */
115 result += (float)(exponent);
116
117 #ifndef _IEEE_LIBM
118 vx = spu_promote(x, 0);
119 dom_chkf_less_than(vx, vc);
120 #endif
121 return (result);
122 }
123
124 #endif /* _LOG2F_H_ */
125