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 #ifndef _EXPD2_H_
43 #define _EXPD2_H_ 1
44
45 #include <spu_intrinsics.h>
46 #include "floord2.h"
47
48 #define LOG2E 1.4426950408889634073599 // 1/log(2)
49
50 /*
51 * FUNCTION
52 * vector double _expd2(vector double x)
53 *
54 * DESCRIPTION
55 * _expd2 computes e raised to the input x for
56 * each of the element of the double word vector.
57 *
58 * Calculation is performed by reducing the input argument
59 * to within a managable range, and then computing the power
60 * series to the 11th degree.
61 *
62 * Range reduction is performed using the property:
63 *
64 * exp(x) = 2^n * exp(r)
65 *
66 * Values for "n" and "r" are determined such that:
67 *
68 * x = n * ln(2) + r, |r| <= ln(2)/2
69 *
70 * n = floor( (x/ln(2)) + 1/2 )
71 * r = x - (n * ln(2))
72 *
73 * To enhance the precision for "r", computation is performed
74 * using a two part representation of ln(2).
75 *
76 * Once the input is reduced, the power series is computed:
77 *
78 * __12_
79 * \
80 * exp(x) = 1 + \ (x^i)/i!
81 * /
82 * /____
83 * i=2
84 *
85 * The resulting value is scaled by 2^n and returned.
86 *
87 */
88
_expd2(vector double x)89 static __inline vector double _expd2(vector double x)
90 {
91 // log(2) in extended machine representable precision
92 vec_double2 ln2_hi = spu_splats(6.9314575195312500E-1); // 3FE62E4000000000
93 vec_double2 ln2_lo = spu_splats(1.4286068203094172E-6); // 3EB7F7D1CF79ABCA
94
95 // coefficients for the power series
96 // vec_double2 f01 = spu_splats(1.00000000000000000000E0); // 1/(1!)
97 vec_double2 f02 = spu_splats(5.00000000000000000000E-1); // 1/(2!)
98 vec_double2 f03 = spu_splats(1.66666666666666666667E-1); // 1/(3!)
99 vec_double2 f04 = spu_splats(4.16666666666666666667E-2); // 1/(4!)
100 vec_double2 f05 = spu_splats(8.33333333333333333333E-3); // 1/(5!)
101 vec_double2 f06 = spu_splats(1.38888888888888888889E-3); // 1/(6!)
102 vec_double2 f07 = spu_splats(1.98412698412698412698E-4); // 1/(7!)
103 vec_double2 f08 = spu_splats(2.48015873015873015873E-5); // 1/(8!)
104 vec_double2 f09 = spu_splats(2.75573192239858906526E-6); // 1/(9!)
105 vec_double2 f10 = spu_splats(2.75573192239858906526E-7); // 1/(10!)
106 vec_double2 f11 = spu_splats(2.50521083854417187751E-8); // 1/(11!)
107 vec_double2 f12 = spu_splats(2.08767569878680989792E-9); // 1/(12!)
108
109 // rx = floor(1/2 + x/log(2))
110 vec_double2 rx = _floord2(spu_madd(x,spu_splats(LOG2E),spu_splats(0.5)));
111
112 // extract the exponent of reduction
113 vec_int4 exp = spu_convts(spu_roundtf(rx),0);
114
115 // reduce the input to within [ -ln(2)/2 ... ln(2)/2 ]
116 vec_double2 r;
117 r = spu_nmsub(rx,ln2_hi,x);
118 r = spu_nmsub(rx,ln2_lo,r);
119
120 vec_double2 result;
121 vec_double2 r2 = spu_mul(r,r);
122
123 // Use Horner's method on the power series
124 /* result = ((((c12*x + c11)*x + c10)*x + c9)*x + c8)*x + c7)*x + c6)*x^6 +
125 ((((((c5*x + c4)*x + c3)*x + c2)*x + c1)*x + c0
126 */
127
128 #ifdef __SPU_EDP__
129 vec_double2 p1, p2, r4, r6;
130
131 p1 = spu_madd(f12, r, f11);
132 p2 = spu_madd(f05, r, f04);
133 r4 = spu_mul(r2, r2);
134 p1 = spu_madd(p1, r, f10);
135 p2 = spu_madd(p2, r, f03);
136 p1 = spu_madd(p1, r, f09);
137 p2 = spu_madd(p2, r, f02);
138 p1 = spu_madd(p1, r, f08);
139 r6 = spu_mul(r2, r4);
140 p1 = spu_madd(p1, r, f07);
141 p2 = spu_madd(p2, r2, r);
142 p1 = spu_madd(p1, r, f06);
143
144 result = spu_madd(r6, p1, p2);
145 result = spu_add(result, spu_splats(1.0));
146
147 #else
148
149 result = spu_madd(r,f12,f11);
150 result = spu_madd(result,r,f10);
151 result = spu_madd(result,r,f09);
152 result = spu_madd(result,r,f08);
153 result = spu_madd(result,r,f07);
154 result = spu_madd(result,r,f06);
155 result = spu_madd(result,r,f05);
156 result = spu_madd(result,r,f04);
157 result = spu_madd(result,r,f03);
158 result = spu_madd(result,r,f02);
159 result = spu_madd(result,r2,r);
160 result = spu_add(result,spu_splats(1.0));
161
162 #endif /* __SPU_EDP__ */
163
164
165 // Scale the result - basically a call to ldexpd2()
166 vec_int4 e1, e2;
167 vec_int4 min = spu_splats(-2044);
168 vec_int4 max = spu_splats(2046);
169 vec_uint4 cmp_min, cmp_max;
170 vec_uint4 shift = (vec_uint4) { 20, 32, 20, 32 };
171 vec_double2 f1, f2;
172
173 /* Clamp the specified exponent to the range -2044 to 2046.
174 */
175 cmp_min = spu_cmpgt(exp, min);
176 cmp_max = spu_cmpgt(exp, max);
177 exp = spu_sel(min, exp, cmp_min);
178 exp = spu_sel(exp, max, cmp_max);
179
180 /* Generate the factors f1 = 2^e1 and f2 = 2^e2
181 */
182 e1 = spu_rlmaska(exp, -1);
183 e2 = spu_sub(exp, e1);
184
185 f1 = (vec_double2)spu_sl(spu_add(e1, 1023), shift);
186 f2 = (vec_double2)spu_sl(spu_add(e2, 1023), shift);
187
188 /* Compute the product x * 2^e1 * 2^e2
189 */
190 result = spu_mul(spu_mul(result, f1), f2);
191
192 return result;
193 }
194
195 #endif /* _EXPD2_H_ */
196 #endif /* __SPU__ */
197