2010-05-18 19:58:33 +04:00
|
|
|
/*
|
2010-09-09 16:16:39 +04:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2010-06-18 20:39:21 +04:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-05 00:19:40 +04:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 20:39:21 +04:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-05 00:19:40 +04:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 19:58:33 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-02-17 04:54:49 +03:00
|
|
|
/* \file
|
|
|
|
* \brief Provides portable memory access primitives
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2011-02-17 04:54:49 +03:00
|
|
|
* This function provides portable primitives for getting and setting of
|
2010-05-18 19:58:33 +04:00
|
|
|
* 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)
|
|
|
|
|
|
|
|
#undef mem_ops_wrap_symbol
|
|
|
|
#define mem_ops_wrap_symbol(fn) mem_ops_wrap_symbol2(fn, MEM_VALUE_T)
|
|
|
|
#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
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Include aligned access routines
|
|
|
|
*/
|
|
|
|
#define INCLUDED_BY_MEM_OPS_H
|
|
|
|
#include "mem_ops_aligned.h"
|
|
|
|
#undef INCLUDED_BY_MEM_OPS_H
|
|
|
|
|
|
|
|
#undef mem_get_be16
|
|
|
|
#define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16)
|
2012-07-14 02:21:29 +04:00
|
|
|
static unsigned MEM_VALUE_T mem_get_be16(const void *vmem) {
|
|
|
|
unsigned MEM_VALUE_T val;
|
|
|
|
const MAU_T *mem = (const MAU_T *)vmem;
|
|
|
|
|
|
|
|
val = mem[0] << 8;
|
|
|
|
val |= mem[1];
|
|
|
|
return val;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_get_be24
|
|
|
|
#define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24)
|
2012-07-14 02:21:29 +04:00
|
|
|
static unsigned MEM_VALUE_T mem_get_be24(const void *vmem) {
|
|
|
|
unsigned MEM_VALUE_T val;
|
|
|
|
const MAU_T *mem = (const MAU_T *)vmem;
|
|
|
|
|
|
|
|
val = mem[0] << 16;
|
|
|
|
val |= mem[1] << 8;
|
|
|
|
val |= mem[2];
|
|
|
|
return val;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_get_be32
|
|
|
|
#define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32)
|
2012-07-14 02:21:29 +04:00
|
|
|
static unsigned MEM_VALUE_T mem_get_be32(const void *vmem) {
|
|
|
|
unsigned MEM_VALUE_T val;
|
|
|
|
const MAU_T *mem = (const MAU_T *)vmem;
|
|
|
|
|
|
|
|
val = mem[0] << 24;
|
|
|
|
val |= mem[1] << 16;
|
|
|
|
val |= mem[2] << 8;
|
|
|
|
val |= mem[3];
|
|
|
|
return val;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_get_le16
|
|
|
|
#define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16)
|
2012-07-14 02:21:29 +04:00
|
|
|
static unsigned MEM_VALUE_T mem_get_le16(const void *vmem) {
|
|
|
|
unsigned MEM_VALUE_T val;
|
|
|
|
const MAU_T *mem = (const MAU_T *)vmem;
|
|
|
|
|
|
|
|
val = mem[1] << 8;
|
|
|
|
val |= mem[0];
|
|
|
|
return val;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_get_le24
|
|
|
|
#define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24)
|
2012-07-14 02:21:29 +04:00
|
|
|
static unsigned MEM_VALUE_T mem_get_le24(const void *vmem) {
|
|
|
|
unsigned MEM_VALUE_T val;
|
|
|
|
const MAU_T *mem = (const MAU_T *)vmem;
|
|
|
|
|
|
|
|
val = mem[2] << 16;
|
|
|
|
val |= mem[1] << 8;
|
|
|
|
val |= mem[0];
|
|
|
|
return val;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_get_le32
|
|
|
|
#define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32)
|
2012-07-14 02:21:29 +04:00
|
|
|
static unsigned MEM_VALUE_T mem_get_le32(const void *vmem) {
|
|
|
|
unsigned MEM_VALUE_T val;
|
|
|
|
const MAU_T *mem = (const MAU_T *)vmem;
|
|
|
|
|
|
|
|
val = mem[3] << 24;
|
|
|
|
val |= mem[2] << 16;
|
|
|
|
val |= mem[1] << 8;
|
|
|
|
val |= mem[0];
|
|
|
|
return val;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define mem_get_s_generic(end,sz) \
|
2012-07-14 02:21:29 +04:00
|
|
|
static signed MEM_VALUE_T mem_get_s##end##sz(const void *vmem) {\
|
|
|
|
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);\
|
|
|
|
}
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#undef mem_get_sbe16
|
|
|
|
#define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16)
|
2012-11-03 02:39:14 +04:00
|
|
|
mem_get_s_generic(be, 16)
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#undef mem_get_sbe24
|
|
|
|
#define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24)
|
2012-11-03 02:39:14 +04:00
|
|
|
mem_get_s_generic(be, 24)
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#undef mem_get_sbe32
|
|
|
|
#define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32)
|
2012-11-03 02:39:14 +04:00
|
|
|
mem_get_s_generic(be, 32)
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#undef mem_get_sle16
|
|
|
|
#define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16)
|
2012-11-03 02:39:14 +04:00
|
|
|
mem_get_s_generic(le, 16)
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#undef mem_get_sle24
|
|
|
|
#define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24)
|
2012-11-03 02:39:14 +04:00
|
|
|
mem_get_s_generic(le, 24)
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#undef mem_get_sle32
|
|
|
|
#define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32)
|
2012-11-03 02:39:14 +04:00
|
|
|
mem_get_s_generic(le, 32)
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#undef mem_put_be16
|
|
|
|
#define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16)
|
2012-07-14 02:21:29 +04:00
|
|
|
static void mem_put_be16(void *vmem, MEM_VALUE_T val) {
|
|
|
|
MAU_T *mem = (MAU_T *)vmem;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
mem[0] = (val >> 8) & 0xff;
|
|
|
|
mem[1] = (val >> 0) & 0xff;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_put_be24
|
|
|
|
#define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24)
|
2012-07-14 02:21:29 +04:00
|
|
|
static void mem_put_be24(void *vmem, MEM_VALUE_T val) {
|
|
|
|
MAU_T *mem = (MAU_T *)vmem;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
mem[0] = (val >> 16) & 0xff;
|
|
|
|
mem[1] = (val >> 8) & 0xff;
|
|
|
|
mem[2] = (val >> 0) & 0xff;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_put_be32
|
|
|
|
#define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32)
|
2012-07-14 02:21:29 +04:00
|
|
|
static void mem_put_be32(void *vmem, MEM_VALUE_T val) {
|
|
|
|
MAU_T *mem = (MAU_T *)vmem;
|
|
|
|
|
|
|
|
mem[0] = (val >> 24) & 0xff;
|
|
|
|
mem[1] = (val >> 16) & 0xff;
|
|
|
|
mem[2] = (val >> 8) & 0xff;
|
|
|
|
mem[3] = (val >> 0) & 0xff;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_put_le16
|
|
|
|
#define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16)
|
2012-07-14 02:21:29 +04:00
|
|
|
static void mem_put_le16(void *vmem, MEM_VALUE_T val) {
|
|
|
|
MAU_T *mem = (MAU_T *)vmem;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
mem[0] = (val >> 0) & 0xff;
|
|
|
|
mem[1] = (val >> 8) & 0xff;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_put_le24
|
|
|
|
#define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24)
|
2012-07-14 02:21:29 +04:00
|
|
|
static void mem_put_le24(void *vmem, MEM_VALUE_T val) {
|
|
|
|
MAU_T *mem = (MAU_T *)vmem;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
mem[0] = (val >> 0) & 0xff;
|
|
|
|
mem[1] = (val >> 8) & 0xff;
|
|
|
|
mem[2] = (val >> 16) & 0xff;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef mem_put_le32
|
|
|
|
#define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32)
|
2012-07-14 02:21:29 +04:00
|
|
|
static void mem_put_le32(void *vmem, MEM_VALUE_T val) {
|
|
|
|
MAU_T *mem = (MAU_T *)vmem;
|
|
|
|
|
|
|
|
mem[0] = (val >> 0) & 0xff;
|
|
|
|
mem[1] = (val >> 8) & 0xff;
|
|
|
|
mem[2] = (val >> 16) & 0xff;
|
|
|
|
mem[3] = (val >> 24) & 0xff;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|