mem_ops.h 6.55 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10 11
 */

Adrian Grange's avatar
Adrian Grange committed
12 13
#ifndef AOM_PORTS_MEM_OPS_H_
#define AOM_PORTS_MEM_OPS_H_
John Koleszar's avatar
John Koleszar committed
14

James Zern's avatar
James Zern committed
15 16
/* \file
 * \brief Provides portable memory access primitives
John Koleszar's avatar
John Koleszar committed
17
 *
James Zern's avatar
James Zern committed
18
 * This function provides portable primitives for getting and setting of
John Koleszar's avatar
John Koleszar committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 * signed and unsigned integers in 16, 24, and 32 bit sizes. The operations
 * can be performed on unaligned data regardless of hardware support for
 * unaligned accesses.
 *
 * The type used to pass the integral values may be changed by defining
 * MEM_VALUE_T with the appropriate type. The type given must be an integral
 * numeric type.
 *
 * The actual functions instantiated have the MEM_VALUE_T type name pasted
 * on to the symbol name. This allows the developer to instantiate these
 * operations for multiple types within the same translation unit. This is
 * of somewhat questionable utility, but the capability exists nonetheless.
 * Users not making use of this functionality should call the functions
 * without the type name appended, and the preprocessor will take care of
 * it.
 *
 * NOTE: This code is not supported on platforms where char > 1 octet ATM.
 */

#ifndef MAU_T
/* Minimum Access Unit for this target */
#define MAU_T unsigned char
#endif

#ifndef MEM_VALUE_T
#define MEM_VALUE_T int
#endif

#undef MEM_VALUE_T_SZ_BITS
#define MEM_VALUE_T_SZ_BITS (sizeof(MEM_VALUE_T) << 3)

clang-format's avatar
clang-format committed
50
#undef mem_ops_wrap_symbol
John Koleszar's avatar
John Koleszar committed
51
#define mem_ops_wrap_symbol(fn) mem_ops_wrap_symbol2(fn, MEM_VALUE_T)
clang-format's avatar
clang-format committed
52 53 54 55
#undef mem_ops_wrap_symbol2
#define mem_ops_wrap_symbol2(fn, typ) mem_ops_wrap_symbol3(fn, typ)
#undef mem_ops_wrap_symbol3
#define mem_ops_wrap_symbol3(fn, typ) fn##_as_##typ
John Koleszar's avatar
John Koleszar committed
56 57 58 59 60 61

/*
 * Include aligned access routines
 */
#define INCLUDED_BY_MEM_OPS_H
#include "mem_ops_aligned.h"
clang-format's avatar
clang-format committed
62
#undef INCLUDED_BY_MEM_OPS_H
John Koleszar's avatar
John Koleszar committed
63

clang-format's avatar
clang-format committed
64
#undef mem_get_be16
John Koleszar's avatar
John Koleszar committed
65
#define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16)
John Koleszar's avatar
John Koleszar committed
66
static unsigned MEM_VALUE_T mem_get_be16(const void *vmem) {
clang-format's avatar
clang-format committed
67 68
  unsigned MEM_VALUE_T val;
  const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
69 70 71 72

  val = mem[0] << 8;
  val |= mem[1];
  return val;
John Koleszar's avatar
John Koleszar committed
73 74
}

clang-format's avatar
clang-format committed
75
#undef mem_get_be24
John Koleszar's avatar
John Koleszar committed
76
#define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24)
John Koleszar's avatar
John Koleszar committed
77
static unsigned MEM_VALUE_T mem_get_be24(const void *vmem) {
clang-format's avatar
clang-format committed
78 79
  unsigned MEM_VALUE_T val;
  const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
80 81 82 83 84

  val = mem[0] << 16;
  val |= mem[1] << 8;
  val |= mem[2];
  return val;
John Koleszar's avatar
John Koleszar committed
85 86
}

clang-format's avatar
clang-format committed
87
#undef mem_get_be32
John Koleszar's avatar
John Koleszar committed
88
#define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32)
John Koleszar's avatar
John Koleszar committed
89
static unsigned MEM_VALUE_T mem_get_be32(const void *vmem) {
clang-format's avatar
clang-format committed
90 91
  unsigned MEM_VALUE_T val;
  const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
92 93 94 95 96 97

  val = mem[0] << 24;
  val |= mem[1] << 16;
  val |= mem[2] << 8;
  val |= mem[3];
  return val;
John Koleszar's avatar
John Koleszar committed
98 99
}

