owl_patch 0.8.0

Rust SDK for Rebel Technology Owl2/3 devices
Documentation
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
/*******************************************************************************************************************
Copyright (c) 2012 Cycling '74

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************************************************/

#ifndef GENLIB_OPS_H
#define GENLIB_OPS_H 1

#include "genlib_common.h"	// common to common code and any host code
#include "genlib.h"			// this file is different for different "hosts"
#ifdef clamp
#undef clamp
#endif

//////////// genlib_ops.h ////////////

// system constants
#define GENLIB_DBL_EPSILON (__DBL_EPSILON__)
#define GENLIB_FLT_EPSILON (__FLT_EPSILON__)

#ifdef GENLIB_USE_DOUBLE
#define GENLIB_EPSILON GENLIB_DBL_EPSILON
#else
#define GENLIB_EPSILON GENLIB_FLT_EPSILON
#endif
#define GENLIB_PI (3.14159265358979323846264338327950288)
#define GENLIB_PI_OVER_2 (1.57079632679489661923132169163975144)
#define GENLIB_PI_OVER_4 (0.785398163397448309615660845819875721)
#define GENLIB_1_OVER_LOG_2 (1.442695040888963)

// denormal numbers cannot occur when hosted in MSP:
#if defined(MSP_ON_CLANG) || defined(ARM_CORTEX)
	#define GENLIB_NO_DENORM_TEST 1
#endif

// assumes v is a 64-bit double:
#ifdef GENLIB_USE_DOUBLE
#define GENLIB_IS_NAN_DOUBLE(v)			(((((uint32_t *)&(v))[1])&0x7fe00000)==0x7fe00000)
#define GENLIB_FIX_NAN_DOUBLE(v)		((v)=GENLIB_IS_NAN_DOUBLE(v)?0.:(v))

#ifdef GENLIB_NO_DENORM_TEST
	#define GENLIB_IS_DENORM_DOUBLE(v)	(v)
	#define GENLIB_FIX_DENORM_DOUBLE(v)	(v)
#else
	#define GENLIB_IS_DENORM_DOUBLE(v)	((((((uint32_t *)&(v))[1])&0x7fe00000)==0)&&((v)!=0.))
	#define GENLIB_FIX_DENORM_DOUBLE(v)	((v)=GENLIB_IS_DENORM_DOUBLE(v)?0.f:(v))
#endif
#define GENLIB_IS_NAN		GENLIB_IS_NAN_DOUBLE
#define GENLIB_FIX_NAN		GENLIB_FIX_NAN_DOUBLE
#define GENLIB_IS_DENORM	GENLIB_IS_DENORM_DOUBLE
#define GENLIB_FIX_DENORM	GENLIB_FIX_DENORM_DOUBLE
#else
#define GENLIB_IS_NAN_FLOAT(v)			((v)!=(v))
#define GENLIB_FIX_NAN_FLOAT(v)			((v)=GENLIB_IS_NAN_FLOAT(v)?0.:(v))

#ifdef GENLIB_NO_DENORM_TEST
	#define GENLIB_IS_DENORM_FLOAT(v)	(v)
	#define GENLIB_FIX_DENORM_FLOAT(v)	(v)
#else
	#define GENLIB_IS_DENORM_FLOAT(v)	((v)!=0.&&fabs(v)<__FLT_MIN__)
	#define GENLIB_FIX_DENORM_FLOAT(v)	((v)=GENLIB_IS_DENORM_FLOAT(v)?0.f:(v))
#endif
#define GENLIB_IS_NAN		GENLIB_IS_NAN_FLOAT
#define GENLIB_FIX_NAN		GENLIB_FIX_NAN_FLOAT
#define GENLIB_IS_DENORM	GENLIB_IS_DENORM_FLOAT
#define GENLIB_FIX_DENORM	GENLIB_FIX_DENORM_FLOAT
#endif

#define GENLIB_QUANT(f1,f2)			(floor((f1)*(f2)+0.5)/(f2))

inline t_sample genlib_isnan(t_sample v) { return GENLIB_IS_NAN(v); }
inline t_sample fixnan(t_sample v) { return GENLIB_FIX_NAN(v); }
inline t_sample fixdenorm(t_sample v) { return GENLIB_FIX_DENORM(v); }
inline t_sample isdenorm(t_sample v) { return GENLIB_IS_DENORM(v); }

inline t_sample fasterpow(t_sample x, t_sample p);
inline t_sample fasterexp(t_sample x);
inline t_sample fastercosfull(t_sample x);
inline t_sample fastersinfull(t_sample x);
inline t_sample fastertanfull(t_sample x);

inline t_sample safemod(t_sample f, t_sample m) {
	if (m > GENLIB_DBL_EPSILON || m < -GENLIB_DBL_EPSILON) {
		if (m<0)
			m = -m; // modulus needs to be absolute value
		if (f>=m) {
			if (f>=(m*2.)) {
				t_sample d = f / m;
				d = d - (long) d;
				f = d * m;
			}
			else {
				f -= m;
			}
		}
		else if (f<=(-m)) {
			if (f<=(-m*2.)) {
				t_sample d = f / m;
				d = d - (long) d;
				f = d * m;
			}
			 else {
				f += m;
			}
		}
	} else {
		f = 0.0; //don't divide by zero
	}
	return f;
}


inline t_sample safediv(t_sample num, t_sample denom) {
	return denom == 0. ? 0. : num/denom;
}

// fixnan for case of negative base and non-integer exponent:
inline t_sample safepow(t_sample base, t_sample exponent) {
	return fixnan(powf(base, exponent));
}

inline t_sample absdiff(t_sample a, t_sample b) { return fabs(a-b); }

/* inline t_sample exp2(t_sample v) { return powf(2., v); } */

