byte_order.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* byte_order.c - byte order related platform dependent routines,
  2. *
  3. * Copyright (c) 2008, Aleksey Kravchenko <[email protected]>
  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. #ifndef rhash_ctz
  18. # if _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64) /* if MSVC++ >= 2002 on x86/x64 */
  19. # include <intrin.h>
  20. # pragma intrinsic(_BitScanForward)
  21. /**
  22. * Returns index of the trailing bit of x.
  23. *
  24. * @param x the number to process
  25. * @return zero-based index of the trailing bit
  26. */
  27. unsigned rhash_ctz(unsigned x)
  28. {
  29. unsigned long index;
  30. unsigned char isNonzero = _BitScanForward(&index, x); /* MSVC intrinsic */
  31. return (isNonzero ? (unsigned)index : 0);
  32. }
  33. # else /* _MSC_VER >= 1300... */
  34. /**
  35. * Returns index of the trailing bit of a 32-bit number.
  36. * This is a plain C equivalent for GCC __builtin_ctz() bit scan.
  37. *
  38. * @param x the number to process
  39. * @return zero-based index of the trailing bit
  40. */
  41. unsigned rhash_ctz(unsigned x)
  42. {
  43. /* array for conversion to bit position */
  44. static unsigned char bit_pos[32] = {
  45. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  46. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  47. };
  48. /* The De Bruijn bit-scan was devised in 1997, according to Donald Knuth
  49. * by Martin Lauter. The constant 0x077CB531UL is a De Bruijn sequence,
  50. * which produces a unique pattern of bits into the high 5 bits for each
  51. * possible bit position that it is multiplied against.
  52. * See http://graphics.stanford.edu/~seander/bithacks.html
  53. * and http://chessprogramming.wikispaces.com/BitScan */
  54. return (unsigned)bit_pos[((uint32_t)((x & -x) * 0x077CB531U)) >> 27];
  55. }
  56. # endif /* _MSC_VER >= 1300... */
  57. #endif /* rhash_ctz */
  58. /**
  59. * Copy a memory block with simultaneous exchanging byte order.
  60. * The byte order is changed from little-endian 32-bit integers
  61. * to big-endian (or vice-versa).
  62. *
  63. * @param to the pointer where to copy memory block
  64. * @param index the index to start writing from
  65. * @param from the source block to copy
  66. * @param length length of the memory block
  67. */
  68. void rhash_swap_copy_str_to_u32(void* to, int index, const void* from, size_t length)
  69. {
  70. /* if all pointers and length are 32-bits aligned */
  71. if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 3) ) {
  72. /* copy memory as 32-bit words */
  73. const uint32_t* src = (const uint32_t*)from;
  74. const uint32_t* end = (const uint32_t*)((const char*)src + length);
  75. uint32_t* dst = (uint32_t*)((char*)to + index);
  76. for (; src < end; dst++, src++)
  77. *dst = bswap_32(*src);
  78. } else {
  79. const char* src = (const char*)from;
  80. for (length += index; (size_t)index < length; index++)
  81. ((char*)to)[index ^ 3] = *(src++);
  82. }
  83. }
  84. /**
  85. * Copy a memory block with changed byte order.
  86. * The byte order is changed from little-endian 64-bit integers
  87. * to big-endian (or vice-versa).
  88. *
  89. * @param to the pointer where to copy memory block
  90. * @param index the index to start writing from
  91. * @param from the source block to copy
  92. * @param length length of the memory block
  93. */
  94. void rhash_swap_copy_str_to_u64(void* to, int index, const void* from, size_t length)
  95. {
  96. /* if all pointers and length are 64-bits aligned */
  97. if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 7) ) {
  98. /* copy aligned memory block as 64-bit integers */
  99. const uint64_t* src = (const uint64_t*)from;
  100. const uint64_t* end = (const uint64_t*)((const char*)src + length);
  101. uint64_t* dst = (uint64_t*)((char*)to + index);
  102. while (src < end) *(dst++) = bswap_64( *(src++) );
  103. } else {
  104. const char* src = (const char*)from;
  105. for (length += index; (size_t)index < length; index++) ((char*)to)[index ^ 7] = *(src++);
  106. }
  107. }
  108. /**
  109. * Copy data from a sequence of 64-bit words to a binary string of given length,
  110. * while changing byte order.
  111. *
  112. * @param to the binary string to receive data
  113. * @param from the source sequence of 64-bit words
  114. * @param length the size in bytes of the data being copied
  115. */
  116. void rhash_swap_copy_u64_to_str(void* to, const void* from, size_t length)
  117. {
  118. /* if all pointers and length are 64-bits aligned */
  119. if ( 0 == (( (uintptr_t)to | (uintptr_t)from | length ) & 7) ) {
  120. /* copy aligned memory block as 64-bit integers */
  121. const uint64_t* src = (const uint64_t*)from;
  122. const uint64_t* end = (const uint64_t*)((const char*)src + length);
  123. uint64_t* dst = (uint64_t*)to;
  124. while (src < end) *(dst++) = bswap_64( *(src++) );
  125. } else {
  126. size_t index;
  127. char* dst = (char*)to;
  128. for (index = 0; index < length; index++) *(dst++) = ((char*)from)[index ^ 7];
  129. }
  130. }
  131. /**
  132. * Exchange byte order in the given array of 32-bit integers.
  133. *
  134. * @param arr the array to process
  135. * @param length array length
  136. */
  137. void rhash_u32_mem_swap(unsigned* arr, int length)
  138. {
  139. unsigned* end = arr + length;
  140. for (; arr < end; arr++) {
  141. *arr = bswap_32(*arr);
  142. }
  143. }
  144. #ifdef HAS_INTEL_CPUID
  145. #include <cpuid.h>
  146. static uint64_t get_cpuid_features(void)
  147. {
  148. uint32_t tmp, edx, ecx;
  149. if (__get_cpuid(1, &tmp, &tmp, &ecx, &edx))
  150. return ((((uint64_t)ecx) << 32) ^ edx);
  151. return 0;
  152. }
  153. int has_cpu_feature(unsigned feature_bit)
  154. {
  155. static uint64_t features;
  156. const uint64_t feature = ((uint64_t)1) << feature_bit;
  157. if (!features)
  158. features = (get_cpuid_features() | 1);
  159. return !!(features & feature);
  160. }
  161. #endif