Path: blob/master/Utilities/cmlibrhash/librhash/byte_order.c
3150 views
/* byte_order.c - byte order related platform dependent routines,1*2* Copyright (c) 2008, Aleksey Kravchenko <[email protected]>3*4* Permission to use, copy, modify, and/or distribute this software for any5* purpose with or without fee is hereby granted.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH8* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY9* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,10* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM11* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE12* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR13* PERFORMANCE OF THIS SOFTWARE.14*/15#include "byte_order.h"1617#ifndef rhash_ctz1819# if _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64) /* if MSVC++ >= 2002 on x86/x64 */20# include <intrin.h>21# pragma intrinsic(_BitScanForward)2223/**24* Returns index of the trailing bit of x.25*26* @param x the number to process27* @return zero-based index of the trailing bit28*/29unsigned rhash_ctz(unsigned x)30{31unsigned long index;32unsigned char isNonzero = _BitScanForward(&index, x); /* MSVC intrinsic */33return (isNonzero ? (unsigned)index : 0);34}35# else /* _MSC_VER >= 1300... */3637/**38* Returns index of the trailing bit of a 32-bit number.39* This is a plain C equivalent for GCC __builtin_ctz() bit scan.40*41* @param x the number to process42* @return zero-based index of the trailing bit43*/44unsigned rhash_ctz(unsigned x)45{46/* array for conversion to bit position */47static unsigned char bit_pos[32] = {480, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,4931, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 950};5152/* The De Bruijn bit-scan was devised in 1997, according to Donald Knuth53* by Martin Lauter. The constant 0x077CB531UL is a De Bruijn sequence,54* which produces a unique pattern of bits into the high 5 bits for each55* possible bit position that it is multiplied against.56* See http://graphics.stanford.edu/~seander/bithacks.html57* and http://chessprogramming.wikispaces.com/BitScan */58return (unsigned)bit_pos[((uint32_t)((x & -x) * 0x077CB531U)) >> 27];59}60# endif /* _MSC_VER >= 1300... */61#endif /* rhash_ctz */6263/**64* Copy a memory block with simultaneous exchanging byte order.65* The byte order is changed from little-endian 32-bit integers66* to big-endian (or vice-versa).67*68* @param to the pointer where to copy memory block69* @param index the index to start writing from70* @param from the source block to copy71* @param length length of the memory block72*/73void rhash_swap_copy_str_to_u32(void* to, int index, const void* from, size_t length)74{75/* if all pointers and length are 32-bits aligned */76if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 3) ) {77/* copy memory as 32-bit words */78const uint32_t* src = (const uint32_t*)from;79const uint32_t* end = (const uint32_t*)((const char*)src + length);80uint32_t* dst = (uint32_t*)((char*)to + index);81for (; src < end; dst++, src++)82*dst = bswap_32(*src);83} else {84const char* src = (const char*)from;85for (length += index; (size_t)index < length; index++)86((char*)to)[index ^ 3] = *(src++);87}88}8990/**91* Copy a memory block with changed byte order.92* The byte order is changed from little-endian 64-bit integers93* to big-endian (or vice-versa).94*95* @param to the pointer where to copy memory block96* @param index the index to start writing from97* @param from the source block to copy98* @param length length of the memory block99*/100void rhash_swap_copy_str_to_u64(void* to, int index, const void* from, size_t length)101{102/* if all pointers and length are 64-bits aligned */103if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 7) ) {104/* copy aligned memory block as 64-bit integers */105const uint64_t* src = (const uint64_t*)from;106const uint64_t* end = (const uint64_t*)((const char*)src + length);107uint64_t* dst = (uint64_t*)((char*)to + index);108while (src < end) *(dst++) = bswap_64( *(src++) );109} else {110const char* src = (const char*)from;111for (length += index; (size_t)index < length; index++) ((char*)to)[index ^ 7] = *(src++);112}113}114115/**116* Copy data from a sequence of 64-bit words to a binary string of given length,117* while changing byte order.118*119* @param to the binary string to receive data120* @param from the source sequence of 64-bit words121* @param length the size in bytes of the data being copied122*/123void rhash_swap_copy_u64_to_str(void* to, const void* from, size_t length)124{125/* if all pointers and length are 64-bits aligned */126if ( 0 == (( (uintptr_t)to | (uintptr_t)from | length ) & 7) ) {127/* copy aligned memory block as 64-bit integers */128const uint64_t* src = (const uint64_t*)from;129const uint64_t* end = (const uint64_t*)((const char*)src + length);130uint64_t* dst = (uint64_t*)to;131while (src < end) *(dst++) = bswap_64( *(src++) );132} else {133size_t index;134char* dst = (char*)to;135for (index = 0; index < length; index++) *(dst++) = ((char*)from)[index ^ 7];136}137}138139/**140* Exchange byte order in the given array of 32-bit integers.141*142* @param arr the array to process143* @param length array length144*/145void rhash_u32_mem_swap(unsigned* arr, int length)146{147unsigned* end = arr + length;148for (; arr < end; arr++) {149*arr = bswap_32(*arr);150}151}152153#ifdef HAS_INTEL_CPUID154#include <cpuid.h>155156static uint64_t get_cpuid_features(void)157{158uint32_t tmp, edx, ecx;159if (__get_cpuid(1, &tmp, &tmp, &ecx, &edx))160return ((((uint64_t)ecx) << 32) ^ edx);161return 0;162}163164int has_cpu_feature(unsigned feature_bit)165{166static uint64_t features;167const uint64_t feature = ((uint64_t)1) << feature_bit;168if (!features)169features = (get_cpuid_features() | 1);170return !!(features & feature);171}172#endif173174175