/* inline t_sample trunc(t_sample v) { */
/* 	t_sample epsilon = (v<0.0) * -2 * 1E-9 + 1E-9; */
/* 	// copy to long so it gets truncated (probably cheaper than floor()) */
/* 	long val = v + epsilon; */
/* 	return val; */
/* } */

inline t_sample sign(t_sample v) {
	return v > t_sample(0) ? t_sample(1) : v < t_sample(0) ? t_sample(-1) : t_sample(0);
}

inline long is_poweroftwo(long x) {
	return (x & (x - 1)) == 0;
}

inline uint64_t next_power_of_two(uint64_t v) {
    v--;
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    v |= v >> 32;
    v++;
    return v;
}

inline t_sample fold(t_sample v, t_sample lo1, t_sample hi1){
	t_sample lo;
	t_sample hi;
	if(lo1 == hi1){ return lo1; }
	if (lo1 > hi1) {
		hi = lo1; lo = hi1;
	} else {
		lo = lo1; hi = hi1;
	}
	const t_sample range = hi - lo;
	long numWraps = 0;
	if(v >= hi){
		v -= range;
		if(v >= hi){
			numWraps = (long)((v - lo)/range);
			v -= range * (t_sample)numWraps;
		}
		numWraps++;
	} else if(v < lo){
		v += range;
		if(v < lo){
			numWraps = (long)((v - lo)/range) - 1;
			v -= range * (t_sample)numWraps;
		}
		numWraps--;
	}
	if(numWraps & 1) v = hi + lo - v;	// flip sign for odd folds
	return v;
}

inline t_sample wrap(t_sample v, t_sample lo1, t_sample hi1){
	t_sample lo;
	t_sample hi;
	if(lo1 == hi1) return lo1;
	if (lo1 > hi1) {
		hi = lo1; lo = hi1;
	} else {
		lo = lo1; hi = hi1;
	}
	const t_sample range = hi - lo;
	if (v >= lo && v < hi) return v;
	if (range <= 0.000000001) return lo;	// no point...
	const long numWraps = long((v-lo)/range) - (v < lo);
	return v - range * t_sample(numWraps);
}

// this version gives far better performance when wrapping is relatively rare
// and typically double of wraps is very low (>1%)
// but catastrophic if wraps is high (1000%+)
inline t_sample genlib_wrapfew(t_sample v, t_sample lo, t_sample hi){
	const t_sample range = hi - lo;
	while (v >= hi) v -= range;
	while (v < lo) v += range;
	return v;
}

inline t_sample phasewrap(t_sample val) {
	const t_sample twopi = GENLIB_PI*2.;
	const t_sample oneovertwopi = 1./twopi;
	if (val>= twopi || val <= twopi) {
		t_sample d = val * oneovertwopi;	//multiply faster
		d = d - (long)d;
		val = d * twopi;
	}
	if (val > GENLIB_PI) val -= twopi;
	if (val < -GENLIB_PI) val += twopi;
	return val;
}

/// 8th order Taylor series approximation to a cosine.
/// r must be in [-pi, pi].
inline t_sample genlib_cosT8(t_sample r) {
	const t_sample t84 = 56.;
	const t_sample t83 = 1680.;
	const t_sample t82 = 20160.;
	const t_sample t81 = 2.4801587302e-05;
	const t_sample t73 = 42.;
	const t_sample t72 = 840.;
	const t_sample t71 = 1.9841269841e-04;
	if(r < GENLIB_PI_OVER_4 && r > -GENLIB_PI_OVER_4){
		t_sample rr = r*r;
		return 1. - rr * t81 * (t82 - rr * (t83 - rr * (t84 - rr)));
	}
	else if(r > 0.){
		r -= GENLIB_PI_OVER_2;
		t_sample rr = r*r;
		return -r * (1. - t71 * rr * (t72 - rr * (t73 - rr)));
	}
	else{
		r += GENLIB_PI_OVER_2;
		t_sample rr = r*r;
		return r * (1. - t71 * rr * (t72 - rr * (t73 - rr)));
	}
}

//inline double genlib_sin_fast(const double r){
//	const double y = (4./GENLIB_PI) * r + (-4./(GENLIB_PI*GENLIB_PI)) * r * fabs(r);
//	return 0.225 * (y * fabs(y) - y) + y;   // Q * y + P * y * abs(y)
//}
//
//inline t_sample genlib_sinP7(t_sample n){
//	t_sample nn = n*n;
//	return n * (t_sample(3.138982) + nn * (t_sample(-5.133625) + nn * (t_sample(2.428288) - nn * t_sample(0.433645))));
//}
//
//inline t_sample genlib_sinP9(t_sample n){
//	t_sample nn = n*n;
//	return n * (GENLIB_PI + nn * (t_sample(-5.1662729) + nn * (t_sample(2.5422065) + nn * (t_sample(-0.5811243) + nn * t_sample(0.0636716)))));
//}
//
//inline t_sample genlib_sinT7(t_sample r){
//	const t_sample t84 = 56.;
//	const t_sample t83 = 1680.;
//	const t_sample t82 = 20160.;
//	const t_sample t81 = 2.4801587302e-05;
//	const t_sample t73 = 42.;
//	const t_sample t72 = 840.;
//	const t_sample t71 = 1.9841269841e-04;
//	if(r < GENLIB_PI_OVER_4 && r > -GENLIB_PI_OVER_4){
//		t_sample rr = r*r;
//		return r * (1. - t71 * rr * (t72 - rr * (t73 - rr)));
//	}
//	else if(r > 0.){
//		r -= GENLIB_PI_OVER_2;
//		t_sample rr = r*r;
//		return t_sample(1.) - rr * t81 * (t82 - rr * (t83 - rr * (t84 - rr)));
//	}
//	else{
//		r += GENLIB_PI_OVER_2;
//		t_sample rr = r*r;
//		return t_sample(-1.) + rr * t81 * (t82 - rr * (t83 - rr * (t84 - rr)));
//	}
//}

