bitshuffle-generic.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "shuffle-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 of an array of arbitrarily typed elements. */
  40. #define TRANS_ELEM_TYPE(in, out, lda, ldb, type_t) { \
  41. type_t* in_type = (type_t*) in; \
  42. type_t* out_type = (type_t*) out; \
  43. size_t ii, jj, kk; \
  44. for (ii = 0; ii + 7 < lda; ii += 8) { \
  45. for (jj = 0; jj < ldb; jj++) { \
  46. for (kk = 0; kk < 8; kk++) { \
  47. out_type[jj*lda + ii + kk] = \
  48. in_type[ii*ldb + kk * ldb + jj]; \
  49. } \
  50. } \
  51. } \
  52. for (ii = lda - lda % 8; ii < lda; ii ++) { \
  53. for (jj = 0; jj < ldb; jj++) { \
  54. out_type[jj*lda + ii] = in_type[ii*ldb + jj]; \
  55. } \
  56. } \
  57. }
  58. /* Private functions */
  59. BLOSC_NO_EXPORT int64_t
  60. bshuf_trans_byte_elem_remainder(void* in, void* out, const size_t size,
  61. const size_t elem_size, const size_t start);
  62. BLOSC_NO_EXPORT int64_t
  63. bshuf_trans_byte_elem_scal(void* in, void* out, const size_t size,
  64. const size_t elem_size);
  65. BLOSC_NO_EXPORT int64_t
  66. bshuf_trans_bit_byte_remainder(void* in, void* out, const size_t size,
  67. const size_t elem_size, const size_t start_byte);
  68. BLOSC_NO_EXPORT int64_t
  69. bshuf_trans_elem(void* in, void* out, const size_t lda,
  70. const size_t ldb, const size_t elem_size);
  71. BLOSC_NO_EXPORT int64_t
  72. bshuf_trans_bitrow_eight(void* in, void* out, const size_t size,
  73. const size_t elem_size);
  74. BLOSC_NO_EXPORT int64_t
  75. bshuf_shuffle_bit_eightelem_scal(void* in, void* out,
  76. const size_t size, const size_t elem_size);
  77. /* Bitshuffle the data.
  78. *
  79. * Transpose the bits within elements.
  80. *
  81. * Parameters
  82. * ----------
  83. * in : input buffer, must be of size * elem_size bytes
  84. * out : output buffer, must be of size * elem_size bytes
  85. * size : number of elements in input
  86. * elem_size : element size of typed data
  87. * tmp_buffer : temporary buffer with the same `size` than `in` and `out`
  88. *
  89. * Returns
  90. * -------
  91. * nothing -- this cannot fail
  92. *
  93. */
  94. BLOSC_NO_EXPORT int64_t
  95. bshuf_trans_bit_elem_scal(void* in, void* out, const size_t size,
  96. const size_t elem_size, void* tmp_buf);
  97. /* Unshuffle bitshuffled data.
  98. *
  99. * Untranspose the bits within elements.
  100. *
  101. * To properly unshuffle bitshuffled data, *size* and *elem_size* must
  102. * match the parameters used to shuffle the data.
  103. *
  104. * Parameters
  105. * ----------
  106. * in : input buffer, must be of size * elem_size bytes
  107. * out : output buffer, must be of size * elem_size bytes
  108. * size : number of elements in input
  109. * elem_size : element size of typed data
  110. * tmp_buffer : temporary buffer with the same `size` than `in` and `out`
  111. *
  112. * Returns
  113. * -------
  114. * nothing -- this cannot fail
  115. *
  116. */
  117. BLOSC_NO_EXPORT int64_t
  118. bshuf_untrans_bit_elem_scal(void* in, void* out, const size_t size,
  119. const size_t elem_size, void* tmp_buf);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif /* BITSHUFFLE_GENERIC_H */