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 #ifdef __SPU__
39 #ifndef _ERFD2_H_
40 #define _ERFD2_H_ 1
41
42 #include <spu_intrinsics.h>
43
44 #include "expd2.h"
45 #include "recipd2.h"
46 #include "divd2.h"
47 #include "erf_utils.h"
48
49 /*
50 * FUNCTION
51 * vector double _erfd2(vector double x)
52 *
53 * DESCRIPTION
54 * The erfd2 function computes the error function of each element of x.
55 *
56 * C99 Special Cases:
57 * - erf(+0) returns +0
58 * - erf(-0) returns -0
59 * - erf(+infinite) returns +1
60 * - erf(-infinite) returns -1
61 *
62 * Other Cases:
63 * - erf(Nan) returns Nan
64 *
65 */
66
_erfd2(vector double x)67 static __inline vector double _erfd2(vector double x)
68 {
69 vec_uchar16 dup_even = ((vec_uchar16) { 0,1,2,3, 0,1,2,3, 8, 9,10,11, 8, 9,10,11 });
70 vec_double2 onehalfd = spu_splats(0.5);
71 vec_double2 oned = spu_splats(1.0);
72 vec_double2 sign_mask = spu_splats(-0.0);
73
74 /* This is where we switch from Taylor Series to Continued Fraction approximation */
75 vec_float4 approx_point = spu_splats(1.77f);
76
77 vec_double2 xabs, xsqu, xsign;
78 vec_double2 tresult, presult, result;
79
80 xsign = spu_and(x, sign_mask);
81 xabs = spu_andc(x, sign_mask);
82 xsqu = spu_mul(x, x);
83
84 /*
85 * Taylor Series Expansion near Zero
86 */
87 TAYLOR_ERF(xabs, xsqu, tresult);
88
89 /*
90 * Continued Fraction Approximation of Erfc().
91 * erf = 1 - erfc
92 */
93 CONTFRAC_ERFC(xabs, xsqu, presult);
94 presult = spu_sub(oned, presult);
95
96
97 /*
98 * Select the appropriate approximation.
99 */
100 vec_float4 xf = spu_roundtf(xabs);
101 xf = spu_shuffle(xf, xf, dup_even);
102 result = spu_sel(tresult, presult, (vec_ullong2)spu_cmpgt(xf, approx_point));
103
104
105 /*
106 * Special cases/errors.
107 */
108
109 /* x = +/- infinite, return +/-1 */
110 /* x = nan, return x */
111 result = spu_sel(result, oned, spu_testsv(x, SPU_SV_NEG_INFINITY | SPU_SV_POS_INFINITY));
112 result = spu_sel(result, x, spu_testsv(x, SPU_SV_NEG_DENORM | SPU_SV_POS_DENORM));
113
114 /*
115 * Preserve sign in result, since erf(-x) = -erf(x)
116 */
117 result = spu_or(result, xsign);
118
119 return result;
120 }
121
122 #endif /* _ERFD2_H_ */
123 #endif /* __SPU__ */
124