// use these if r is not known to be in [-pi, pi]:
inline t_sample genlib_cosT8_safe(t_sample r) { return genlib_cosT8(phasewrap(r)); }
//inline double genlib_sin_fast_safe(double r) { return genlib_sin_fast(phasewrap(r)); }
//inline t_sample genlib_sinP7_safe(t_sample r) { return genlib_sinP7(phasewrap(r)); }
//inline t_sample genlib_sinP9_safe(t_sample r) { return genlib_sinP9(phasewrap(r)); }
//inline t_sample genlib_sinT7_safe(t_sample r) { return genlib_sinT7(phasewrap(r)); }



/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

inline float genlib_fastersin (float x) {
	static const float fouroverpi = 1.2732395447351627f;
	static const float fouroverpisq = 0.40528473456935109f;
	static const float q = 0.77633023248007499f;
	union { float f; uint32_t i; } p = { 0.22308510060189463f };
	union { float f; uint32_t i; } vx = { x };
	uint32_t sign = vx.i & 0x80000000;
	vx.i &= 0x7FFFFFFF;
	float qpprox = fouroverpi * x - fouroverpisq * x * vx.f;
	p.i |= sign;
	return qpprox * (q + p.f * qpprox);
}

inline float genlib_fastercos (float x) {
	static const float twooverpi = 0.63661977236758134f;
	static const float p = 0.54641335845679634f;
	union { float f; uint32_t i; } vx = { x };
	vx.i &= 0x7FFFFFFF;
	float qpprox = 1.0f - twooverpi * vx.f;
	return qpprox + p * qpprox * (1.0f - qpprox * qpprox);
}

inline float genlib_fastersinfull (float x) {
	static const float twopi = 6.2831853071795865f;
	static const float invtwopi = 0.15915494309189534f;
	int k = x * invtwopi;
	float half = (x < 0) ? -0.5f : 0.5f;
	return genlib_fastersin ((half + k) * twopi - x);
}

inline float genlib_fastercosfull (float x) {
	static const float halfpi = 1.5707963267948966f;
	return genlib_fastersinfull (x + halfpi);
}

inline float genlib_fastertanfull (float x) {
	static const float twopi = 6.2831853071795865f;
	static const float invtwopi = 0.15915494309189534f;
	int k = x * invtwopi;
	float half = (x < 0) ? -0.5f : 0.5f;
	float xnew = x - (half + k) * twopi;
	return genlib_fastersin (xnew) / genlib_fastercos (xnew);
}


#define cast_uint32_t static_cast<uint32_t>
inline float genlib_fasterpow2 (float p) {
	float clipp = (p < -126) ? -126.0f : p;
	union { uint32_t i; float f; } v = { cast_uint32_t ( (1 << 23) * (clipp + 126.94269504f) ) };
	return v.f;
}

inline float genlib_fasterexp (float p) {
	return genlib_fasterpow2 (1.442695040f * p);
}

inline float genlib_fasterlog2 (float x) {
	union { float f; uint32_t i; } vx = { x };
	float y = vx.i;
	y *= 1.1920928955078125e-7f;
	return y - 126.94269504f;
}

inline float genlib_fasterpow (float x, float p) {
	return genlib_fasterpow2(p * genlib_fasterlog2 (x));
}

////////////////////////////////////////////////////////////////

inline t_sample fastertanfull(t_sample x) {
	return (t_sample)genlib_fastertanfull((float)x);
}

inline t_sample fastersinfull(t_sample x) {
	return (t_sample)genlib_fastersinfull((float)x);
}

inline t_sample fastercosfull(t_sample x) {
	return (t_sample)genlib_fastercosfull((float)x);
}

inline t_sample fasterexp(t_sample x) {
	return (t_sample)genlib_fasterexp((float)x);
}

inline t_sample fasterpow(t_sample x, t_sample p) {
	return (t_sample)genlib_fasterpow((float)x, (float)p);
}
/****************************************************************/



inline t_sample minimum(t_sample x, t_sample y) { return (y<x?y:x); }
inline t_sample maximum(t_sample x, t_sample y) { return (x<y?y:x); }

inline t_sample clamp(t_sample x, t_sample minVal, t_sample maxVal) {
	return minimum(maximum(x,minVal),maxVal);
}

template<typename T>
inline T smoothstep(t_sample e0, t_sample e1, T x) {
	T t = clamp( safediv(x-T(e0),T(e1-e0)), 0., 1. );
	return t*t*(T(3) - T(2)*t);
}

inline t_sample mix(t_sample x, t_sample y, t_sample a) {
	return x+a*(y-x);
}

inline t_sample scale(t_sample in, t_sample inlow, t_sample inhigh, t_sample outlow, t_sample outhigh, t_sample power)
{
	t_sample value;
	t_sample inscale = safediv(1., inhigh - inlow);
	t_sample outdiff = outhigh - outlow;

	value = (in - inlow) * inscale;
	if (value > 0.0)
		value = powf(value, power);
	else if (value < 0.0)
		value = -powf(-value, power);
	value = (value * outdiff) + outlow;

	return value;
}

inline t_sample linear_interp(t_sample a, t_sample x, t_sample y) {
	return x+a*(y-x);
}

inline t_sample cosine_interp(t_sample a, t_sample x, t_sample y) {
	const t_sample a2 = (t_sample(1.)-genlib_cosT8_safe(a*t_sample(GENLIB_PI)))/t_sample(2.);
	return(x*(t_sample(1.)-a2)+y*a2);
}

inline t_sample cubic_interp(t_sample a, t_sample w, t_sample x, t_sample y, t_sample z) {
	const t_sample a2 = a*a;
	const t_sample f0 = z - y - w + x;
	const t_sample f1 = w - x - f0;
	const t_sample f2 = y - w;
	const t_sample f3 = x;
	return(f0*a*a2 + f1*a2 + f2*a + f3);
}

