File: erf.cl

package info (click to toggle)
pocl 7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 29,768 kB
  • sloc: lisp: 151,669; ansic: 135,425; cpp: 65,801; python: 1,846; sh: 1,084; ruby: 255; pascal: 231; tcl: 180; makefile: 174; asm: 81; java: 72; xml: 49
file content (181 lines) | stat: -rw-r--r-- 5,060 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* OpenCL built-in library: erf()

   Copyright (c) 2015 Michal Babej / Tampere University of Technology

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "hsail_templates.h"

/*
 * The following code is adapted from
 * http://www.johndcook.com/blog/2009/01/19/stand-alone-error-function-erf/
 * which is licensed Public Domain.
 *
 * severely limited precision, only useful for floats.
 * TODO it's probably not accurate enough on the entire range.
 */

float _cl_builtin_erff(float g)
{
  float x = fabs(g);
  if (x >= 4.0f)
    return (g > 0.0f) ? 1.0f : -1.0f;

  // constants
  float a1 =  0.254829592f;
  float a2 = -0.284496736f;
  float a3 =  1.421413741f;
  float a4 = -1.453152027f;
  float a5 =  1.061405429f;
  float p  =  0.3275911f;

  // A&S formula 7.1.26
  float t = 1.0f / fma(p, x, 1.0f);
  float temp = fma(a5, t, a4);
  temp = fma(temp, t, a3);
  temp = fma(temp, t, a2);
  temp = fma(temp, t, a1);
  temp *= t;
  float y = fma(temp, exp(-x*x), -1.0f);

  return (g > 0.0f) ? -y : y;
}

float _cl_builtin_erfcf(float g);





/* The following code is adapted from
 * from cpython/Modules/mathmodule.c
 *
 * Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 2014, 2015 Python Software Foundation; All Rights Reserved
 * which has Python Software License (BSD compatible).
 *
 * Changes:
 * - adapt to OpenCL builtins (isnan etc)
 * - use fma() where possible.
 */


#define ERF_SERIES_CUTOFF 1.5
#define ERF_SERIES_TERMS 25
#define ERFC_CONTFRAC_CUTOFF 30.0
#define ERFC_CONTFRAC_TERMS 50

#define SQRTPI 1.772453850905516027298167483341145182798

/*
   Error function, via power series.
   Given a finite float x, return an approximation to erf(x).
   Converges reasonably fast for small x.
*/

double m_erf_series(double x)
{
    double x2, acc, fk, result, temp;
    int i;

    x2 = x * x;
    acc = 0.0;
    fk = (double)ERF_SERIES_TERMS + 0.5;
    for (i = 0; i < ERF_SERIES_TERMS; i++) {
        temp = acc / fk;
        //acc = 2.0 + x2 * temp;
        acc = fma(x2, temp, 2.0);
        fk -= 1.0;
    }
    return (acc * x * exp(-x2) / SQRTPI);
}

/*
   Complementary error function, via continued fraction expansion.
   Given a positive float x, return an approximation to erfc(x).  Converges
   reasonably fast for x large (say, x > 2.0), and should be safe from
   overflow if x and nterms are not too large.  On an IEEE 754 machine, with x
   <= 30.0, we're safe up to nterms = 100.  For x >= 30.0, erfc(x) is smaller
   than the smallest representable nonzero float.  */

double m_erfc_contfrac(double x)
{
    double x2, a, da, p, p_last, q, q_last, b, result;
    int i;

    if (x >= ERFC_CONTFRAC_CUTOFF)
        return 0.0;

    x2 = x*x;
    a = 0.0;
    da = 0.5;
    p = 1.0; p_last = 0.0;
    q = da + x2; q_last = 1.0;
    for (i = 0; i < ERFC_CONTFRAC_TERMS; i++) {
        double temp;
        a += da;
        da += 2.0;
        b = da + x2;
        //temp = p; p = b*p - a*p_last; p_last = temp;
        temp = p; p = fma(b, p, -a*p_last); p_last = temp;
        //temp = q; q = b*q - a*q_last; q_last = temp;
        temp = q; q = fma(b, q, -a*q_last); q_last = temp;
    }
    return (p / q * x * exp(-x2) / SQRTPI);
}

/* Error function erf(x), for general x */

double _cl_builtin_erf(double x)
{
    double absx, cf;

    if (isnan(x))
        return x;
    absx = fabs(x);
    if (absx < ERF_SERIES_CUTOFF)
        return m_erf_series(x);
    else {
        cf = m_erfc_contfrac(absx);
        return (x > 0.0) ? 1.0 - cf : cf - 1.0;
    }
}

/* Complementary error function erfc(x), for general x. */

double _cl_builtin_erfc(double x)
{
    double absx, cf;

    if (isnan(x))
        return x;
    absx = fabs(x);
    if (absx < ERF_SERIES_CUTOFF)
        return 1.0 - m_erf_series(x);
    else {
        cf = m_erfc_contfrac(absx);
        return (x > 0.0) ? cf : 2.0 - cf;
    }
}


IMPLEMENT_EXPR_ALL(erf, V_V, _cl_builtin_erff(a), _cl_builtin_erf(a))

IMPLEMENT_EXPR_ALL(erfc, V_V, _cl_builtin_erfcf(a), _cl_builtin_erfc(a))