2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
11 #ifndef VPX_PORTS_MEM_OPS_H_
12 #define VPX_PORTS_MEM_OPS_H_
15 * \brief Provides portable memory access primitives
17 * This function provides portable primitives for getting and setting of
18 * signed and unsigned integers in 16, 24, and 32 bit sizes. The operations
19 * can be performed on unaligned data regardless of hardware support for
22 * The type used to pass the integral values may be changed by defining
23 * MEM_VALUE_T with the appropriate type. The type given must be an integral
26 * The actual functions instantiated have the MEM_VALUE_T type name pasted
27 * on to the symbol name. This allows the developer to instantiate these
28 * operations for multiple types within the same translation unit. This is
29 * of somewhat questionable utility, but the capability exists nonetheless.
30 * Users not making use of this functionality should call the functions
31 * without the type name appended, and the preprocessor will take care of
34 * NOTE: This code is not supported on platforms where char > 1 octet ATM.
38 /* Minimum Access Unit for this target */
39 #define MAU_T unsigned char
43 #define MEM_VALUE_T int
46 #undef MEM_VALUE_T_SZ_BITS
47 #define MEM_VALUE_T_SZ_BITS (sizeof(MEM_VALUE_T) << 3)
49 #undef mem_ops_wrap_symbol
50 #define mem_ops_wrap_symbol(fn) mem_ops_wrap_symbol2(fn, MEM_VALUE_T)
51 #undef mem_ops_wrap_symbol2
52 #define mem_ops_wrap_symbol2(fn,typ) mem_ops_wrap_symbol3(fn,typ)
53 #undef mem_ops_wrap_symbol3
54 #define mem_ops_wrap_symbol3(fn,typ) fn##_as_##typ
57 * Include aligned access routines
59 #define INCLUDED_BY_MEM_OPS_H
60 #include "mem_ops_aligned.h"
61 #undef INCLUDED_BY_MEM_OPS_H
64 #define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16)
65 static unsigned MEM_VALUE_T
mem_get_be16(const void *vmem
) {
66 unsigned MEM_VALUE_T val
;
67 const MAU_T
*mem
= (const MAU_T
*)vmem
;
75 #define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24)
76 static unsigned MEM_VALUE_T
mem_get_be24(const void *vmem
) {
77 unsigned MEM_VALUE_T val
;
78 const MAU_T
*mem
= (const MAU_T
*)vmem
;
87 #define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32)
88 static unsigned MEM_VALUE_T
mem_get_be32(const void *vmem
) {
89 unsigned MEM_VALUE_T val
;
90 const MAU_T
*mem
= (const MAU_T
*)vmem
;
100 #define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16)
101 static unsigned MEM_VALUE_T
mem_get_le16(const void *vmem
) {
102 unsigned MEM_VALUE_T val
;
103 const MAU_T
*mem
= (const MAU_T
*)vmem
;
111 #define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24)
112 static unsigned MEM_VALUE_T
mem_get_le24(const void *vmem
) {
113 unsigned MEM_VALUE_T val
;
114 const MAU_T
*mem
= (const MAU_T
*)vmem
;
123 #define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32)
124 static unsigned MEM_VALUE_T
mem_get_le32(const void *vmem
) {
125 unsigned MEM_VALUE_T val
;
126 const MAU_T
*mem
= (const MAU_T
*)vmem
;
135 #define mem_get_s_generic(end,sz) \
136 static VPX_INLINE signed MEM_VALUE_T mem_get_s##end##sz(const void *vmem) {\
137 const MAU_T *mem = (const MAU_T*)vmem;\
138 signed MEM_VALUE_T val = mem_get_##end##sz(mem);\
139 return (val << (MEM_VALUE_T_SZ_BITS - sz)) >> (MEM_VALUE_T_SZ_BITS - sz);\
143 #define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
144 mem_get_s_generic(be
, 16)
147 #define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
148 mem_get_s_generic(be
, 24)
151 #define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
152 mem_get_s_generic(be
, 32)
155 #define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
156 mem_get_s_generic(le
, 16)
159 #define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
160 mem_get_s_generic(le
, 24)
163 #define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
164 mem_get_s_generic(le
, 32)
167 #define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
168 static VPX_INLINE
void mem_put_be16(void *vmem
, MEM_VALUE_T val
) {
169 MAU_T
*mem
= (MAU_T
*)vmem
;
171 mem
[0] = (MAU_T
)((val
>> 8) & 0xff);
172 mem
[1] = (MAU_T
)((val
>> 0) & 0xff);
176 #define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
177 static VPX_INLINE
void mem_put_be24(void *vmem
, MEM_VALUE_T val
) {
178 MAU_T
*mem
= (MAU_T
*)vmem
;
180 mem
[0] = (MAU_T
)((val
>> 16) & 0xff);
181 mem
[1] = (MAU_T
)((val
>> 8) & 0xff);
182 mem
[2] = (MAU_T
)((val
>> 0) & 0xff);
186 #define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
187 static VPX_INLINE
void mem_put_be32(void *vmem
, MEM_VALUE_T val
) {
188 MAU_T
*mem
= (MAU_T
*)vmem
;
190 mem
[0] = (MAU_T
)((val
>> 24) & 0xff);
191 mem
[1] = (MAU_T
)((val
>> 16) & 0xff);
192 mem
[2] = (MAU_T
)((val
>> 8) & 0xff);
193 mem
[3] = (MAU_T
)((val
>> 0) & 0xff);
197 #define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
198 static VPX_INLINE
void mem_put_le16(void *vmem
, MEM_VALUE_T val
) {
199 MAU_T
*mem
= (MAU_T
*)vmem
;
201 mem
[0] = (MAU_T
)((val
>> 0) & 0xff);
202 mem
[1] = (MAU_T
)((val
>> 8) & 0xff);
206 #define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
207 static VPX_INLINE
void mem_put_le24(void *vmem
, MEM_VALUE_T val
) {
208 MAU_T
*mem
= (MAU_T
*)vmem
;
210 mem
[0] = (MAU_T
)((val
>> 0) & 0xff);
211 mem
[1] = (MAU_T
)((val
>> 8) & 0xff);
212 mem
[2] = (MAU_T
)((val
>> 16) & 0xff);
216 #define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
217 static VPX_INLINE
void mem_put_le32(void *vmem
, MEM_VALUE_T val
) {
218 MAU_T
*mem
= (MAU_T
*)vmem
;
220 mem
[0] = (MAU_T
)((val
>> 0) & 0xff);
221 mem
[1] = (MAU_T
)((val
>> 8) & 0xff);
222 mem
[2] = (MAU_T
)((val
>> 16) & 0xff);
223 mem
[3] = (MAU_T
)((val
>> 24) & 0xff);
226 #endif // VPX_PORTS_MEM_OPS_H_