// Breeuwsma catmull-rom spline interpolation
inline t_sample spline_interp(t_sample a, t_sample w, t_sample x, t_sample y, t_sample z) {
	const t_sample a2 = a*a;
	const t_sample f0 = t_sample(-0.5)*w + t_sample(1.5)*x - t_sample(1.5)*y + t_sample(0.5)*z;
	const t_sample f1 = w - t_sample(2.5)*x + t_sample(2)*y - t_sample(0.5)*z;
	const t_sample f2 = t_sample(-0.5)*w + t_sample(0.5)*y;
	return(f0*a*a2 + f1*a2 + f2*a + x);
}

template<typename T1, typename T2>
inline T1 neqp(T1 x, T2 y) {
	return ((((x) != T1(y))) ? (x) : T1(0));
}

template<typename T1, typename T2>
inline T1 gtp(T1 x, T2 y) { return ((((x) > T1(y))) ? (x) : T1(0)); }
template<typename T1, typename T2>
inline T1 gtep(T1 x, T2 y) { return ((((x) >= T1(y))) ? (x) : T1(0)); }
template<typename T1, typename T2>
inline T1 ltp(T1 x, T2 y) { return ((((x) < T1(y))) ? (x) : T1(0)); }
template<typename T1, typename T2>
inline T1 ltep(T1 x, T2 y) { return ((((x) <= T1(y))) ? (x) : T1(0)); }

inline t_sample fract(t_sample x) { float unused; return (t_sample)modff((float)x, &unused); }

// log2(x) = log(x)/log(2)
template<typename T>
inline T log2(T x) {
	return logf(x)*GENLIB_1_OVER_LOG_2;
}

inline t_sample atodb(t_sample in) {
	return (in <=0.) ? -999. : (20. * log10f(in));
}

inline t_sample dbtoa(t_sample in) {
	return powf(10., in * 0.05);
}

inline t_sample ftom(t_sample in, t_sample tuning=440.) {
	return 69. + 17.31234050465299 * logf(safediv(in, tuning));
}

inline t_sample mtof(t_sample in, t_sample tuning=440.) {
	return tuning * expf(.057762265 * (in - 69.0));
}

inline t_sample mstosamps(t_sample ms, t_sample samplerate=44100.) {
	return samplerate * ms * t_sample(0.001);
}

inline t_sample sampstoms(t_sample s, t_sample samplerate=44100.) {
	return t_sample(1000.) * s / samplerate;
}

inline t_sample triangle(t_sample phase, t_sample p1) {
	phase = wrap(phase, 0., 1.);
	p1 = clamp(p1, 0., 1.);
	if (phase < p1)
		return (p1) ? phase/p1 : 0.;
	else
		return (p1==1.) ? phase : 1. - ((phase - p1) / (1. - p1));
}

struct Delta {
	t_sample history;
	Delta() { reset(); }
	inline void reset(t_sample init=0) { history=init; }

	inline t_sample operator()(t_sample in1) {
		t_sample ret = in1 - history;
		history = in1;
		return ret;
	}
};
struct Change {
	t_sample history;
	Change() { reset(); }
	inline void reset(t_sample init=0) { history=init; }

	inline t_sample operator()(t_sample in1) {
		t_sample ret = in1 - history;
		history = in1;
		return sign(ret);
	}
};

struct Rate {
	t_sample phase, diff, mult, invmult, prev;
	int wantlock, quant;

	Rate() { reset(); }

	inline void reset() {
		phase = diff = prev = 0;
		mult = invmult = 1;
		wantlock = 1;
		quant = 1;
	}

	inline t_sample perform_lock(t_sample in1, t_sample in2) {
		// did multiplier change?
		if (in2 != mult && !genlib_isnan(in2)) {
			mult = in2;
			invmult = safediv(1., mult);
			wantlock = 1;
		}
		t_sample diff = in1 - prev;

		if (diff < t_sample(-0.5)) {
			diff += t_sample(1);
		} else if (diff > t_sample(0.5)) {
			diff -= t_sample(1);
		}

		if (wantlock) {
			// recalculate phase
			phase = (in1 - GENLIB_QUANT(in1, quant)) * invmult
						+ GENLIB_QUANT(in1, quant * mult);
			diff = 0;
			wantlock = 0;
		} else {
			// diff is always between -0.5 and 0.5
			phase += diff * invmult;
		}

		if (phase > t_sample(1.) || phase < t_sample(-0.)) {
			phase = phase - (long)(phase);
		}

		prev = in1;

		return phase;
	}

	inline t_sample perform_cycle(t_sample in1, t_sample in2) {
		// did multiplier change?
		if (in2 != mult && !genlib_isnan(in2)) {
			mult = in2;
			invmult = safediv(1., mult);
			wantlock = 1;
		}
		t_sample diff = in1 - prev;

		if (diff < t_sample(-0.5)) {
			if (wantlock) {
				wantlock = 0;
				phase = in1 * invmult;
				diff = t_sample(0);
			} else {
				diff += t_sample(1);
			}
		} else if (diff > t_sample(0.5)) {
			if (wantlock) {
				wantlock = 0;
				phase = in1 * invmult;
				diff = t_sample(0);
			} else {
				diff -= t_sample(1);
			}
		}

		// diff is always between -0.5 and 0.5
		phase += diff * invmult;

		if (phase > t_sample(1.) || phase < t_sample(-0.)) {
			phase = phase - (long)(phase);
		}

		prev = in1;

		return phase;
	}

