aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/plugin_interface/Unroll.h
blob: 12ae864a5ecb1ab258255f7d9e6a54166c5641cc (plain)
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
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

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

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

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

/*

These macros allow one to write code which can be compiled optimally depending on
what loop constructs the compiler can best generate code.

*/

#ifndef _Unroll_
#define _Unroll_

#if 1

// loop type
#define FOR_IS_FASTER 1
#define WHILE_IS_FASTER 0
// indexing type
#define PREINCREMENT_IS_FASTER 1
#define POSTINCREMENT_IS_FASTER 0

#else

// loop type
#define FOR_IS_FASTER 1
#define WHILE_IS_FASTER 0
// indexing type
#define PREINCREMENT_IS_FASTER 0
#define POSTINCREMENT_IS_FASTER 1

#endif


// LOOPING MACROS :

#if FOR_IS_FASTER

#define LOOP(length, stmt) for (int xxi=0; xxi<(length); ++xxi) { stmt; }
	
#elif WHILE_IS_FASTER

#define LOOP(length, stmt)			\
	{	int xxn = (length);			\
		while (--xxn) {				\
			stmt;					\
		}							\
	}
	
#endif



// above macros are not friendly to the debugger
#if FOR_IS_FASTER

#define LooP(length) for (int xxi=0; xxi<(length); ++xxi)
	
#elif WHILE_IS_FASTER

#define LooP(length) for (int xxi=(length); --xxi;)
	
#endif


// LOOP INDEXING :

/*
meanings of the indexing macros:
	ZXP = dereference and pre or post increment
	ZX = dereference
	PZ = preincrement (if applicable)
	ZP = postincrement (if applicable)
	ZOFF = offset from the pointer of the first element of the array
		(preincrement requires a ZOFF of 1 which is pre-subtracted from the
		base pointer. For other indexing types ZOFF is zero)
*/

#if PREINCREMENT_IS_FASTER
#define ZXP(z) (*++(z))
#define ZX(z) (*(z))
#define PZ(z) (++(z))
#define ZP(z) (z)
#define ZOFF (1)
#elif POSTINCREMENT_IS_FASTER
#define ZXP(z) (*(z)++)
#define ZX(z) (*(z))
#define PZ(z) (z)
#define ZP(z) ((z)++)
#define ZOFF (0)
#endif

// ACCESSING INLETS AND OUTLETS :

// unit inputs
#define ZIN(i) (IN(i) - ZOFF)	// get buffer pointer offset for iteration
#define ZIN0(i) (IN(i)[0])		// get first sample

// unit outputs
#define ZOUT(i) (OUT(i) - ZOFF)		// get buffer pointer offset for iteration
#define ZOUT0(i) (OUT(i)[0])		// get first sample

#include "SC_BoundsMacros.h"

#ifndef NDEBUG
# define NDEBUG
#endif
#include <assert.h>

inline void Clear(int numSamples, float *out)
{
	//assert((((long)(out+ZOFF) & 7) == 0)); // pointer must be 8 byte aligned
	
	if ((numSamples & 1) == 0) {
		// copying doubles is faster on powerpc.
		double *outd = (double*)out - ZOFF;
		LOOP(numSamples >> 1, ZXP(outd) = 0.; );
	} else {
		out -= ZOFF;
		LOOP(numSamples, ZXP(out) = 0.f; );
	}
}

inline void Copy(int numSamples, float *out, float *in)
{
	// pointers must be 8 byte aligned
	//assert((((long)(out+ZOFF) & 7) == 0) && (((long)(in+ZOFF) & 7) == 0)); 
	if (in == out) return;
	if ((numSamples & 1) == 0) {
		// copying doubles is faster on powerpc.
		double *outd = (double*)out - ZOFF;
		double *ind = (double*)in - ZOFF;
		LOOP(numSamples >> 1, ZXP(outd) = ZXP(ind); );
	} else {
		in -= ZOFF;
		out -= ZOFF;
		LOOP(numSamples, ZXP(out) = ZXP(in); );
	}
}

inline void Fill(int numSamples, float *out, float level)
{
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) = level; );
}

inline void Fill(int numSamples, float *out, float level, float slope)
{
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) = level; level += slope; );
}

inline void Accum(int numSamples, float *out, float *in)
{
	in  -= ZOFF;
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) += ZXP(in); );
}

inline void Scale(int numSamples, float *out, float level)
{
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) *= level;);
}

inline float Scale(int numSamples, float *out, float level, float slope)
{
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) *= level; level += slope;);
	return level;
}

inline float Scale(int numSamples, float *out, float *in, float level, float slope)
{
	in  -= ZOFF;
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) = ZXP(in) * level; level += slope;);
	return level;
}

inline float ScaleMix(int numSamples, float *out, float *in, float level, float slope)
{
	in  -= ZOFF;
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) += ZXP(in) * level; level += slope;);
	return level;
}

inline void Scale(int numSamples, float *out, float *in, float level)
{
	in  -= ZOFF;
	out -= ZOFF;
	LOOP(numSamples, ZXP(out) = ZXP(in) * level; );
}

// in these the pointers are assumed to already have been pre-offset.
inline void ZCopy(int numSamples, float *out, float *in)
{
	// pointers must be 8 byte aligned
	//assert((((long)(out+ZOFF) & 7) == 0) && (((long)(in+ZOFF) & 7) == 0)); 
	if (in == out) return;
	if ((numSamples & 1) == 0) {
		// copying doubles is faster on powerpc.
		double *outd = (double*)(out + ZOFF) - ZOFF;
		double *ind = (double*)(in + ZOFF) - ZOFF;
		LOOP(numSamples >> 1, ZXP(outd) = ZXP(ind); );
	} else {
		LOOP(numSamples, ZXP(out) = ZXP(in); );
	}
}

inline void ZClear(int numSamples, float *out)
{
	// pointers must be 8 byte aligned
	//assert((((long)(out+ZOFF) & 7) == 0) && (((long)(in+ZOFF) & 7) == 0)); 
	if ((numSamples & 1) == 0) {
		// copying doubles is faster on powerpc.
		double *outd = (double*)(out + ZOFF) - ZOFF;
		LOOP(numSamples >> 1, ZXP(outd) = 0.; );
	} else {
		LOOP(numSamples, ZXP(out) = 0.f; );
	}
}

inline void ZAccum(int numSamples, float *out, float *in)
{
	LOOP(numSamples, ZXP(out) += ZXP(in); );
}



#endif