132
|
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 * Copyright: Public Domain
|
|
11 * License: Public Domain
|
|
12 * Authors: Walter Bright
|
|
13 */
|
|
14 module std.intrinsic;
|
|
15
|
184
|
16 // LLVMDC doesn't use this module!
|
|
17 version( LLVMDC )
|
|
18 {
|
|
19 static assert(0, "LLVMDC does not support the std.intrinsic module with Tango");
|
|
20 }
|
132
|
21
|
|
22 /**
|
|
23 * Scans the bits in v starting with bit 0, looking
|
|
24 * for the first set bit.
|
|
25 * Returns:
|
|
26 * The bit number of the first bit set.
|
|
27 * The return value is undefined if v is zero.
|
|
28 */
|
|
29 int bsf( uint v );
|
|
30
|
|
31
|
|
32 /**
|
|
33 * Scans the bits in v from the most significant bit
|
|
34 * to the least significant bit, looking
|
|
35 * for the first set bit.
|
|
36 * Returns:
|
|
37 * The bit number of the first bit set.
|
|
38 * The return value is undefined if v is zero.
|
|
39 * Example:
|
|
40 * ---
|
|
41 * import std.intrinsic;
|
|
42 *
|
|
43 * int main()
|
|
44 * {
|
|
45 * uint v;
|
|
46 * int x;
|
|
47 *
|
|
48 * v = 0x21;
|
|
49 * x = bsf(v);
|
|
50 * printf("bsf(x%x) = %d\n", v, x);
|
|
51 * x = bsr(v);
|
|
52 * printf("bsr(x%x) = %d\n", v, x);
|
|
53 * return 0;
|
|
54 * }
|
|
55 * ---
|
|
56 * Output:
|
|
57 * bsf(x21) = 0<br>
|
|
58 * bsr(x21) = 5
|
|
59 */
|
|
60 int bsr( uint v );
|
|
61
|
|
62
|
|
63 /**
|
|
64 * Tests the bit.
|
|
65 */
|
|
66 int bt( uint* p, uint bitnum );
|
|
67
|
|
68
|
|
69 /**
|
|
70 * Tests and complements the bit.
|
|
71 */
|
|
72 int btc( uint* p, uint bitnum );
|
|
73
|
|
74
|
|
75 /**
|
|
76 * Tests and resets (sets to 0) the bit.
|
|
77 */
|
|
78 int btr( uint* p, uint bitnum );
|
|
79
|
|
80
|
|
81 /**
|
|
82 * Tests and sets the bit.
|
|
83 * Params:
|
|
84 * p = a non-NULL pointer to an array of uints.
|
|
85 * index = a bit number, starting with bit 0 of p[0],
|
|
86 * and progressing. It addresses bits like the expression:
|
|
87 ---
|
|
88 p[index / (uint.sizeof*8)] & (1 << (index & ((uint.sizeof*8) - 1)))
|
|
89 ---
|
|
90 * Returns:
|
|
91 * A non-zero value if the bit was set, and a zero
|
|
92 * if it was clear.
|
|
93 *
|
|
94 * Example:
|
|
95 * ---
|
|
96 import std.intrinsic;
|
|
97
|
|
98 int main()
|
|
99 {
|
|
100 uint array[2];
|
|
101
|
|
102 array[0] = 2;
|
|
103 array[1] = 0x100;
|
|
104
|
|
105 printf("btc(array, 35) = %d\n", <b>btc</b>(array, 35));
|
|
106 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
|
|
107
|
|
108 printf("btc(array, 35) = %d\n", <b>btc</b>(array, 35));
|
|
109 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
|
|
110
|
|
111 printf("bts(array, 35) = %d\n", <b>bts</b>(array, 35));
|
|
112 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
|
|
113
|
|
114 printf("btr(array, 35) = %d\n", <b>btr</b>(array, 35));
|
|
115 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
|
|
116
|
|
117 printf("bt(array, 1) = %d\n", <b>bt</b>(array, 1));
|
|
118 printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);
|
|
119
|
|
120 return 0;
|
|
121 }
|
|
122 * ---
|
|
123 * Output:
|
|
124 <pre>
|
|
125 btc(array, 35) = 0
|
|
126 array = [0]:x2, [1]:x108
|
|
127 btc(array, 35) = -1
|
|
128 array = [0]:x2, [1]:x100
|
|
129 bts(array, 35) = 0
|
|
130 array = [0]:x2, [1]:x108
|
|
131 btr(array, 35) = -1
|
|
132 array = [0]:x2, [1]:x100
|
|
133 bt(array, 1) = -1
|
|
134 array = [0]:x2, [1]:x100
|
|
135 </pre>
|
|
136 */
|
|
137 int bts( uint* p, uint bitnum );
|
|
138
|
|
139
|
|
140 /**
|
|
141 * Swaps bytes in a 4 byte uint end-to-end, i.e. byte 0 becomes
|
|
142 * byte 3, byte 1 becomes byte 2, byte 2 becomes byte 1, byte 3
|
|
143 * becomes byte 0.
|
|
144 */
|
|
145 uint bswap( uint v );
|
|
146
|
|
147
|
|
148 /**
|
|
149 * Reads I/O port at port_address.
|
|
150 */
|
|
151 ubyte inp( uint port_address );
|
|
152
|
|
153
|
|
154 /**
|
|
155 * ditto
|
|
156 */
|
|
157 ushort inpw( uint port_address );
|
|
158
|
|
159
|
|
160 /**
|
|
161 * ditto
|
|
162 */
|
|
163 uint inpl( uint port_address );
|
|
164
|
|
165
|
|
166 /**
|
|
167 * Writes and returns value to I/O port at port_address.
|
|
168 */
|
|
169 ubyte outp( uint port_address, ubyte value );
|
|
170
|
|
171
|
|
172 /**
|
|
173 * ditto
|
|
174 */
|
|
175 ushort outpw( uint port_address, ushort value );
|
|
176
|
|
177
|
|
178 /**
|
|
179 * ditto
|
|
180 */
|
|
181 uint outpl( uint port_address, uint value ); |