File: vct.c

package info (click to toggle)
snd 3.4-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,148 kB
  • ctags: 12,594
  • sloc: ansic: 86,516; lisp: 3,480; sh: 1,507; makefile: 119
file content (334 lines) | stat: -rw-r--r-- 8,386 bytes parent folder | download
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
/* vct support 
 *
 * a vct is a Guile "smob" containing a float array and its size
 * we use these in Snd because Guile's floating-point operations are incredibly slow
 * and Snd/CLM are applications where no user is willing to wait on a multiply.
 *
 * C side:
 *   void init_vct(void)                   called to declare the various functions and the vct type in Guile
 *   int vct_p(SCM obj)                    is obj a vct
 *   SCM make_vct(int len, float *data)    make a new vct
 *   vct *get_vct(SCM arg)                 given SCM arg, return vct object
 *   void set_vct_print_length(int val)    set vct print length (default 10)
 *
 * Scheme side:
 *   (make-vct len)                        make new vct
 *   (vct? obj)                            is obj a vct
 *   (vct-ref v index)                     return vct->data[index]
 *   (vct-set! v index val)                vct->data[index] = val
 *   (vct-copy v)                          return a copy of v
 *   (vct-length v)                        return length of vct->data
 *   (vct-add! v1 v2)                      add (element-wise) v2 to v1 (changing v1), return v1
 *   (vct-offset! v1 scl)                  add scl to each element of v1 (changing v1), return v1
 *   (vct-multiply! v1 v2)                 multiply (element-wise, a windowing operation) v1 by v2, changing v1, return v1
 *   (vct-scale! v1 scl)                   multiply each element of v1 by scl, chaning v1, return v1
 *   (vct-fill! v1 val)                    set each element of v1 to val, returning v1
 *   (list->vct lst)                       return vct with elements of lst
 *
 * The intended use is a sort of latter-day array-processing system that handles huge
 * one-dimensional vectors -- fft's, etc.  Some of these functions can be found in
 * snd-gh.c in the Snd package; others can be found in the CLM package, (clm2scm.c).
 */

#if defined(HAVE_CONFIG_H)
  #include "config.h"
#endif
#ifdef DEBUG_MEMORY
  #include <stdlib.h>
  #include "sndlib.h"
#endif

#if HAVE_GUILE

#include "vct.h"

#ifndef CALLOC
  #define CALLOC(a,b)  calloc(a,b)
  #define MALLOC(a)    malloc(a)
  #define FREE(a)      free(a)
  #define REALLOC(a,b) realloc(a,b)
#endif

#define VCT_PRINT_LENGTH 10
#define ERRN1(a,b) SCM_ASSERT((gh_number_p(a)),a,SCM_ARG1,b)
#define ERRN2(a,b) SCM_ASSERT((gh_number_p(a)),a,SCM_ARG2,b)
#define ERRN3(a,b) SCM_ASSERT((gh_number_p(a)),a,SCM_ARG3,b)

#define RTNBOOL(a) return((a) ? SCM_BOOL_T : SCM_BOOL_F)
#define RTNINT(a) return(gh_int2scm(a))
#define RTNFLT(a) return(gh_double2scm(a))

#define GH_TYPE_OF(a) (SCM_TYP16(a))

#ifndef MIN
  #define MIN(a,b) ((a > b) ? (b) : (a))
#endif

static int vct_tag = 0;
static int vct_print_length = VCT_PRINT_LENGTH;
void set_vct_print_length(int len) {vct_print_length = len;}

static SCM mark_vct(SCM obj)
{
  SCM_SETGC8MARK(obj);
  return(SCM_BOOL_F);
}

int vct_p(SCM obj)
{
  return((SCM_NIMP(obj)) && (GH_TYPE_OF(obj) == (SCM)vct_tag));
}

static SCM g_vct_p(SCM obj) {RTNBOOL(vct_p(obj));}

vct *get_vct(SCM arg)
{
  if (vct_p(arg))
    return((vct *)gh_cdr(arg));
  return(NULL);
}

