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