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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* -*- mode: C -*- */
/* Some "inline" functions for generic scalar types */
#ifdef TRANSPOSE_2D_ARRAY
static SLang_Array_Type *TRANSPOSE_2D_ARRAY (SLang_Array_Type *at, SLang_Array_Type *bt)
{
GENERIC_TYPE *a_data, *b_data;
int nr, nc, i;
nr = at->dims[0];
nc = at->dims[1];
a_data = (GENERIC_TYPE *) at->data;
b_data = (GENERIC_TYPE *) bt->data;
for (i = 0; i < nr; i++)
{
GENERIC_TYPE *offset = b_data + i;
int j;
for (j = 0; j < nc; j++)
{
*offset = *a_data++;
offset += nr;
}
}
return bt;
}
#undef TRANSPOSE_2D_ARRAY
#endif
#ifdef INNERPROD_FUNCTION
static void INNERPROD_FUNCTION
(SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
unsigned int a_loops, unsigned int a_stride,
unsigned int b_loops, unsigned int b_inc,
unsigned int inner_loops)
{
GENERIC_TYPE_A *a;
GENERIC_TYPE_B *b;
GENERIC_TYPE_C *c;
c = (GENERIC_TYPE_C *) ct->data;
b = (GENERIC_TYPE_B *) bt->data;
a = (GENERIC_TYPE_A *) at->data;
while (a_loops--)
{
GENERIC_TYPE_B *bb;
unsigned int j;
bb = b;
for (j = 0; j < inner_loops; j++)
{
double x = (double) a[j];
if (x != 0.0)
{
unsigned int k;
for (k = 0; k < b_loops; k++)
c[k] += x * bb[k];
}
bb += b_inc;
}
c += b_loops;
a += a_stride;
}
}
#undef INNERPROD_FUNCTION
#undef GENERIC_TYPE_A
#undef GENERIC_TYPE_B
#undef GENERIC_TYPE_C
#endif
#ifdef INNERPROD_COMPLEX_A
static void INNERPROD_COMPLEX_A
(SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
unsigned int a_loops, unsigned int a_stride,
unsigned int b_loops, unsigned int b_inc,
unsigned int inner_loops)
{
double *a;
GENERIC_TYPE *b;
double *c;
c = (double *) ct->data;
b = (GENERIC_TYPE *) bt->data;
a = (double *) at->data;
a_stride *= 2;
while (a_loops--)
{
GENERIC_TYPE *bb;
unsigned int bb_loops;
bb = b;
bb_loops = b_loops;
while (bb_loops--)
{
double real_sum;
double imag_sum;
unsigned int iloops;
double *aa;
GENERIC_TYPE *bbb;
aa = a;
bbb = bb;
iloops = inner_loops;
real_sum = 0.0;
imag_sum = 0.0;
while (iloops--)
{
real_sum += aa[0] * (double)bbb[0];
imag_sum += aa[1] * (double)bbb[0];
aa += 2;
bbb += b_inc;
}
*c++ = real_sum;
*c++ = imag_sum;
bb++;
}
a += a_stride;
}
}
static void INNERPROD_A_COMPLEX
(SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
unsigned int a_loops, unsigned int a_stride,
unsigned int b_loops, unsigned int b_inc,
unsigned int inner_loops)
{
GENERIC_TYPE *a;
double *b;
double *c;
c = (double *) ct->data;
b = (double *) bt->data;
a = (GENERIC_TYPE *) at->data;
b_inc *= 2;
while (a_loops--)
{
double *bb;
unsigned int bb_loops;
bb = b;
bb_loops = b_loops;
while (bb_loops--)
{
double real_sum;
double imag_sum;
unsigned int iloops;
GENERIC_TYPE *aa;
double *bbb;
aa = a;
bbb = bb;
iloops = inner_loops;
real_sum = 0.0;
imag_sum = 0.0;
while (iloops--)
{
real_sum += (double)aa[0] * bbb[0];
imag_sum += (double)aa[0] * bbb[1];
aa += 1;
bbb += b_inc;
}
*c++ = real_sum;
*c++ = imag_sum;
bb += 2;
}
a += a_stride;
}
}
#undef INNERPROD_A_COMPLEX
#undef INNERPROD_COMPLEX_A
#endif /* INNERPROD_COMPLEX_A */
#ifdef INNERPROD_COMPLEX_COMPLEX
static void INNERPROD_COMPLEX_COMPLEX
(SLang_Array_Type *at, SLang_Array_Type *bt, SLang_Array_Type *ct,
unsigned int a_loops, unsigned int a_stride,
unsigned int b_loops, unsigned int b_inc,
unsigned int inner_loops)
{
double *a;
double *b;
double *c;
c = (double *) ct->data;
b = (double *) bt->data;
a = (double *) at->data;
a_stride *= 2;
b_inc *= 2;
while (a_loops--)
{
double *bb;
unsigned int bb_loops;
bb = b;
bb_loops = b_loops;
while (bb_loops--)
{
double real_sum;
double imag_sum;
unsigned int iloops;
double *aa;
double *bbb;
aa = a;
bbb = bb;
iloops = inner_loops;
real_sum = 0.0;
imag_sum = 0.0;
while (iloops--)
{
real_sum += aa[0]*bbb[0] - aa[1]*bbb[1];
imag_sum += aa[0]*bbb[1] + aa[1]*bbb[0];
aa += 2;
bbb += b_inc;
}
*c++ = real_sum;
*c++ = imag_sum;
bb += 2;
}
a += a_stride;
}
}
#undef INNERPROD_COMPLEX_COMPLEX
#endif
#ifdef SUM_FUNCTION
#if SLANG_HAS_FLOAT
static int SUM_FUNCTION (VOID_STAR xp, unsigned int inc, unsigned int num, VOID_STAR yp)
{
GENERIC_TYPE *x, *xmax;
double sum;
sum = 0.0;
x = (GENERIC_TYPE *) xp;
xmax = x + num;
#if _SLANG_OPTIMIZE_FOR_SPEED
if (inc == 1)
{
while (x < xmax)
{
sum += (double) *x;
x++;
}
}
else
#endif
while (x < xmax)
{
sum += (double) *x;
x += inc;
}
*(SUM_RESULT_TYPE *)yp = (SUM_RESULT_TYPE) sum;
return 0;
}
#endif /* SLANG_HAS_FLOAT */
#undef SUM_FUNCTION
#undef SUM_RESULT_TYPE
#endif
#ifdef MIN_FUNCTION
static int
MIN_FUNCTION (VOID_STAR ip, unsigned int inc, unsigned int num, VOID_STAR sp)
{
unsigned int n;
GENERIC_TYPE m;
GENERIC_TYPE *i = (GENERIC_TYPE *)ip;
if (-1 == check_for_empty_array ("min", num))
return -1;
m = i[0];
for (n = inc; n < num; n += inc)
if (m > i[n]) m = i[n];
*(GENERIC_TYPE *)sp = m;
return 0;
}
#undef MIN_FUNCTION
#endif
#ifdef MAX_FUNCTION
static int
MAX_FUNCTION (VOID_STAR ip, unsigned int inc, unsigned int num, VOID_STAR s)
{
unsigned int n;
GENERIC_TYPE m;
GENERIC_TYPE *i = (GENERIC_TYPE *) ip;
if (-1 == check_for_empty_array ("max", num))
return -1;
m = i[0];
for (n = inc; n < num; n += inc)
if (m < i[n]) m = i[n];
*(GENERIC_TYPE *)s = m;
return 0;
}
#undef MAX_FUNCTION
#endif
#ifdef CUMSUM_FUNCTION
#ifdef SLANG_HAS_FLOAT
static int
CUMSUM_FUNCTION (SLtype xtype, VOID_STAR xp, unsigned int inc,
unsigned int num,
SLtype ytype, VOID_STAR yp, VOID_STAR clientdata)
{
GENERIC_TYPE *x, *xmax;
CUMSUM_RESULT_TYPE *y;
double c;
(void) xtype;
(void) ytype;
(void) clientdata;
x = (GENERIC_TYPE *) xp;
y = (CUMSUM_RESULT_TYPE *) yp;
xmax = x + num;
c = 0.0;
while (x < xmax)
{
c += (double) *x;
*y = (CUMSUM_RESULT_TYPE) c;
x += inc;
y += inc;
}
return 0;
}
#endif /* SLANG_HAS_FLOAT */
#undef CUMSUM_FUNCTION
#undef CUMSUM_RESULT_TYPE
#endif
#ifdef GENERIC_TYPE
# undef GENERIC_TYPE
#endif
|