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 _ERFCD2_H_
40 #define _ERFCD2_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 _erfcd2(vector double x)
52 *
53 * DESCRIPTION
54 * The erfcd2 function computes the complement error function of each element of x.
55 *
56 * Accuracy Note: We would benefit from a rational approximation in the domain
57 * 1.2 < x < 2.0 and also around x = 2.5.
58 *
59 * C99 Special Cases:
60 * - erfc(+0) returns +1
61 * - erfc(-0) returns +1
62 * - erfc(+infinite) returns +0
63 * - erfc(-infinite) returns +2
64 *
65 * Other Cases:
66 * - erfc(Nan) returns Nan
67 *
68 */
69
_erfcd2(vector double x)70 static __inline vector double _erfcd2(vector double x)
71 {
72 vec_uchar16 dup_even = ((vec_uchar16) { 0,1,2,3, 0,1,2,3, 8,9,10,11, 8,9,10,11 });
73 vec_double2 onehalfd = spu_splats(0.5);
74 vec_double2 zerod = spu_splats(0.0);
75 vec_double2 oned = spu_splats(1.0);
76 vec_double2 twod = spu_splats(2.0);
77 vec_double2 sign_mask = spu_splats(-0.0);
78
79 /* This is where we switch from near zero approx. */
80 vec_float4 approx_point = spu_splats(1.71f);
81
82 vec_double2 xabs, xsqu, xsign;
83 vec_uint4 isneg;
84 vec_double2 tresult, presult, result;
85
86 xsign = spu_and(x, sign_mask);
87 xabs = spu_andc(x, sign_mask);
88 xsqu = spu_mul(x, x);
89
90 /*
91 * Use Taylor Series for x near 0
92 * Preserve sign of x in result, since erf(-x) = -erf(x)
93 * This approximation is for erf, so adjust for erfc.
94 */
95 TAYLOR_ERF(xabs, xsqu, tresult);
96 tresult = spu_or(tresult, xsign);
97 tresult = spu_sub(oned, tresult);
98
99 /*
100 * Now, use the Continued Fractions approximation away
101 * from 0. If x < 0, use erfc(-x) = 2 - erfc(x)
102 */
103 CONTFRAC_ERFC(xabs, xsqu, presult);
104 isneg = (vec_uint4)spu_shuffle(x, x, dup_even);
105 isneg = spu_rlmaska(isneg, -32);
106 presult = spu_sel(presult, spu_sub(twod, presult), (vec_ullong2)isneg);
107
108 /*
109 * Select the appropriate approximation.
110 */
111 vec_float4 xf = spu_roundtf(xabs);
112 xf = spu_shuffle(xf, xf, dup_even);
113 result = spu_sel(tresult, presult, (vec_ullong2)spu_cmpgt(xf, approx_point));
114
115 /*
116 * Special cases
117 */
118 result = spu_sel(result, twod, spu_testsv(x, SPU_SV_NEG_INFINITY));
119 result = spu_sel(result, zerod, spu_testsv(x, SPU_SV_POS_INFINITY));
120 result = spu_sel(result, x, spu_testsv(x, SPU_SV_NEG_DENORM | SPU_SV_POS_DENORM));
121
122 return result;
123 }
124
125 #endif /* _ERFCD2_H_ */
126 #endif /* __SPU__ */
127