aboutsummaryrefslogtreecommitdiffstats
path: root/benchmarks/polybench-syn/linear-algebra/solvers/durbin.c
blob: 5c853a77620781ac1a556f66ed14482bc34316a1 (plain)
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
/**
 * This version is stamped on May 10, 2016
 *
 * Contact:
 *   Louis-Noel Pouchet <pouchet.ohio-state.edu>
 *   Tomofumi Yuki <tomofumi.yuki.fr>
 *
 * Web address: http://polybench.sourceforge.net
 */
/* durbin.c: this file is part of PolyBench/C */

#ifndef SYNTHESIS
#include <stdio.h>
#endif

unsigned int divider(unsigned int x, unsigned int y)
{
    unsigned int r0, q0, y0, y1;

    r0 = x;
    q0 = 0;
    y0 = y;
    y1 = y;
    do
    {
        y1 = 2 * y1;
    } while (y1 <= x);
    do
    {
        y1 = y1 / 2;
        q0 = 2 * q0;
        if (r0 >= y1)
        {
            r0 = r0 - y1;
            q0 = q0 + 1;
        }
    } while ((int)y1 != (int)y0);
    return q0;
}

int sdivider(int N, int D) {
    if (D < 0) {
        if (N < 0)
            return divider(-N, -D);
        else
            return -divider(N, -D);
    } else {
        if (N < 0)
            return -divider(-N, D);
        else
            return divider(N, D);
    }
}

unsigned int divider(unsigned int x, unsigned int y)
{
    unsigned int r0, q0, y0, y1;

    r0 = x;
    q0 = 0;
    y0 = y;
    y1 = y;
    do
    {
        y1 = 2 * y1;
    } while (y1 <= x);
    do
    {
        y1 = y1 / 2;
        q0 = 2 * q0;
        if (r0 >= y1)
        {
            r0 = r0 - y1;
            q0 = q0 + 1;
        }
    } while ((int)y1 != (int)y0);
    return q0;
}

int sdivider(int N, int D) {
    if (D < 0) {
        if (N < 0)
            return divider(-N, -D);
        else
            return -divider(N, -D);
    } else {
        if (N < 0)
            return -divider(-N, D);
        else
            return divider(N, D);
    }
}

#define plus(i) i = i + ONE
/* Include polybench common header. */
static
void init_array (int n,
                 int r[ 40 + 0])
{
    int ONE = 1;
    int i;

    for (i = 0; i < n; plus(i))
    {
        r[i] = (n+ONE-i);
    }
}



static
int print_array(int n,
                int y[ 40 + 0])

{
    int ONE = 1;
    int i;
    int res = 0;

    for (i = 0; i < n; plus(i)) {
        res  += y[i];
    }

#ifndef SYNTHESIS
    printf("finished = %u\n", res);
#endif

    return res;
}

static
void kernel_durbin(int n,
                   int r[ 40 + 0],
                   int y[ 40 + 0])
{
    int z[40];
    int alpha;
    int beta;
    int sum;

    int ONE = 1;
    int i,k;
    y[0] = -r[0];
    beta = 1;
    alpha = -r[0];

    for (k = 1; k < n; plus(k)) {
        beta = (ONE-alpha*alpha)*beta;
        sum = 0;
        for (i=0; i<k; plus(i)) {
            sum += r[k-i-ONE]*y[i];
        }
        alpha = - sdivider(r[k] + sum, beta);

        for (i=0; i<k; plus(i)) {
            z[i] = y[i] + alpha*y[k-i-ONE];
        }
        for (i=0; i<k; plus(i)) {
            y[i] = z[i];
        }
        y[k] = alpha;
    }
}


int main()
{
    int n = 40;
    int r[40 + 0];
    int y[40 + 0];

    init_array (n, r);
    kernel_durbin (n, r, y);

    return print_array(n, y);
}