mem_ops_aligned.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_VPX_PORTS_MEM_OPS_ALIGNED_H_
  11. #define VPX_VPX_PORTS_MEM_OPS_ALIGNED_H_
  12. #include "vpx/vpx_integer.h"
  13. /* \file
  14. * \brief Provides portable memory access primitives for operating on aligned
  15. * data
  16. *
  17. * This file is split from mem_ops.h for easier maintenance. See mem_ops.h
  18. * for a more detailed description of these primitives.
  19. */
  20. #ifndef INCLUDED_BY_MEM_OPS_H
  21. #error Include mem_ops.h, not mem_ops_aligned.h directly.
  22. #endif
  23. /* Architectures that provide instructions for doing this byte swapping
  24. * could redefine these macros.
  25. */
  26. #define swap_endian_16(val, raw) \
  27. do { \
  28. val = (uint16_t)(((raw >> 8) & 0x00ff) | ((raw << 8) & 0xff00)); \
  29. } while (0)
  30. #define swap_endian_32(val, raw) \
  31. do { \
  32. val = ((raw >> 24) & 0x000000ff) | ((raw >> 8) & 0x0000ff00) | \
  33. ((raw << 8) & 0x00ff0000) | ((raw << 24) & 0xff000000); \
  34. } while (0)
  35. #define swap_endian_16_se(val, raw) \
  36. do { \
  37. swap_endian_16(val, raw); \
  38. val = ((val << 16) >> 16); \
  39. } while (0)
  40. #define swap_endian_32_se(val, raw) swap_endian_32(val, raw)
  41. #define mem_get_ne_aligned_generic(end, sz) \
  42. static VPX_INLINE unsigned MEM_VALUE_T mem_get_##end##sz##_aligned( \
  43. const void *vmem) { \
  44. const uint##sz##_t *mem = (const uint##sz##_t *)vmem; \
  45. return *mem; \
  46. }
  47. #define mem_get_sne_aligned_generic(end, sz) \
  48. static VPX_INLINE signed MEM_VALUE_T mem_get_s##end##sz##_aligned( \
  49. const void *vmem) { \
  50. const int##sz##_t *mem = (const int##sz##_t *)vmem; \
  51. return *mem; \
  52. }
  53. #define mem_get_se_aligned_generic(end, sz) \
  54. static VPX_INLINE unsigned MEM_VALUE_T mem_get_##end##sz##_aligned( \
  55. const void *vmem) { \
  56. const uint##sz##_t *mem = (const uint##sz##_t *)vmem; \
  57. unsigned MEM_VALUE_T val, raw = *mem; \
  58. swap_endian_##sz(val, raw); \
  59. return val; \
  60. }
  61. #define mem_get_sse_aligned_generic(end, sz) \
  62. static VPX_INLINE signed MEM_VALUE_T mem_get_s##end##sz##_aligned( \
  63. const void *vmem) { \
  64. const int##sz##_t *mem = (const int##sz##_t *)vmem; \
  65. unsigned MEM_VALUE_T val, raw = *mem; \
  66. swap_endian_##sz##_se(val, raw); \
  67. return val; \
  68. }
  69. #define mem_put_ne_aligned_generic(end, sz) \
  70. static VPX_INLINE void mem_put_##end##sz##_aligned(void *vmem, \
  71. MEM_VALUE_T val) { \
  72. uint##sz##_t *mem = (uint##sz##_t *)vmem; \
  73. *mem = (uint##sz##_t)val; \
  74. }
  75. #define mem_put_se_aligned_generic(end, sz) \
  76. static VPX_INLINE void mem_put_##end##sz##_aligned(void *vmem, \
  77. MEM_VALUE_T val) { \
  78. uint##sz##_t *mem = (uint##sz##_t *)vmem, raw; \
  79. swap_endian_##sz(raw, val); \
  80. *mem = (uint##sz##_t)raw; \
  81. }
  82. #include "vpx_config.h"
  83. #if CONFIG_BIG_ENDIAN
  84. #define mem_get_be_aligned_generic(sz) mem_get_ne_aligned_generic(be, sz)
  85. #define mem_get_sbe_aligned_generic(sz) mem_get_sne_aligned_generic(be, sz)
  86. #define mem_get_le_aligned_generic(sz) mem_get_se_aligned_generic(le, sz)
  87. #define mem_get_sle_aligned_generic(sz) mem_get_sse_aligned_generic(le, sz)
  88. #define mem_put_be_aligned_generic(sz) mem_put_ne_aligned_generic(be, sz)
  89. #define mem_put_le_aligned_generic(sz) mem_put_se_aligned_generic(le, sz)
  90. #else
  91. #define mem_get_be_aligned_generic(sz) mem_get_se_aligned_generic(be, sz)
  92. #define mem_get_sbe_aligned_generic(sz) mem_get_sse_aligned_generic(be, sz)
  93. #define mem_get_le_aligned_generic(sz) mem_get_ne_aligned_generic(le, sz)
  94. #define mem_get_sle_aligned_generic(sz) mem_get_sne_aligned_generic(le, sz)
  95. #define mem_put_be_aligned_generic(sz) mem_put_se_aligned_generic(be, sz)
  96. #define mem_put_le_aligned_generic(sz) mem_put_ne_aligned_generic(le, sz)
  97. #endif
  98. /* clang-format off */
  99. #undef mem_get_be16_aligned
  100. #define mem_get_be16_aligned mem_ops_wrap_symbol(mem_get_be16_aligned)
  101. mem_get_be_aligned_generic(16)
  102. #undef mem_get_be32_aligned
  103. #define mem_get_be32_aligned mem_ops_wrap_symbol(mem_get_be32_aligned)
  104. mem_get_be_aligned_generic(32)
  105. #undef mem_get_le16_aligned
  106. #define mem_get_le16_aligned mem_ops_wrap_symbol(mem_get_le16_aligned)
  107. mem_get_le_aligned_generic(16)
  108. #undef mem_get_le32_aligned
  109. #define mem_get_le32_aligned mem_ops_wrap_symbol(mem_get_le32_aligned)
  110. mem_get_le_aligned_generic(32)
  111. #undef mem_get_sbe16_aligned
  112. #define mem_get_sbe16_aligned mem_ops_wrap_symbol(mem_get_sbe16_aligned)
  113. mem_get_sbe_aligned_generic(16)
  114. #undef mem_get_sbe32_aligned
  115. #define mem_get_sbe32_aligned mem_ops_wrap_symbol(mem_get_sbe32_aligned)
  116. mem_get_sbe_aligned_generic(32)
  117. #undef mem_get_sle16_aligned
  118. #define mem_get_sle16_aligned mem_ops_wrap_symbol(mem_get_sle16_aligned)
  119. mem_get_sle_aligned_generic(16)
  120. #undef mem_get_sle32_aligned
  121. #define mem_get_sle32_aligned mem_ops_wrap_symbol(mem_get_sle32_aligned)
  122. mem_get_sle_aligned_generic(32)
  123. #undef mem_put_be16_aligned
  124. #define mem_put_be16_aligned mem_ops_wrap_symbol(mem_put_be16_aligned)
  125. mem_put_be_aligned_generic(16)
  126. #undef mem_put_be32_aligned
  127. #define mem_put_be32_aligned mem_ops_wrap_symbol(mem_put_be32_aligned)
  128. mem_put_be_aligned_generic(32)
  129. #undef mem_put_le16_aligned
  130. #define mem_put_le16_aligned mem_ops_wrap_symbol(mem_put_le16_aligned)
  131. mem_put_le_aligned_generic(16)
  132. #undef mem_put_le32_aligned
  133. #define mem_put_le32_aligned mem_ops_wrap_symbol(mem_put_le32_aligned)
  134. mem_put_le_aligned_generic(32)
  135. #undef mem_get_ne_aligned_generic
  136. #undef mem_get_se_aligned_generic
  137. #undef mem_get_sne_aligned_generic
  138. #undef mem_get_sse_aligned_generic
  139. #undef mem_put_ne_aligned_generic
  140. #undef mem_put_se_aligned_generic
  141. #undef swap_endian_16
  142. #undef swap_endian_32
  143. #undef swap_endian_16_se
  144. #undef swap_endian_32_se
  145. /* clang-format on */
  146. #endif // VPX_VPX_PORTS_MEM_OPS_ALIGNED_H_