	inline t_sample perform_off(t_sample in1, t_sample in2) {
		// did multiplier change?
		if (in2 != mult && !genlib_isnan(in2)) {
			mult = in2;
			invmult = safediv(1., mult);
			wantlock = 1;
		}
		t_sample diff = in1 - prev;

		if (diff < t_sample(-0.5)) {
			diff += t_sample(1);
		} else if (diff > t_sample(0.5)) {
			diff -= t_sample(1);
		}

		phase += diff * invmult;

		if (phase > t_sample(1.) || phase < t_sample(-0.)) {
			phase = phase - (long)(phase);
		}

		prev = in1;

		return phase;
	}
};

struct DCBlock {
	t_sample x1, y1;
	DCBlock() { reset(); }
	inline void reset() { x1=0; y1=0; }

	inline t_sample operator()(t_sample in1) {
		t_sample y = in1 - x1 + y1*t_sample(0.9997);
		x1 = in1;
		y1 = y;
		return y;
	}
};

struct Noise {
	unsigned long last;
	static long uniqueTickCount(void) {
		static long lasttime = 0;
		long time = genlib_ticks();
		return (time <= lasttime) ? (++lasttime) : (lasttime = time);
	}

	Noise() { reset(); }
	Noise(t_sample seed) { reset(seed); }
	void reset() { last = uniqueTickCount() * uniqueTickCount(); }
	void reset(t_sample seed) { last = seed; }

	inline t_sample operator()() {
		last = 1664525L * last + 1013904223L;
		union {
			uint32_t ui32;
			float f;
		} u = { 0x3f800000 | (0x007fffff & last) }; // type-punning

		return (u.f * 2.f) - 3.f;
	}
};

struct Phasor {
	t_sample phase;
	Phasor() { reset(); }
	void reset(t_sample v=0.) { phase=v; }
	inline t_sample operator()(t_sample freq, t_sample invsamplerate) {
		const t_sample pincr = freq * invsamplerate;
		//phase = genlib_wrapfew(phase + pincr, 0., 1.); // faster for low frequencies, but explodes with high frequencies
		phase = wrap(phase + pincr, 0., 1.);
		return phase;
	}
};

struct PlusEquals {
	t_sample count;
	PlusEquals() { reset(); }
	void reset(t_sample v=0.) { count=v; }

	// reset post-application mode:
	inline t_sample post(t_sample incr, t_sample reset, t_sample min, t_sample max) {
		count = reset ? min : wrap(count+incr, min, max);
		return count;
	}
	inline t_sample post(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
		count = reset ? min : count+incr;
		return count;
	}

	// reset pre-application mode:
	inline t_sample pre(t_sample incr, t_sample reset, t_sample min, t_sample max) {
		count = reset ? min+incr : wrap(count+incr, min, max);
		return count;
	}
	inline t_sample pre(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
		count = reset ? min+incr : count+incr;
		return count;
	}
};

struct MulEquals {
	t_sample count;
	MulEquals() { reset(); }
	void reset(t_sample v=0.) { count=v; }

	// reset post-application mode:
	inline t_sample post(t_sample incr, t_sample reset, t_sample min, t_sample max) {
		count = reset ? min : wrap(fixdenorm(count*incr), min, max);
		return count;
	}
	inline t_sample post(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
		count = reset ? min : fixdenorm(count*incr);
		return count;
	}

	// reset pre-application mode:
	inline t_sample pre(t_sample incr, t_sample reset, t_sample min, t_sample max) {
		count = reset ? min*incr : wrap(fixdenorm(count*incr), min, max);
		return count;
	}
	inline t_sample pre(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
		count = reset ? min*incr : fixdenorm(count*incr);
		return count;
	}
};

struct Sah {
	t_sample prev, output;
	Sah() { reset(); }
	void reset(t_sample o=0.) {
		output = prev = o;
	}

	inline t_sample operator()(t_sample in, t_sample trig, t_sample thresh) {
		if (prev <= thresh && trig > thresh) {
			output = in;
		}
		prev = trig;
		return output;
	}
};

struct Train {
	t_sample phase, state;
	Train() { reset(); }
	void reset(t_sample p=0) { phase = p; state = 0.; }

	inline t_sample operator()(t_sample pulseinterval, t_sample width, t_sample pulsephase) {
		if (width <= t_sample(0.)) {
			state = t_sample(0.);	// no pulse!
		} else if (width >= 1.) {
			state = t_sample(1.); // constant pulse!
		} else {
			const t_sample interval = maximum(pulseinterval, t_sample(1.));	// >= 1.
			const t_sample p1 = clamp(pulsephase, t_sample(0.), t_sample(1.));	// [0..1]
			const t_sample p2 = p1+width;						// (p1..p1+1)
			const t_sample pincr = t_sample(1.)/interval;			// (0..1]
			phase += pincr;								// +ve
			if (state) {	// on:
				if (phase > p2) {
					state = t_sample(0.);				// turn off
					phase -= (int)(1.+phase-p2);		// wrap phase back down
				}
			} else {		// off:
				if (phase > p1) {
					state = t_sample(1.);				// turn on.
				}
			}
		}
		return state;
	}
};

struct Delay {
	t_sample * memory;
	long size, wrap, maxdelay;
	long reader, writer;

	t_genlib_data * dataRef;

	Delay() : memory(0) {
		size = wrap = maxdelay = 0;
		reader = writer = 0;
		dataRef = 0;
	}
	~Delay() {
		if (dataRef != 0) {
			// store write position for persistence:
			genlib_data_setcursor(dataRef, writer);
			// decrement reference count:
			genlib_data_release(dataRef);
		}
	}

