| 1 |
|
| /* byte_order.c - byte order related platform dependent routines, |
| 2 |
|
| * |
| 3 |
|
| * Copyright (c) 2008, Aleksey Kravchenko <rhash.admin@gmail.com> |
| 4 |
|
| * |
| 5 |
|
| * Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
|
| * purpose with or without fee is hereby granted. |
| 7 |
|
| * |
| 8 |
|
| * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 9 |
|
| * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 10 |
|
| * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 11 |
|
| * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 12 |
|
| * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 13 |
|
| * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 14 |
|
| * PERFORMANCE OF THIS SOFTWARE. |
| 15 |
|
| */ |
| 16 |
|
| #include "byte_order.h" |
| 17 |
|
| |
| 18 |
|
| #ifndef rhash_ctz |
| 19 |
|
| |
| 20 |
|
| # if _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64) /* if MSVC++ >= 2002 on x86/x64 */ |
| 21 |
|
| # include <intrin.h> |
| 22 |
|
| # pragma intrinsic(_BitScanForward) |
| 23 |
|
| |
| 24 |
|
| /** |
| 25 |
|
| * Returns index of the trailing bit of x. |
| 26 |
|
| * |
| 27 |
|
| * @param x the number to process |
| 28 |
|
| * @return zero-based index of the trailing bit |
| 29 |
|
| */ |
| 30 |
|
| unsigned rhash_ctz(unsigned x) |
| 31 |
|
| { |
| 32 |
|
| unsigned long index; |
| 33 |
|
| unsigned char isNonzero = _BitScanForward(&index, x); /* MSVC intrinsic */ |
| 34 |
|
| return (isNonzero ? (unsigned)index : 0); |
| 35 |
|
| } |
| 36 |
|
| # else /* _MSC_VER >= 1300... */ |
| 37 |
|
| |
| 38 |
|
| /** |
| 39 |
|
| * Returns index of the trailing bit of a 32-bit number. |
| 40 |
|
| * This is a plain C equivalent for GCC __builtin_ctz() bit scan. |
| 41 |
|
| * |
| 42 |
|
| * @param x the number to process |
| 43 |
|
| * @return zero-based index of the trailing bit |
| 44 |
|
| */ |
| 45 |
|
| unsigned rhash_ctz(unsigned x) |
| 46 |
|
| { |
| 47 |
|
| /* array for conversion to bit position */ |
| 48 |
|
| static unsigned char bit_pos[32] = { |
| 49 |
|
| 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, |
| 50 |
|
| 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 |
| 51 |
|
| }; |
| 52 |
|
| |
| 53 |
|
| /* The De Bruijn bit-scan was devised in 1997, according to Donald Knuth |
| 54 |
|
| * by Martin Lauter. The constant 0x077CB531UL is a De Bruijn sequence, |
| 55 |
|
| * which produces a unique pattern of bits into the high 5 bits for each |
| 56 |
|
| * possible bit position that it is multiplied against. |
| 57 |
|
| * See http://graphics.stanford.edu/~seander/bithacks.html |
| 58 |
|
| * and http://chessprogramming.wikispaces.com/BitScan */ |
| 59 |
|
| return (unsigned)bit_pos[((uint32_t)((x & -x) * 0x077CB531U)) >> 27]; |
| 60 |
|
| } |
| 61 |
|
| # endif /* _MSC_VER >= 1300... */ |
| 62 |
|
| #endif /* rhash_ctz */ |
| 63 |
|
| |
| 64 |
|
| #ifndef rhash_popcount |
| 65 |
|
| /** |
| 66 |
|
| * Returns the number of 1-bits in x. |
| 67 |
|
| * |
| 68 |
|
| * @param x the value to process |
| 69 |
|
| * @return the number of 1-bits |
| 70 |
|
| */ |
| 71 |
|
| unsigned rhash_popcount(unsigned x) |
| 72 |
|
| { |
| 73 |
|
| x -= (x >>1) & 0x55555555; |
| 74 |
|
| x = ((x >> 2) & 0x33333333) + (x & 0x33333333); |
| 75 |
|
| x = ((x >> 4) + x) & 0x0f0f0f0f; |
| 76 |
|
| return (x * 0x01010101) >> 24; |
| 77 |
|
| } |
| 78 |
|
| #endif /* rhash_popcount */ |
| 79 |
|
| |
| 80 |
|
| /** |
| 81 |
|
| * Copy a memory block with simultaneous exchanging byte order. |
| 82 |
|
| * The byte order is changed from little-endian 32-bit integers |
| 83 |
|
| * to big-endian (or vice-versa). |
| 84 |
|
| * |
| 85 |
|
| * @param to the pointer where to copy memory block |
| 86 |
|
| * @param index the index to start writing from |
| 87 |
|
| * @param from the source block to copy |
| 88 |
|
| * @param length length of the memory block |
| 89 |
|
| */ |
| 90 |
24 |
| void rhash_swap_copy_str_to_u32(void* to, int index, const void* from, size_t length) |
| 91 |
|
| { |
| 92 |
|
| /* if all pointers and length are 32-bits aligned */ |
| 93 |
24 |
▶ | if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 3) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 3) ) { |
24 |
0 |
24 |
|
| 94 |
|
| /* copy memory as 32-bit words */ |
| 95 |
24 |
| const uint32_t* src = (const uint32_t*)from; |
| 96 |
24 |
| const uint32_t* end = (const uint32_t*)((const char*)src + length); |
| 97 |
24 |
| uint32_t* dst = (uint32_t*)((char*)to + index); |
| 98 |
149 |
▶ | for (; src < end; dst++, src++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (; src < end; dst++, src++) |
125 |
24 |
149 |
|
| 99 |
125 |
| *dst = bswap_32(*src); |
| 100 |
|
| } else { |
| 101 |
0 |
| const char* src = (const char*)from; |
| 102 |
0 |
▶ | for (length += index; (size_t)index < length; index++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (length += index; (size_t)index < length; index++) |
0 |
0 |
0 |
|
| 103 |
0 |
| ((char*)to)[index ^ 3] = *(src++); |
| 104 |
|
| } |
| 105 |
24 |
| } |
| 106 |
|
| |
| 107 |
|
| /** |
| 108 |
|
| * Copy a memory block with changed byte order. |
| 109 |
|
| * The byte order is changed from little-endian 64-bit integers |
| 110 |
|
| * to big-endian (or vice-versa). |
| 111 |
|
| * |
| 112 |
|
| * @param to the pointer where to copy memory block |
| 113 |
|
| * @param index the index to start writing from |
| 114 |
|
| * @param from the source block to copy |
| 115 |
|
| * @param length length of the memory block |
| 116 |
|
| */ |
| 117 |
6 |
| void rhash_swap_copy_str_to_u64(void* to, int index, const void* from, size_t length) |
| 118 |
|
| { |
| 119 |
|
| /* if all pointers and length are 64-bits aligned */ |
| 120 |
6 |
▶ | if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 7) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 7) ) { |
6 |
0 |
6 |
|
| 121 |
|
| /* copy aligned memory block as 64-bit integers */ |
| 122 |
6 |
| const uint64_t* src = (const uint64_t*)from; |
| 123 |
6 |
| const uint64_t* end = (const uint64_t*)((const char*)src + length); |
| 124 |
6 |
| uint64_t* dst = (uint64_t*)((char*)to + index); |
| 125 |
50 |
▶ | while (src < end) *(dst++) = bswap_64( *(src++) ); |
Branch: 1 branch
| Branch | True | False | Total |
| while (src < end) *(dst++) = bswap_64( *(src++) ); |
44 |
6 |
50 |
|
| 126 |
|
| } else { |
| 127 |
0 |
| const char* src = (const char*)from; |
| 128 |
0 |
▶ | for (length += index; (size_t)index < length; index++) ((char*)to)[index ^ 7] = *(src++); |
Branch: 1 branch
| Branch | True | False | Total |
| for (length += index; (size_t)index < length; index++) ((char*)to)[index ^ 7] = *(src++); |
0 |
0 |
0 |
|
| 129 |
|
| } |
| 130 |
6 |
| } |
| 131 |
|
| |
| 132 |
|
| /** |
| 133 |
|
| * Copy data from a sequence of 64-bit words to a binary string of given length, |
| 134 |
|
| * while changing byte order. |
| 135 |
|
| * |
| 136 |
|
| * @param to the binary string to receive data |
| 137 |
|
| * @param from the source sequence of 64-bit words |
| 138 |
|
| * @param length the size in bytes of the data being copied |
| 139 |
|
| */ |
| 140 |
3 |
| void rhash_swap_copy_u64_to_str(void* to, const void* from, size_t length) |
| 141 |
|
| { |
| 142 |
|
| /* if all pointers and length are 64-bits aligned */ |
| 143 |
3 |
▶ | if ( 0 == (( (uintptr_t)to | (uintptr_t)from | length ) & 7) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( 0 == (( (uintptr_t)to | (uintptr_t)from | length ) & 7) ) { |
3 |
0 |
3 |
|
| 144 |
|
| /* copy aligned memory block as 64-bit integers */ |
| 145 |
3 |
| const uint64_t* src = (const uint64_t*)from; |
| 146 |
3 |
| const uint64_t* end = (const uint64_t*)((const char*)src + length); |
| 147 |
3 |
| uint64_t* dst = (uint64_t*)to; |
| 148 |
25 |
▶ | while (src < end) *(dst++) = bswap_64( *(src++) ); |
Branch: 1 branch
| Branch | True | False | Total |
| while (src < end) *(dst++) = bswap_64( *(src++) ); |
22 |
3 |
25 |
|
| 149 |
|
| } else { |
| 150 |
|
| size_t index; |
| 151 |
0 |
| char* dst = (char*)to; |
| 152 |
0 |
▶ | for (index = 0; index < length; index++) *(dst++) = ((char*)from)[index ^ 7]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (index = 0; index < length; index++) *(dst++) = ((char*)from)[index ^ 7]; |
0 |
0 |
0 |
|
| 153 |
|
| } |
| 154 |
3 |
| } |
| 155 |
|
| |
| 156 |
|
| /** |
| 157 |
|
| * Exchange byte order in the given array of 32-bit integers. |
| 158 |
|
| * |
| 159 |
|
| * @param arr the array to process |
| 160 |
|
| * @param length array length |
| 161 |
|
| */ |
| 162 |
2 |
| void rhash_u32_mem_swap(unsigned* arr, int length) |
| 163 |
|
| { |
| 164 |
2 |
| unsigned* end = arr + length; |
| 165 |
12 |
▶ | for (; arr < end; arr++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; arr < end; arr++) { |
10 |
2 |
12 |
|
| 166 |
10 |
| *arr = bswap_32(*arr); |
| 167 |
|
| } |
| 168 |
2 |
| } |
| 169 |
|
| |
| 170 |
|
| #ifdef HAS_INTEL_CPUID |
| 171 |
|
| #include <cpuid.h> |
| 172 |
|
| |
| 173 |
2 |
| static uint64_t get_cpuid_features(void) |
| 174 |
|
| { |
| 175 |
|
| uint32_t tmp, edx, ecx; |
| 176 |
2 |
▶ | if (__get_cpuid(1, &tmp, &tmp, &ecx, &edx)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (__get_cpuid(1, &tmp, &tmp, &ecx, &edx)) |
2 |
0 |
2 |
|
| 177 |
2 |
| return ((((uint64_t)ecx) << 32) ^ edx); |
| 178 |
0 |
| return 0; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
2 |
| int has_cpu_feature(unsigned feature_bit) |
| 182 |
|
| { |
| 183 |
|
| static uint64_t features; |
| 184 |
2 |
| const uint64_t feature = ((uint64_t)1) << feature_bit; |
| 185 |
2 |
▶ | if (!features) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!features) |
2 |
0 |
2 |
|
| 186 |
2 |
| features = (get_cpuid_features() | 1); |
| 187 |
2 |
| return !!(features & feature); |
| 188 |
|
| } |
| 189 |
|
| #endif |