1 /* --------------------------------------------------------------  */
2 /* (C)Copyright 2007,2008,                                         */
3 /* International Business Machines Corporation                     */
4 /* All Rights Reserved.                                            */
5 /*                                                                 */
6 /* Redistribution and use in source and binary forms, with or      */
7 /* without modification, are permitted provided that the           */
8 /* following conditions are met:                                   */
9 /*                                                                 */
10 /* - Redistributions of source code must retain the above copyright*/
11 /*   notice, this list of conditions and the following disclaimer. */
12 /*                                                                 */
13 /* - Redistributions in binary form must reproduce the above       */
14 /*   copyright notice, this list of conditions and the following   */
15 /*   disclaimer in the documentation and/or other materials        */
16 /*   provided with the distribution.                               */
17 /*                                                                 */
18 /* - Neither the name of IBM Corporation nor the names of its      */
19 /*   contributors may be used to endorse or promote products       */
20 /*   derived from this software without specific prior written     */
21 /*   permission.                                                   */
22 /*                                                                 */
23 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND          */
24 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,     */
25 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        */
26 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        */
27 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            */
28 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    */
29 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    */
30 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    */
31 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        */
32 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN       */
33 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    */
34 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  */
35 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
36 /* --------------------------------------------------------------  */
37 /* PROLOG END TAG zYx                                              */
38 
39 #ifdef __SPU__
40 #ifndef _LOG1PF4_H_
41 #define _LOG1PF4_H_	1
42 
43 #include <spu_intrinsics.h>
44 #include "simdmath.h"
45 
46 #include "logf4.h"
47 #include "divf4.h"
48 
49 /*
50  * FUNCTION
51  *	vector float _log1pf4(vector float x)
52  *
53  * DESCRIPTION
54  *	The function _log1pf4 computes the natural logarithm of x + 1
55  *	for each of the float word elements of x.
56  *
57  *
58  */
59 
60 #define LOG1PF4_P0 0.0000000000000000000000000e+00f
61 #define LOG1PF4_P1 1.0000000000000000000000000e+00f
62 #define LOG1PF4_P2 1.4220868022897381610647471e+00f
63 #define LOG1PF4_P3 5.4254553902256308361984338e-01f
64 #define LOG1PF4_P4 4.5971908823142115796400731e-02f
65 
66 #define LOG1PF4_Q0 1.0000000000000000000000000e+00f
67 #define LOG1PF4_Q1 1.9220868007537357247116461e+00f
68 #define LOG1PF4_Q2 1.1702556461286610645089468e+00f
69 #define LOG1PF4_Q3 2.4040413392943396631018516e-01f
70 #define LOG1PF4_Q4 1.0637426466449625625521058e-02f
71 
72 
_log1pf4(vector float x)73 static __inline vector float _log1pf4(vector float x)
74 {
75   vector float onef  = spu_splats(1.0f);
76   vector float range = spu_splats(0.35f);
77   vector unsigned int use_log;
78   vector float pr, qr;
79   vector float eresult;
80   vector float rresult;
81   vector float result;
82 
83   use_log = spu_cmpabsgt(x, range);
84 
85   /*
86    * Calculate directly using log(x+1)
87    */
88   eresult = _logf4(spu_add(x, onef));
89 
90   /*
91    * For x in [-0.35,0.35], use a rational approximation.
92    */
93   pr = spu_madd(x, spu_splats((float)LOG1PF4_P4), spu_splats((float)LOG1PF4_P3));
94   qr = spu_madd(x, spu_splats((float)LOG1PF4_Q4), spu_splats((float)LOG1PF4_Q3));
95   pr = spu_madd(pr, x, spu_splats((float)LOG1PF4_P2));
96   qr = spu_madd(qr, x, spu_splats((float)LOG1PF4_Q2));
97   pr = spu_madd(pr, x, spu_splats((float)LOG1PF4_P1));
98   qr = spu_madd(qr, x, spu_splats((float)LOG1PF4_Q1));
99   pr = spu_madd(pr, x, spu_splats((float)LOG1PF4_P0));
100   qr = spu_madd(qr, x, spu_splats((float)LOG1PF4_Q0));
101   rresult = _divf4(pr, qr);
102 
103   /*
104    * Select either direct calculation or rational approximation.
105    */
106   result = spu_sel(rresult, eresult, use_log);
107 
108   return result;
109 }
110 
111 #endif /* _LOG1PF4_H_ */
112 #endif /* __SPU__ */
113