2 * This module contains a collection of bit-level operations.
4 * Copyright: Copyright (c) 2005-2008, The D Runtime Project
5 * License: BSD Style, see LICENSE
6 * Authors: Walter Bright, Don Clugston, Sean Kelly
14 * Scans the bits in v starting with bit 0, looking
15 * for the first set bit.
17 * The bit number of the first bit set.
18 * The return value is undefined if v is zero.
24 * Scans the bits in v from the most significant bit
25 * to the least significant bit, looking
26 * for the first set bit.
28 * The bit number of the first bit set.
29 * The return value is undefined if v is zero.
32 * import core.bitmanip;
41 * printf("bsf(x%x) = %d\n", v, x);
43 * printf("bsr(x%x) = %d\n", v, x);
57 int bt( uint* p, uint bitnum );
61 * Tests and complements the bit.
63 int btc( uint* p, uint bitnum );
67 * Tests and resets (sets to 0) the bit.
69 int btr( uint* p, uint bitnum );
73 * Tests and sets the bit.
75 * p = a non-NULL pointer to an array of uints.
76 * index = a bit number, starting with bit 0 of p[0],
77 * and progressing. It addresses bits like the expression:
79 p[index / (uint.sizeof*8)] & (1 << (index & ((uint.sizeof*8) - 1)))
82 * A non-zero value if the bit was set, and a zero
96 printf("btc(array, 35) = %d\n", <b>btc</b>(array, 35));
97 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
99 printf("btc(array, 35) = %d\n", <b>btc</b>(array, 35));
100 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
102 printf("bts(array, 35) = %d\n", <b>bts</b>(array, 35));
103 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
105 printf("btr(array, 35) = %d\n", <b>btr</b>(array, 35));
106 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
108 printf("bt(array, 1) = %d\n", <b>bt</b>(array, 1));
109 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
117 array = [0]:x2, [1]:x108
119 array = [0]:x2, [1]:x100
121 array = [0]:x2, [1]:x108
123 array = [0]:x2, [1]:x100
125 array = [0]:x2, [1]:x100
128 int bts( uint* p, uint bitnum );
132 * Swaps bytes in a 4 byte uint end-to-end, i.e. byte 0 becomes
133 * byte 3, byte 1 becomes byte 2, byte 2 becomes byte 1, byte 3
136 uint bswap( uint v );
140 * Reads I/O port at port_address.
142 ubyte inp( uint port_address );
148 ushort inpw( uint port_address );
154 uint inpl( uint port_address );
158 * Writes and returns value to I/O port at port_address.
160 ubyte outp( uint port_address, ubyte value );
166 ushort outpw( uint port_address, ushort value );
172 uint outpl( uint port_address, uint value );
176 public import std.intrinsic;
181 * Calculates the number of set bits in a 32-bit integer.
185 // Avoid branches, and the potential for cache misses which
186 // could be incurred with a table lookup.
188 // We need to mask alternate bits to prevent the
189 // sum from overflowing.
190 // add neighbouring bits. Each bit is 0 or 1.
191 x = x - ((x>>1) & 0x5555_5555);
192 // now each two bits of x is a number 00,01 or 10.
193 // now add neighbouring pairs
194 x = ((x&0xCCCC_CCCC)>>2) + (x&0x3333_3333);
195 // now each nibble holds 0000-0100. Adding them won't
196 // overflow any more, so we don't need to mask any more
198 // Now add the nibbles, then the bytes, then the words
199 // We still need to mask to prevent double-counting.
200 // Note that if we used a rotate instead of a shift, we
201 // wouldn't need the masks, and could just divide the sum
202 // by 8 to account for the double-counting.
203 // On some CPUs, it may be faster to perform a multiply.
219 assert( popcnt( 0 ) == 0 );
220 assert( popcnt( 7 ) == 3 );
221 assert( popcnt( 0xAA )== 4 );
222 assert( popcnt( 0x8421_1248 ) == 8 );
223 assert( popcnt( 0xFFFF_FFFF ) == 32 );
224 assert( popcnt( 0xCCCC_CCCC ) == 16 );
225 assert( popcnt( 0x7777_7777 ) == 24 );
231 * Reverses the order of bits in a 32-bit integer.
233 uint bitswap( uint x )
236 version( D_InlineAsm_X86 )
240 // Author: Tiago Gasiba.
243 and EDX, 0x5555_5555;
244 and EAX, 0x5555_5555;
249 and EDX, 0x3333_3333;
250 and EAX, 0x3333_3333;
255 and EDX, 0x0f0f_0f0f;
256 and EAX, 0x0f0f_0f0f;
264 // swap odd and even bits
265 x = ((x >> 1) & 0x5555_5555) | ((x & 0x5555_5555) << 1);
266 // swap consecutive pairs
267 x = ((x >> 2) & 0x3333_3333) | ((x & 0x3333_3333) << 2);
269 x = ((x >> 4) & 0x0F0F_0F0F) | ((x & 0x0F0F_0F0F) << 4);
271 x = ((x >> 8) & 0x00FF_00FF) | ((x & 0x00FF_00FF) << 8);
272 // swap 2-byte long pairs
273 x = ( x >> 16 ) | ( x << 16);
284 assert( bitswap( 0x8000_0100 ) == 0x0080_0001 );