File: cs_par_base.c

package info (click to toggle)
csound 1%3A6.03.2~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 45,708 kB
  • ctags: 30,934
  • sloc: ansic: 166,298; cpp: 57,275; python: 9,433; xml: 1,302; yacc: 1,072; lex: 833; perl: 635; java: 618; lisp: 562; sh: 514; makefile: 459; tcl: 341
file content (683 lines) | stat: -rw-r--r-- 19,585 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
    cs_par_base.c:

    Copyright (C) 2009: Chris Wilson and John ffitch

    This file is part of Csound.

    The Csound Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    Csound is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

#include <stdio.h>
#include <stdlib.h>

#include "csoundCore.h"

#include "cs_par_base.h"

int csp_thread_index_get(CSOUND *csound)
{
    void *threadId = csound->GetCurrentThreadID();

    int index = 0;
    THREADINFO *current = csound->multiThreadedThreadInfo;

    if (UNLIKELY(current == NULL)) {
      return -1;
    }

    while (current != NULL) {
      if (UNLIKELY(pthread_equal(*(pthread_t *)threadId,
                                 *(pthread_t *)current->threadId))) {
        free(threadId);
        return index;
      }
      index++;
      current = current->next;
    }
    return -1;
}


/* **** An implementation of Barriers for MAC that lacks them **** */
#if defined(__MACH__) || defined(ANDROID) || defined(NACL)
/*#define BARRIER_SERIAL_THREAD (-1)

typedef struct {
  pthread_mutex_t mut;
  pthread_cond_t cond;
  unsigned int count, max, iteration;
} barrier_t;
*/
extern int barrier_init(barrier_t *b, void *,unsigned int max);
extern int barrier_destroy(barrier_t *b);
extern int barrier_wait(barrier_t *b);

#ifndef PTHREAD_BARRIER_SERIAL_THREAD
/*#define pthread_barrier_t barrier_t */
#define PTHREAD_BARRIER_SERIAL_THREAD BARRIER_SERIAL_THREAD
#define pthread_barrier_init(barrier, attr, count) \
  barrier_init(barrier,NULL,count)
#define pthread_barrier_destroy barrier_destroy
#define pthread_barrier_wait barrier_wait
#endif

int barrier_init(barrier_t *b, void *dump, unsigned int max)
{
    if (UNLIKELY(max == 0)) return EINVAL;

    if (UNLIKELY(pthread_mutex_init(&b->mut, NULL))) {
      return -1;
    }

    if (UNLIKELY(pthread_cond_init(&b->cond, NULL))) {
      int err = errno;
      pthread_mutex_destroy(&b->mut);
      errno = err;
      return -1;
    }

    b->count = 0;
    b->iteration = 0;
    b->max = max;

    return 0;
}

int barrier_destroy(barrier_t *b)
{
    if (UNLIKELY(b->count > 0)) return EBUSY;

    pthread_cond_destroy(&b->cond);
    pthread_mutex_destroy(&b->mut);

    return 0;
}

/* when barrier is passed, all threads except one return 0 */
int barrier_wait(barrier_t *b)
{
    int ret, it;

    pthread_mutex_lock(&b->mut);
    b->count++;
    it = b->iteration;
    if (b->count >= b->max) {
      b->count = 0;
      b->iteration++;
      pthread_cond_broadcast(&b->cond);
      ret = BARRIER_SERIAL_THREAD;
    }
    else {
      while (it == b->iteration) pthread_cond_wait(&b->cond, &b->mut);
      ret = 0;
    }
    pthread_mutex_unlock(&b->mut);

    return ret;
}
#endif

/***********************************************************************
 * parallel primitives
 */
