supernova: fix for small audio vector sizes
[supercollider.git] / lang / LangPrimSource / PyrBitPrim.cpp
blobb6d2d9b113f00132763a05e61eabac1ba92a8b4b
1 /*
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"
28 #include "clz.h"
31 int prNumBits(VMGlobals *g, int numArgsPushed);
32 int prNumBits(VMGlobals *g, int numArgsPushed)
34 PyrSlot *a = g->sp;
35 SetRaw(a, NUMBITS(slotRawInt(a)));
36 return errNone;
39 int prLog2Ceil(VMGlobals *g, int numArgsPushed);
40 int prLog2Ceil(VMGlobals *g, int numArgsPushed)
42 PyrSlot *a = g->sp;
43 SetRaw(a, LOG2CEIL(slotRawInt(a)));
44 return errNone;
47 int prCLZ(VMGlobals *g, int numArgsPushed);
48 int prCLZ(VMGlobals *g, int numArgsPushed)
50 PyrSlot *a = g->sp;
51 SetRaw(a, CLZ(slotRawInt(a)));
52 return errNone;
55 int prCTZ(VMGlobals *g, int numArgsPushed);
56 int prCTZ(VMGlobals *g, int numArgsPushed)
58 PyrSlot *a = g->sp;
59 SetRaw(a, CTZ(slotRawInt(a)));
60 return errNone;
63 int prNextPowerOfTwo(VMGlobals *g, int numArgsPushed);
64 int prNextPowerOfTwo(VMGlobals *g, int numArgsPushed)
66 PyrSlot *a;
68 a = g->sp;
70 SetRaw(a, NEXTPOWEROFTWO(slotRawInt(a)));
71 return errNone;
74 int prIsPowerOfTwo(VMGlobals *g, int numArgsPushed);
75 int prIsPowerOfTwo(VMGlobals *g, int numArgsPushed)
77 PyrSlot *a = g->sp;
78 SetBool(a, ISPOWEROFTWO(slotRawInt(a)));
79 return errNone;
82 int prBinaryGrayCode(VMGlobals *g, int numArgsPushed);
83 int prBinaryGrayCode(VMGlobals *g, int numArgsPushed)
85 PyrSlot *a;
87 a = g->sp;
88 SetRaw(a, GRAYCODE(slotRawInt(a)));
89 return errNone;
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;
97 PyrSlot *c = g->sp;
99 int32 bit, mask;
100 int err = slotIntVal(b, &bit);
101 if (err) return err;
102 bit = bit & 31;
103 mask = 1L << bit;
104 if (IsFalse(c)) {
105 SetRaw(a, slotRawInt(a) & ~mask);
106 } else {
107 SetRaw(a, slotRawInt(a) | mask);
109 return errNone;
112 int prHammingDistance(VMGlobals *g, int numArgsPushed);
113 int prHammingDistance(VMGlobals *g, int numArgsPushed)
115 PyrSlot *a = g->sp - 1;
116 PyrSlot *b = g->sp;
118 if (NotInt(a) || NotInt(b))
119 return errWrongType;
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;
127 mask = mask << 1;
129 SetRaw(a, count);
131 return errNone;
134 void initBitPrimitives();
135 void initBitPrimitives()
137 int base, index = 0;
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);
154 #if _SC_PLUGINS_
156 #include "SCPlugin.h"
158 // export the function that SC will call to load the plug in.
159 #pragma export on
160 extern "C" { SCPlugIn* loadPlugIn(void); }
161 #pragma export off
164 // define plug in object
165 class APlugIn : public SCPlugIn
167 public:
168 APlugIn();
169 virtual ~APlugIn();
171 virtual void AboutToCompile();
174 APlugIn::APlugIn()
176 // constructor for plug in
179 APlugIn::~APlugIn()
181 // destructor for plug in
184 void APlugIn::AboutToCompile()
186 // this is called each time the class library is compiled.
187 initBitPrimitives();
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();
197 #endif