static scm_sizet free_vct(SCM obj)
{
  vct *v = (vct *)gh_cdr(obj);
  if (v->data) FREE(v->data);
  v->data = NULL;
  FREE(v);
  return(0);
}

static int print_vct(SCM obj, SCM port, scm_print_state *pstate)
{
  int len,i;
  char *buf;
  vct *v = (vct *)gh_cdr(obj);
  scm_puts("#<vct",port);
  len = vct_print_length;
  if (len > v->length) len = v->length;
  if (len > 0)
    {
      buf = (char *)CALLOC(32,sizeof(char));
      for (i=0;i<len;i++)
	{
	  sprintf(buf," %.3f",v->data[i]);
	  scm_puts(buf,port);
	}
      if (v->length > vct_print_length)
	scm_puts(" ...",port);
      FREE(buf);
    }
  scm_puts(">",port);
  return(1);
}

static SCM equalp_vct(SCM obj1, SCM obj2)
{
  vct *v1,*v2;
  int i;
  v1 = (vct *)gh_cdr(obj1);
  v2 = (vct *)gh_cdr(obj2);
  if (v1->length != v2->length) return(SCM_BOOL_F);
  for (i=0;i<v1->length;i++)
    if (v1->data[i] != v2->data[i])
      return(SCM_BOOL_F);
  return(SCM_BOOL_T);
}

SCM make_vct(int len, float *data)
{
  SCM ans;
  vct *new_vct;
  new_vct = (vct *)CALLOC(1,sizeof(vct));
  new_vct->length = len;
  new_vct->data = data;
  SCM_NEWCELL(ans);
  SCM_SETCAR(ans,vct_tag);
  SCM_SETCDR(ans,(SCM)new_vct);
  return(ans);
}

#if (!HAVE_MAKE_SMOB_TYPE)
static scm_smobfuns vct_smobfuns = {
  &mark_vct,
  &free_vct,
  &print_vct,
  &equalp_vct};
#endif

static SCM g_make_vct(SCM len)
{
  int size;
  ERRN1(len,S_make_vct);
  size = gh_scm2int(len);
  return(make_vct(size,(float *)CALLOC(size,sizeof(float))));
}

static SCM copy_vct(SCM obj)
{
  vct *v;
  float *copied_data;
  int len,i;
  ERRVCT1(obj,S_vct_copy);
  v = get_vct(obj);
  if (v)
    {
      len = v->length;
      copied_data = (float *)CALLOC(len,sizeof(float));
      for (i=0;i<len;i++) copied_data[i] = v->data[i];
      return(make_vct(len,copied_data));
    }
  return(SCM_BOOL_F);
}

static SCM vct_length(SCM obj)
{
  vct *v = get_vct(obj);
  ERRVCT1(obj,S_vct_length);
  if (v)
    RTNINT(v->length);
  RTNINT(0);
}

static SCM vct_ref(SCM obj, SCM pos)
{
  vct *v = get_vct(obj);
  int loc;
  ERRVCT1(obj,S_vct_ref);
  ERRN2(pos,S_vct_ref);
  if (v)
    {
      loc = gh_scm2int(pos);
      if ((loc >= 0) && (loc < v->length))
	RTNFLT(v->data[loc]);
      else scm_misc_error(S_vct_ref,"invalid index",SCM_LIST2(obj,pos));
    }
  else scm_misc_error(S_vct_ref,"nil vct?",SCM_EOL);
  RTNFLT(0.0);
}

static SCM vct_set(SCM obj, SCM pos, SCM val)
{
  vct *v = get_vct(obj);
  int loc;
  ERRVCT1(obj,S_vct_setB);
  ERRN2(pos,S_vct_setB);
  ERRN3(val,S_vct_setB);
  if (v)
    {
      loc = gh_scm2int(pos);
      if ((loc >= 0) && (loc < v->length))
	v->data[loc] = gh_scm2double(val);
      else scm_misc_error(S_vct_setB,"invalid index",SCM_LIST3(obj,pos,val));
    }
  else scm_misc_error(S_vct_setB,"nil vct?",SCM_EOL);
  return(val);
}

