2 * This module contains a specialized bitset implementation.
4 * Copyright: Copyright (C) 2005-2006 Digital Mars, www.digitalmars.com.
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, in both source and binary form, subject to the following
16 * o The origin of this software must not be misrepresented; you must not
17 * claim that you wrote the original software. If you use this software
18 * in a product, an acknowledgment in the product documentation would be
19 * appreciated but is not required.
20 * o Altered source versions must be plainly marked as such, and must not
21 * be misrepresented as being the original software.
22 * o This notice may not be removed or altered from any source
24 * Authors: Walter Bright, David Friedman, Sean Kelly
27 module rt.gc.cdgc.bits;
29 import os = rt.gc.cdgc.os;
31 import cstring = tango.stdc.string;
33 private extern (C) void onOutOfMemoryError();
43 // use the unoptimized version
49 else version (D_InlineAsm_X86)
56 const int BITS_PER_WORD = 32;
57 const int BITS_SHIFT = 5;
58 const int BITS_MASK = 31;
61 size_t nwords = 0; // allocated words in data[] excluding sentinals
62 size_t nbits = 0; // number of bits in data[] excluding sentinals
64 /// Get the number of bytes needed to store nbits bits
67 return (nwords + 2) * uint.sizeof; // +2 for sentinels
70 void Dtor(os.Vis vis = os.Vis.PRIV)
72 // Even when os.dealloc() can be called with a null pointer, the extra
73 // call might be significant. On hard GC benchmarks making the test for
74 // null here (i.e. not making the call) can reduce the GC time by
78 os.dealloc(data, data_size, vis);
87 ((nbits + (BITS_PER_WORD - 1)) >> BITS_SHIFT));
90 void alloc(size_t nbits, os.Vis vis = os.Vis.PRIV)
93 this.nwords = (nbits + (BITS_PER_WORD - 1)) >> BITS_SHIFT;
94 this.data = cast(uint*) os.alloc(data_size, vis);
106 //return (cast(bit *)(data + 1))[i];
107 return data[1 + (i >> BITS_SHIFT)] & (1 << (i & BITS_MASK));
117 //(cast(bit *)(data + 1))[i] = 1;
118 data[1 + (i >> BITS_SHIFT)] |= (1 << (i & BITS_MASK));
128 //(cast(bit *)(data + 1))[i] = 0;
129 data[1 + (i >> BITS_SHIFT)] &= ~(1 << (i & BITS_MASK));
132 uint testClear(size_t i)
136 return std.intrinsic.btr(data + 1, i);
152 //result = (cast(bit *)(data + 1))[i];
153 //(cast(bit *)(data + 1))[i] = 0;
155 uint* p = &data[1 + (i >> BITS_SHIFT)];
156 uint mask = (1 << (i & BITS_MASK));
157 uint result = *p & mask;
165 version(MEMCPY_NON_SIG_SAFE) {
166 uint * d1=data+1,dEnd=d1+nwords;
170 cstring.memset(data + 1, 0, nwords * uint.sizeof);
177 assert(nwords == f.nwords);
181 version(MEMCPY_NON_SIG_SAFE) {
182 uint * d1=data+1,d2=f.data+1,dEnd=d1+nwords;
183 for (;d1!=dEnd;++d1,++d2)
186 cstring.memcpy(data + 1, f.data + 1, nwords * uint.sizeof);
206 assert(b.test(123) == 0);
207 assert(b.testClear(123) == 0);
209 assert(b.test(123) != 0);
210 assert(b.testClear(123) != 0);
211 assert(b.test(123) == 0);
215 assert(b.test(785) != 0);
216 assert(b.test(0) != 0);
218 assert(b.test(785) == 0);
219 assert(b.test(0) == 0);
225 assert(b.test(38) != 0);
232 // vim: set et sw=4 sts=4 :