Merge "Fix one gcc compiler warning"
[libvpx.git] / vpx_ports / mem_ops.h
blobc178b8b7400235a5aa000caa92e444728868856d
1 /*
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.
9 */
12 /* \file mem_ops.h
13 * \brief Provides portable memory access primatives
15 * This function provides portable primatives 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.
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.
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.
32 * NOTE: This code is not supported on platforms where char > 1 octet ATM.
35 #ifndef MAU_T
36 /* Minimum Access Unit for this target */
37 #define MAU_T unsigned char
38 #endif
40 #ifndef MEM_VALUE_T
41 #define MEM_VALUE_T int
42 #endif
44 #undef MEM_VALUE_T_SZ_BITS
45 #define MEM_VALUE_T_SZ_BITS (sizeof(MEM_VALUE_T) << 3)
47 #undef mem_ops_wrap_symbol
48 #define mem_ops_wrap_symbol(fn) mem_ops_wrap_symbol2(fn, MEM_VALUE_T)
49 #undef mem_ops_wrap_symbol2
50 #define mem_ops_wrap_symbol2(fn,typ) mem_ops_wrap_symbol3(fn,typ)
51 #undef mem_ops_wrap_symbol3
52 #define mem_ops_wrap_symbol3(fn,typ) fn##_as_##typ
55 * Include aligned access routines
57 #define INCLUDED_BY_MEM_OPS_H
58 #include "mem_ops_aligned.h"
59 #undef INCLUDED_BY_MEM_OPS_H
61 #undef mem_get_be16
62 #define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16)
63 static unsigned MEM_VALUE_T mem_get_be16(const void *vmem)
65 unsigned MEM_VALUE_T val;
66 const MAU_T *mem = (const MAU_T *)vmem;
68 val = mem[0] << 8;
69 val |= mem[1];
70 return val;
73 #undef mem_get_be24
74 #define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24)
75 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;
80 val = mem[0] << 16;
81 val |= mem[1] << 8;
82 val |= mem[2];
83 return val;
86 #undef mem_get_be32
87 #define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32)
88 static unsigned MEM_VALUE_T mem_get_be32(const void *vmem)
90 unsigned MEM_VALUE_T val;
91 const MAU_T *mem = (const MAU_T *)vmem;
93 val = mem[0] << 24;
94 val |= mem[1] << 16;
95 val |= mem[2] << 8;
96 val |= mem[3];
97 return val;
100 #undef mem_get_le16
101 #define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16)
102 static unsigned MEM_VALUE_T mem_get_le16(const void *vmem)
104 unsigned MEM_VALUE_T val;
105 const MAU_T *mem = (const MAU_T *)vmem;
107 val = mem[1] << 8;
108 val |= mem[0];
109 return val;
112 #undef mem_get_le24
113 #define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24)
114 static unsigned MEM_VALUE_T mem_get_le24(const void *vmem)
116 unsigned MEM_VALUE_T val;
117 const MAU_T *mem = (const MAU_T *)vmem;
119 val = mem[2] << 16;
120 val |= mem[1] << 8;
121 val |= mem[0];
122 return val;
125 #undef mem_get_le32
126 #define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32)
127 static unsigned MEM_VALUE_T mem_get_le32(const void *vmem)
129 unsigned MEM_VALUE_T val;
130 const MAU_T *mem = (const MAU_T *)vmem;
132 val = mem[3] << 24;
133 val |= mem[2] << 16;
134 val |= mem[1] << 8;
135 val |= mem[0];
136 return val;
139 #define mem_get_s_generic(end,sz) \
140 static signed MEM_VALUE_T mem_get_s##end##sz(const void *vmem) {\
141 const MAU_T *mem = (const MAU_T*)vmem;\
142 signed MEM_VALUE_T val = mem_get_##end##sz(mem);\
143 return (val << (MEM_VALUE_T_SZ_BITS - sz)) >> (MEM_VALUE_T_SZ_BITS - sz);\
146 #undef mem_get_sbe16
147 #define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
148 mem_get_s_generic(be, 16);
150 #undef mem_get_sbe24
151 #define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
152 mem_get_s_generic(be, 24);
154 #undef mem_get_sbe32
155 #define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
156 mem_get_s_generic(be, 32);
158 #undef mem_get_sle16
159 #define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
160 mem_get_s_generic(le, 16);
162 #undef mem_get_sle24
163 #define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
164 mem_get_s_generic(le, 24);
166 #undef mem_get_sle32
167 #define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
168 mem_get_s_generic(le, 32);
170 #undef mem_put_be16
171 #define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
172 static void mem_put_be16(void *vmem, MEM_VALUE_T val)
174 MAU_T *mem = (MAU_T *)vmem;
176 mem[0] = (val >> 8) & 0xff;
177 mem[1] = (val >> 0) & 0xff;
180 #undef mem_put_be24
181 #define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
182 static void mem_put_be24(void *vmem, MEM_VALUE_T val)
184 MAU_T *mem = (MAU_T *)vmem;
186 mem[0] = (val >> 16) & 0xff;
187 mem[1] = (val >> 8) & 0xff;
188 mem[2] = (val >> 0) & 0xff;
191 #undef mem_put_be32
192 #define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
193 static void mem_put_be32(void *vmem, MEM_VALUE_T val)
195 MAU_T *mem = (MAU_T *)vmem;
197 mem[0] = (val >> 24) & 0xff;
198 mem[1] = (val >> 16) & 0xff;
199 mem[2] = (val >> 8) & 0xff;
200 mem[3] = (val >> 0) & 0xff;
203 #undef mem_put_le16
204 #define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
205 static void mem_put_le16(void *vmem, MEM_VALUE_T val)
207 MAU_T *mem = (MAU_T *)vmem;
209 mem[0] = (val >> 0) & 0xff;
210 mem[1] = (val >> 8) & 0xff;
213 #undef mem_put_le24
214 #define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
215 static void mem_put_le24(void *vmem, MEM_VALUE_T val)
217 MAU_T *mem = (MAU_T *)vmem;
219 mem[0] = (val >> 0) & 0xff;
220 mem[1] = (val >> 8) & 0xff;
221 mem[2] = (val >> 16) & 0xff;
224 #undef mem_put_le32
225 #define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
226 static void mem_put_le32(void *vmem, MEM_VALUE_T val)
228 MAU_T *mem = (MAU_T *)vmem;
230 mem[0] = (val >> 0) & 0xff;
231 mem[1] = (val >> 8) & 0xff;
232 mem[2] = (val >> 16) & 0xff;
233 mem[3] = (val >> 24) & 0xff;