summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/x_arithmetic.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/x_arithmetic.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/x_arithmetic.c1792
1 files changed, 1792 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/x_arithmetic.c b/apps/plugins/pdbox/PDa/src/x_arithmetic.c
new file mode 100644
index 0000000000..ad309f7a68
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/x_arithmetic.c
@@ -0,0 +1,1792 @@
1/* Copyright (c) 1997-1999 Miller Puckette.
2* For information on usage and redistribution, and for a DISCLAIMER OF ALL
3* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
4
5/* arithmetic: binops ala C language. The 4 functions and relationals are
6done on floats; the logical and bitwise binops convert their
7inputs to int and their outputs back to float. */
8
9#include "m_pd.h"
10#include <math.h>
11
12
13/* MSW and OSX don't appear to have single-precision ANSI math */
14
15#define sinf sin
16#define cosf cos
17#define atanf atan
18#define atan2f atan2
19#define sqrtf sqrt
20#define logf log
21#define expf exp
22#define fabsf fabs
23#define powf pow
24
25
26typedef struct _binop
27{
28 t_object x_obj;
29 t_float x_f1;
30 t_float x_f2;
31} t_binop;
32
33/* ------------------ binop1: +, -, *, / ----------------------------- */
34
35static void *binop1_new(t_class *floatclass, t_floatarg f)
36{
37 t_binop *x = (t_binop *)pd_new(floatclass);
38 outlet_new(&x->x_obj, &s_float);
39 floatinlet_new(&x->x_obj, &x->x_f2);
40 x->x_f1 = 0;
41 x->x_f2 = f;
42 return (x);
43}
44
45/* --------------------- addition ------------------------------- */
46
47static t_class *binop1_plus_class;
48
49static void *binop1_plus_new(t_floatarg f)
50{
51 return (binop1_new(binop1_plus_class, f));
52}
53
54static void binop1_plus_bang(t_binop *x)
55{
56 outlet_float(x->x_obj.ob_outlet, x->x_f1 + x->x_f2);
57}
58
59static void binop1_plus_float(t_binop *x, t_float f)
60{
61 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) + x->x_f2);
62}
63
64/* --------------------- subtraction ------------------------------- */
65
66static t_class *binop1_minus_class;
67
68static void *binop1_minus_new(t_floatarg f)
69{
70 return (binop1_new(binop1_minus_class, f));
71}
72
73static void binop1_minus_bang(t_binop *x)
74{
75 outlet_float(x->x_obj.ob_outlet, x->x_f1 - x->x_f2);
76}
77
78static void binop1_minus_float(t_binop *x, t_float f)
79{
80 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) - x->x_f2);
81}
82
83/* --------------------- multiplication ------------------------------- */
84
85static t_class *binop1_times_class;
86
87static void *binop1_times_new(t_floatarg f)
88{
89 return (binop1_new(binop1_times_class, f));
90}
91
92static void binop1_times_bang(t_binop *x)
93{
94 outlet_float(x->x_obj.ob_outlet, x->x_f1 * x->x_f2);
95}
96
97static void binop1_times_float(t_binop *x, t_float f)
98{
99 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) * x->x_f2);
100}
101
102/* --------------------- division ------------------------------- */
103
104static t_class *binop1_div_class;
105
106static void *binop1_div_new(t_floatarg f)
107{
108 return (binop1_new(binop1_div_class, f));
109}
110
111static void binop1_div_bang(t_binop *x)
112{
113 outlet_float(x->x_obj.ob_outlet,
114 (x->x_f2 != 0 ? x->x_f1 / x->x_f2 : 0));
115}
116
117static void binop1_div_float(t_binop *x, t_float f)
118{
119 x->x_f1 = f;
120 outlet_float(x->x_obj.ob_outlet,
121 (x->x_f2 != 0 ? x->x_f1 / x->x_f2 : 0));
122}
123
124/* ------------------------ pow -------------------------------- */
125
126static t_class *binop1_pow_class;
127
128static void *binop1_pow_new(t_floatarg f)
129{
130 return (binop1_new(binop1_pow_class, f));
131}
132
133static void binop1_pow_bang(t_binop *x)
134{
135 outlet_float(x->x_obj.ob_outlet,
136 (x->x_f1 > 0 ? powf(x->x_f1, x->x_f2) : 0));
137}
138
139static void binop1_pow_float(t_binop *x, t_float f)
140{
141 x->x_f1 = f;
142 outlet_float(x->x_obj.ob_outlet,
143 (x->x_f1 > 0 ? powf(x->x_f1, x->x_f2) : 0));
144}
145
146/* ------------------------ max -------------------------------- */
147
148static t_class *binop1_max_class;
149
150static void *binop1_max_new(t_floatarg f)
151{
152 return (binop1_new(binop1_max_class, f));
153}
154
155static void binop1_max_bang(t_binop *x)
156{
157 outlet_float(x->x_obj.ob_outlet,
158 (x->x_f1 > x->x_f2 ? x->x_f1 : x->x_f2));
159}
160
161static void binop1_max_float(t_binop *x, t_float f)
162{
163 x->x_f1 = f;
164 outlet_float(x->x_obj.ob_outlet,
165 (x->x_f1 > x->x_f2 ? x->x_f1 : x->x_f2));
166}
167
168/* ------------------------ min -------------------------------- */
169
170static t_class *binop1_min_class;
171
172static void *binop1_min_new(t_floatarg f)
173{
174 return (binop1_new(binop1_min_class, f));
175}
176
177static void binop1_min_bang(t_binop *x)
178{
179 outlet_float(x->x_obj.ob_outlet,
180 (x->x_f1 < x->x_f2 ? x->x_f1 : x->x_f2));
181}
182
183static void binop1_min_float(t_binop *x, t_float f)
184{
185 x->x_f1 = f;
186 outlet_float(x->x_obj.ob_outlet,
187 (x->x_f1 < x->x_f2 ? x->x_f1 : x->x_f2));
188}
189
190/* ------------------ binop2: ==, !=, >, <, >=, <=. -------------------- */
191
192static void *binop2_new(t_class *floatclass, t_floatarg f)
193{
194 t_binop *x = (t_binop *)pd_new(floatclass);
195 outlet_new(&x->x_obj, &s_float);
196 floatinlet_new(&x->x_obj, &x->x_f2);
197 x->x_f1 = 0;
198 x->x_f2 = f;
199 return (x);
200}
201
202/* --------------------- == ------------------------------- */
203
204static t_class *binop2_ee_class;
205
206static void *binop2_ee_new(t_floatarg f)
207{
208 return (binop2_new(binop2_ee_class, f));
209}
210
211static void binop2_ee_bang(t_binop *x)
212{
213 outlet_float(x->x_obj.ob_outlet, x->x_f1 == x->x_f2);
214}
215
216static void binop2_ee_float(t_binop *x, t_float f)
217{
218 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) == x->x_f2);
219}
220
221/* --------------------- != ------------------------------- */
222
223static t_class *binop2_ne_class;
224
225static void *binop2_ne_new(t_floatarg f)
226{
227 return (binop2_new(binop2_ne_class, f));
228}
229
230static void binop2_ne_bang(t_binop *x)
231{
232 outlet_float(x->x_obj.ob_outlet, x->x_f1 != x->x_f2);
233}
234
235static void binop2_ne_float(t_binop *x, t_float f)
236{
237 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) != x->x_f2);
238}
239
240/* --------------------- > ------------------------------- */
241
242static t_class *binop2_gt_class;
243
244static void *binop2_gt_new(t_floatarg f)
245{
246 return (binop2_new(binop2_gt_class, f));
247}
248
249static void binop2_gt_bang(t_binop *x)
250{
251 outlet_float(x->x_obj.ob_outlet, x->x_f1 > x->x_f2);
252}
253
254static void binop2_gt_float(t_binop *x, t_float f)
255{
256 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) > x->x_f2);
257}
258
259/* --------------------- < ------------------------------- */
260
261static t_class *binop2_lt_class;
262
263static void *binop2_lt_new(t_floatarg f)
264{
265 return (binop2_new(binop2_lt_class, f));
266}
267
268static void binop2_lt_bang(t_binop *x)
269{
270 outlet_float(x->x_obj.ob_outlet, x->x_f1 < x->x_f2);
271}
272
273static void binop2_lt_float(t_binop *x, t_float f)
274{
275 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) < x->x_f2);
276}
277
278/* --------------------- >= ------------------------------- */
279
280static t_class *binop2_ge_class;
281
282static void *binop2_ge_new(t_floatarg f)
283{
284 return (binop2_new(binop2_ge_class, f));
285}
286
287static void binop2_ge_bang(t_binop *x)
288{
289 outlet_float(x->x_obj.ob_outlet, x->x_f1 >= x->x_f2);
290}
291
292static void binop2_ge_float(t_binop *x, t_float f)
293{
294 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) >= x->x_f2);
295}
296
297/* --------------------- <= ------------------------------- */
298
299static t_class *binop2_le_class;
300
301static void *binop2_le_new(t_floatarg f)
302{
303 return (binop2_new(binop2_le_class, f));
304}
305
306static void binop2_le_bang(t_binop *x)
307{
308 outlet_float(x->x_obj.ob_outlet, x->x_f1 <= x->x_f2);
309}
310
311static void binop2_le_float(t_binop *x, t_float f)
312{
313 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) <= x->x_f2);
314}
315
316/* ------------- binop3: &, |, &&, ||, <<, >>, %, mod, div ------------------ */
317
318static void *binop3_new(t_class *fixclass, t_floatarg f)
319{
320 t_binop *x = (t_binop *)pd_new(fixclass);
321 outlet_new(&x->x_obj, &s_float);
322 floatinlet_new(&x->x_obj, &x->x_f2);
323 x->x_f1 = 0;
324 x->x_f2 = f;
325 return (x);
326}
327
328/* --------------------------- & ---------------------------- */
329
330static t_class *binop3_ba_class;
331
332static void *binop3_ba_new(t_floatarg f)
333{
334 return (binop3_new(binop3_ba_class, f));
335}
336
337static void binop2_ba_bang(t_binop *x)
338{
339 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) & (int)(x->x_f2));
340}
341
342static void binop2_ba_float(t_binop *x, t_float f)
343{
344 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) & (int)(x->x_f2));
345}
346
347/* --------------------------- && ---------------------------- */
348
349static t_class *binop3_la_class;
350
351static void *binop3_la_new(t_floatarg f)
352{
353 return (binop3_new(binop3_la_class, f));
354}
355
356static void binop2_la_bang(t_binop *x)
357{
358 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) && (int)(x->x_f2));
359}
360
361static void binop2_la_float(t_binop *x, t_float f)
362{
363 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) && (int)(x->x_f2));
364}
365
366/* --------------------------- | ---------------------------- */
367
368static t_class *binop3_bo_class;
369
370static void *binop3_bo_new(t_floatarg f)
371{
372 return (binop3_new(binop3_bo_class, f));
373}
374
375static void binop2_bo_bang(t_binop *x)
376{
377 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) | (int)(x->x_f2));
378}
379
380static void binop2_bo_float(t_binop *x, t_float f)
381{
382 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) | (int)(x->x_f2));
383}
384
385/* --------------------------- || ---------------------------- */
386
387static t_class *binop3_lo_class;
388
389static void *binop3_lo_new(t_floatarg f)
390{
391 return (binop3_new(binop3_lo_class, f));
392}
393
394static void binop2_lo_bang(t_binop *x)
395{
396 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) || (int)(x->x_f2));
397}
398
399static void binop2_lo_float(t_binop *x, t_float f)
400{
401 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) || (int)(x->x_f2));
402}
403
404/* --------------------------- << ---------------------------- */
405
406static t_class *binop3_ls_class;
407
408static void *binop3_ls_new(t_floatarg f)
409{
410 return (binop3_new(binop3_ls_class, f));
411}
412
413static void binop2_ls_bang(t_binop *x)
414{
415 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) << (int)(x->x_f2));
416}
417
418static void binop2_ls_float(t_binop *x, t_float f)
419{
420 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) << (int)(x->x_f2));
421}
422
423/* --------------------------- >> ---------------------------- */
424
425static t_class *binop3_rs_class;
426
427static void *binop3_rs_new(t_floatarg f)
428{
429 return (binop3_new(binop3_rs_class, f));
430}
431
432static void binop2_rs_bang(t_binop *x)
433{
434 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) >> (int)(x->x_f2));
435}
436
437static void binop2_rs_float(t_binop *x, t_float f)
438{
439 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) >> (int)(x->x_f2));
440}
441
442/* --------------------------- % ---------------------------- */
443
444static t_class *binop3_pc_class;
445
446static void *binop3_pc_new(t_floatarg f)
447{
448 return (binop3_new(binop3_pc_class, f));
449}
450
451static void binop2_pc_bang(t_binop *x)
452{
453 int n2 = x->x_f2;
454 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) % (n2 ? n2 : 1));
455}
456
457static void binop2_pc_float(t_binop *x, t_float f)
458{
459 int n2 = x->x_f2;
460 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) % (n2 ? n2 : 1));
461}
462
463/* --------------------------- mod ---------------------------- */
464
465static t_class *binop3_mod_class;
466
467static void *binop3_mod_new(t_floatarg f)
468{
469 return (binop3_new(binop3_mod_class, f));
470}
471
472static void binop3_mod_bang(t_binop *x)
473{
474 int n2 = x->x_f2, result;
475 if (n2 < 0) n2 = -n2;
476 else if (!n2) n2 = 1;
477 result = ((int)(x->x_f1)) % n2;
478 if (result < 0) result += n2;
479 outlet_float(x->x_obj.ob_outlet, (t_float)result);
480}
481
482static void binop3_mod_float(t_binop *x, t_float f)
483{
484 x->x_f1 = f;
485 binop3_mod_bang(x);
486}
487
488/* --------------------------- div ---------------------------- */
489
490static t_class *binop3_div_class;
491
492static void *binop3_div_new(t_floatarg f)
493{
494 return (binop3_new(binop3_div_class, f));
495}
496
497static void binop3_div_bang(t_binop *x)
498{
499 int n1 = x->x_f1, n2 = x->x_f2, result;
500 if (n2 < 0) n2 = -n2;
501 else if (!n2) n2 = 1;
502 if (n1 < 0) n1 -= (n2-1);
503 result = n1 / n2;
504 outlet_float(x->x_obj.ob_outlet, (t_float)result);
505}
506
507static void binop3_div_float(t_binop *x, t_float f)
508{
509 x->x_f1 = f;
510 binop3_div_bang(x);
511}
512
513/* -------------------- mathematical functions ------------------ */
514
515static t_class *sin_class; /* ----------- sin --------------- */
516
517static void *sin_new(void)
518{
519 t_object *x = (t_object *)pd_new(sin_class);
520 outlet_new(x, &s_float);
521 return (x);
522}
523
524static void sin_float(t_object *x, t_float f)
525{
526 outlet_float(x->ob_outlet, sinf(f));
527}
528
529static t_class *cos_class; /* ----------- cos --------------- */
530
531static void *cos_new(void)
532{
533 t_object *x = (t_object *)pd_new(cos_class);
534 outlet_new(x, &s_float);
535 return (x);
536}
537
538static void cos_float(t_object *x, t_float f)
539{
540 outlet_float(x->ob_outlet, cosf(f));
541}
542
543static t_class *tan_class; /* ----------- tan --------------- */
544
545static void *tan_new(void)
546{
547 t_object *x = (t_object *)pd_new(tan_class);
548 outlet_new(x, &s_float);
549 return (x);
550}
551
552static void tan_float(t_object *x, t_float f)
553{
554 float c = cosf(f);
555 float t = (c == 0 ? 0 : sinf(f)/c);
556 outlet_float(x->ob_outlet, t);
557}
558
559static t_class *atan_class; /* ----------- atan --------------- */
560
561static void *atan_new(void)
562{
563 t_object *x = (t_object *)pd_new(atan_class);
564 outlet_new(x, &s_float);
565 return (x);
566}
567
568static void atan_float(t_object *x, t_float f)
569{
570 outlet_float(x->ob_outlet, atanf(f));
571}
572
573static t_class *atan2_class; /* ----------- atan2 --------------- */
574
575typedef struct _atan2
576{
577 t_object x_ob;
578 float x_y;
579} t_atan2;
580
581static void *atan2_new(void)
582{
583 t_atan2 *x = (t_atan2 *)pd_new(atan2_class);
584 floatinlet_new(&x->x_ob, &x->x_y);
585 outlet_new(&x->x_ob, &s_float);
586 return (x);
587}
588
589static void atan2_float(t_atan2 *x, t_float f)
590{
591 float r = (f == 0 && x->x_y == 0 ? 0 : atan2f(x->x_y, f));
592 outlet_float(x->x_ob.ob_outlet, r);
593}
594
595static t_class *sqrt_class; /* ----------- sqrt --------------- */
596
597static void *sqrt_new(void)
598{
599 t_object *x = (t_object *)pd_new(sqrt_class);
600 outlet_new(x, &s_float);
601 return (x);
602}
603
604static void sqrt_float(t_object *x, t_float f)
605{
606 float r = (f > 0 ? sqrtf(f) : 0);
607 outlet_float(x->ob_outlet, r);
608}
609
610static t_class *log_class; /* ----------- log --------------- */
611
612static void *log_new(void)
613{
614 t_object *x = (t_object *)pd_new(log_class);
615 outlet_new(x, &s_float);
616 return (x);
617}
618
619static void log_float(t_object *x, t_float f)
620{
621 float r = (f > 0 ? logf(f) : -1000);
622 outlet_float(x->ob_outlet, r);
623}
624
625
626static t_class *exp_class; /* ----------- exp --------------- */
627
628static void *exp_new(void)
629{
630 t_object *x = (t_object *)pd_new(exp_class);
631 outlet_new(x, &s_float);
632 return (x);
633}
634
635#define MAXLOG 87.3365
636static void exp_float(t_object *x, t_float f)
637{
638 float g;
639#ifdef MSW
640 char buf[10];
641#endif
642 if (f > MAXLOG) f = MAXLOG;
643 g = expf(f);
644 outlet_float(x->ob_outlet, g);
645}
646
647static t_class *abs_class; /* ----------- abs --------------- */
648
649static void *abs_new(void)
650{
651 t_object *x = (t_object *)pd_new(abs_class);
652 outlet_new(x, &s_float);
653 return (x);
654}
655
656static void abs_float(t_object *x, t_float f)
657{
658 outlet_float(x->ob_outlet, fabsf(f));
659}
660
661/* ------------------------ misc ------------------------ */
662
663static t_class *clip_class;
664
665typedef struct _clip
666{
667 t_object x_ob;
668 float x_f1;
669 float x_f2;
670} t_clip;
671
672static void *clip_new(t_floatarg f1, t_floatarg f2)
673{
674 t_clip *x = (t_clip *)pd_new(clip_class);
675 floatinlet_new(&x->x_ob, &x->x_f1);
676 floatinlet_new(&x->x_ob, &x->x_f2);
677 outlet_new(&x->x_ob, &s_float);
678 x->x_f1 = f1;
679 x->x_f2 = f2;
680 return (x);
681}
682
683static void clip_float(t_clip *x, t_float f)
684{
685 outlet_float(x->x_ob.ob_outlet, (f < x->x_f1 ? x->x_f1 : (
686 f > x->x_f2 ? x->x_f2 : f)));
687}
688
689static void clip_setup(void)
690{
691 clip_class = class_new(gensym("clip"), (t_newmethod)clip_new, 0,
692 sizeof(t_clip), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
693 class_addfloat(clip_class, clip_float);
694}
695
696void x_arithmetic_setup(void)
697{
698 t_symbol *binop1_sym = gensym("operators");
699 t_symbol *binop23_sym = gensym("otherbinops");
700 t_symbol *math_sym = gensym("math");
701
702 binop1_plus_class = class_new(gensym("+"), (t_newmethod)binop1_plus_new, 0,
703 sizeof(t_binop), 0, A_DEFFLOAT, 0);
704 class_addbang(binop1_plus_class, binop1_plus_bang);
705 class_addfloat(binop1_plus_class, (t_method)binop1_plus_float);
706 class_sethelpsymbol(binop1_plus_class, binop1_sym);
707
708 binop1_minus_class = class_new(gensym("-"),
709 (t_newmethod)binop1_minus_new, 0,
710 sizeof(t_binop), 0, A_DEFFLOAT, 0);
711 class_addbang(binop1_minus_class, binop1_minus_bang);
712 class_addfloat(binop1_minus_class, (t_method)binop1_minus_float);
713 class_sethelpsymbol(binop1_minus_class, binop1_sym);
714
715 binop1_times_class = class_new(gensym("*"),
716 (t_newmethod)binop1_times_new, 0,
717 sizeof(t_binop), 0, A_DEFFLOAT, 0);
718 class_addbang(binop1_times_class, binop1_times_bang);
719 class_addfloat(binop1_times_class, (t_method)binop1_times_float);
720 class_sethelpsymbol(binop1_times_class, binop1_sym);
721
722 binop1_div_class = class_new(gensym("/"),
723 (t_newmethod)binop1_div_new, 0,
724 sizeof(t_binop), 0, A_DEFFLOAT, 0);
725 class_addbang(binop1_div_class, binop1_div_bang);
726 class_addfloat(binop1_div_class, (t_method)binop1_div_float);
727 class_sethelpsymbol(binop1_div_class, binop1_sym);
728
729 binop1_pow_class = class_new(gensym("pow"),
730 (t_newmethod)binop1_pow_new, 0,
731 sizeof(t_binop), 0, A_DEFFLOAT, 0);
732 class_addbang(binop1_pow_class, binop1_pow_bang);
733 class_addfloat(binop1_pow_class, (t_method)binop1_pow_float);
734 class_sethelpsymbol(binop1_pow_class, binop1_sym);
735
736 binop1_max_class = class_new(gensym("max"),
737 (t_newmethod)binop1_max_new, 0,
738 sizeof(t_binop), 0, A_DEFFLOAT, 0);
739 class_addbang(binop1_max_class, binop1_max_bang);
740 class_addfloat(binop1_max_class, (t_method)binop1_max_float);
741 class_sethelpsymbol(binop1_max_class, binop1_sym);
742
743 binop1_min_class = class_new(gensym("min"),
744 (t_newmethod)binop1_min_new, 0,
745 sizeof(t_binop), 0, A_DEFFLOAT, 0);
746 class_addbang(binop1_min_class, binop1_min_bang);
747 class_addfloat(binop1_min_class, (t_method)binop1_min_float);
748 class_sethelpsymbol(binop1_min_class, binop1_sym);
749
750 /* ------------------ binop2 ----------------------- */
751
752 binop2_ee_class = class_new(gensym("=="), (t_newmethod)binop2_ee_new, 0,
753 sizeof(t_binop), 0, A_DEFFLOAT, 0);
754 class_addbang(binop2_ee_class, binop2_ee_bang);
755 class_addfloat(binop2_ee_class, (t_method)binop2_ee_float);
756 class_sethelpsymbol(binop2_ee_class, binop23_sym);
757
758 binop2_ne_class = class_new(gensym("!="), (t_newmethod)binop2_ne_new, 0,
759 sizeof(t_binop), 0, A_DEFFLOAT, 0);
760 class_addbang(binop2_ne_class, binop2_ne_bang);
761 class_addfloat(binop2_ne_class, (t_method)binop2_ne_float);
762 class_sethelpsymbol(binop2_ne_class, binop23_sym);
763
764 binop2_gt_class = class_new(gensym(">"), (t_newmethod)binop2_gt_new, 0,
765 sizeof(t_binop), 0, A_DEFFLOAT, 0);
766 class_addbang(binop2_gt_class, binop2_gt_bang);
767 class_addfloat(binop2_gt_class, (t_method)binop2_gt_float);
768 class_sethelpsymbol(binop2_gt_class, binop23_sym);
769
770 binop2_lt_class = class_new(gensym("<"), (t_newmethod)binop2_lt_new, 0,
771 sizeof(t_binop), 0, A_DEFFLOAT, 0);
772 class_addbang(binop2_lt_class, binop2_lt_bang);
773 class_addfloat(binop2_lt_class, (t_method)binop2_lt_float);
774 class_sethelpsymbol(binop2_lt_class, binop23_sym);
775
776 binop2_ge_class = class_new(gensym(">="), (t_newmethod)binop2_ge_new, 0,
777 sizeof(t_binop), 0, A_DEFFLOAT, 0);
778 class_addbang(binop2_ge_class, binop2_ge_bang);
779 class_addfloat(binop2_ge_class, (t_method)binop2_ge_float);
780 class_sethelpsymbol(binop2_ge_class, binop23_sym);
781
782 binop2_le_class = class_new(gensym("<="), (t_newmethod)binop2_le_new, 0,
783 sizeof(t_binop), 0, A_DEFFLOAT, 0);
784 class_addbang(binop2_le_class, binop2_le_bang);
785 class_addfloat(binop2_le_class, (t_method)binop2_le_float);
786 class_sethelpsymbol(binop2_le_class, binop23_sym);
787
788 /* ------------------ binop3 ----------------------- */
789
790 binop3_ba_class = class_new(gensym("&"), (t_newmethod)binop3_ba_new, 0,
791 sizeof(t_binop), 0, A_DEFFLOAT, 0);
792 class_addbang(binop3_ba_class, binop2_ba_bang);
793 class_addfloat(binop3_ba_class, (t_method)binop2_ba_float);
794 class_sethelpsymbol(binop3_ba_class, binop23_sym);
795
796 binop3_la_class = class_new(gensym("&&"), (t_newmethod)binop3_la_new, 0,
797 sizeof(t_binop), 0, A_DEFFLOAT, 0);
798 class_addbang(binop3_la_class, binop2_la_bang);
799 class_addfloat(binop3_la_class, (t_method)binop2_la_float);
800 class_sethelpsymbol(binop3_la_class, binop23_sym);
801
802 binop3_bo_class = class_new(gensym("|"), (t_newmethod)binop3_bo_new, 0,
803 sizeof(t_binop), 0, A_DEFFLOAT, 0);
804 class_addbang(binop3_bo_class, binop2_bo_bang);
805 class_addfloat(binop3_bo_class, (t_method)binop2_bo_float);
806 class_sethelpsymbol(binop3_bo_class, binop23_sym);
807
808 binop3_lo_class = class_new(gensym("||"), (t_newmethod)binop3_lo_new, 0,
809 sizeof(t_binop), 0, A_DEFFLOAT, 0);
810 class_addbang(binop3_lo_class, binop2_lo_bang);
811 class_addfloat(binop3_lo_class, (t_method)binop2_lo_float);
812 class_sethelpsymbol(binop3_lo_class, binop23_sym);
813
814 binop3_ls_class = class_new(gensym("<<"), (t_newmethod)binop3_ls_new, 0,
815 sizeof(t_binop), 0, A_DEFFLOAT, 0);
816 class_addbang(binop3_ls_class, binop2_ls_bang);
817 class_addfloat(binop3_ls_class, (t_method)binop2_ls_float);
818 class_sethelpsymbol(binop3_ls_class, binop23_sym);
819
820 binop3_rs_class = class_new(gensym(">>"), (t_newmethod)binop3_rs_new, 0,
821 sizeof(t_binop), 0, A_DEFFLOAT, 0);
822 class_addbang(binop3_rs_class, binop2_rs_bang);
823 class_addfloat(binop3_rs_class, (t_method)binop2_rs_float);
824 class_sethelpsymbol(binop3_rs_class, binop23_sym);
825
826 binop3_pc_class = class_new(gensym("%"), (t_newmethod)binop3_pc_new, 0,
827 sizeof(t_binop), 0, A_DEFFLOAT, 0);
828 class_addbang(binop3_pc_class, binop2_pc_bang);
829 class_addfloat(binop3_pc_class, (t_method)binop2_pc_float);
830 class_sethelpsymbol(binop3_pc_class, binop23_sym);
831
832 binop3_mod_class = class_new(gensym("mod"), (t_newmethod)binop3_mod_new, 0,
833 sizeof(t_binop), 0, A_DEFFLOAT, 0);
834 class_addbang(binop3_mod_class, binop3_mod_bang);
835 class_addfloat(binop3_mod_class, (t_method)binop3_mod_float);
836 class_sethelpsymbol(binop3_mod_class, binop23_sym);
837
838 binop3_div_class = class_new(gensym("div"), (t_newmethod)binop3_div_new, 0,
839 sizeof(t_binop), 0, A_DEFFLOAT, 0);
840 class_addbang(binop3_div_class, binop3_div_bang);
841 class_addfloat(binop3_div_class, (t_method)binop3_div_float);
842 class_sethelpsymbol(binop3_div_class, binop23_sym);
843
844 /* ------------------- math functions --------------- */
845
846 sin_class = class_new(gensym("sin"), sin_new, 0,
847 sizeof(t_object), 0, 0);
848 class_addfloat(sin_class, (t_method)sin_float);
849 class_sethelpsymbol(sin_class, math_sym);
850
851 cos_class = class_new(gensym("cos"), cos_new, 0,
852 sizeof(t_object), 0, 0);
853 class_addfloat(cos_class, (t_method)cos_float);
854 class_sethelpsymbol(cos_class, math_sym);
855
856 tan_class = class_new(gensym("tan"), tan_new, 0,
857 sizeof(t_object), 0, 0);
858 class_addfloat(tan_class, (t_method)tan_float);
859 class_sethelpsymbol(tan_class, math_sym);
860
861 atan_class = class_new(gensym("atan"), atan_new, 0,
862 sizeof(t_object), 0, 0);
863 class_addfloat(atan_class, (t_method)atan_float);
864 class_sethelpsymbol(atan_class, math_sym);
865
866 atan2_class = class_new(gensym("atan2"), atan2_new, 0,
867 sizeof(t_atan2), 0, 0);
868 class_addfloat(atan2_class, (t_method)atan2_float);
869 class_sethelpsymbol(atan2_class, math_sym);
870
871 sqrt_class = class_new(gensym("sqrt"), sqrt_new, 0,
872 sizeof(t_object), 0, 0);
873 class_addfloat(sqrt_class, (t_method)sqrt_float);
874 class_sethelpsymbol(sqrt_class, math_sym);
875
876 log_class = class_new(gensym("log"), log_new, 0,
877 sizeof(t_object), 0, 0);
878 class_addfloat(log_class, (t_method)log_float);
879 class_sethelpsymbol(log_class, math_sym);
880
881 exp_class = class_new(gensym("exp"), exp_new, 0,
882 sizeof(t_object), 0, 0);
883 class_addfloat(exp_class, (t_method)exp_float);
884 class_sethelpsymbol(exp_class, math_sym);
885
886 abs_class = class_new(gensym("abs"), abs_new, 0,
887 sizeof(t_object), 0, 0);
888 class_addfloat(abs_class, (t_method)abs_float);
889 class_sethelpsymbol(abs_class, math_sym);
890
891/* ------------------------ misc ------------------------ */
892
893 clip_setup();
894}
895
896
897/* Copyright (c) 1997-1999 Miller Puckette.
898* For information on usage and redistribution, and for a DISCLAIMER OF ALL
899* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
900
901/* arithmetic: binops ala C language. The 4 functions and relationals are
902done on floats; the logical and bitwise binops convert their
903inputs to int and their outputs back to float. */
904
905#include "m_pd.h"
906#include <math.h>
907
908
909/* MSW and OSX don't appear to have single-precision ANSI math */
910
911#define sinf sin
912#define cosf cos
913#define atanf atan
914#define atan2f atan2
915#define sqrtf sqrt
916#define logf log
917#define expf exp
918#define fabsf fabs
919#define powf pow
920
921
922typedef struct _binop
923{
924 t_object x_obj;
925 t_float x_f1;
926 t_float x_f2;
927} t_binop;
928
929/* ------------------ binop1: +, -, *, / ----------------------------- */
930
931static void *binop1_new(t_class *floatclass, t_floatarg f)
932{
933 t_binop *x = (t_binop *)pd_new(floatclass);
934 outlet_new(&x->x_obj, &s_float);
935 floatinlet_new(&x->x_obj, &x->x_f2);
936 x->x_f1 = 0;
937 x->x_f2 = f;
938 return (x);
939}
940
941/* --------------------- addition ------------------------------- */
942
943static t_class *binop1_plus_class;
944
945static void *binop1_plus_new(t_floatarg f)
946{
947 return (binop1_new(binop1_plus_class, f));
948}
949
950static void binop1_plus_bang(t_binop *x)
951{
952 outlet_float(x->x_obj.ob_outlet, x->x_f1 + x->x_f2);
953}
954
955static void binop1_plus_float(t_binop *x, t_float f)
956{
957 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) + x->x_f2);
958}
959
960/* --------------------- subtraction ------------------------------- */
961
962static t_class *binop1_minus_class;
963
964static void *binop1_minus_new(t_floatarg f)
965{
966 return (binop1_new(binop1_minus_class, f));
967}
968
969static void binop1_minus_bang(t_binop *x)
970{
971 outlet_float(x->x_obj.ob_outlet, x->x_f1 - x->x_f2);
972}
973
974static void binop1_minus_float(t_binop *x, t_float f)
975{
976 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) - x->x_f2);
977}
978
979/* --------------------- multiplication ------------------------------- */
980
981static t_class *binop1_times_class;
982
983static void *binop1_times_new(t_floatarg f)
984{
985 return (binop1_new(binop1_times_class, f));
986}
987
988static void binop1_times_bang(t_binop *x)
989{
990 outlet_float(x->x_obj.ob_outlet, x->x_f1 * x->x_f2);
991}
992
993static void binop1_times_float(t_binop *x, t_float f)
994{
995 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) * x->x_f2);
996}
997
998/* --------------------- division ------------------------------- */
999
1000static t_class *binop1_div_class;
1001
1002static void *binop1_div_new(t_floatarg f)
1003{
1004 return (binop1_new(binop1_div_class, f));
1005}
1006
1007static void binop1_div_bang(t_binop *x)
1008{
1009 outlet_float(x->x_obj.ob_outlet,
1010 (x->x_f2 != 0 ? x->x_f1 / x->x_f2 : 0));
1011}
1012
1013static void binop1_div_float(t_binop *x, t_float f)
1014{
1015 x->x_f1 = f;
1016 outlet_float(x->x_obj.ob_outlet,
1017 (x->x_f2 != 0 ? x->x_f1 / x->x_f2 : 0));
1018}
1019
1020/* ------------------------ pow -------------------------------- */
1021
1022static t_class *binop1_pow_class;
1023
1024static void *binop1_pow_new(t_floatarg f)
1025{
1026 return (binop1_new(binop1_pow_class, f));
1027}
1028
1029static void binop1_pow_bang(t_binop *x)
1030{
1031 outlet_float(x->x_obj.ob_outlet,
1032 (x->x_f1 > 0 ? powf(x->x_f1, x->x_f2) : 0));
1033}
1034
1035static void binop1_pow_float(t_binop *x, t_float f)
1036{
1037 x->x_f1 = f;
1038 outlet_float(x->x_obj.ob_outlet,
1039 (x->x_f1 > 0 ? powf(x->x_f1, x->x_f2) : 0));
1040}
1041
1042/* ------------------------ max -------------------------------- */
1043
1044static t_class *binop1_max_class;
1045
1046static void *binop1_max_new(t_floatarg f)
1047{
1048 return (binop1_new(binop1_max_class, f));
1049}
1050
1051static void binop1_max_bang(t_binop *x)
1052{
1053 outlet_float(x->x_obj.ob_outlet,
1054 (x->x_f1 > x->x_f2 ? x->x_f1 : x->x_f2));
1055}
1056
1057static void binop1_max_float(t_binop *x, t_float f)
1058{
1059 x->x_f1 = f;
1060 outlet_float(x->x_obj.ob_outlet,
1061 (x->x_f1 > x->x_f2 ? x->x_f1 : x->x_f2));
1062}
1063
1064/* ------------------------ min -------------------------------- */
1065
1066static t_class *binop1_min_class;
1067
1068static void *binop1_min_new(t_floatarg f)
1069{
1070 return (binop1_new(binop1_min_class, f));
1071}
1072
1073static void binop1_min_bang(t_binop *x)
1074{
1075 outlet_float(x->x_obj.ob_outlet,
1076 (x->x_f1 < x->x_f2 ? x->x_f1 : x->x_f2));
1077}
1078
1079static void binop1_min_float(t_binop *x, t_float f)
1080{
1081 x->x_f1 = f;
1082 outlet_float(x->x_obj.ob_outlet,
1083 (x->x_f1 < x->x_f2 ? x->x_f1 : x->x_f2));
1084}
1085
1086/* ------------------ binop2: ==, !=, >, <, >=, <=. -------------------- */
1087
1088static void *binop2_new(t_class *floatclass, t_floatarg f)
1089{
1090 t_binop *x = (t_binop *)pd_new(floatclass);
1091 outlet_new(&x->x_obj, &s_float);
1092 floatinlet_new(&x->x_obj, &x->x_f2);
1093 x->x_f1 = 0;
1094 x->x_f2 = f;
1095 return (x);
1096}
1097
1098/* --------------------- == ------------------------------- */
1099
1100static t_class *binop2_ee_class;
1101
1102static void *binop2_ee_new(t_floatarg f)
1103{
1104 return (binop2_new(binop2_ee_class, f));
1105}
1106
1107static void binop2_ee_bang(t_binop *x)
1108{
1109 outlet_float(x->x_obj.ob_outlet, x->x_f1 == x->x_f2);
1110}
1111
1112static void binop2_ee_float(t_binop *x, t_float f)
1113{
1114 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) == x->x_f2);
1115}
1116
1117/* --------------------- != ------------------------------- */
1118
1119static t_class *binop2_ne_class;
1120
1121static void *binop2_ne_new(t_floatarg f)
1122{
1123 return (binop2_new(binop2_ne_class, f));
1124}
1125
1126static void binop2_ne_bang(t_binop *x)
1127{
1128 outlet_float(x->x_obj.ob_outlet, x->x_f1 != x->x_f2);
1129}
1130
1131static void binop2_ne_float(t_binop *x, t_float f)
1132{
1133 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) != x->x_f2);
1134}
1135
1136/* --------------------- > ------------------------------- */
1137
1138static t_class *binop2_gt_class;
1139
1140static void *binop2_gt_new(t_floatarg f)
1141{
1142 return (binop2_new(binop2_gt_class, f));
1143}
1144
1145static void binop2_gt_bang(t_binop *x)
1146{
1147 outlet_float(x->x_obj.ob_outlet, x->x_f1 > x->x_f2);
1148}
1149
1150static void binop2_gt_float(t_binop *x, t_float f)
1151{
1152 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) > x->x_f2);
1153}
1154
1155/* --------------------- < ------------------------------- */
1156
1157static t_class *binop2_lt_class;
1158
1159static void *binop2_lt_new(t_floatarg f)
1160{
1161 return (binop2_new(binop2_lt_class, f));
1162}
1163
1164static void binop2_lt_bang(t_binop *x)
1165{
1166 outlet_float(x->x_obj.ob_outlet, x->x_f1 < x->x_f2);
1167}
1168
1169static void binop2_lt_float(t_binop *x, t_float f)
1170{
1171 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) < x->x_f2);
1172}
1173
1174/* --------------------- >= ------------------------------- */
1175
1176static t_class *binop2_ge_class;
1177
1178static void *binop2_ge_new(t_floatarg f)
1179{
1180 return (binop2_new(binop2_ge_class, f));
1181}
1182
1183static void binop2_ge_bang(t_binop *x)
1184{
1185 outlet_float(x->x_obj.ob_outlet, x->x_f1 >= x->x_f2);
1186}
1187
1188static void binop2_ge_float(t_binop *x, t_float f)
1189{
1190 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) >= x->x_f2);
1191}
1192
1193/* --------------------- <= ------------------------------- */
1194
1195static t_class *binop2_le_class;
1196
1197static void *binop2_le_new(t_floatarg f)
1198{
1199 return (binop2_new(binop2_le_class, f));
1200}
1201
1202static void binop2_le_bang(t_binop *x)
1203{
1204 outlet_float(x->x_obj.ob_outlet, x->x_f1 <= x->x_f2);
1205}
1206
1207static void binop2_le_float(t_binop *x, t_float f)
1208{
1209 outlet_float(x->x_obj.ob_outlet, (x->x_f1 = f) <= x->x_f2);
1210}
1211
1212/* ------------- binop3: &, |, &&, ||, <<, >>, %, mod, div ------------------ */
1213
1214static void *binop3_new(t_class *fixclass, t_floatarg f)
1215{
1216 t_binop *x = (t_binop *)pd_new(fixclass);
1217 outlet_new(&x->x_obj, &s_float);
1218 floatinlet_new(&x->x_obj, &x->x_f2);
1219 x->x_f1 = 0;
1220 x->x_f2 = f;
1221 return (x);
1222}
1223
1224/* --------------------------- & ---------------------------- */
1225
1226static t_class *binop3_ba_class;
1227
1228static void *binop3_ba_new(t_floatarg f)
1229{
1230 return (binop3_new(binop3_ba_class, f));
1231}
1232
1233static void binop2_ba_bang(t_binop *x)
1234{
1235 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) & (int)(x->x_f2));
1236}
1237
1238static void binop2_ba_float(t_binop *x, t_float f)
1239{
1240 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) & (int)(x->x_f2));
1241}
1242
1243/* --------------------------- && ---------------------------- */
1244
1245static t_class *binop3_la_class;
1246
1247static void *binop3_la_new(t_floatarg f)
1248{
1249 return (binop3_new(binop3_la_class, f));
1250}
1251
1252static void binop2_la_bang(t_binop *x)
1253{
1254 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) && (int)(x->x_f2));
1255}
1256
1257static void binop2_la_float(t_binop *x, t_float f)
1258{
1259 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) && (int)(x->x_f2));
1260}
1261
1262/* --------------------------- | ---------------------------- */
1263
1264static t_class *binop3_bo_class;
1265
1266static void *binop3_bo_new(t_floatarg f)
1267{
1268 return (binop3_new(binop3_bo_class, f));
1269}
1270
1271static void binop2_bo_bang(t_binop *x)
1272{
1273 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) | (int)(x->x_f2));
1274}
1275
1276static void binop2_bo_float(t_binop *x, t_float f)
1277{
1278 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) | (int)(x->x_f2));
1279}
1280
1281/* --------------------------- || ---------------------------- */
1282
1283static t_class *binop3_lo_class;
1284
1285static void *binop3_lo_new(t_floatarg f)
1286{
1287 return (binop3_new(binop3_lo_class, f));
1288}
1289
1290static void binop2_lo_bang(t_binop *x)
1291{
1292 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) || (int)(x->x_f2));
1293}
1294
1295static void binop2_lo_float(t_binop *x, t_float f)
1296{
1297 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) || (int)(x->x_f2));
1298}
1299
1300/* --------------------------- << ---------------------------- */
1301
1302static t_class *binop3_ls_class;
1303
1304static void *binop3_ls_new(t_floatarg f)
1305{
1306 return (binop3_new(binop3_ls_class, f));
1307}
1308
1309static void binop2_ls_bang(t_binop *x)
1310{
1311 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) << (int)(x->x_f2));
1312}
1313
1314static void binop2_ls_float(t_binop *x, t_float f)
1315{
1316 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) << (int)(x->x_f2));
1317}
1318
1319/* --------------------------- >> ---------------------------- */
1320
1321static t_class *binop3_rs_class;
1322
1323static void *binop3_rs_new(t_floatarg f)
1324{
1325 return (binop3_new(binop3_rs_class, f));
1326}
1327
1328static void binop2_rs_bang(t_binop *x)
1329{
1330 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) >> (int)(x->x_f2));
1331}
1332
1333static void binop2_rs_float(t_binop *x, t_float f)
1334{
1335 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) >> (int)(x->x_f2));
1336}
1337
1338/* --------------------------- % ---------------------------- */
1339
1340static t_class *binop3_pc_class;
1341
1342static void *binop3_pc_new(t_floatarg f)
1343{
1344 return (binop3_new(binop3_pc_class, f));
1345}
1346
1347static void binop2_pc_bang(t_binop *x)
1348{
1349 int n2 = x->x_f2;
1350 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1)) % (n2 ? n2 : 1));
1351}
1352
1353static void binop2_pc_float(t_binop *x, t_float f)
1354{
1355 int n2 = x->x_f2;
1356 outlet_float(x->x_obj.ob_outlet, ((int)(x->x_f1 = f)) % (n2 ? n2 : 1));
1357}
1358
1359/* --------------------------- mod ---------------------------- */
1360
1361static t_class *binop3_mod_class;
1362
1363static void *binop3_mod_new(t_floatarg f)
1364{
1365 return (binop3_new(binop3_mod_class, f));
1366}
1367
1368static void binop3_mod_bang(t_binop *x)
1369{
1370 int n2 = x->x_f2, result;
1371 if (n2 < 0) n2 = -n2;
1372 else if (!n2) n2 = 1;
1373 result = ((int)(x->x_f1)) % n2;
1374 if (result < 0) result += n2;
1375 outlet_float(x->x_obj.ob_outlet, (t_float)result);
1376}
1377
1378static void binop3_mod_float(t_binop *x, t_float f)
1379{
1380 x->x_f1 = f;
1381 binop3_mod_bang(x);
1382}
1383
1384/* --------------------------- div ---------------------------- */
1385
1386static t_class *binop3_div_class;
1387
1388static void *binop3_div_new(t_floatarg f)
1389{
1390 return (binop3_new(binop3_div_class, f));
1391}
1392
1393static void binop3_div_bang(t_binop *x)
1394{
1395 int n1 = x->x_f1, n2 = x->x_f2, result;
1396 if (n2 < 0) n2 = -n2;
1397 else if (!n2) n2 = 1;
1398 if (n1 < 0) n1 -= (n2-1);
1399 result = n1 / n2;
1400 outlet_float(x->x_obj.ob_outlet, (t_float)result);
1401}
1402
1403static void binop3_div_float(t_binop *x, t_float f)
1404{
1405 x->x_f1 = f;
1406 binop3_div_bang(x);
1407}
1408
1409/* -------------------- mathematical functions ------------------ */
1410
1411static t_class *sin_class; /* ----------- sin --------------- */
1412
1413static void *sin_new(void)
1414{
1415 t_object *x = (t_object *)pd_new(sin_class);
1416 outlet_new(x, &s_float);
1417 return (x);
1418}
1419
1420static void sin_float(t_object *x, t_float f)
1421{
1422 outlet_float(x->ob_outlet, sinf(f));
1423}
1424
1425static t_class *cos_class; /* ----------- cos --------------- */
1426
1427static void *cos_new(void)
1428{
1429 t_object *x = (t_object *)pd_new(cos_class);
1430 outlet_new(x, &s_float);
1431 return (x);
1432}
1433
1434static void cos_float(t_object *x, t_float f)
1435{
1436 outlet_float(x->ob_outlet, cosf(f));
1437}
1438
1439static t_class *tan_class; /* ----------- tan --------------- */
1440
1441static void *tan_new(void)
1442{
1443 t_object *x = (t_object *)pd_new(tan_class);
1444 outlet_new(x, &s_float);
1445 return (x);
1446}
1447
1448static void tan_float(t_object *x, t_float f)
1449{
1450 float c = cosf(f);
1451 float t = (c == 0 ? 0 : sinf(f)/c);
1452 outlet_float(x->ob_outlet, t);
1453}
1454
1455static t_class *atan_class; /* ----------- atan --------------- */
1456
1457static void *atan_new(void)
1458{
1459 t_object *x = (t_object *)pd_new(atan_class);
1460 outlet_new(x, &s_float);
1461 return (x);
1462}
1463
1464static void atan_float(t_object *x, t_float f)
1465{
1466 outlet_float(x->ob_outlet, atanf(f));
1467}
1468
1469static t_class *atan2_class; /* ----------- atan2 --------------- */
1470
1471typedef struct _atan2
1472{
1473 t_object x_ob;
1474 float x_y;
1475} t_atan2;
1476
1477static void *atan2_new(void)
1478{
1479 t_atan2 *x = (t_atan2 *)pd_new(atan2_class);
1480 floatinlet_new(&x->x_ob, &x->x_y);
1481 outlet_new(&x->x_ob, &s_float);
1482 return (x);
1483}
1484
1485static void atan2_float(t_atan2 *x, t_float f)
1486{
1487 float r = (f == 0 && x->x_y == 0 ? 0 : atan2f(x->x_y, f));
1488 outlet_float(x->x_ob.ob_outlet, r);
1489}
1490
1491static t_class *sqrt_class; /* ----------- sqrt --------------- */
1492
1493static void *sqrt_new(void)
1494{
1495 t_object *x = (t_object *)pd_new(sqrt_class);
1496 outlet_new(x, &s_float);
1497 return (x);
1498}
1499
1500static void sqrt_float(t_object *x, t_float f)
1501{
1502 float r = (f > 0 ? sqrtf(f) : 0);
1503 outlet_float(x->ob_outlet, r);
1504}
1505
1506static t_class *log_class; /* ----------- log --------------- */
1507
1508static void *log_new(void)
1509{
1510 t_object *x = (t_object *)pd_new(log_class);
1511 outlet_new(x, &s_float);
1512 return (x);
1513}
1514
1515static void log_float(t_object *x, t_float f)
1516{
1517 float r = (f > 0 ? logf(f) : -1000);
1518 outlet_float(x->ob_outlet, r);
1519}
1520
1521
1522static t_class *exp_class; /* ----------- exp --------------- */
1523
1524static void *exp_new(void)
1525{
1526 t_object *x = (t_object *)pd_new(exp_class);
1527 outlet_new(x, &s_float);
1528 return (x);
1529}
1530
1531#define MAXLOG 87.3365
1532static void exp_float(t_object *x, t_float f)
1533{
1534 float g;
1535#ifdef MSW
1536 char buf[10];
1537#endif
1538 if (f > MAXLOG) f = MAXLOG;
1539 g = expf(f);
1540 outlet_float(x->ob_outlet, g);
1541}
1542
1543static t_class *abs_class; /* ----------- abs --------------- */
1544
1545static void *abs_new(void)
1546{
1547 t_object *x = (t_object *)pd_new(abs_class);
1548 outlet_new(x, &s_float);
1549 return (x);
1550}
1551
1552static void abs_float(t_object *x, t_float f)
1553{
1554 outlet_float(x->ob_outlet, fabsf(f));
1555}
1556
1557/* ------------------------ misc ------------------------ */
1558
1559static t_class *clip_class;
1560
1561typedef struct _clip
1562{
1563 t_object x_ob;
1564 float x_f1;
1565 float x_f2;
1566} t_clip;
1567
1568static void *clip_new(t_floatarg f1, t_floatarg f2)
1569{
1570 t_clip *x = (t_clip *)pd_new(clip_class);
1571 floatinlet_new(&x->x_ob, &x->x_f1);
1572 floatinlet_new(&x->x_ob, &x->x_f2);
1573 outlet_new(&x->x_ob, &s_float);
1574 x->x_f1 = f1;
1575 x->x_f2 = f2;
1576 return (x);
1577}
1578
1579static void clip_float(t_clip *x, t_float f)
1580{
1581 outlet_float(x->x_ob.ob_outlet, (f < x->x_f1 ? x->x_f1 : (
1582 f > x->x_f2 ? x->x_f2 : f)));
1583}
1584
1585static void clip_setup(void)
1586{
1587 clip_class = class_new(gensym("clip"), (t_newmethod)clip_new, 0,
1588 sizeof(t_clip), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
1589 class_addfloat(clip_class, clip_float);
1590}
1591
1592void x_arithmetic_setup(void)
1593{
1594 t_symbol *binop1_sym = gensym("operators");
1595 t_symbol *binop23_sym = gensym("otherbinops");
1596 t_symbol *math_sym = gensym("math");
1597
1598 binop1_plus_class = class_new(gensym("+"), (t_newmethod)binop1_plus_new, 0,
1599 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1600 class_addbang(binop1_plus_class, binop1_plus_bang);
1601 class_addfloat(binop1_plus_class, (t_method)binop1_plus_float);
1602 class_sethelpsymbol(binop1_plus_class, binop1_sym);
1603
1604 binop1_minus_class = class_new(gensym("-"),
1605 (t_newmethod)binop1_minus_new, 0,
1606 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1607 class_addbang(binop1_minus_class, binop1_minus_bang);
1608 class_addfloat(binop1_minus_class, (t_method)binop1_minus_float);
1609 class_sethelpsymbol(binop1_minus_class, binop1_sym);
1610
1611 binop1_times_class = class_new(gensym("*"),
1612 (t_newmethod)binop1_times_new, 0,
1613 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1614 class_addbang(binop1_times_class, binop1_times_bang);
1615 class_addfloat(binop1_times_class, (t_method)binop1_times_float);
1616 class_sethelpsymbol(binop1_times_class, binop1_sym);
1617
1618 binop1_div_class = class_new(gensym("/"),
1619 (t_newmethod)binop1_div_new, 0,
1620 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1621 class_addbang(binop1_div_class, binop1_div_bang);
1622 class_addfloat(binop1_div_class, (t_method)binop1_div_float);
1623 class_sethelpsymbol(binop1_div_class, binop1_sym);
1624
1625 binop1_pow_class = class_new(gensym("pow"),
1626 (t_newmethod)binop1_pow_new, 0,
1627 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1628 class_addbang(binop1_pow_class, binop1_pow_bang);
1629 class_addfloat(binop1_pow_class, (t_method)binop1_pow_float);
1630 class_sethelpsymbol(binop1_pow_class, binop1_sym);
1631
1632 binop1_max_class = class_new(gensym("max"),
1633 (t_newmethod)binop1_max_new, 0,
1634 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1635 class_addbang(binop1_max_class, binop1_max_bang);
1636 class_addfloat(binop1_max_class, (t_method)binop1_max_float);
1637 class_sethelpsymbol(binop1_max_class, binop1_sym);
1638
1639 binop1_min_class = class_new(gensym("min"),
1640 (t_newmethod)binop1_min_new, 0,
1641 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1642 class_addbang(binop1_min_class, binop1_min_bang);
1643 class_addfloat(binop1_min_class, (t_method)binop1_min_float);
1644 class_sethelpsymbol(binop1_min_class, binop1_sym);
1645
1646 /* ------------------ binop2 ----------------------- */
1647
1648 binop2_ee_class = class_new(gensym("=="), (t_newmethod)binop2_ee_new, 0,
1649 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1650 class_addbang(binop2_ee_class, binop2_ee_bang);
1651 class_addfloat(binop2_ee_class, (t_method)binop2_ee_float);
1652 class_sethelpsymbol(binop2_ee_class, binop23_sym);
1653
1654 binop2_ne_class = class_new(gensym("!="), (t_newmethod)binop2_ne_new, 0,
1655 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1656 class_addbang(binop2_ne_class, binop2_ne_bang);
1657 class_addfloat(binop2_ne_class, (t_method)binop2_ne_float);
1658 class_sethelpsymbol(binop2_ne_class, binop23_sym);
1659
1660 binop2_gt_class = class_new(gensym(">"), (t_newmethod)binop2_gt_new, 0,
1661 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1662 class_addbang(binop2_gt_class, binop2_gt_bang);
1663 class_addfloat(binop2_gt_class, (t_method)binop2_gt_float);
1664 class_sethelpsymbol(binop2_gt_class, binop23_sym);
1665
1666 binop2_lt_class = class_new(gensym("<"), (t_newmethod)binop2_lt_new, 0,
1667 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1668 class_addbang(binop2_lt_class, binop2_lt_bang);
1669 class_addfloat(binop2_lt_class, (t_method)binop2_lt_float);
1670 class_sethelpsymbol(binop2_lt_class, binop23_sym);
1671
1672 binop2_ge_class = class_new(gensym(">="), (t_newmethod)binop2_ge_new, 0,
1673 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1674 class_addbang(binop2_ge_class, binop2_ge_bang);
1675 class_addfloat(binop2_ge_class, (t_method)binop2_ge_float);
1676 class_sethelpsymbol(binop2_ge_class, binop23_sym);
1677
1678 binop2_le_class = class_new(gensym("<="), (t_newmethod)binop2_le_new, 0,
1679 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1680 class_addbang(binop2_le_class, binop2_le_bang);
1681 class_addfloat(binop2_le_class, (t_method)binop2_le_float);
1682 class_sethelpsymbol(binop2_le_class, binop23_sym);
1683
1684 /* ------------------ binop3 ----------------------- */
1685
1686 binop3_ba_class = class_new(gensym("&"), (t_newmethod)binop3_ba_new, 0,
1687 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1688 class_addbang(binop3_ba_class, binop2_ba_bang);
1689 class_addfloat(binop3_ba_class, (t_method)binop2_ba_float);
1690 class_sethelpsymbol(binop3_ba_class, binop23_sym);
1691
1692 binop3_la_class = class_new(gensym("&&"), (t_newmethod)binop3_la_new, 0,
1693 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1694 class_addbang(binop3_la_class, binop2_la_bang);
1695 class_addfloat(binop3_la_class, (t_method)binop2_la_float);
1696 class_sethelpsymbol(binop3_la_class, binop23_sym);
1697
1698 binop3_bo_class = class_new(gensym("|"), (t_newmethod)binop3_bo_new, 0,
1699 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1700 class_addbang(binop3_bo_class, binop2_bo_bang);
1701 class_addfloat(binop3_bo_class, (t_method)binop2_bo_float);
1702 class_sethelpsymbol(binop3_bo_class, binop23_sym);
1703
1704 binop3_lo_class = class_new(gensym("||"), (t_newmethod)binop3_lo_new, 0,
1705 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1706 class_addbang(binop3_lo_class, binop2_lo_bang);
1707 class_addfloat(binop3_lo_class, (t_method)binop2_lo_float);
1708 class_sethelpsymbol(binop3_lo_class, binop23_sym);
1709
1710 binop3_ls_class = class_new(gensym("<<"), (t_newmethod)binop3_ls_new, 0,
1711 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1712 class_addbang(binop3_ls_class, binop2_ls_bang);
1713 class_addfloat(binop3_ls_class, (t_method)binop2_ls_float);
1714 class_sethelpsymbol(binop3_ls_class, binop23_sym);
1715
1716 binop3_rs_class = class_new(gensym(">>"), (t_newmethod)binop3_rs_new, 0,
1717 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1718 class_addbang(binop3_rs_class, binop2_rs_bang);
1719 class_addfloat(binop3_rs_class, (t_method)binop2_rs_float);
1720 class_sethelpsymbol(binop3_rs_class, binop23_sym);
1721
1722 binop3_pc_class = class_new(gensym("%"), (t_newmethod)binop3_pc_new, 0,
1723 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1724 class_addbang(binop3_pc_class, binop2_pc_bang);
1725 class_addfloat(binop3_pc_class, (t_method)binop2_pc_float);
1726 class_sethelpsymbol(binop3_pc_class, binop23_sym);
1727
1728 binop3_mod_class = class_new(gensym("mod"), (t_newmethod)binop3_mod_new, 0,
1729 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1730 class_addbang(binop3_mod_class, binop3_mod_bang);
1731 class_addfloat(binop3_mod_class, (t_method)binop3_mod_float);
1732 class_sethelpsymbol(binop3_mod_class, binop23_sym);
1733
1734 binop3_div_class = class_new(gensym("div"), (t_newmethod)binop3_div_new, 0,
1735 sizeof(t_binop), 0, A_DEFFLOAT, 0);
1736 class_addbang(binop3_div_class, binop3_div_bang);
1737 class_addfloat(binop3_div_class, (t_method)binop3_div_float);
1738 class_sethelpsymbol(binop3_div_class, binop23_sym);
1739
1740 /* ------------------- math functions --------------- */
1741
1742 sin_class = class_new(gensym("sin"), sin_new, 0,
1743 sizeof(t_object), 0, 0);
1744 class_addfloat(sin_class, (t_method)sin_float);
1745 class_sethelpsymbol(sin_class, math_sym);
1746
1747 cos_class = class_new(gensym("cos"), cos_new, 0,
1748 sizeof(t_object), 0, 0);
1749 class_addfloat(cos_class, (t_method)cos_float);
1750 class_sethelpsymbol(cos_class, math_sym);
1751
1752 tan_class = class_new(gensym("tan"), tan_new, 0,
1753 sizeof(t_object), 0, 0);
1754 class_addfloat(tan_class, (t_method)tan_float);
1755 class_sethelpsymbol(tan_class, math_sym);
1756
1757 atan_class = class_new(gensym("atan"), atan_new, 0,
1758 sizeof(t_object), 0, 0);
1759 class_addfloat(atan_class, (t_method)atan_float);
1760 class_sethelpsymbol(atan_class, math_sym);
1761
1762 atan2_class = class_new(gensym("atan2"), atan2_new, 0,
1763 sizeof(t_atan2), 0, 0);
1764 class_addfloat(atan2_class, (t_method)atan2_float);
1765 class_sethelpsymbol(atan2_class, math_sym);
1766
1767 sqrt_class = class_new(gensym("sqrt"), sqrt_new, 0,
1768 sizeof(t_object), 0, 0);
1769 class_addfloat(sqrt_class, (t_method)sqrt_float);
1770 class_sethelpsymbol(sqrt_class, math_sym);
1771
1772 log_class = class_new(gensym("log"), log_new, 0,
1773 sizeof(t_object), 0, 0);
1774 class_addfloat(log_class, (t_method)log_float);
1775 class_sethelpsymbol(log_class, math_sym);
1776
1777 exp_class = class_new(gensym("exp"), exp_new, 0,
1778 sizeof(t_object), 0, 0);
1779 class_addfloat(exp_class, (t_method)exp_float);
1780 class_sethelpsymbol(exp_class, math_sym);
1781
1782 abs_class = class_new(gensym("abs"), abs_new, 0,
1783 sizeof(t_object), 0, 0);
1784 class_addfloat(abs_class, (t_method)abs_float);
1785 class_sethelpsymbol(abs_class, math_sym);
1786
1787/* ------------------------ misc ------------------------ */
1788
1789 clip_setup();
1790}
1791
1792