byte_order.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* byte_order.c - byte order related platform dependent routines,
  2. *
  3. * Copyright: 2008-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
  15. */
  16. #include "byte_order.h"
  17. #if !(__GNUC__ >= 4 || (__GNUC__ ==3 && __GNUC_MINOR__ >= 4)) /* if !GCC or GCC < 4.3 */
  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 /* !(GCC >= 4.3) */
  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 == (( (int)((char*)to - (char*)0) | ((char*)from - (char*)0) | 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. while (src < end) *(dst++) = bswap_32( *(src++) );
  77. } else {
  78. const char* src = (const char*)from;
  79. for (length += index; (size_t)index < length; index++) ((char*)to)[index ^ 3] = *(src++);
  80. }
  81. }
  82. /**
  83. * Copy a memory block with changed byte order.
  84. * The byte order is changed from little-endian 64-bit integers
  85. * to big-endian (or vice-versa).
  86. *
  87. * @param to the pointer where to copy memory block
  88. * @param index the index to start writing from
  89. * @param from the source block to copy
  90. * @param length length of the memory block
  91. */
  92. void rhash_swap_copy_str_to_u64(void* to, int index, const void* from, size_t length)
  93. {
  94. /* if all pointers and length are 64-bits aligned */
  95. if ( 0 == (( (int)((char*)to - (char*)0) | ((char*)from - (char*)0) | index | length ) & 7) ) {
  96. /* copy aligned memory block as 64-bit integers */
  97. const uint64_t* src = (const uint64_t*)from;
  98. const uint64_t* end = (const uint64_t*)((const char*)src + length);
  99. uint64_t* dst = (uint64_t*)((char*)to + index);
  100. while (src < end) *(dst++) = bswap_64( *(src++) );
  101. } else {
  102. const char* src = (const char*)from;
  103. for (length += index; (size_t)index < length; index++) ((char*)to)[index ^ 7] = *(src++);
  104. }
  105. }
  106. /**
  107. * Copy data from a sequence of 64-bit words to a binary string of given length,
  108. * while changing byte order.
  109. *
  110. * @param to the binary string to receive data
  111. * @param from the source sequence of 64-bit words
  112. * @param length the size in bytes of the data being copied
  113. */
  114. void rhash_swap_copy_u64_to_str(void* to, const void* from, size_t length)
  115. {
  116. /* if all pointers and length are 64-bits aligned */
  117. if ( 0 == (( (int)((char*)to - (char*)0) | ((char*)from - (char*)0) | length ) & 7) ) {
  118. /* copy aligned memory block as 64-bit integers */
  119. const uint64_t* src = (const uint64_t*)from;
  120. const uint64_t* end = (const uint64_t*)((const char*)src + length);
  121. uint64_t* dst = (uint64_t*)to;
  122. while (src < end) *(dst++) = bswap_64( *(src++) );
  123. } else {
  124. size_t index;
  125. char* dst = (char*)to;
  126. for (index = 0; index < length; index++) *(dst++) = ((char*)from)[index ^ 7];
  127. }
  128. }
  129. /**
  130. * Exchange byte order in the given array of 32-bit integers.
  131. *
  132. * @param arr the array to process
  133. * @param length array length
  134. */
  135. void rhash_u32_mem_swap(unsigned *arr, int length)
  136. {
  137. unsigned* end = arr + length;
  138. for (; arr < end; arr++) {
  139. *arr = bswap_32(*arr);
  140. }
  141. }