	inline void reset(const char * name, long d) {
		// if needed, acquire the Data's global reference:
		if (dataRef == 0) {

			void * ref = genlib_obtain_reference_from_string(name);
			dataRef = genlib_obtain_data_from_reference(ref);
			if (dataRef == 0) {
				genlib_report_error("failed to acquire data");
				return;
			}

			// scale maxdelay to next highest power of 2:
			maxdelay = d;
			size = maxdelay < 2 ? 2 : maxdelay;
			size = next_power_of_two(size);

			// first reset should resize the memory:
			genlib_data_resize(dataRef, size, 1);

			t_genlib_data_info info;
			if (genlib_data_getinfo(dataRef, &info) == GENLIB_ERR_NONE) {
				if (info.dim != size) {
					// at this point, could resolve by reducing to
					// maxdelay = size = next_power_of_two(info.dim+1)/2;
					// but really, if this happens, it means more than one
					// object is referring to the same t_gen_dsp_data.
					// which is probably bad news.
					genlib_report_error("delay memory size error");
					memory = 0;
					return;
				}
				memory = info.data;
				writer = genlib_data_getcursor(dataRef);
			} else {
				genlib_report_error("failed to acquire data info");
			}

		} else {
			// subsequent reset should zero the memory & heads:
			set_zero64(memory, size);
			writer = 0;
		}

		reader = writer;
		wrap = size-1;
	}

	// called at bufferloop end, updates read pointer time
	inline void step() {
		reader++;
		if (reader >= size) reader = 0;
	}

	inline void write(t_sample x) {
		writer = reader;	// update write ptr
		memory[writer] = x;
	}

	inline t_sample read_step(t_sample d) {
		// extra half for nice rounding:
		// min 1 sample delay for read before write (r != w)
		const t_sample r = t_sample(size + reader) - clamp(d-t_sample(0.5), (reader != writer), maxdelay);
		long r1 = long(r);
		return memory[r1 & wrap];
	}

	inline t_sample read_linear(t_sample d) {
		// min 1 sample delay for read before write (r != w)
		t_sample c = clamp(d, (reader != writer), maxdelay);
		const t_sample r = t_sample(size + reader) - c;
		long r1 = long(r);
		long r2 = r1+1;
		t_sample a = r - (t_sample)r1;
		t_sample x = memory[r1 & wrap];
		t_sample y = memory[r2 & wrap];
		return linear_interp(a, x, y);
	}

	inline t_sample read_cosine(t_sample d) {
		// min 1 sample delay for read before write (r != w)
		const t_sample r = t_sample(size + reader) - clamp(d, (reader != writer), maxdelay);
		long r1 = long(r);
		long r2 = r1+1;
		t_sample a = r - (t_sample)r1;
		t_sample x = memory[r1 & wrap];
		t_sample y = memory[r2 & wrap];
		return cosine_interp(a, x, y);
	}

	// cubic requires extra sample of compensation:
	inline t_sample read_cubic(t_sample d) {
		// min 1 sample delay for read before write (r != w)
		// plus extra 1 sample compensation for 4-point interpolation
		const t_sample r = t_sample(size + reader) - clamp(d, t_sample(1.)+t_sample(reader != writer), maxdelay);
		long r1 = long(r);
		long r2 = r1+1;
		long r3 = r1+2;
		long r4 = r1+3;
		t_sample a = r - (t_sample)r1;
		t_sample w = memory[r1 & wrap];
		t_sample x = memory[r2 & wrap];
		t_sample y = memory[r3 & wrap];
		t_sample z = memory[r4 & wrap];
		return cubic_interp(a, w, x, y, z);
	}

	// spline requires extra sample of compensation:
	inline t_sample read_spline(t_sample d) {
		// min 1 sample delay for read before write (r != w)
		// plus extra 1 sample compensation for 4-point interpolation
		const t_sample r = t_sample(size + reader) - clamp(d, t_sample(1.)+t_sample(reader != writer), maxdelay);
		long r1 = long(r);
		long r2 = r1+1;
		long r3 = r1+2;
		long r4 = r1+3;
		t_sample a = r - (t_sample)r1;
		t_sample w = memory[r1 & wrap];
		t_sample x = memory[r2 & wrap];
		t_sample y = memory[r3 & wrap];
		t_sample z = memory[r4 & wrap];
		return spline_interp(a, w, x, y, z);
	}
};

template<typename T=t_sample>
struct DataInterface {
	long dim, channels;
	T * mData;
	void * mDataReference;		// this was t_symbol *mName
	int modified;

	DataInterface() : dim(0), channels(1), mData(0), modified(0) { mDataReference = 0; }

	// raw reading/writing/overdubbing (internal use only, no bounds checking)
	inline t_sample read(long index, long channel=0) const {
		return mData[channel+index*channels];
	}
	inline void write(T value, long index, long channel=0) {
		mData[channel+index*channels] = value;
		modified = 1;
	}
	// NO LONGER USED:
	inline void overdub(T value, long index, long channel=0) {
		mData[channel+index*channels] += value;
		modified = 1;
	}

	// averaging overdub (used by splat)
	inline void blend(T value, long index, long channel, t_sample alpha) {
		long offset = channel+index*channels;
		const T old = mData[offset];
		mData[offset] = old + alpha * (value - old);
		modified = 1;
	}

	// NO LONGER USED:
	inline void read_ok(long index, long channel=0, bool ok=1) const {
		return ok ? mData[channel+index*channels] : T(0);
	}
	inline void write_ok(T value, long index, long channel=0, bool ok=1) {
		if (ok) mData[channel+index*channels] = value;
	}
	inline void overdub_ok(T value, long index, long channel=0, bool ok=1) {
		if (ok) mData[channel+index*channels] += value;
	}

	// Bounds strategies:
	inline long index_clamp(long index) const { return clamp(index, 0, dim-1); }
	inline long index_wrap(long index) const { return wrap(index, 0, dim); }
	inline long index_fold(long index) const { return fold(index, 0, dim); }
	inline bool index_oob(long index) const { return (index < 0 || index >= dim); }
	inline bool index_inbounds(long index) const { return (index >=0 && index < dim); }