void csp_barrier_alloc(CSOUND *csound, pthread_barrier_t **barrier,
                       int thread_count)
{
    if (UNLIKELY(barrier == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter barrier"));
    if (UNLIKELY(thread_count < 1))
      csound->Die(csound, Str("Invalid Parameter thread_count must be > 0"));

    *barrier = (pthread_barrier_t *)csound->Malloc(csound,
                                                   sizeof(pthread_barrier_t));
    if (UNLIKELY(*barrier == NULL)) {
        csound->Die(csound, Str("Failed to allocate barrier"));
    }
    pthread_barrier_init(*barrier, NULL, thread_count);
}

void csp_barrier_dealloc(CSOUND *csound, pthread_barrier_t **barrier)
{
    if (UNLIKELY(barrier == NULL || *barrier == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter barrier"));

    pthread_barrier_destroy(*barrier);
}

/***********************************************************************
 * semaphore
 */

//void csp_semaphore_alloc(CSOUND *csound, sem_t **sem, int max_threads)
//{
//    if (UNLIKELY(sem == NULL))
//      csound->Die(csound, "Invalid NULL Parameter sem");
//    if (*sem != NULL) {
//      int val;
//      sem_getvalue(*sem, &val);
//      csound->DebugMsg(csound, "Already a semaphore: %d %p\n", val, *sem);
//      sem_destroy(*sem);
//      csound->Free(csound, *sem);
//    }
//
//    *sem = csound->Malloc(csound, sizeof(sem_t));
//    if (UNLIKELY(*sem == NULL)) {
//      csound->Die(csound, "Failed to allocate semaphore");
//    }
//    sem_init(*sem, 0, 1);
//}
//
//void csp_semaphore_dealloc(CSOUND *csound, sem_t **sem)
//{
//    if (UNLIKELY(sem == NULL || *sem == NULL))
//      csound->Die(csound, "Invalid NULL Parameter sem");
//    sem_destroy(*sem);
//    csound->Free(csound, *sem);
//    *sem = NULL;
//}
//
//void csp_semaphore_wait(CSOUND *csound, sem_t *sem)
//{
//    if (UNLIKELY(sem == NULL))
//       csound->Die(csound, "Invalid NULL Parameter sem");
//
//    TRACE_1("[%i] wait:\n", csp_thread_index_get(csound));
//    {
//      /* int val, res; */
//      /* res = sem_getvalue(sem, &val); */
//      /* csound->DebugMsg(csound, "before sem_wait: %d %i:\t", val, */
//      /*         csp_thread_index_get(csound)); */
//      sem_wait(sem);
//      /* res = sem_getvalue(sem, &val); */
//      /* csound->DebugMsg(csound, "after sem_wait: %d %i\n", val, */
//      /*         csp_thread_index_get(csound)); */
//    }
//    TRACE_1("[%i] continue:\n", csp_thread_index_get(csound));
//}
//
//void csp_semaphore_grow(CSOUND *csound, sem_t *sem)
//{
//    /* int val, res; */
//    /* res = sem_getvalue(sem, &val); */
//    /* csound->DebugMsg(csound, "before sem_grow: %d %i\t",
//               val, csp_thread_index_get(csound)); */
//    sem_post(sem);
//    /* res = sem_getvalue(sem, &val); */
//    /* csound->DebugMsg(csound, "after sem_grow: %d %i\n",
//               val, csp_thread_index_get(csound)); */
//}
//
//void csp_semaphore_release(CSOUND *csound, sem_t *sem)
//{
//    /* TRACE_2("[%i] post:\n", csp_thread_index_get(csound)); */
//    /* { */
//    /*   int val, res; */
//    /*   res = sem_getvalue(sem, &val); */
//    /*   csound->DebugMsg(csound, "before sem_release: %d %i\n", */
//    /*           val, csp_thread_index_get(csound)); */
//    /* } */
//}
//
//void csp_semaphore_release_end(CSOUND *csound, sem_t *sem)
//{
//    /* int val, res; */
//    /* res = sem_getvalue(sem, &val); */
//    /* csound->DebugMsg(csound, "before sem_release_end: %d %i\t", */
//    /*         val, csp_thread_index_get(csound)); */
//    sem_post(sem);
//    /* res = sem_getvalue(sem, &val); */
//    /* csound->DebugMsg(csound, "after sem_release_end: %d %i\n", */
//    /*         val, csp_thread_index_get(csound)); */
//}
//
//void csp_semaphore_release_print(CSOUND *csound, sem_t *sem)
//{
//    int val, res;
//    if (UNLIKELY(sem == NULL))
//      csound->Die(csound, "Invalid NULL Parameter sem");
//    res = sem_getvalue(sem, &val);
//    csound->DebugMsg(csound, "sem_release_print: %d %p\n",
//            val, csp_thread_index_get(csound));
//}
//

/***********************************************************************
 * set data structure
 */

/* static prototypes */
static int set_element_delloc(CSOUND *csound,
                              struct set_element_t **set_element);
static int set_element_alloc(CSOUND *csound,
                             struct set_element_t **set_element,
                             char *data);
static int set_is_set(CSOUND *csound, struct set_t *set);
#if 0
static int
  set_element_is_set_element(CSOUND *csound,
                             struct set_element_t *set_element);
#endif

int csp_set_alloc(CSOUND *csound, struct set_t **set,
                  set_element_data_eq *ele_eq_func,
                  set_element_data_print *ele_print_func)
{
    if (UNLIKELY(set == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter set"));

    *set = csound->Malloc(csound, sizeof(struct set_t));
    if (UNLIKELY(*set == NULL)) {
      csound->Die(csound, Str("Failed to allocate set"));
    }
    memset(*set, 0, sizeof(struct set_t));
    strncpy((*set)->hdr, SET_HDR, HDR_LEN);
    (*set)->ele_eq_func = ele_eq_func;
    (*set)->ele_print_func = ele_print_func;
    (*set)->cache = NULL;

    return CSOUND_SUCCESS;
}

int csp_set_dealloc(CSOUND *csound, struct set_t **set)
{
    struct set_element_t *ele;
    if (UNLIKELY(set == NULL || *set == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter set"));
    if (UNLIKELY(!set_is_set(csound, *set)))
      csound->Die(csound, Str("Invalid Parameter set not a set"));

    if ((*set)->cache != NULL) csound->Free(csound, (*set)->cache);

    ele = (*set)->head;
    while (ele != NULL) {
      struct set_element_t *next = ele->next;
      set_element_delloc(csound, &ele);
      ele = next;
    }

    csound->Free(csound, *set);
    *set = NULL;

    return CSOUND_SUCCESS;
}

static int set_element_alloc(CSOUND *csound,
                             struct set_element_t **set_element,
                             char *data)
{
    if (UNLIKELY(set_element == NULL || data == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter data"));

    *set_element = csound->Malloc(csound, sizeof(struct set_element_t));
    if (UNLIKELY(*set_element == NULL)) {
      csound->Die(csound, Str("Failed to allocate set element"));
    }
    memset(*set_element, 0, sizeof(struct set_element_t));
    strncpy((*set_element)->hdr, SET_ELEMENT_HDR, HDR_LEN);
    (*set_element)->data = data;

    return CSOUND_SUCCESS;
}

static int set_element_delloc(CSOUND *csound,
                              struct set_element_t **set_element)
{
    if (UNLIKELY(set_element == NULL || *set_element == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter set_element"));
    csound->Free(csound, *set_element);
    *set_element = NULL;

    return CSOUND_SUCCESS;
}

static int set_is_set(CSOUND *csound, struct set_t *set)
{
    char buf[4];
    if (set == NULL) return 0;
    strncpy(buf, (char *)set, HDR_LEN);
    buf[3] = 0;
    return strcmp(buf, SET_HDR) == 0;
}

#if 0
static int
  set_element_is_set_element(CSOUND *csound,
                             struct set_element_t *set_element)
{
    char buf[4];
    if (set_element == NULL) return 0;
    strncpy(buf, (char *)set_element, HDR_LEN);
    buf[3] = 0;
    return strcmp(buf, SET_ELEMENT_HDR) == 0;
}
#endif

int csp_set_alloc_string(CSOUND *csound, struct set_t **set)
{
    return csp_set_alloc(csound, set,
                         csp_set_element_string_eq,
                         csp_set_element_string_print);
}

int csp_set_element_string_eq(struct set_element_t *ele1,
                              struct set_element_t *ele2)
{
    return strcmp((char *)ele1->data, (char *)ele2->data) == 0;
}

int csp_set_element_ptr_eq(struct set_element_t *ele1,
                           struct set_element_t *ele2)
{
    return (ele1->data == ele2->data);
}

void csp_set_element_string_print(CSOUND *csound,
                                  struct set_element_t *ele)
{
    csound->Message(csound, (char *)ele->data);
}

void csp_set_element_ptr_print(CSOUND *csound,
                               struct set_element_t *ele)
{
    csound->Message(csound, "%p", ele->data);
}

static int set_update_cache(CSOUND *csound, struct set_t *set)
{
    if (set->cache != NULL) {
      csound->Free(csound, set->cache);
      set->cache = NULL;
    }
    if (set->count > 0) {
      struct set_element_t *ele;
      int ctr = 0;
      set->cache =
        csound->Malloc(csound,
                       sizeof(struct set_element_t *) * set->count);
      ele = set->head;
      while (ele != NULL) {
        set->cache[ctr] = ele;
        ctr++;
        ele = ele->next;
      }
    }
    return CSOUND_SUCCESS;
}

/*
 * if out_set_element is not NULL and the element corresponding to
 * data is not found it will not be changed
 */
static int set_element_get(CSOUND *csound, struct set_t *set,
                           char *data,
                           struct set_element_t **out_set_element)
{
#ifdef SET_DEBUG
    if (UNLIKELY(set == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter set"));
    if (UNLIKELY(data == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter data"));
    if (UNLIKELY(out_set_element == NULL))
      csound->Die(csound, Str("Invalid NULL Parameter out_set_element"));
#endif
    {
      struct set_element_t *ele = set->head;
      struct set_element_t data_ele = { SET_ELEMENT_HDR, data, 0 };
      while (ele != NULL) {
        if (set->ele_eq_func(ele, &data_ele)) {
          *out_set_element = ele;
          break;
        }
        ele = ele->next;
      }
    }
    return CSOUND_SUCCESS;
}

int csp_set_add(CSOUND *csound, struct set_t *set, void *data)
{
    struct set_element_t *ele = NULL;
#ifdef SET_DEBUG
    if (UNLIKELY(set == NULL))
      csound->Die(csound, "Invalid NULL Parameter set");
    if (UNLIKELY(data == NULL))
      csound->Die(csound, "Invalid NULL Parameter data");
#endif

    if (csp_set_exists(csound, set, data)) {
        return CSOUND_SUCCESS;
    }

    set_element_alloc(csound, &ele, data);
    if (set->head == NULL) {
      set->head = ele;
      set->tail = ele;
    }
    else {
      set->tail->next = ele;
      set->tail = ele;
    }
    set->count++;

    set_update_cache(csound, set);

    return CSOUND_SUCCESS;
}

int csp_set_remove(CSOUND *csound, struct set_t *set, void *data)
{
#ifdef SET_DEBUG
    if (UNLIKELY(set == NULL))
      csound->Die(csound, "Invalid NULL Parameter set");
    if (UNLIKELY(data == NULL))
      csound->Die(csound, "Invalid NULL Parameter data");
#endif
    {
      struct set_element_t *ele = set->head, *prev = NULL;
      struct set_element_t data_ele = { SET_ELEMENT_HDR, data, 0 };
      while (ele != NULL) {
        if (set->ele_eq_func(ele, &data_ele)) {
          if (ele == set->head && ele == set->tail) {
            set->head = NULL;
            set->tail = NULL;
          }
          else if (ele == set->head) {
            set->head = ele->next;
          }
          else {
            prev->next = ele->next;
          }
          set_element_delloc(csound, &ele);
          set->count--;
          break;
        }
        prev = ele;
        ele = ele->next;
      }
    }
    set_update_cache(csound, set);

    return CSOUND_SUCCESS;
}

int csp_set_exists(CSOUND *csound, struct set_t *set, void *data)
{
    struct set_element_t *ele = NULL;
#ifdef SET_DEBUG
    if (UNLIKELY(set == NULL))
      csound->Die(csound, "Invalid NULL Parameter set");
    if (UNLIKELY(data == NULL))
      csound->Die(csound, "Invalid NULL Parameter data");
#endif
    set_element_get(csound, set, data, &ele);

    return (ele == NULL ? 0 : 1);
}

int csp_set_print(CSOUND *csound, struct set_t *set)
{
    struct set_element_t *ele;
#ifdef SET_DEBUG
    if (UNLIKELY(set == NULL))
      csound->Die(csound, "Invalid NULL Parameter set");
    if (UNLIKELY(!set_is_set(csound, set)))
      csound->Die(csound, "Invalid Parameter set not a set");
#endif

    ele = set->head;

    csound->Message(csound, "{ ");
    while (ele != NULL) {
      set->ele_print_func(csound, ele);
      if (ele->next != NULL) csound->Message(csound, ", ");
      ele = ele->next;
    }
    csound->Message(csound, " }\n");

    return CSOUND_SUCCESS;
}

inline int csp_set_count(struct set_t *set)
{
#ifdef SET_DEBUG
    if (UNLIKELY(set == NULL))
      csound->Die(csound, "Invalid NULL Parameter set");
    if (UNLIKELY(!set_is_set(csound, set)))
      csound->Die(csound, "Invalid Parameter set not a set");
#endif

    return set->count;
}

/* 0 indexed */
inline int csp_set_get_num(struct set_t *set, int num, void **data)
{
#ifdef SET_DEBUG
    if (UNLIKELY(set == NULL))
      csound->Die(csound, "Invalid NULL Parameter set");
    if (UNLIKELY(!set_is_set(csound, set)))
      csound->Die(csound, "Invalid Parameter set not a set");
    if (UNLIKELY(um >= set->count))
      csound->Die(csound, "Invalid Parameter num is out of bounds");
    if (UNLIKELY(data == NULL))
      csound->Die(csound, "Invalid NULL Parameter data");
#endif

    *data = set->cache[num]->data;
    /*
    if (set->cache != NULL) {

    }
    else {
    int ctr = 0;
    struct set_element_t *ele = set->head;
    while (ctr < num && ele != NULL) {
    ctr++;
    ele = ele->next;
        }
        if (ctr == num && ele != NULL) {
            *data = ele->data;
        }
    }
    */
    return CSOUND_SUCCESS;
}

int csp_set_union(CSOUND *csound, struct set_t *first,
                  struct set_t *second, struct set_t **result)
{
    int ctr = 0;
    int first_len;
    int second_len;
#ifdef SET_DEBUG
    if (UNLIKELY(first == NULL))
      csound->Die(csound, "Invalid NULL Parameter first");
    if (UNLIKELY(!set_is_set(csound, first)))
      csound->Die(csound, "Invalid Parameter set not a first");
    if (UNLIKELY(second == NULL))
      csound->Die(csound, "Invalid NULL Parameter second");
    if (UNLIKELY(!set_is_set(csound, second)))
      csound->Die(csound, "Invalid Parameter set not a second");
    if (UNLIKELY(result == NULL))
 csound->Die(csound, "Invalid NULL Parameter result");
    if (UNLIKELY(first->ele_eq_func != second->ele_eq_func))
      csound->Die(csound,
                  "Invalid sets for comparison (different equality)");
#endif

    csp_set_alloc(csound, result,
                  first->ele_eq_func, first->ele_print_func);

    first_len = csp_set_count(first);
    second_len = csp_set_count(second);

    while (ctr < first_len) {
      void *data = NULL;
      csp_set_get_num(first, ctr, &data);
      csp_set_add(csound, *result, data);
      ctr++;
    }

    ctr = 0;
    while (ctr < second_len) {
      void *data = NULL;
      csp_set_get_num(second, ctr, &data);
      csp_set_add(csound, *result, data);
      ctr++;
    }

    return CSOUND_SUCCESS;
}

int csp_set_intersection(CSOUND *csound, struct set_t *first,
                         struct set_t *second, struct set_t **result)
{
    int ctr = 0;
    int first_len;
#ifdef SET_DEBUG
    if (UNLIKELY(first == NULL))
      csound->Die(csound, "Invalid NULL Parameter first");
    if (UNLIKELY(!set_is_set(csound, first)))
      csound->Die(csound, "Invalid Parameter set not a first");
    if (UNLIKELY(second == NULL))
      csound->Die(csound, "Invalid NULL Parameter second");
    if (UNLIKELY(!set_is_set(csound, second)))
      csound->Die(csound, "Invalid Parameter set not a second");
    if (UNLIKELY(result == NULL))
      csound->Die(csound, "Invalid NULL Parameter result");
    if (UNLIKELY(first->ele_eq_func != second->ele_eq_func))
      csound->Die(csound,
                  "Invalid sets for comparison (different equality)");
#endif

    csp_set_alloc(csound, result,
                  first->ele_eq_func, first->ele_print_func);

    first_len = csp_set_count(first);

    while (ctr < first_len) {
      void *data = NULL;
      csp_set_get_num(first, ctr, &data);
      if (csp_set_exists(csound, second, data)) {
        csp_set_add(csound, *result, data);
      }
      ctr++;
    }

    return CSOUND_SUCCESS;
}