|
28 | 28 | #ifndef __IA32INTRIN_H
|
29 | 29 | #define __IA32INTRIN_H
|
30 | 30 |
|
| 31 | +/** Find the first set bit starting from the lsb. Result is undefined if |
| 32 | + * input is 0. |
| 33 | + * |
| 34 | + * \headerfile <x86intrin.h> |
| 35 | + * |
| 36 | + * This intrinsic corresponds to the <c> BSF </c> instruction or the |
| 37 | + * <c> TZCNT </c> instruction. |
| 38 | + * |
| 39 | + * \param __A |
| 40 | + * A 32-bit integer operand. |
| 41 | + * \returns A 32-bit integer containing the bit number. |
| 42 | + */ |
| 43 | +static __inline__ int __attribute__((__always_inline__, __nodebug__)) |
| 44 | +__bsfd(int __A) { |
| 45 | + return __builtin_ctz(__A); |
| 46 | +} |
| 47 | + |
| 48 | +/** Find the first set bit starting from the msb. Result is undefined if |
| 49 | + * input is 0. |
| 50 | + * |
| 51 | + * \headerfile <x86intrin.h> |
| 52 | + * |
| 53 | + * This intrinsic corresponds to the <c> BSR </c> instruction or the |
| 54 | + * <c> LZCNT </c> instruction and an <c> XOR </c>. |
| 55 | + * |
| 56 | + * \param __A |
| 57 | + * A 32-bit integer operand. |
| 58 | + * \returns A 32-bit integer containing the bit number. |
| 59 | + */ |
| 60 | +static __inline__ int __attribute__((__always_inline__, __nodebug__)) |
| 61 | +__bsrd(int __A) { |
| 62 | + return 31 - __builtin_clz(__A); |
| 63 | +} |
| 64 | + |
| 65 | +/** Swaps the bytes in the input. Converting little endian to big endian or |
| 66 | + * vice versa. |
| 67 | + * |
| 68 | + * \headerfile <x86intrin.h> |
| 69 | + * |
| 70 | + * This intrinsic corresponds to the <c> BSWAP </c> instruction. |
| 71 | + * |
| 72 | + * \param __A |
| 73 | + * A 32-bit integer operand. |
| 74 | + * \returns A 32-bit integer containing the swapped bytes. |
| 75 | + */ |
| 76 | +static __inline__ int __attribute__((__always_inline__, __nodebug__)) |
| 77 | +__bswapd(int __A) { |
| 78 | + return __builtin_bswap32(__A); |
| 79 | +} |
| 80 | + |
| 81 | +#define _bswap(A) __bswapd((A)) |
| 82 | +#define _bit_scan_forward(A) __bsfd((A)) |
| 83 | +#define _bit_scan_reverse(A) __bsrd((A)) |
| 84 | + |
| 85 | +#ifdef __x86_64__ |
| 86 | +/** Find the first set bit starting from the lsb. Result is undefined if |
| 87 | + * input is 0. |
| 88 | + * |
| 89 | + * \headerfile <x86intrin.h> |
| 90 | + * |
| 91 | + * This intrinsic corresponds to the <c> BSF </c> instruction or the |
| 92 | + * <c> TZCNT </c> instruction. |
| 93 | + * |
| 94 | + * \param __A |
| 95 | + * A 64-bit integer operand. |
| 96 | + * \returns A 32-bit integer containing the bit number. |
| 97 | + */ |
| 98 | +static __inline__ int __attribute__((__always_inline__, __nodebug__)) |
| 99 | +__bsfq(long long __A) { |
| 100 | + return __builtin_ctzll(__A); |
| 101 | +} |
| 102 | + |
| 103 | +/** Find the first set bit starting from the msb. Result is undefined if |
| 104 | + * input is 0. |
| 105 | + * |
| 106 | + * \headerfile <x86intrin.h> |
| 107 | + * |
| 108 | + * This intrinsic corresponds to the <c> BSR </c> instruction or the |
| 109 | + * <c> LZCNT </c> instruction and an <c> XOR </c>. |
| 110 | + * |
| 111 | + * \param __A |
| 112 | + * A 64-bit integer operand. |
| 113 | + * \returns A 32-bit integer containing the bit number. |
| 114 | + */ |
| 115 | +static __inline__ int __attribute__((__always_inline__, __nodebug__)) |
| 116 | +__bsrq(long long __A) { |
| 117 | + return 63 - __builtin_clzll(__A); |
| 118 | +} |
| 119 | + |
| 120 | +/** Swaps the bytes in the input. Converting little endian to big endian or |
| 121 | + * vice versa. |
| 122 | + * |
| 123 | + * \headerfile <x86intrin.h> |
| 124 | + * |
| 125 | + * This intrinsic corresponds to the <c> BSWAP </c> instruction. |
| 126 | + * |
| 127 | + * \param __A |
| 128 | + * A 64-bit integer operand. |
| 129 | + * \returns A 64-bit integer containing the swapped bytes. |
| 130 | + */ |
| 131 | +static __inline__ long long __attribute__((__always_inline__, __nodebug__)) |
| 132 | +__bswapq(long long __A) { |
| 133 | + return __builtin_bswap64(__A); |
| 134 | +} |
| 135 | + |
| 136 | +#define _bswap64(A) __bswapq((A)) |
| 137 | +#endif |
| 138 | + |
31 | 139 | /** Counts the number of bits in the source operand having a value of 1.
|
32 | 140 | *
|
33 | 141 | * \headerfile <x86intrin.h>
|
|
0 commit comments