	// channel bounds:
	inline long channel_clamp(long c) const { return clamp(c, 0, channels-1); }
	inline long channel_wrap(long c) const { return wrap(c, 0, channels); }
	inline long channel_fold(long c) const { return fold(c, 0, channels); }
	inline bool channel_oob(long c) const { return (c < 0 || c >= channels); }
	inline bool channel_inbounds(long c) const { return !channel_oob(c); }

	// Indexing strategies:
	// [0..1] -> [0..(dim-1)]
	inline t_sample phase2index(t_sample phase) const { return phase * t_sample(dim-1); }
	// [0..1] -> [min..max]
	inline t_sample subphase2index(t_sample phase, long min, long max) const {
		min = index_clamp(min);
		max = index_clamp(max);
		return t_sample(min) + phase * t_sample(max-min);
	}
	// [-1..1] -> [0..(dim-1)]
	inline t_sample signal2index(t_sample signal) const { return phase2index((signal+t_sample(1.)) * t_sample(0.5)); }

	inline T peek(t_sample index, long channel=0) const {
		const long i = (long)index;
		if (index_oob(i) || channel_oob(channel)) {
			return 0.;
		} else {
			return read(i, channel);
		}
	}

	inline T index(t_sample index, long channel=0) const {
		channel = channel_clamp(channel);
		// no-interp:
		long i = (long)index;
		// bound:
		i = index_clamp(i);
		return read(i, channel);
	}

	inline T cell(t_sample index, long channel=0) const {
		channel = channel_clamp(channel);
		// no-interp:
		long i = (long)index;
		// bound:
		i = index_wrap(i);
		return read(i, channel);
	}

	inline T cycle(t_sample phase, long channel=0) const {
		channel = channel_clamp(channel);
		t_sample index = phase2index(phase);
		// interp:
		long i1 = (long)index;
		long i2 = i1+1;
		const t_sample alpha = index - (t_sample)i1;
		// bound:
		i1 = index_wrap(i1);
		i2 = index_wrap(i2);
		// interp:
		T v1 = read(i1, channel);
		T v2 = read(i2, channel);
		return mix(v1, v2, alpha);
	}

	inline T lookup(t_sample signal, long channel=0) const {
		channel = channel_clamp(channel);
		t_sample index = signal2index(signal);
		// interp:
		long i1 = (long)index;
		long i2 = i1+1;
		t_sample alpha = index - (t_sample)i1;
		// bound:
		i1 = index_clamp(i1);
		i2 = index_clamp(i2);
		// interp:
		T v1 = read(i1, channel);
		T v2 = read(i2, channel);
		return mix(v1, v2, alpha);
	}
	// NO LONGER USED:
	inline void poke(t_sample value, t_sample index, long channel=0) {
		const long i = (long)index;
		if (!(index_oob(i) || channel_oob(channel))) {
			write(fixdenorm(value), i, channel);
		}
	}
	// NO LONGER USED:
	inline void splat_adding(t_sample value, t_sample phase, long channel=0) {
		const t_sample valuef = fixdenorm(value);
		channel = channel_clamp(channel);
		t_sample index = phase2index(phase);
		// interp:
		long i1 = (long)index;
		long i2 = i1+1;
		const t_sample alpha = index - (t_sample)i1;
		// bound:
		i1 = index_wrap(i1);
		i2 = index_wrap(i2);
		// interp:
		overdub(valuef*(1.-alpha), i1, channel);
		overdub(valuef*alpha,      i2, channel);
	}
	// NO LONGER USED:
	inline void splat(t_sample value, t_sample phase, long channel=0) {
		const t_sample valuef = fixdenorm(value);
		channel = channel_clamp(channel);
		t_sample index = phase2index(phase);
		// interp:
		long i1 = (long)index;
		long i2 = i1+1;
		const t_sample alpha = index - (t_sample)i1;
		// bound:
		i1 = index_wrap(i1);
		i2 = index_wrap(i2);
		// interp:
		const T v1 = read(i1, channel);
		const T v2 = read(i2, channel);
		write(v1 + (1.-alpha)*(valuef-v1), i1, channel);
		write(v2 + (alpha)*(valuef-v2), i2, channel);
	}
};

// DATA_MAXIMUM_ELEMENTS * 8 bytes = 256 mb limit
#define DATA_MAXIMUM_ELEMENTS	(33554432)

struct Data : public DataInterface<t_sample> {
	t_genlib_data * dataRef;	// a pointer to some external source of the data

	Data() : DataInterface<t_sample>() {
		dataRef = 0;
	}
	~Data() {
		//genlib_report_message("releasing data handle %d", dataRef);
		if (dataRef != 0) {
			genlib_data_release(dataRef);
		}
	}
	void reset(const char * name, long s, long c) {
		// if needed, acquire the Data's global reference:
		if (dataRef == 0) {
			void * ref = genlib_obtain_reference_from_string(name);
			dataRef = genlib_obtain_data_from_reference(ref);
			if (dataRef == 0) {
				genlib_report_error("failed to acquire data");
				return;
			}
		}
		genlib_data_resize(dataRef, s, c);
		getinfo();
	}
	bool setbuffer(void * bufferRef) {
		//genlib_report_message("set buffer %p", bufferRef);
		if (dataRef == 0) {
			// error: no data, or obtain?
			return false;
		}
		genlib_data_setbuffer(dataRef, bufferRef);
		getinfo();
		return true;
	}

	void getinfo() {
		t_genlib_data_info info;
		if (genlib_data_getinfo(dataRef, &info) == GENLIB_ERR_NONE) {
			mData = info.data;
			dim = info.dim;
			channels = info.channels;
		} else {
			genlib_report_error("failed to acquire data info");
		}
	}
};

// Used by SineData
struct DataLocal : public DataInterface<t_sample> {
	DataLocal() : DataInterface<t_sample>() {}
	~DataLocal() {
		if (mData) sysmem_freeptr(mData);
		mData = 0;
	}

