d_arithmetic.c 24 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/*  arithmetic binops (+, -, *, /).
If no creation argument is given, there are two signal inlets for vector/vector
operation; otherwise it's vector/scalar and the second inlet takes a float
to reset the value.
*/

#include "m_pd.h"

/* ----------------------------- plus ----------------------------- */
static t_class *plus_class, *scalarplus_class;

typedef struct _plus
{
    t_object x_obj;
19
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
20
21
22
23
24
} t_plus;

typedef struct _scalarplus
{
    t_object x_obj;
25
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
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
    t_float x_g;            /* inlet value */
} t_scalarplus;

static void *plus_new(t_symbol *s, int argc, t_atom *argv)
{
    if (argc > 1) post("+~: extra arguments ignored");
    if (argc) 
    {
        t_scalarplus *x = (t_scalarplus *)pd_new(scalarplus_class);
        floatinlet_new(&x->x_obj, &x->x_g);
        x->x_g = atom_getfloatarg(0, argc, argv);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
    else
    {
        t_plus *x = (t_plus *)pd_new(plus_class);
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
}

t_int *plus_perform(t_int *w)
{
53
54
55
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
56
57
58
59
60
61
62
    int n = (int)(w[4]);
    while (n--) *out++ = *in1++ + *in2++; 
    return (w+5);
}

t_int *plus_perf8(t_int *w)
{
63
64
65
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
66
67
68
    int n = (int)(w[4]);
    for (; n; n -= 8, in1 += 8, in2 += 8, out += 8)
    {
69
70
        t_sample f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
        t_sample f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];
Miller Puckette's avatar
Miller Puckette committed
71

72
73
        t_sample g0 = in2[0], g1 = in2[1], g2 = in2[2], g3 = in2[3];
        t_sample g4 = in2[4], g5 = in2[5], g6 = in2[6], g7 = in2[7];
Miller Puckette's avatar
Miller Puckette committed
74
75
76
77
78
79
80
81
82

        out[0] = f0 + g0; out[1] = f1 + g1; out[2] = f2 + g2; out[3] = f3 + g3;
        out[4] = f4 + g4; out[5] = f5 + g5; out[6] = f6 + g6; out[7] = f7 + g7;
    }
    return (w+5);
}

t_int *scalarplus_perform(t_int *w)
{
83
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
84
    t_float f = *(t_float *)(w[2]);
85
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
86
87
88
89
90
91
92
    int n = (int)(w[4]);
    while (n--) *out++ = *in++ + f; 
    return (w+5);
}

t_int *scalarplus_perf8(t_int *w)
{
93
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
94
    t_float g = *(t_float *)(w[2]);
95
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
96
97
98
    int n = (int)(w[4]);
    for (; n; n -= 8, in += 8, out += 8)
    {
99
100
        t_sample f0 = in[0], f1 = in[1], f2 = in[2], f3 = in[3];
        t_sample f4 = in[4], f5 = in[5], f6 = in[6], f7 = in[7];
Miller Puckette's avatar
Miller Puckette committed
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

        out[0] = f0 + g; out[1] = f1 + g; out[2] = f2 + g; out[3] = f3 + g;
        out[4] = f4 + g; out[5] = f5 + g; out[6] = f6 + g; out[7] = f7 + g;
    }
    return (w+5);
}

void dsp_add_plus(t_sample *in1, t_sample *in2, t_sample *out, int n)
{
    if (n&7)
        dsp_add(plus_perform, 4, in1, in2, out, n);
    else        
        dsp_add(plus_perf8, 4, in1, in2, out, n);
}

static void plus_dsp(t_plus *x, t_signal **sp)
{
    dsp_add_plus(sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}

static void scalarplus_dsp(t_scalarplus *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(scalarplus_perform, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
    else        
        dsp_add(scalarplus_perf8, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
}

static void plus_setup(void)
{
    plus_class = class_new(gensym("+~"), (t_newmethod)plus_new, 0,
        sizeof(t_plus), 0, A_GIMME, 0);
    class_addmethod(plus_class, (t_method)plus_dsp, gensym("dsp"), 0);
    CLASS_MAINSIGNALIN(plus_class, t_plus, x_f);
    class_sethelpsymbol(plus_class, gensym("sigbinops"));
    scalarplus_class = class_new(gensym("+~"), 0, 0,
        sizeof(t_scalarplus), 0, 0);
    CLASS_MAINSIGNALIN(scalarplus_class, t_scalarplus, x_f);
    class_addmethod(scalarplus_class, (t_method)scalarplus_dsp, gensym("dsp"),
        0);
    class_sethelpsymbol(scalarplus_class, gensym("sigbinops"));
}

/* ----------------------------- minus ----------------------------- */
static t_class *minus_class, *scalarminus_class;

typedef struct _minus
{
    t_object x_obj;
152
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
153
154
155
156
157
} t_minus;

typedef struct _scalarminus
{
    t_object x_obj;
158
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
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
    t_float x_g;
} t_scalarminus;

static void *minus_new(t_symbol *s, int argc, t_atom *argv)
{
    if (argc > 1) post("-~: extra arguments ignored");
    if (argc) 
    {
        t_scalarminus *x = (t_scalarminus *)pd_new(scalarminus_class);
        floatinlet_new(&x->x_obj, &x->x_g);
        x->x_g = atom_getfloatarg(0, argc, argv);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
    else
    {
        t_minus *x = (t_minus *)pd_new(minus_class);
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
}

t_int *minus_perform(t_int *w)
{
186
187
188
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
189
190
191
192
193
194
195
    int n = (int)(w[4]);
    while (n--) *out++ = *in1++ - *in2++; 
    return (w+5);
}

t_int *minus_perf8(t_int *w)
{
196
197
198
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
199
200
201
    int n = (int)(w[4]);
    for (; n; n -= 8, in1 += 8, in2 += 8, out += 8)
    {
202
203
        t_sample f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
        t_sample f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];
Miller Puckette's avatar
Miller Puckette committed
204

205
206
        t_sample g0 = in2[0], g1 = in2[1], g2 = in2[2], g3 = in2[3];
        t_sample g4 = in2[4], g5 = in2[5], g6 = in2[6], g7 = in2[7];
Miller Puckette's avatar
Miller Puckette committed
207
208
209
210
211
212
213
214
215

        out[0] = f0 - g0; out[1] = f1 - g1; out[2] = f2 - g2; out[3] = f3 - g3;
        out[4] = f4 - g4; out[5] = f5 - g5; out[6] = f6 - g6; out[7] = f7 - g7;
    }
    return (w+5);
}

t_int *scalarminus_perform(t_int *w)
{
216
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
217
    t_float f = *(t_float *)(w[2]);
218
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
219
220
221
222
223
224
225
    int n = (int)(w[4]);
    while (n--) *out++ = *in++ - f; 
    return (w+5);
}

t_int *scalarminus_perf8(t_int *w)
{
226
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
227
    t_float g = *(t_float *)(w[2]);
228
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
229
230
231
    int n = (int)(w[4]);
    for (; n; n -= 8, in += 8, out += 8)
    {
232
233
        t_sample f0 = in[0], f1 = in[1], f2 = in[2], f3 = in[3];
        t_sample f4 = in[4], f5 = in[5], f6 = in[6], f7 = in[7];
Miller Puckette's avatar
Miller Puckette committed
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

        out[0] = f0 - g; out[1] = f1 - g; out[2] = f2 - g; out[3] = f3 - g;
        out[4] = f4 - g; out[5] = f5 - g; out[6] = f6 - g; out[7] = f7 - g;
    }
    return (w+5);
}

static void minus_dsp(t_minus *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(minus_perform, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
    else        
        dsp_add(minus_perf8, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}

static void scalarminus_dsp(t_scalarminus *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(scalarminus_perform, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
    else        
        dsp_add(scalarminus_perf8, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
}

static void minus_setup(void)
{
    minus_class = class_new(gensym("-~"), (t_newmethod)minus_new, 0,
        sizeof(t_minus), 0, A_GIMME, 0);
    CLASS_MAINSIGNALIN(minus_class, t_minus, x_f);
    class_addmethod(minus_class, (t_method)minus_dsp, gensym("dsp"), 0);
    class_sethelpsymbol(minus_class, gensym("sigbinops"));
    scalarminus_class = class_new(gensym("-~"), 0, 0,
        sizeof(t_scalarminus), 0, 0);
    CLASS_MAINSIGNALIN(scalarminus_class, t_scalarminus, x_f);
    class_addmethod(scalarminus_class, (t_method)scalarminus_dsp, gensym("dsp"),
        0);
    class_sethelpsymbol(scalarminus_class, gensym("sigbinops"));
}

/* ----------------------------- times ----------------------------- */

static t_class *times_class, *scalartimes_class;

typedef struct _times
{
    t_object x_obj;
283
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
284
285
286
287
288
} t_times;

typedef struct _scalartimes
{
    t_object x_obj;
289
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
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
    t_float x_g;
} t_scalartimes;

static void *times_new(t_symbol *s, int argc, t_atom *argv)
{
    if (argc > 1) post("*~: extra arguments ignored");
    if (argc) 
    {
        t_scalartimes *x = (t_scalartimes *)pd_new(scalartimes_class);
        floatinlet_new(&x->x_obj, &x->x_g);
        x->x_g = atom_getfloatarg(0, argc, argv);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
    else
    {
        t_times *x = (t_times *)pd_new(times_class);
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
}

t_int *times_perform(t_int *w)
{
317
318
319
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
320
321
322
323
324
325
326
    int n = (int)(w[4]);
    while (n--) *out++ = *in1++ * *in2++; 
    return (w+5);
}

t_int *times_perf8(t_int *w)
{
327
328
329
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
330
331
332
    int n = (int)(w[4]);
    for (; n; n -= 8, in1 += 8, in2 += 8, out += 8)
    {
333
334
        t_sample f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
        t_sample f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];
Miller Puckette's avatar
Miller Puckette committed
335

336
337
        t_sample g0 = in2[0], g1 = in2[1], g2 = in2[2], g3 = in2[3];
        t_sample g4 = in2[4], g5 = in2[5], g6 = in2[6], g7 = in2[7];
Miller Puckette's avatar
Miller Puckette committed
338
339
340
341
342
343
344
345
346

        out[0] = f0 * g0; out[1] = f1 * g1; out[2] = f2 * g2; out[3] = f3 * g3;
        out[4] = f4 * g4; out[5] = f5 * g5; out[6] = f6 * g6; out[7] = f7 * g7;
    }
    return (w+5);
}

t_int *scalartimes_perform(t_int *w)
{
347
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
348
    t_float f = *(t_float *)(w[2]);
349
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
350
351
352
353
354
355
356
    int n = (int)(w[4]);
    while (n--) *out++ = *in++ * f; 
    return (w+5);
}

t_int *scalartimes_perf8(t_int *w)
{
357
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
358
    t_float g = *(t_float *)(w[2]);
359
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
360
361
362
    int n = (int)(w[4]);
    for (; n; n -= 8, in += 8, out += 8)
    {
363
364
        t_sample f0 = in[0], f1 = in[1], f2 = in[2], f3 = in[3];
        t_sample f4 = in[4], f5 = in[5], f6 = in[6], f7 = in[7];
Miller Puckette's avatar
Miller Puckette committed
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

        out[0] = f0 * g; out[1] = f1 * g; out[2] = f2 * g; out[3] = f3 * g;
        out[4] = f4 * g; out[5] = f5 * g; out[6] = f6 * g; out[7] = f7 * g;
    }
    return (w+5);
}

static void times_dsp(t_times *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(times_perform, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
    else        
        dsp_add(times_perf8, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}

static void scalartimes_dsp(t_scalartimes *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(scalartimes_perform, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
    else        
        dsp_add(scalartimes_perf8, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
}

static void times_setup(void)
{
    times_class = class_new(gensym("*~"), (t_newmethod)times_new, 0,
        sizeof(t_times), 0, A_GIMME, 0);
    CLASS_MAINSIGNALIN(times_class, t_times, x_f);
    class_addmethod(times_class, (t_method)times_dsp, gensym("dsp"), 0);
    class_sethelpsymbol(times_class, gensym("sigbinops"));
    scalartimes_class = class_new(gensym("*~"), 0, 0,
        sizeof(t_scalartimes), 0, 0);
    CLASS_MAINSIGNALIN(scalartimes_class, t_scalartimes, x_f);
    class_addmethod(scalartimes_class, (t_method)scalartimes_dsp, gensym("dsp"),
        0);
    class_sethelpsymbol(scalartimes_class, gensym("sigbinops"));
}

/* ----------------------------- over ----------------------------- */
static t_class *over_class, *scalarover_class;

typedef struct _over
{
    t_object x_obj;
413
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
414
415
416
417
418
} t_over;

typedef struct _scalarover
{
    t_object x_obj;
419
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
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
    t_float x_g;
} t_scalarover;

static void *over_new(t_symbol *s, int argc, t_atom *argv)
{
    if (argc > 1) post("/~: extra arguments ignored");
    if (argc) 
    {
        t_scalarover *x = (t_scalarover *)pd_new(scalarover_class);
        floatinlet_new(&x->x_obj, &x->x_g);
        x->x_g = atom_getfloatarg(0, argc, argv);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
    else
    {
        t_over *x = (t_over *)pd_new(over_class);
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
}

t_int *over_perform(t_int *w)
{
447
448
449
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
450
451
452
    int n = (int)(w[4]);
    while (n--)
    {
453
        t_sample g = *in2++;
Miller Puckette's avatar
Miller Puckette committed
454
455
456
457
458
459
460
        *out++ = (g ? *in1++ / g : 0); 
    }
    return (w+5);
}

t_int *over_perf8(t_int *w)
{
461
462
463
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
464
465
466
    int n = (int)(w[4]);
    for (; n; n -= 8, in1 += 8, in2 += 8, out += 8)
    {
467
468
        t_sample f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
        t_sample f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];
Miller Puckette's avatar
Miller Puckette committed
469

470
471
        t_sample g0 = in2[0], g1 = in2[1], g2 = in2[2], g3 = in2[3];
        t_sample g4 = in2[4], g5 = in2[5], g6 = in2[6], g7 = in2[7];
Miller Puckette's avatar
Miller Puckette committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

        out[0] = (g0? f0 / g0 : 0);
        out[1] = (g1? f1 / g1 : 0);
        out[2] = (g2? f2 / g2 : 0);
        out[3] = (g3? f3 / g3 : 0);
        out[4] = (g4? f4 / g4 : 0);
        out[5] = (g5? f5 / g5 : 0);
        out[6] = (g6? f6 / g6 : 0);
        out[7] = (g7? f7 / g7 : 0);
    }
    return (w+5);
}

t_int *scalarover_perform(t_int *w)
{
487
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
488
    t_float f = *(t_float *)(w[2]);
489
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
490
491
492
493
494
495
496
497
    int n = (int)(w[4]);
    if(f) f = 1./f;
    while (n--) *out++ = *in++ * f; 
    return (w+5);
}

t_int *scalarover_perf8(t_int *w)
{
498
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
499
    t_float g = *(t_float *)(w[2]);
500
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
501
502
503
504
    int n = (int)(w[4]);
    if (g) g = 1.f / g;
    for (; n; n -= 8, in += 8, out += 8)
    {
505
506
        t_sample f0 = in[0], f1 = in[1], f2 = in[2], f3 = in[3];
        t_sample f4 = in[4], f5 = in[5], f6 = in[6], f7 = in[7];
Miller Puckette's avatar
Miller Puckette committed
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

        out[0] = f0 * g; out[1] = f1 * g; out[2] = f2 * g; out[3] = f3 * g;
        out[4] = f4 * g; out[5] = f5 * g; out[6] = f6 * g; out[7] = f7 * g;
    }
    return (w+5);
}

static void over_dsp(t_over *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(over_perform, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
    else        
        dsp_add(over_perf8, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}

static void scalarover_dsp(t_scalarover *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(scalarover_perform, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
    else        
        dsp_add(scalarover_perf8, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
}

static void over_setup(void)
{
    over_class = class_new(gensym("/~"), (t_newmethod)over_new, 0,
        sizeof(t_over), 0, A_GIMME, 0);
    CLASS_MAINSIGNALIN(over_class, t_over, x_f);
    class_addmethod(over_class, (t_method)over_dsp, gensym("dsp"), 0);
    class_sethelpsymbol(over_class, gensym("sigbinops"));
    scalarover_class = class_new(gensym("/~"), 0, 0,
        sizeof(t_scalarover), 0, 0);
    CLASS_MAINSIGNALIN(scalarover_class, t_scalarover, x_f);
    class_addmethod(scalarover_class, (t_method)scalarover_dsp, gensym("dsp"),
        0);
    class_sethelpsymbol(scalarover_class, gensym("sigbinops"));
}

/* ----------------------------- max ----------------------------- */
static t_class *max_class, *scalarmax_class;

typedef struct _max
{
    t_object x_obj;
555
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
556
557
558
559
560
} t_max;

typedef struct _scalarmax
{
    t_object x_obj;
561
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
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
    t_float x_g;
} t_scalarmax;

static void *max_new(t_symbol *s, int argc, t_atom *argv)
{
    if (argc > 1) post("max~: extra arguments ignored");
    if (argc) 
    {
        t_scalarmax *x = (t_scalarmax *)pd_new(scalarmax_class);
        floatinlet_new(&x->x_obj, &x->x_g);
        x->x_g = atom_getfloatarg(0, argc, argv);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
    else
    {
        t_max *x = (t_max *)pd_new(max_class);
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
}

t_int *max_perform(t_int *w)
{
589
590
591
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
592
593
594
    int n = (int)(w[4]);
    while (n--)
    {
595
        t_sample f = *in1++, g = *in2++;
Miller Puckette's avatar
Miller Puckette committed
596
597
598
599
600
601
602
        *out++ = (f > g ? f : g); 
    }
    return (w+5);
}

t_int *max_perf8(t_int *w)
{
603
604
605
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
606
607
608
    int n = (int)(w[4]);
    for (; n; n -= 8, in1 += 8, in2 += 8, out += 8)
    {
609
610
        t_sample f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
        t_sample f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];
Miller Puckette's avatar
Miller Puckette committed
611

612
613
        t_sample g0 = in2[0], g1 = in2[1], g2 = in2[2], g3 = in2[3];
        t_sample g4 = in2[4], g5 = in2[5], g6 = in2[6], g7 = in2[7];
Miller Puckette's avatar
Miller Puckette committed
614
615
616
617
618
619
620
621
622
623
624

        out[0] = (f0 > g0 ? f0 : g0); out[1] = (f1 > g1 ? f1 : g1);
        out[2] = (f2 > g2 ? f2 : g2); out[3] = (f3 > g3 ? f3 : g3);
        out[4] = (f4 > g4 ? f4 : g4); out[5] = (f5 > g5 ? f5 : g5);
        out[6] = (f6 > g6 ? f6 : g6); out[7] = (f7 > g7 ? f7 : g7);
    }
    return (w+5);
}

t_int *scalarmax_perform(t_int *w)
{
625
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
626
    t_float f = *(t_float *)(w[2]);
627
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
628
629
630
    int n = (int)(w[4]);
    while (n--)
    {
631
        t_sample g = *in++;
Miller Puckette's avatar
Miller Puckette committed
632
633
634
635
636
637
638
        *out++ = (f > g ? f : g); 
    }
    return (w+5);
}

t_int *scalarmax_perf8(t_int *w)
{
639
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
640
    t_float g = *(t_float *)(w[2]);
641
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
642
643
644
    int n = (int)(w[4]);
    for (; n; n -= 8, in += 8, out += 8)
    {
645
646
        t_sample f0 = in[0], f1 = in[1], f2 = in[2], f3 = in[3];
        t_sample f4 = in[4], f5 = in[5], f6 = in[6], f7 = in[7];
Miller Puckette's avatar
Miller Puckette committed
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
684
685
686
687
688
689
690
691
692
693
694
695
696

        out[0] = (f0 > g ? f0 : g); out[1] = (f1 > g ? f1 : g);
        out[2] = (f2 > g ? f2 : g); out[3] = (f3 > g ? f3 : g);
        out[4] = (f4 > g ? f4 : g); out[5] = (f5 > g ? f5 : g);
        out[6] = (f6 > g ? f6 : g); out[7] = (f7 > g ? f7 : g);
    }
    return (w+5);
}

static void max_dsp(t_max *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(max_perform, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
    else        
        dsp_add(max_perf8, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}

static void scalarmax_dsp(t_scalarmax *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(scalarmax_perform, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
    else        
        dsp_add(scalarmax_perf8, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
}

static void max_setup(void)
{
    max_class = class_new(gensym("max~"), (t_newmethod)max_new, 0,
        sizeof(t_max), 0, A_GIMME, 0);
    CLASS_MAINSIGNALIN(max_class, t_max, x_f);
    class_addmethod(max_class, (t_method)max_dsp, gensym("dsp"), 0);
    class_sethelpsymbol(max_class, gensym("sigbinops"));
    scalarmax_class = class_new(gensym("max~"), 0, 0,
        sizeof(t_scalarmax), 0, 0);
    CLASS_MAINSIGNALIN(scalarmax_class, t_scalarmax, x_f);
    class_addmethod(scalarmax_class, (t_method)scalarmax_dsp, gensym("dsp"),
        0);
    class_sethelpsymbol(scalarmax_class, gensym("sigbinops"));
}

/* ----------------------------- min ----------------------------- */
static t_class *min_class, *scalarmin_class;

typedef struct _min
{
    t_object x_obj;
697
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
698
699
700
701
702
703
} t_min;

typedef struct _scalarmin
{
    t_object x_obj;
    t_float x_g;
704
    t_float x_f;
Miller Puckette's avatar
Miller Puckette committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
} t_scalarmin;

static void *min_new(t_symbol *s, int argc, t_atom *argv)
{
    if (argc > 1) post("min~: extra arguments ignored");
    if (argc) 
    {
        t_scalarmin *x = (t_scalarmin *)pd_new(scalarmin_class);
        floatinlet_new(&x->x_obj, &x->x_g);
        x->x_g = atom_getfloatarg(0, argc, argv);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
    else
    {
        t_min *x = (t_min *)pd_new(min_class);
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
        outlet_new(&x->x_obj, &s_signal);
        x->x_f = 0;
        return (x);
    }
}

t_int *min_perform(t_int *w)
{
731
732
733
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
734
735
736
    int n = (int)(w[4]);
    while (n--)
    {
737
        t_sample f = *in1++, g = *in2++;
Miller Puckette's avatar
Miller Puckette committed
738
739
740
741
742
743
744
        *out++ = (f < g ? f : g); 
    }
    return (w+5);
}

t_int *min_perf8(t_int *w)
{
745
746
747
    t_sample *in1 = (t_sample *)(w[1]);
    t_sample *in2 = (t_sample *)(w[2]);
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
748
749
750
    int n = (int)(w[4]);
    for (; n; n -= 8, in1 += 8, in2 += 8, out += 8)
    {
751
752
        t_sample f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
        t_sample f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];
Miller Puckette's avatar
Miller Puckette committed
753

754
755
        t_sample g0 = in2[0], g1 = in2[1], g2 = in2[2], g3 = in2[3];
        t_sample g4 = in2[4], g5 = in2[5], g6 = in2[6], g7 = in2[7];
Miller Puckette's avatar
Miller Puckette committed
756
757
758
759
760
761
762
763
764
765
766

        out[0] = (f0 < g0 ? f0 : g0); out[1] = (f1 < g1 ? f1 : g1);
        out[2] = (f2 < g2 ? f2 : g2); out[3] = (f3 < g3 ? f3 : g3);
        out[4] = (f4 < g4 ? f4 : g4); out[5] = (f5 < g5 ? f5 : g5);
        out[6] = (f6 < g6 ? f6 : g6); out[7] = (f7 < g7 ? f7 : g7);
    }
    return (w+5);
}

t_int *scalarmin_perform(t_int *w)
{
767
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
768
    t_float f = *(t_float *)(w[2]);
769
    t_sample *out = (t_sample *)(w[3]);
Miller Puckette's avatar
Miller Puckette committed
770
771
772
    int n = (int)(w[4]);
    while (n--)
    {
773
        t_sample g = *in++;
Miller Puckette's avatar
Miller Puckette committed
774
775
776
777
778
779
780
        *out++ = (f < g ? f : g); 
    }
    return (w+5);
}

t_int *scalarmin_perf8(t_int *w)
{
781
    t_sample *in = (t_sample *)(w[1]);
Miller Puckette's avatar
Miller Puckette committed
782
783
784
785
786
    t_float g = *(t_float *)(w[2]);
    t_float *out = (t_float *)(w[3]);
    int n = (int)(w[4]);
    for (; n; n -= 8, in += 8, out += 8)
    {
787
788
        t_sample f0 = in[0], f1 = in[1], f2 = in[2], f3 = in[3];
        t_sample f4 = in[4], f5 = in[5], f6 = in[6], f7 = in[7];
Miller Puckette's avatar
Miller Puckette committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843

        out[0] = (f0 < g ? f0 : g); out[1] = (f1 < g ? f1 : g);
        out[2] = (f2 < g ? f2 : g); out[3] = (f3 < g ? f3 : g);
        out[4] = (f4 < g ? f4 : g); out[5] = (f5 < g ? f5 : g);
        out[6] = (f6 < g ? f6 : g); out[7] = (f7 < g ? f7 : g);
    }
    return (w+5);
}

static void min_dsp(t_min *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(min_perform, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
    else        
        dsp_add(min_perf8, 4,
            sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}

static void scalarmin_dsp(t_scalarmin *x, t_signal **sp)
{
    if (sp[0]->s_n&7)
        dsp_add(scalarmin_perform, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
    else        
        dsp_add(scalarmin_perf8, 4, sp[0]->s_vec, &x->x_g,
            sp[1]->s_vec, sp[0]->s_n);
}

static void min_setup(void)
{
    min_class = class_new(gensym("min~"), (t_newmethod)min_new, 0,
        sizeof(t_min), 0, A_GIMME, 0);
    CLASS_MAINSIGNALIN(min_class, t_min, x_f);
    class_addmethod(min_class, (t_method)min_dsp, gensym("dsp"), 0);
    class_sethelpsymbol(min_class, gensym("sigbinops"));
    scalarmin_class = class_new(gensym("min~"), 0, 0,
        sizeof(t_scalarmin), 0, 0);
    CLASS_MAINSIGNALIN(scalarmin_class, t_scalarmin, x_f);
    class_addmethod(scalarmin_class, (t_method)scalarmin_dsp, gensym("dsp"),
        0);
    class_sethelpsymbol(scalarmin_class, gensym("sigbinops"));
}

/* ----------------------- global setup routine ---------------- */
void d_arithmetic_setup(void)
{
    plus_setup();
    minus_setup();
    times_setup();
    over_setup();
    max_setup();
    min_setup();
}