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_VPX_PORTS_MEM_OPS_H_
12 #define VPX_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 = ((unsigned MEM_VALUE_T)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 = ((unsigned MEM_VALUE_T)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 /* clang-format off */
143 #undef mem_get_sbe16
144 #define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
145 mem_get_s_generic(be, 16)
146
147 #undef mem_get_sbe24
148 #define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
149 mem_get_s_generic(be, 24)
150
151 #undef mem_get_sbe32
152 #define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
153 mem_get_s_generic(be, 32)
154
155 #undef mem_get_sle16
156 #define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
157 mem_get_s_generic(le, 16)
158
159 #undef mem_get_sle24
160 #define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
161 mem_get_s_generic(le, 24)
162
163 #undef mem_get_sle32
164 #define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
165 mem_get_s_generic(le, 32)
166
167 #undef mem_put_be16
168 #define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
mem_put_be16(void * vmem,MEM_VALUE_T val)169 static VPX_INLINE void mem_put_be16(void *vmem, MEM_VALUE_T val) {
170 MAU_T *mem = (MAU_T *)vmem;
171
172 mem[0] = (MAU_T)((val >> 8) & 0xff);
173 mem[1] = (MAU_T)((val >> 0) & 0xff);
174 }
175
176 #undef mem_put_be24
177 #define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
mem_put_be24(void * vmem,MEM_VALUE_T val)178 static VPX_INLINE void mem_put_be24(void *vmem, MEM_VALUE_T val) {
179 MAU_T *mem = (MAU_T *)vmem;
180
181 mem[0] = (MAU_T)((val >> 16) & 0xff);
182 mem[1] = (MAU_T)((val >> 8) & 0xff);
183 mem[2] = (MAU_T)((val >> 0) & 0xff);
184 }
185
186 #undef mem_put_be32
187 #define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
mem_put_be32(void * vmem,MEM_VALUE_T val)188 static VPX_INLINE void mem_put_be32(void *vmem, MEM_VALUE_T val) {
189 MAU_T *mem = (MAU_T *)vmem;
190
191 mem[0] = (MAU_T)((val >> 24) & 0xff);
192 mem[1] = (MAU_T)((val >> 16) & 0xff);
193 mem[2] = (MAU_T)((val >> 8) & 0xff);
194 mem[3] = (MAU_T)((val >> 0) & 0xff);
195 }
196
197 #undef mem_put_le16
198 #define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
mem_put_le16(void * vmem,MEM_VALUE_T val)199 static VPX_INLINE void mem_put_le16(void *vmem, MEM_VALUE_T val) {
200 MAU_T *mem = (MAU_T *)vmem;
201
202 mem[0] = (MAU_T)((val >> 0) & 0xff);
203 mem[1] = (MAU_T)((val >> 8) & 0xff);
204 }
205
206 #undef mem_put_le24
207 #define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
mem_put_le24(void * vmem,MEM_VALUE_T val)208 static VPX_INLINE void mem_put_le24(void *vmem, MEM_VALUE_T val) {
209 MAU_T *mem = (MAU_T *)vmem;
210
211 mem[0] = (MAU_T)((val >> 0) & 0xff);
212 mem[1] = (MAU_T)((val >> 8) & 0xff);
213 mem[2] = (MAU_T)((val >> 16) & 0xff);
214 }
215
216 #undef mem_put_le32
217 #define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
mem_put_le32(void * vmem,MEM_VALUE_T val)218 static VPX_INLINE void mem_put_le32(void *vmem, MEM_VALUE_T val) {
219 MAU_T *mem = (MAU_T *)vmem;
220
221 mem[0] = (MAU_T)((val >> 0) & 0xff);
222 mem[1] = (MAU_T)((val >> 8) & 0xff);
223 mem[2] = (MAU_T)((val >> 16) & 0xff);
224 mem[3] = (MAU_T)((val >> 24) & 0xff);
225 }
226 /* clang-format on */
227 #endif // VPX_VPX_PORTS_MEM_OPS_H_
228