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
11 #ifndef VPX_PORTS_MEM_OPS_H_
12 #define VPX_PORTS_MEM_OPS_H_
13
14 /* \file
15 * \brief Provides portable memory access primitives
16 *
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
20 * unaligned accesses.
21 *
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
24 * numeric type.
25 *
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
32 * it.
33 *
34 * NOTE: This code is not supported on platforms where char > 1 octet ATM.
35 */
36
37 #ifndef MAU_T
38 /* Minimum Access Unit for this target */
39 #define MAU_T unsigned char
40 #endif
41
42 #ifndef MEM_VALUE_T
43 #define MEM_VALUE_T int
44 #endif
45
46 #undef MEM_VALUE_T_SZ_BITS
47 #define MEM_VALUE_T_SZ_BITS (sizeof(MEM_VALUE_T) << 3)
48
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
55
56 /*
57 * Include aligned access routines
58 */
59 #define INCLUDED_BY_MEM_OPS_H
60 #include "mem_ops_aligned.h"
61 #undef INCLUDED_BY_MEM_OPS_H
62
63 #undef mem_get_be16
64 #define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16)
mem_get_be16(const void * vmem)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;
68
69 val = mem[0] << 8;
70 val |= mem[1];
71 return val;
72 }
73
74 #undef mem_get_be24
75 #define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24)
mem_get_be24(const void * vmem)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;
79
80 val = mem[0] << 16;
81 val |= mem[1] << 8;
82 val |= mem[2];
83 return val;
84 }
85
86 #undef mem_get_be32
87 #define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32)
mem_get_be32(const void * vmem)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;
91
92 val = mem[0] << 24;
93 val |= mem[1] << 16;
94 val |= mem[2] << 8;
95 val |= mem[3];
96 return val;
97 }
98
99 #undef mem_get_le16
100 #define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16)
mem_get_le16(const void * vmem)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;
104
105 val = mem[1] << 8;
106 val |= mem[0];
107 return val;
108 }
109
110 #undef mem_get_le24
111 #define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24)
mem_get_le24(const void * vmem)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;
115
116 val = mem[2] << 16;
117 val |= mem[1] << 8;
118 val |= mem[0];
119 return val;
120 }
121
122 #undef mem_get_le32
123 #define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32)
mem_get_le32(const void * vmem)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;
127
128 val = mem[3] << 24;
129 val |= mem[2] << 16;
130 val |= mem[1] << 8;
131 val |= mem[0];
132 return val;
133 }
134
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);\
140 }
141
142 #undef mem_get_sbe16
143 #define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
144 mem_get_s_generic(be, 16)
145
146 #undef mem_get_sbe24
147 #define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
148 mem_get_s_generic(be, 24)
149
150 #undef mem_get_sbe32
151 #define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
152 mem_get_s_generic(be, 32)
153
154 #undef mem_get_sle16
155 #define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
156 mem_get_s_generic(le, 16)
157
158 #undef mem_get_sle24
159 #define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
160 mem_get_s_generic(le, 24)
161
162 #undef mem_get_sle32
163 #define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
164 mem_get_s_generic(le, 32)
165
166 #undef mem_put_be16
167 #define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
mem_put_be16(void * vmem,MEM_VALUE_T val)168 static VPX_INLINE void mem_put_be16(void *vmem, MEM_VALUE_T val) {
169 MAU_T *mem = (MAU_T *)vmem;
170
171 mem[0] = (val >> 8) & 0xff;
172 mem[1] = (val >> 0) & 0xff;
173 }
174
175 #undef mem_put_be24
176 #define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
mem_put_be24(void * vmem,MEM_VALUE_T val)177 static VPX_INLINE void mem_put_be24(void *vmem, MEM_VALUE_T val) {
178 MAU_T *mem = (MAU_T *)vmem;
179
180 mem[0] = (val >> 16) & 0xff;
181 mem[1] = (val >> 8) & 0xff;
182 mem[2] = (val >> 0) & 0xff;
183 }
184
185 #undef mem_put_be32
186 #define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
mem_put_be32(void * vmem,MEM_VALUE_T val)187 static VPX_INLINE void mem_put_be32(void *vmem, MEM_VALUE_T val) {
188 MAU_T *mem = (MAU_T *)vmem;
189
190 mem[0] = (val >> 24) & 0xff;
191 mem[1] = (val >> 16) & 0xff;
192 mem[2] = (val >> 8) & 0xff;
193 mem[3] = (val >> 0) & 0xff;
194 }
195
196 #undef mem_put_le16
197 #define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
mem_put_le16(void * vmem,MEM_VALUE_T val)198 static VPX_INLINE void mem_put_le16(void *vmem, MEM_VALUE_T val) {
199 MAU_T *mem = (MAU_T *)vmem;
200
201 mem[0] = (val >> 0) & 0xff;
202 mem[1] = (val >> 8) & 0xff;
203 }
204
205 #undef mem_put_le24
206 #define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
mem_put_le24(void * vmem,MEM_VALUE_T val)207 static VPX_INLINE void mem_put_le24(void *vmem, MEM_VALUE_T val) {
208 MAU_T *mem = (MAU_T *)vmem;
209
210 mem[0] = (val >> 0) & 0xff;
211 mem[1] = (val >> 8) & 0xff;
212 mem[2] = (val >> 16) & 0xff;
213 }
214
215 #undef mem_put_le32
216 #define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
mem_put_le32(void * vmem,MEM_VALUE_T val)217 static VPX_INLINE void mem_put_le32(void *vmem, MEM_VALUE_T val) {
218 MAU_T *mem = (MAU_T *)vmem;
219
220 mem[0] = (val >> 0) & 0xff;
221 mem[1] = (val >> 8) & 0xff;
222 mem[2] = (val >> 16) & 0xff;
223 mem[3] = (val >> 24) & 0xff;
224 }
225
226 #endif // VPX_PORTS_MEM_OPS_H_
227