bitshuffle-generic.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Author: Francesc Alted <francesc@blosc.org>
  4. See LICENSES/BLOSC.txt for details about copyright and rights to use.
  5. **********************************************************************/
  6. /* Generic (non-hardware-accelerated) shuffle/unshuffle routines.
  7. These are used when hardware-accelerated functions aren't available
  8. for a particular platform; they are also used by the hardware-
  9. accelerated functions to handle any remaining elements in a block
  10. which isn't a multiple of the hardware's vector size. */
  11. #ifndef BITSHUFFLE_GENERIC_H
  12. #define BITSHUFFLE_GENERIC_H
  13. #include "blosc-common.h"
  14. #include <stdlib.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /* Macros. */
  19. #define CHECK_MULT_EIGHT(n) if (n % 8) return -80;
  20. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  21. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  22. #define CHECK_ERR(count) if (count < 0) { return count; }
  23. /* ---- Worker code not requiring special instruction sets. ----
  24. *
  25. * The following code does not use any x86 specific vectorized instructions
  26. * and should compile on any machine
  27. *
  28. */
  29. /* Transpose 8x8 bit array packed into a single quadword *x*.
  30. * *t* is workspace. */
  31. #define TRANS_BIT_8X8(x, t) { \
  32. t = (x ^ (x >> 7)) & 0x00AA00AA00AA00AALL; \
  33. x = x ^ t ^ (t << 7); \
  34. t = (x ^ (x >> 14)) & 0x0000CCCC0000CCCCLL; \
  35. x = x ^ t ^ (t << 14); \
  36. t = (x ^ (x >> 28)) & 0x00000000F0F0F0F0LL; \
  37. x = x ^ t ^ (t << 28); \
  38. }
  39. /* Transpose 8x8 bit array along the diagonal from upper right
  40. to lower left */
  41. #define TRANS_BIT_8X8_BE(x, t) { \
  42. t = (x ^ (x >> 9)) & 0x0055005500550055LL; \
  43. x = x ^ t ^ (t << 9); \
  44. t = (x ^ (x >> 18)) & 0x0000333300003333LL; \
  45. x = x ^ t ^ (t << 18); \
  46. t = (x ^ (x >> 36)) & 0x000000000F0F0F0FLL; \
  47. x = x ^ t ^ (t << 36); \
  48. }
  49. /* Transpose of an array of arbitrarily typed elements. */
  50. #define TRANS_ELEM_TYPE(in, out, lda, ldb, type_t) { \
  51. type_t* in_type = (type_t*) in; \
  52. type_t* out_type = (type_t*) out; \
  53. size_t ii, jj, kk; \
  54. for (ii = 0; ii + 7 < lda; ii += 8) { \
  55. for (jj = 0; jj < ldb; jj++) { \
  56. for (kk = 0; kk < 8; kk++) { \
  57. out_type[jj*lda + ii + kk] = \
  58. in_type[ii*ldb + kk * ldb + jj]; \
  59. } \
  60. } \
  61. } \
  62. for (ii = lda - lda % 8; ii < lda; ii ++) { \
  63. for (jj = 0; jj < ldb; jj++) { \
  64. out_type[jj*lda + ii] = in_type[ii*ldb + jj]; \
  65. } \
  66. } \
  67. }
  68. /* Private functions */
  69. BLOSC_NO_EXPORT int64_t
  70. bshuf_trans_byte_elem_remainder(const void* in, void* out, const size_t size,
  71. const size_t elem_size, const size_t start);
  72. BLOSC_NO_EXPORT int64_t
  73. bshuf_trans_byte_elem_scal(const void* in, void* out, const size_t size,
  74. const size_t elem_size);
  75. BLOSC_NO_EXPORT int64_t
  76. bshuf_trans_bit_byte_remainder(const void* in, void* out, const size_t size,
  77. const size_t elem_size, const size_t start_byte);
  78. BLOSC_NO_EXPORT int64_t
  79. bshuf_trans_elem(const void* in, void* out, const size_t lda,
  80. const size_t ldb, const size_t elem_size);
  81. BLOSC_NO_EXPORT int64_t
  82. bshuf_trans_bitrow_eight(const void* in, void* out, const size_t size,
  83. const size_t elem_size);
  84. BLOSC_NO_EXPORT int64_t
  85. bshuf_shuffle_bit_eightelem_scal(const void* in, void* out,
  86. const size_t size, const size_t elem_size);
  87. /* Bitshuffle the data.
  88. *
  89. * Transpose the bits within elements.
  90. *
  91. * Parameters
  92. * ----------
  93. * in : input buffer, must be of size * elem_size bytes
  94. * out : output buffer, must be of size * elem_size bytes
  95. * size : number of elements in input
  96. * elem_size : element size of typed data
  97. * tmp_buffer : temporary buffer with the same `size` than `in` and `out`
  98. *
  99. * Returns
  100. * -------
  101. * nothing -- this cannot fail
  102. *
  103. */
  104. BLOSC_NO_EXPORT int64_t
  105. bshuf_trans_bit_elem_scal(const void* in, void* out, const size_t size,
  106. const size_t elem_size, void* tmp_buf);
  107. /* Unshuffle bitshuffled data.
  108. *
  109. * Untranspose the bits within elements.
  110. *
  111. * To properly unshuffle bitshuffled data, *size* and *elem_size* must
  112. * match the parameters used to shuffle the data.
  113. *
  114. * Parameters
  115. * ----------
  116. * in : input buffer, must be of size * elem_size bytes
  117. * out : output buffer, must be of size * elem_size bytes
  118. * size : number of elements in input
  119. * elem_size : element size of typed data
  120. * tmp_buffer : temporary buffer with the same `size` than `in` and `out`
  121. *
  122. * Returns
  123. * -------
  124. * nothing -- this cannot fail
  125. *
  126. */
  127. BLOSC_NO_EXPORT int64_t
  128. bshuf_untrans_bit_elem_scal(const void* in, void* out, const size_t size,
  129. const size_t elem_size, void* tmp_buf);
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #endif /* BITSHUFFLE_GENERIC_H */