mem_ops.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_H_
  11. #define VPX_VPX_PORTS_MEM_OPS_H_
  12. /* \file
  13. * \brief Provides portable memory access primitives
  14. *
  15. * This function provides portable primitives for getting and setting of
  16. * signed and unsigned integers in 16, 24, and 32 bit sizes. The operations
  17. * can be performed on unaligned data regardless of hardware support for
  18. * unaligned accesses.
  19. *
  20. * The type used to pass the integral values may be changed by defining
  21. * MEM_VALUE_T with the appropriate type. The type given must be an integral
  22. * numeric type.
  23. *
  24. * The actual functions instantiated have the MEM_VALUE_T type name pasted
  25. * on to the symbol name. This allows the developer to instantiate these
  26. * operations for multiple types within the same translation unit. This is
  27. * of somewhat questionable utility, but the capability exists nonetheless.
  28. * Users not making use of this functionality should call the functions
  29. * without the type name appended, and the preprocessor will take care of
  30. * it.
  31. *
  32. * NOTE: This code is not supported on platforms where char > 1 octet ATM.
  33. */
  34. #ifndef MAU_T
  35. /* Minimum Access Unit for this target */
  36. #define MAU_T unsigned char
  37. #endif
  38. #ifndef MEM_VALUE_T
  39. #define MEM_VALUE_T int
  40. #endif
  41. #undef MEM_VALUE_T_SZ_BITS
  42. #define MEM_VALUE_T_SZ_BITS (sizeof(MEM_VALUE_T) << 3)
  43. #undef mem_ops_wrap_symbol
  44. #define mem_ops_wrap_symbol(fn) mem_ops_wrap_symbol2(fn, MEM_VALUE_T)
  45. #undef mem_ops_wrap_symbol2
  46. #define mem_ops_wrap_symbol2(fn, typ) mem_ops_wrap_symbol3(fn, typ)
  47. #undef mem_ops_wrap_symbol3
  48. #define mem_ops_wrap_symbol3(fn, typ) fn##_as_##typ
  49. /*
  50. * Include aligned access routines
  51. */
  52. #define INCLUDED_BY_MEM_OPS_H
  53. #include "mem_ops_aligned.h"
  54. #undef INCLUDED_BY_MEM_OPS_H
  55. #undef mem_get_be16
  56. #define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16)
  57. static unsigned MEM_VALUE_T mem_get_be16(const void *vmem) {
  58. unsigned MEM_VALUE_T val;
  59. const MAU_T *mem = (const MAU_T *)vmem;
  60. val = mem[0] << 8;
  61. val |= mem[1];
  62. return val;
  63. }
  64. #undef mem_get_be24
  65. #define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24)
  66. static unsigned MEM_VALUE_T mem_get_be24(const void *vmem) {
  67. unsigned MEM_VALUE_T val;
  68. const MAU_T *mem = (const MAU_T *)vmem;
  69. val = mem[0] << 16;
  70. val |= mem[1] << 8;
  71. val |= mem[2];
  72. return val;
  73. }
  74. #undef mem_get_be32
  75. #define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32)
  76. static unsigned MEM_VALUE_T mem_get_be32(const void *vmem) {
  77. unsigned MEM_VALUE_T val;
  78. const MAU_T *mem = (const MAU_T *)vmem;
  79. val = ((unsigned MEM_VALUE_T)mem[0]) << 24;
  80. val |= mem[1] << 16;
  81. val |= mem[2] << 8;
  82. val |= mem[3];
  83. return val;
  84. }
  85. #undef mem_get_le16
  86. #define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16)
  87. static unsigned MEM_VALUE_T mem_get_le16(const void *vmem) {
  88. unsigned MEM_VALUE_T val;
  89. const MAU_T *mem = (const MAU_T *)vmem;
  90. val = mem[1] << 8;
  91. val |= mem[0];
  92. return val;
  93. }
  94. #undef mem_get_le24
  95. #define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24)
  96. static unsigned MEM_VALUE_T mem_get_le24(const void *vmem) {
  97. unsigned MEM_VALUE_T val;
  98. const MAU_T *mem = (const MAU_T *)vmem;
  99. val = mem[2] << 16;
  100. val |= mem[1] << 8;
  101. val |= mem[0];
  102. return val;
  103. }
  104. #undef mem_get_le32
  105. #define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32)
  106. static unsigned MEM_VALUE_T mem_get_le32(const void *vmem) {
  107. unsigned MEM_VALUE_T val;
  108. const MAU_T *mem = (const MAU_T *)vmem;
  109. val = ((unsigned MEM_VALUE_T)mem[3]) << 24;
  110. val |= mem[2] << 16;
  111. val |= mem[1] << 8;
  112. val |= mem[0];
  113. return val;
  114. }
  115. #define mem_get_s_generic(end, sz) \
  116. static VPX_INLINE signed MEM_VALUE_T mem_get_s##end##sz(const void *vmem) { \
  117. const MAU_T *mem = (const MAU_T *)vmem; \
  118. signed MEM_VALUE_T val = mem_get_##end##sz(mem); \
  119. return (val << (MEM_VALUE_T_SZ_BITS - sz)) >> (MEM_VALUE_T_SZ_BITS - sz); \
  120. }
  121. /* clang-format off */
  122. #undef mem_get_sbe16
  123. #define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
  124. mem_get_s_generic(be, 16)
  125. #undef mem_get_sbe24
  126. #define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
  127. mem_get_s_generic(be, 24)
  128. #undef mem_get_sbe32
  129. #define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
  130. mem_get_s_generic(be, 32)
  131. #undef mem_get_sle16
  132. #define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
  133. mem_get_s_generic(le, 16)
  134. #undef mem_get_sle24
  135. #define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
  136. mem_get_s_generic(le, 24)
  137. #undef mem_get_sle32
  138. #define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
  139. mem_get_s_generic(le, 32)
  140. #undef mem_put_be16
  141. #define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
  142. static VPX_INLINE void mem_put_be16(void *vmem, MEM_VALUE_T val) {
  143. MAU_T *mem = (MAU_T *)vmem;
  144. mem[0] = (MAU_T)((val >> 8) & 0xff);
  145. mem[1] = (MAU_T)((val >> 0) & 0xff);
  146. }
  147. #undef mem_put_be24
  148. #define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
  149. static VPX_INLINE void mem_put_be24(void *vmem, MEM_VALUE_T val) {
  150. MAU_T *mem = (MAU_T *)vmem;
  151. mem[0] = (MAU_T)((val >> 16) & 0xff);
  152. mem[1] = (MAU_T)((val >> 8) & 0xff);
  153. mem[2] = (MAU_T)((val >> 0) & 0xff);
  154. }
  155. #undef mem_put_be32
  156. #define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
  157. static VPX_INLINE void mem_put_be32(void *vmem, MEM_VALUE_T val) {
  158. MAU_T *mem = (MAU_T *)vmem;
  159. mem[0] = (MAU_T)((val >> 24) & 0xff);
  160. mem[1] = (MAU_T)((val >> 16) & 0xff);
  161. mem[2] = (MAU_T)((val >> 8) & 0xff);
  162. mem[3] = (MAU_T)((val >> 0) & 0xff);
  163. }
  164. #undef mem_put_le16
  165. #define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
  166. static VPX_INLINE void mem_put_le16(void *vmem, MEM_VALUE_T val) {
  167. MAU_T *mem = (MAU_T *)vmem;
  168. mem[0] = (MAU_T)((val >> 0) & 0xff);
  169. mem[1] = (MAU_T)((val >> 8) & 0xff);
  170. }
  171. #undef mem_put_le24
  172. #define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
  173. static VPX_INLINE void mem_put_le24(void *vmem, MEM_VALUE_T val) {
  174. MAU_T *mem = (MAU_T *)vmem;
  175. mem[0] = (MAU_T)((val >> 0) & 0xff);
  176. mem[1] = (MAU_T)((val >> 8) & 0xff);
  177. mem[2] = (MAU_T)((val >> 16) & 0xff);
  178. }
  179. #undef mem_put_le32
  180. #define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
  181. static VPX_INLINE void mem_put_le32(void *vmem, MEM_VALUE_T val) {
  182. MAU_T *mem = (MAU_T *)vmem;
  183. mem[0] = (MAU_T)((val >> 0) & 0xff);
  184. mem[1] = (MAU_T)((val >> 8) & 0xff);
  185. mem[2] = (MAU_T)((val >> 16) & 0xff);
  186. mem[3] = (MAU_T)((val >> 24) & 0xff);
  187. }
  188. /* clang-format on */
  189. #endif // VPX_VPX_PORTS_MEM_OPS_H_