clang-format's avatar
clang-format committed
100
#undef mem_get_le16
John Koleszar's avatar
John Koleszar committed
101
#define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16)
John Koleszar's avatar
John Koleszar committed
102
static unsigned MEM_VALUE_T mem_get_le16(const void *vmem) {
clang-format's avatar
clang-format committed
103 104
  unsigned MEM_VALUE_T val;
  const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
105 106 107 108

  val = mem[1] << 8;
  val |= mem[0];
  return val;
John Koleszar's avatar
John Koleszar committed
109 110
}

clang-format's avatar
clang-format committed
111
#undef mem_get_le24
John Koleszar's avatar
John Koleszar committed
112
#define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24)
John Koleszar's avatar
John Koleszar committed
113
static unsigned MEM_VALUE_T mem_get_le24(const void *vmem) {
clang-format's avatar
clang-format committed
114 115
  unsigned MEM_VALUE_T val;
  const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
116 117 118 119 120

  val = mem[2] << 16;
  val |= mem[1] << 8;
  val |= mem[0];
  return val;
John Koleszar's avatar
John Koleszar committed
121 122
}

clang-format's avatar
clang-format committed
123
#undef mem_get_le32
John Koleszar's avatar
John Koleszar committed
124
#define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32)
John Koleszar's avatar
John Koleszar committed
125
static unsigned MEM_VALUE_T mem_get_le32(const void *vmem) {
clang-format's avatar
clang-format committed
126 127
  unsigned MEM_VALUE_T val;
  const MAU_T *mem = (const MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
128 129 130 131 132 133

  val = mem[3] << 24;
  val |= mem[2] << 16;
  val |= mem[1] << 8;
  val |= mem[0];
  return val;
John Koleszar's avatar
John Koleszar committed
134 135
}

clang-format's avatar
clang-format committed
136
#define mem_get_s_generic(end, sz)                                            \
Adrian Grange's avatar
Adrian Grange committed
137
  static AOM_INLINE signed MEM_VALUE_T mem_get_s##end##sz(const void *vmem) { \
clang-format's avatar
clang-format committed
138 139 140
    const MAU_T *mem = (const MAU_T *)vmem;                                   \
    signed MEM_VALUE_T val = mem_get_##end##sz(mem);                          \
    return (val << (MEM_VALUE_T_SZ_BITS - sz)) >> (MEM_VALUE_T_SZ_BITS - sz); \
John Koleszar's avatar
John Koleszar committed
141
  }
John Koleszar's avatar
John Koleszar committed
142

clang-format's avatar
clang-format committed
143
#undef mem_get_sbe16
John Koleszar's avatar
John Koleszar committed
144
#define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
John Koleszar's avatar
John Koleszar committed
145
mem_get_s_generic(be, 16)
clang-format's avatar
clang-format committed
146
#undef mem_get_sbe24
John Koleszar's avatar
John Koleszar committed
147
#define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
clang-format's avatar
clang-format committed
148
    mem_get_s_generic(be, 24)
John Koleszar's avatar
John Koleszar committed
149

clang-format's avatar
clang-format committed
150
#undef mem_get_sbe32
John Koleszar's avatar
John Koleszar committed
151
#define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
clang-format's avatar
clang-format committed
152
        mem_get_s_generic(be, 32)
John Koleszar's avatar
John Koleszar committed
153

clang-format's avatar
clang-format committed
154
#undef mem_get_sle16
John Koleszar's avatar
John Koleszar committed
155
#define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
clang-format's avatar
clang-format committed
156
            mem_get_s_generic(le, 16)
John Koleszar's avatar
John Koleszar committed
157

clang-format's avatar
clang-format committed
158
#undef mem_get_sle24
John Koleszar's avatar
John Koleszar committed
159
#define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
clang-format's avatar
clang-format committed
160
                mem_get_s_generic(le, 24)
John Koleszar's avatar
John Koleszar committed
161

clang-format's avatar
clang-format committed
162
#undef mem_get_sle32
John Koleszar's avatar
John Koleszar committed
163
#define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
clang-format's avatar
clang-format committed
164
                    mem_get_s_generic(le, 32)
John Koleszar's avatar
John Koleszar committed
165

