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