blob: 2d44a3a5827d5b44e917b98046012d8e807fb41c [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
James Zernf42d52e2011-02-16 17:54:49 -080012/* \file
13 * \brief Provides portable memory access primitives
John Koleszar0ea50ce2010-05-18 11:58:33 -040014 *
James Zernf42d52e2011-02-16 17:54:49 -080015 * This function provides portable primitives for getting and setting of
John Koleszar0ea50ce2010-05-18 11:58:33 -040016 * 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
35#ifndef MAU_T
36/* Minimum Access Unit for this target */
37#define MAU_T unsigned char
38#endif
39
40#ifndef MEM_VALUE_T
41#define MEM_VALUE_T int
42#endif
43
44#undef MEM_VALUE_T_SZ_BITS
45#define MEM_VALUE_T_SZ_BITS (sizeof(MEM_VALUE_T) << 3)
46
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
53
54/*
55 * Include aligned access routines
56 */
57#define INCLUDED_BY_MEM_OPS_H
58#include "mem_ops_aligned.h"
59#undef INCLUDED_BY_MEM_OPS_H
60
61#undef mem_get_be16
62#define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16)
John Koleszarc6b90392012-07-13 15:21:29 -070063static unsigned MEM_VALUE_T mem_get_be16(const void *vmem) {
64 unsigned MEM_VALUE_T val;
65 const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -040066
John Koleszarc6b90392012-07-13 15:21:29 -070067 val = mem[0] << 8;
68 val |= mem[1];
69 return val;
John Koleszar0ea50ce2010-05-18 11:58:33 -040070}
71
72#undef mem_get_be24
73#define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24)
John Koleszarc6b90392012-07-13 15:21:29 -070074static unsigned MEM_VALUE_T mem_get_be24(const void *vmem) {
75 unsigned MEM_VALUE_T val;
76 const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
John Koleszarc6b90392012-07-13 15:21:29 -070078 val = mem[0] << 16;
79 val |= mem[1] << 8;
80 val |= mem[2];
81 return val;
John Koleszar0ea50ce2010-05-18 11:58:33 -040082}
83
84#undef mem_get_be32
85#define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32)
John Koleszarc6b90392012-07-13 15:21:29 -070086static unsigned MEM_VALUE_T mem_get_be32(const void *vmem) {
87 unsigned MEM_VALUE_T val;
88 const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -040089
John Koleszarc6b90392012-07-13 15:21:29 -070090 val = mem[0] << 24;
91 val |= mem[1] << 16;
92 val |= mem[2] << 8;
93 val |= mem[3];
94 return val;
John Koleszar0ea50ce2010-05-18 11:58:33 -040095}
96
97#undef mem_get_le16
98#define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16)
John Koleszarc6b90392012-07-13 15:21:29 -070099static unsigned MEM_VALUE_T mem_get_le16(const void *vmem) {
100 unsigned MEM_VALUE_T val;
101 const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400102
John Koleszarc6b90392012-07-13 15:21:29 -0700103 val = mem[1] << 8;
104 val |= mem[0];
105 return val;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400106}
107
108#undef mem_get_le24
109#define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24)
John Koleszarc6b90392012-07-13 15:21:29 -0700110static unsigned MEM_VALUE_T mem_get_le24(const void *vmem) {
111 unsigned MEM_VALUE_T val;
112 const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400113
John Koleszarc6b90392012-07-13 15:21:29 -0700114 val = mem[2] << 16;
115 val |= mem[1] << 8;
116 val |= mem[0];
117 return val;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400118}
119
120#undef mem_get_le32
121#define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32)
John Koleszarc6b90392012-07-13 15:21:29 -0700122static unsigned MEM_VALUE_T mem_get_le32(const void *vmem) {
123 unsigned MEM_VALUE_T val;
124 const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400125
John Koleszarc6b90392012-07-13 15:21:29 -0700126 val = mem[3] << 24;
127 val |= mem[2] << 16;
128 val |= mem[1] << 8;
129 val |= mem[0];
130 return val;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400131}
132
133#define mem_get_s_generic(end,sz) \
John Koleszarc6b90392012-07-13 15:21:29 -0700134 static signed MEM_VALUE_T mem_get_s##end##sz(const void *vmem) {\
135 const MAU_T *mem = (const MAU_T*)vmem;\
136 signed MEM_VALUE_T val = mem_get_##end##sz(mem);\
137 return (val << (MEM_VALUE_T_SZ_BITS - sz)) >> (MEM_VALUE_T_SZ_BITS - sz);\
138 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400139
140#undef mem_get_sbe16
141#define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
John Koleszar807acf12012-11-02 15:39:14 -0700142mem_get_s_generic(be, 16)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400143
144#undef mem_get_sbe24
145#define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
John Koleszar807acf12012-11-02 15:39:14 -0700146mem_get_s_generic(be, 24)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400147
148#undef mem_get_sbe32
149#define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
John Koleszar807acf12012-11-02 15:39:14 -0700150mem_get_s_generic(be, 32)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400151
152#undef mem_get_sle16
153#define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
John Koleszar807acf12012-11-02 15:39:14 -0700154mem_get_s_generic(le, 16)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400155
156#undef mem_get_sle24
157#define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
John Koleszar807acf12012-11-02 15:39:14 -0700158mem_get_s_generic(le, 24)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400159
160#undef mem_get_sle32
161#define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
John Koleszar807acf12012-11-02 15:39:14 -0700162mem_get_s_generic(le, 32)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400163
164#undef mem_put_be16
165#define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
John Koleszarc6b90392012-07-13 15:21:29 -0700166static void mem_put_be16(void *vmem, MEM_VALUE_T val) {
167 MAU_T *mem = (MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400168
John Koleszarc6b90392012-07-13 15:21:29 -0700169 mem[0] = (val >> 8) & 0xff;
170 mem[1] = (val >> 0) & 0xff;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400171}
172
173#undef mem_put_be24
174#define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
John Koleszarc6b90392012-07-13 15:21:29 -0700175static void mem_put_be24(void *vmem, MEM_VALUE_T val) {
176 MAU_T *mem = (MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400177
John Koleszarc6b90392012-07-13 15:21:29 -0700178 mem[0] = (val >> 16) & 0xff;
179 mem[1] = (val >> 8) & 0xff;
180 mem[2] = (val >> 0) & 0xff;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400181}
182
183#undef mem_put_be32
184#define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
John Koleszarc6b90392012-07-13 15:21:29 -0700185static void mem_put_be32(void *vmem, MEM_VALUE_T val) {
186 MAU_T *mem = (MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400187
John Koleszarc6b90392012-07-13 15:21:29 -0700188 mem[0] = (val >> 24) & 0xff;
189 mem[1] = (val >> 16) & 0xff;
190 mem[2] = (val >> 8) & 0xff;
191 mem[3] = (val >> 0) & 0xff;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400192}
193
194#undef mem_put_le16
195#define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
John Koleszarc6b90392012-07-13 15:21:29 -0700196static void mem_put_le16(void *vmem, MEM_VALUE_T val) {
197 MAU_T *mem = (MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400198
John Koleszarc6b90392012-07-13 15:21:29 -0700199 mem[0] = (val >> 0) & 0xff;
200 mem[1] = (val >> 8) & 0xff;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400201}
202
203#undef mem_put_le24
204#define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
John Koleszarc6b90392012-07-13 15:21:29 -0700205static void mem_put_le24(void *vmem, MEM_VALUE_T val) {
206 MAU_T *mem = (MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400207
John Koleszarc6b90392012-07-13 15:21:29 -0700208 mem[0] = (val >> 0) & 0xff;
209 mem[1] = (val >> 8) & 0xff;
210 mem[2] = (val >> 16) & 0xff;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400211}
212
213#undef mem_put_le32
214#define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
John Koleszarc6b90392012-07-13 15:21:29 -0700215static void mem_put_le32(void *vmem, MEM_VALUE_T val) {
216 MAU_T *mem = (MAU_T *)vmem;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400217
John Koleszarc6b90392012-07-13 15:21:29 -0700218 mem[0] = (val >> 0) & 0xff;
219 mem[1] = (val >> 8) & 0xff;
220 mem[2] = (val >> 16) & 0xff;
221 mem[3] = (val >> 24) & 0xff;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400222}