2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 Primitives for some bit operations.
26 #include "PyrPrimitive.h"
27 #include "VMGlobals.h"
31 int prNumBits(VMGlobals
*g
, int numArgsPushed
);
32 int prNumBits(VMGlobals
*g
, int numArgsPushed
)
35 SetRaw(a
, NUMBITS(slotRawInt(a
)));
39 int prLog2Ceil(VMGlobals
*g
, int numArgsPushed
);
40 int prLog2Ceil(VMGlobals
*g
, int numArgsPushed
)
43 SetRaw(a
, LOG2CEIL(slotRawInt(a
)));
47 int prCLZ(VMGlobals
*g
, int numArgsPushed
);
48 int prCLZ(VMGlobals
*g
, int numArgsPushed
)
51 SetRaw(a
, CLZ(slotRawInt(a
)));
55 int prCTZ(VMGlobals
*g
, int numArgsPushed
);
56 int prCTZ(VMGlobals
*g
, int numArgsPushed
)
59 SetRaw(a
, CTZ(slotRawInt(a
)));
63 int prNextPowerOfTwo(VMGlobals
*g
, int numArgsPushed
);
64 int prNextPowerOfTwo(VMGlobals
*g
, int numArgsPushed
)
70 SetRaw(a
, NEXTPOWEROFTWO(slotRawInt(a
)));
74 int prIsPowerOfTwo(VMGlobals
*g
, int numArgsPushed
);
75 int prIsPowerOfTwo(VMGlobals
*g
, int numArgsPushed
)
78 SetBool(a
, ISPOWEROFTWO(slotRawInt(a
)));
82 int prBinaryGrayCode(VMGlobals
*g
, int numArgsPushed
);
83 int prBinaryGrayCode(VMGlobals
*g
, int numArgsPushed
)
88 SetRaw(a
, GRAYCODE(slotRawInt(a
)));
92 int prSetBit(VMGlobals
*g
, int numArgsPushed
);
93 int prSetBit(VMGlobals
*g
, int numArgsPushed
)
95 PyrSlot
*a
= g
->sp
- 2;
96 PyrSlot
*b
= g
->sp
- 1;
100 int err
= slotIntVal(b
, &bit
);
105 SetRaw(a
, slotRawInt(a
) & ~mask
);
107 SetRaw(a
, slotRawInt(a
) | mask
);
112 int prHammingDistance(VMGlobals
*g
, int numArgsPushed
);
113 int prHammingDistance(VMGlobals
*g
, int numArgsPushed
)
115 PyrSlot
*a
= g
->sp
- 1;
118 if (NotInt(a
) || NotInt(b
))
121 int aInt
= slotRawInt(a
);
122 int bInt
= slotRawInt(b
);
124 int count
= 0, mask
= 1;
125 for(int i
= 0; i
< 32; i
++) {
126 if((aInt
& mask
) != (bInt
& mask
)) count
= count
+ 1;
134 void initBitPrimitives();
135 void initBitPrimitives()
139 base
= nextPrimitiveIndex();
141 definePrimitive(base
, index
++, "_NextPowerOfTwo", prNextPowerOfTwo
, 1, 0);
142 definePrimitive(base
, index
++, "_IsPowerOfTwo", prIsPowerOfTwo
, 1, 0);
143 definePrimitive(base
, index
++, "_CLZ", prCLZ
, 1, 0);
144 definePrimitive(base
, index
++, "_CTZ", prCTZ
, 1, 0);
145 definePrimitive(base
, index
++, "_NumBits", prNumBits
, 1, 0);
146 definePrimitive(base
, index
++, "_Log2Ceil", prLog2Ceil
, 1, 0);
147 definePrimitive(base
, index
++, "_SetBit", prSetBit
, 3, 0);
148 definePrimitive(base
, index
++, "_BinaryGrayCode", prBinaryGrayCode
, 1, 0);
149 definePrimitive(base
, index
++, "_HammingDistance", prHammingDistance
, 2, 0);
156 #include "SCPlugin.h"
158 // export the function that SC will call to load the plug in.
160 extern "C" { SCPlugIn
* loadPlugIn(void); }
164 // define plug in object
165 class APlugIn
: public SCPlugIn
171 virtual void AboutToCompile();
176 // constructor for plug in
181 // destructor for plug in
184 void APlugIn::AboutToCompile()
186 // this is called each time the class library is compiled.
190 // This function is called when the plug in is loaded into SC.
191 // It returns an instance of APlugIn.
192 SCPlugIn
* loadPlugIn()
194 return new APlugIn();