1 /* --------------------------------------------------------------  */
2 /* (C)Copyright 2001,2008,                                         */
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      */
10 /* without modification, are permitted provided that the           */
11 /* following conditions are met:                                   */
12 /*                                                                 */
13 /* - Redistributions of source code must retain the above copyright*/
14 /*   notice, this list of conditions and the following disclaimer. */
15 /*                                                                 */
16 /* - Redistributions in binary form must reproduce the above       */
17 /*   copyright notice, this list of conditions and the following   */
18 /*   disclaimer in the documentation and/or other materials        */
19 /*   provided with the distribution.                               */
20 /*                                                                 */
21 /* - Neither the name of IBM Corporation nor the names of its      */
22 /*   contributors may be used to endorse or promote products       */
23 /*   derived from this software without specific prior written     */
24 /*   permission.                                                   */
25 /*                                                                 */
26 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND          */
27 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,     */
28 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        */
29 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        */
30 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            */
31 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    */
32 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    */
33 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    */
34 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        */
35 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN       */
36 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    */
37 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  */
38 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
39 /* --------------------------------------------------------------  */
40 /* PROLOG END TAG zYx                                              */
41 #ifdef __SPU__
42 
43 #ifndef _LOG2F4_H_
44 #define _LOG2F4_H_	1
45 
46 #include <spu_intrinsics.h>
47 
48 /*
49  * FUNCTION
50  *	vector float _log2f4(vector float x)
51  *
52  * DESCRIPTION
53  *	The _log2f4 function computes log (base 2) on a vector if inputs
54  *      values x. The _log2f4 function is approximated as a polynomial of
55  *      order 8 (C. Hastings, Jr, 1955).
56  *
57  *                   __8__
58  *		     \
59  *		      \
60  *	log2f(1+x) =  /     Ci*x^i
61  *                   /____
62  *                    i=1
63  *
64  *	for x in the range 0.0 to 1.0
65  *
66  *	C1 =  1.4426898816672
67  *	C2 = -0.72116591947498
68  *	C3 =  0.47868480909345
69  *	C4 = -0.34730547155299
70  *	C5 =  0.24187369696082
71  *	C6 = -0.13753123777116
72  *	C7 =  0.052064690894143
73  *	C8 = -0.0093104962134977
74  *
75  *	This function assumes that x is a non-zero positive value.
76  *
77  */
_log2f4(vector float x)78 static __inline vector float _log2f4(vector float x)
79 {
80   vector signed int exponent;
81   vector float result;
82   vector float x2, x4;
83   vector float hi, lo;
84 
85   /* Extract the exponent from the input X.
86    */
87   exponent = (vector signed int)spu_and(spu_rlmask((vector unsigned int)(x), -23), 0xFF);
88   exponent = spu_add(exponent, -127);
89 
90   /* Compute the remainder after removing the exponent.
91    */
92   x = (vector float)spu_sub((vector signed int)(x), spu_sl(exponent, 23));
93 
94   /* Calculate the log2 of the remainder using the polynomial
95    * approximation.
96    */
97   x = spu_sub(x, spu_splats(1.0f));
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   x2 = spu_mul(x, x);
105   x4 = spu_mul(x2, x2);
106 
107   hi = spu_madd(x, spu_splats(-0.0093104962134977f), spu_splats(0.052064690894143f));
108   hi = spu_madd(x, hi, spu_splats(-0.13753123777116f));
109   hi = spu_madd(x, hi, spu_splats( 0.24187369696082f));
110   hi = spu_madd(x, hi, spu_splats(-0.34730547155299f));
111   lo = spu_madd(x, spu_splats(0.47868480909345f), spu_splats(-0.72116591947498f));
112   lo = spu_madd(x, lo, spu_splats(1.4426898816672f));
113   lo = spu_mul(x, lo);
114   result = spu_madd(x4, hi, lo);
115 
116   /* Add the exponent back into the result.
117    */
118   result = spu_add(result, spu_convtf(exponent, 0));
119 
120   return (result);
121 }
122 
123 #endif /* _LOG2F4_H_ */
124 #endif /* __SPU__ */
125