	void reset(long s, long c) {
		mData=0;
		resize(s, c);
	}

	void resize(long s, long c) {
		if (s * c > DATA_MAXIMUM_ELEMENTS) {
			s = DATA_MAXIMUM_ELEMENTS/c;
			genlib_report_message("warning: resizing data to < 256MB");
		}
		if (mData) {
			sysmem_resizeptr(mData, sizeof(t_sample) * s * c);
		} else {
			mData = (t_sample *)sysmem_newptr(sizeof(t_sample) * s * c);
		}
		if (!mData) {
			genlib_report_error("out of memory");
			resize(512, 1);
			return;
		} else {
			dim = s;
			channels = c;
		}
		set_zero64(mData, dim * channels);
	}

	// copy from a buffer~
	// resizing is safe only during initialization!
	bool setbuffer(void *dataReference) {
		mDataReference = dataReference; // replaced mName
		bool result = false;
		t_genlib_buffer * b;
		t_genlib_buffer_info info;
		if (mDataReference != 0) {
			b = (t_genlib_buffer *)genlib_obtain_buffer_from_reference(mDataReference);
			if (b) {
				if (genlib_buffer_edit_begin(b)==GENLIB_ERR_NONE) {
					if (genlib_buffer_getinfo(b, &info)==GENLIB_ERR_NONE) {
						float * samples = info.b_samples;
						long frames = info.b_frames;
						long nchans = info.b_nchans;
						//long size = info.b_size;
						//long modtime = info.b_modtime;	// cache & compare?

						// resizing is safe only during initialization!
						if (mData == 0) resize(frames, nchans);

						long frames_safe = frames < dim ? frames : dim;
						long channels_safe = nchans < channels ? nchans : channels;
						// copy:
						for (int f=0; f<frames_safe; f++) {
							for (int c=0; c<channels_safe; c++) {
								t_sample value = samples[c+f*nchans];
								write(value, f, c);
							}
						}
						result = true;
					} else {
						genlib_report_message("couldn't get info for buffer\n");
					}
					genlib_buffer_edit_end(b, 1);
				} else {
					genlib_report_message("buffer locked\n");
				}
			}
		} else {
			genlib_report_message("buffer reference not valid");
		}
		return result;
	}
};

struct Buffer : public DataInterface<float> {
	t_genlib_buffer * mBuf;
	t_genlib_buffer_info mInfo;
	float mDummy;		// safe access in case buffer is not valid

	Buffer() : DataInterface<float>() {}

	void reset(const char * name) {
		dim = 1;
		channels = 1;
		mData = &mDummy;
		mDummy = 0.f;
		mBuf = 0;

		// call into genlib:
		mDataReference = genlib_obtain_reference_from_string(name);
	}

	void setbuffer(void * ref) {
		mDataReference = ref;
	}

	void begin() {
		t_genlib_buffer * b = genlib_obtain_buffer_from_reference(mDataReference);
		mBuf = 0;
		if (b) {
			if (genlib_buffer_perform_begin(b) == GENLIB_ERR_NONE) {
				mBuf = b;
			} else {
				//genlib_report_message ("not a buffer~ %s", mName->s_name);
			}
		} else {
			//genlib_report_message("no object %s\n", mName->s_name);
		}

		if (mBuf && genlib_buffer_getinfo(mBuf, &mInfo)==GENLIB_ERR_NONE) {
			// grab data:
			mBuf = b;
			mData = mInfo.b_samples;
			dim = mInfo.b_frames;
			channels = mInfo.b_nchans;
		} else {
			//genlib_report_message("couldn't get info");
			mBuf = 0;
			mData = &mDummy;
			dim = 1;
			channels = 1;
		}
	}

	void end() {
		if (mBuf) {
			genlib_buffer_perform_end(mBuf);
			if (modified) {
				genlib_buffer_dirty(mBuf);
			}
			modified = 0;
		}
		mBuf = 0;
	}
};

struct SineData : public DataLocal {
	SineData() : DataLocal() {
		const int costable_size = 1 << 14;	// 14 bit index (noise floor at around -156 dB)
		mData=0;
		resize(costable_size, 1);
		for (int i=0; i<dim; i++) {
			mData[i] = cos(i * GENLIB_PI * 2. / (t_sample)(dim));
		}
	}

	~SineData() {
		if (mData) sysmem_freeptr(mData);
		mData = 0;
	}
};

template<typename T>
inline int dim(const T& data) { return data.dim; }

template<typename T>
inline int channels(const T& data) { return data.channels; }

// used by cycle when no buffer/data is specified:
struct SineCycle {

	uint32_t phasei, pincr;
	t_sample f2i;

	void reset(t_sample samplerate, t_sample init = 0) {
		phasei = init * t_sample(4294967296.0);
		pincr = 0;
		f2i = t_sample(4294967296.0) / samplerate;
	}

	inline void freq(t_sample f) {
		pincr = f * f2i;
	}

	inline void phase(t_sample f) {
		phasei = f * t_sample(4294967296.0);
	}

	inline t_sample phase() const {
		return phasei * t_sample(0.232830643653869629e-9);
	}

	template<typename T>
	inline t_sample operator()(const DataInterface<T>& buf) {
		T * data = buf.mData;
		// divide uint32_t range down to buffer size (32-bit to 14-bit)
		uint32_t idx = phasei >> 18;
		// compute fractional portion and divide by 18-bit range
		const t_sample frac = t_sample(phasei & 262143) * t_sample(3.81471181759574e-6);
		// index safely in 14-bit range:
		const t_sample y0 = data[idx];
		const t_sample y1 = data[(idx+1) & 16383];
		const t_sample y = linear_interp(frac, y0, y1);
		phasei += pincr;
		return y;
	}
};

#endif