static SCM vct_multiply(SCM obj1, SCM obj2)
{
  int i,lim;
  vct *v1,*v2;
  ERRVCT1(obj1,S_vct_multiplyB);
  ERRVCT2(obj2,S_vct_multiplyB);
  v1 = get_vct(obj1);
  v2 = get_vct(obj2);
  if ((v1) && (v2))
    {
      lim = MIN(v1->length,v2->length);
      for (i=0;i<lim;i++) v1->data[i] *= v2->data[i];
    }
  return(obj1);
}

static SCM vct_add(SCM obj1, SCM obj2)
{
  int i,lim;
  vct *v1,*v2;
  ERRVCT1(obj1,S_vct_addB);
  ERRVCT2(obj2,S_vct_addB);
  v1 = get_vct(obj1);
  v2 = get_vct(obj2);
  if ((v1) && (v2))
    {
      lim = MIN(v1->length,v2->length);
      for (i=0;i<lim;i++) v1->data[i] += v2->data[i];
    }
  return(obj1);
}

static SCM vct_scale(SCM obj1, SCM obj2)
{
  int i;
  vct *v1;
  float scl;
  ERRVCT1(obj1,S_vct_scaleB);
  ERRN2(obj2,S_vct_scaleB);
  v1 = get_vct(obj1);
  scl = gh_scm2double(obj2);
  if (v1)
    for (i=0;i<v1->length;i++) v1->data[i] *= scl;
  return(obj1);
}

static SCM vct_offset(SCM obj1, SCM obj2)
{
  int i;
  vct *v1;
  float scl;
  ERRVCT1(obj1,S_vct_offsetB);
  ERRN2(obj2,S_vct_offsetB);
  v1 = get_vct(obj1);
  scl = gh_scm2double(obj2);
  if (v1)
    for (i=0;i<v1->length;i++) v1->data[i] += scl;
  return(obj1);
}

static SCM vct_fill(SCM obj1, SCM obj2)
{
  int i;
  vct *v1;
  float scl;
  ERRVCT1(obj1,S_vct_fillB);
  ERRN2(obj2,S_vct_fillB);
  v1 = get_vct(obj1);
  scl = gh_scm2double(obj2);
  if (v1)
    for (i=0;i<v1->length;i++) v1->data[i] = scl;
  return(obj1);
}

static SCM list2vct(SCM lst)
{
  int len,i;
  vct *v;
  SCM scv;
  SCM_ASSERT(gh_list_p(lst),lst,SCM_ARG1,S_list2vct);
  len = gh_length(lst);
  scv = make_vct(len,(float *)CALLOC(len,sizeof(float)));
  v = get_vct(scv);
  for (i=0;i<len;i++) v->data[i] = (float)gh_scm2double(scm_list_ref(lst,gh_int2scm(i)));
  return(scv);
}

void init_vct(void)
{
#if HAVE_MAKE_SMOB_TYPE
  vct_tag = scm_make_smob_type_mfpe("vct",sizeof(vct),mark_vct,free_vct,print_vct,equalp_vct);
#else
  vct_tag = scm_newsmob(&vct_smobfuns);
#endif
  gh_new_procedure1_0(S_vct_length,vct_length);
  gh_new_procedure2_0(S_vct_ref,vct_ref);
  gh_new_procedure3_0(S_vct_setB,vct_set);
  gh_new_procedure2_0(S_vct_multiplyB,vct_multiply);
  gh_new_procedure2_0(S_vct_scaleB,vct_scale);
  gh_new_procedure2_0(S_vct_fillB,vct_fill);
  gh_new_procedure2_0(S_vct_addB,vct_add);
  gh_new_procedure2_0(S_vct_offsetB,vct_offset);
  gh_new_procedure1_0(S_make_vct,g_make_vct);
  gh_new_procedure1_0(S_vct_copy,copy_vct);
  gh_new_procedure1_0(S_vct_p,g_vct_p);
  gh_new_procedure1_0(S_list2vct,list2vct);
}
#endif