clang-format's avatar
clang-format committed
166
#undef mem_put_be16
John Koleszar's avatar
John Koleszar committed
167
#define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
Adrian Grange's avatar
Adrian Grange committed
168
                        static AOM_INLINE
clang-format's avatar
clang-format committed
169
    void mem_put_be16(void *vmem, MEM_VALUE_T val) {
John Koleszar's avatar
John Koleszar committed
170
  MAU_T *mem = (MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
171

John Koleszar's avatar
John Koleszar committed
172 173
  mem[0] = (val >> 8) & 0xff;
  mem[1] = (val >> 0) & 0xff;
John Koleszar's avatar
John Koleszar committed
174 175
}

clang-format's avatar
clang-format committed
176
#undef mem_put_be24
John Koleszar's avatar
John Koleszar committed
177
#define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
Adrian Grange's avatar
Adrian Grange committed
178
static AOM_INLINE void mem_put_be24(void *vmem, MEM_VALUE_T val) {
John Koleszar's avatar
John Koleszar committed
179
  MAU_T *mem = (MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
180

John Koleszar's avatar
John Koleszar committed
181
  mem[0] = (val >> 16) & 0xff;
clang-format's avatar
clang-format committed
182 183
  mem[1] = (val >> 8) & 0xff;
  mem[2] = (val >> 0) & 0xff;
John Koleszar's avatar
John Koleszar committed
184 185
}

clang-format's avatar
clang-format committed
186
#undef mem_put_be32
John Koleszar's avatar
John Koleszar committed
187
#define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
Adrian Grange's avatar
Adrian Grange committed
188
static AOM_INLINE void mem_put_be32(void *vmem, MEM_VALUE_T val) {
John Koleszar's avatar
John Koleszar committed
189 190 191 192
  MAU_T *mem = (MAU_T *)vmem;

  mem[0] = (val >> 24) & 0xff;
  mem[1] = (val >> 16) & 0xff;
clang-format's avatar
clang-format committed
193 194
  mem[2] = (val >> 8) & 0xff;
  mem[3] = (val >> 0) & 0xff;
John Koleszar's avatar
John Koleszar committed
195 196
}

clang-format's avatar
clang-format committed
197
#undef mem_put_le16
John Koleszar's avatar
John Koleszar committed
198
#define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
Adrian Grange's avatar
Adrian Grange committed
199
static AOM_INLINE void mem_put_le16(void *vmem, MEM_VALUE_T val) {
John Koleszar's avatar
John Koleszar committed
200
  MAU_T *mem = (MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
201

clang-format's avatar
clang-format committed
202 203
  mem[0] = (val >> 0) & 0xff;
  mem[1] = (val >> 8) & 0xff;
John Koleszar's avatar
John Koleszar committed
204 205
}

clang-format's avatar
clang-format committed
206
#undef mem_put_le24
John Koleszar's avatar
John Koleszar committed
207
#define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
Adrian Grange's avatar
Adrian Grange committed
208
static AOM_INLINE void mem_put_le24(void *vmem, MEM_VALUE_T val) {
John Koleszar's avatar
John Koleszar committed
209
  MAU_T *mem = (MAU_T *)vmem;
John Koleszar's avatar
John Koleszar committed
210

clang-format's avatar
clang-format committed
211 212
  mem[0] = (val >> 0) & 0xff;
  mem[1] = (val >> 8) & 0xff;
John Koleszar's avatar
John Koleszar committed
213
  mem[2] = (val >> 16) & 0xff;
John Koleszar's avatar
John Koleszar committed
214 215
}

clang-format's avatar
clang-format committed
216
#undef mem_put_le32
John Koleszar's avatar
John Koleszar committed
217
#define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
Adrian Grange's avatar
Adrian Grange committed
218
static AOM_INLINE void mem_put_le32(void *vmem, MEM_VALUE_T val) {
John Koleszar's avatar
John Koleszar committed
219 220
  MAU_T *mem = (MAU_T *)vmem;

clang-format's avatar
clang-format committed
221 222
  mem[0] = (val >> 0) & 0xff;
  mem[1] = (val >> 8) & 0xff;
John Koleszar's avatar
John Koleszar committed
223 224
  mem[2] = (val >> 16) & 0xff;
  mem[3] = (val >> 24) & 0xff;
John Koleszar's avatar
John Koleszar committed
225
}
226

Adrian Grange's avatar
Adrian Grange committed
227
#endif  // AOM_PORTS_MEM_OPS_H_