1 /**
2  * These functions are built-in intrinsics to the compiler.
3  *
4  * Intrinsic functions are functions built in to the compiler, usually to take
5  * advantage of specific CPU features that are inefficient to handle via
6  * external functions.  The compiler's optimizer and code generator are fully
7  * integrated in with intrinsic functions, bringing to bear their full power on
8  * them. This can result in some surprising speedups.
9  *
10  * Note that this module is only present in Tango because the module name is
11  * hardcoded into DMD, see http://d.puremagic.com/issues/show_bug.cgi?id=178
12  * To correctly use this functionality in Tango, import tango.core.BitManip.
13  *
14  * Copyright: Public Domain
15  * License:   Public Domain
16  * Authors:   Walter Bright
17  */
18 module std.intrinsic;
19 
20 
21 /**
22  * Scans the bits in v starting with bit 0, looking
23  * for the first set bit.
24  * Returns:
25  *	The bit number of the first bit set.
26  *	The return value is undefined if v is zero.
27  */
28 int bsf( size_t v );
29 
30 
31 /**
32  * Scans the bits in v from the most significant bit
33  * to the least significant bit, looking
34  * for the first set bit.
35  * Returns:
36  *	The bit number of the first bit set.
37  *	The return value is undefined if v is zero.
38  * Example:
39  * ---
40  * import std.intrinsic;
41  *
42  * int main()
43  * {
44  *     uint v;
45  *     int x;
46  *
47  *     v = 0x21;
48  *     x = bsf(v);
49  *     printf("bsf(x%x) = %d\n", v, x);
50  *     x = bsr(v);
51  *     printf("bsr(x%x) = %d\n", v, x);
52  *     return 0;
53  * }
54  * ---
55  * Output:
56  *  bsf(x21) = 0<br>
57  *  bsr(x21) = 5
58  */
59 int bsr( size_t v );
60 
61 
62 /**
63  * Tests the bit.
64  */
65 int bt( size_t* p, size_t bitnum );
66 
67 
68 /**
69  * Tests and complements the bit.
70  */
71 int btc( size_t* p, size_t bitnum );
72 
73 
74 /**
75  * Tests and resets (sets to 0) the bit.
76  */
77 int btr( size_t* p, size_t bitnum );
78 
79 
80 /**
81  * Tests and sets the bit.
82  * Params:
83  * p = a non-NULL pointer to an array of size_ts.
84  * index = a bit number, starting with bit 0 of p[0],
85  * and progressing. It addresses bits like the expression:
86 ---
87 p[index / (size_t.sizeof*8)] & (1 << (index & ((size_t.sizeof*8) - 1)))
88 ---
89  * Returns:
90  * 	A non-zero value if the bit was set, and a zero
91  *	if it was clear.
92  *
93  * Example:
94  * ---
95 import std.intrinsic;
96 
97 int main()
98 {
99     size_t array[2];
100 
101     array[0] = 2;
102     array[1] = 0x100;
103 
104     printf("btc(array, 35) = %d\n", <b>btc</b>(array, 35));
105     printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
106 
107     printf("btc(array, 35) = %d\n", <b>btc</b>(array, 35));
108     printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
109 
110     printf("bts(array, 35) = %d\n", <b>bts</b>(array, 35));
111     printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
112 
113     printf("btr(array, 35) = %d\n", <b>btr</b>(array, 35));
114     printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
115 
116     printf("bt(array, 1) = %d\n", <b>bt</b>(array, 1));
117     printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
118 
119     return 0;
120 }
121  * ---
122  * Output:
123 <pre>
124 btc(array, 35) = 0
125 array = [0]:x2, [1]:x108
126 btc(array, 35) = -1
127 array = [0]:x2, [1]:x100
128 bts(array, 35) = 0
129 array = [0]:x2, [1]:x108
130 btr(array, 35) = -1
131 array = [0]:x2, [1]:x100
132 bt(array, 1) = -1
133 array = [0]:x2, [1]:x100
134 </pre>
135  */
136 
137 int bts( size_t* p, size_t bitnum );
138 
139 /**
140  * Swaps bytes in a 4 byte uint end-to-end, i.e. byte 0 becomes
141  * byte 3, byte 1 becomes byte 2, byte 2 becomes byte 1, byte 3
142  * becomes byte 0.
143  */
144 uint bswap( uint v );
145 
146 
147 /**
148  * Reads I/O port at port_address.
149  */
150 ubyte inp( uint port_address );
151 
152 
153 /**
154  * ditto
155  */
156 ushort inpw( uint port_address );
157 
158 
159 /**
160  * ditto
161  */
162 uint inpl( uint port_address );
163 
164 
165 /**
166  * Writes and returns value to I/O port at port_address.
167  */
168 ubyte outp( uint port_address, ubyte value );
169 
170 
171 /**
172  * ditto
173  */
174 ushort outpw( uint port_address, ushort value );
175 
176 
177 /**
178  * ditto
179  */
180 uint outpl( uint port_address, uint value );