michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: /* \file michael@0: * \brief Provides portable memory access primitives michael@0: * michael@0: * This function provides portable primitives for getting and setting of michael@0: * signed and unsigned integers in 16, 24, and 32 bit sizes. The operations michael@0: * can be performed on unaligned data regardless of hardware support for michael@0: * unaligned accesses. michael@0: * michael@0: * The type used to pass the integral values may be changed by defining michael@0: * MEM_VALUE_T with the appropriate type. The type given must be an integral michael@0: * numeric type. michael@0: * michael@0: * The actual functions instantiated have the MEM_VALUE_T type name pasted michael@0: * on to the symbol name. This allows the developer to instantiate these michael@0: * operations for multiple types within the same translation unit. This is michael@0: * of somewhat questionable utility, but the capability exists nonetheless. michael@0: * Users not making use of this functionality should call the functions michael@0: * without the type name appended, and the preprocessor will take care of michael@0: * it. michael@0: * michael@0: * NOTE: This code is not supported on platforms where char > 1 octet ATM. michael@0: */ michael@0: michael@0: #ifndef MAU_T michael@0: /* Minimum Access Unit for this target */ michael@0: #define MAU_T unsigned char michael@0: #endif michael@0: michael@0: #ifndef MEM_VALUE_T michael@0: #define MEM_VALUE_T int michael@0: #endif michael@0: michael@0: #undef MEM_VALUE_T_SZ_BITS michael@0: #define MEM_VALUE_T_SZ_BITS (sizeof(MEM_VALUE_T) << 3) michael@0: michael@0: #undef mem_ops_wrap_symbol michael@0: #define mem_ops_wrap_symbol(fn) mem_ops_wrap_symbol2(fn, MEM_VALUE_T) michael@0: #undef mem_ops_wrap_symbol2 michael@0: #define mem_ops_wrap_symbol2(fn,typ) mem_ops_wrap_symbol3(fn,typ) michael@0: #undef mem_ops_wrap_symbol3 michael@0: #define mem_ops_wrap_symbol3(fn,typ) fn##_as_##typ michael@0: michael@0: /* michael@0: * Include aligned access routines michael@0: */ michael@0: #define INCLUDED_BY_MEM_OPS_H michael@0: #include "mem_ops_aligned.h" michael@0: #undef INCLUDED_BY_MEM_OPS_H michael@0: michael@0: #undef mem_get_be16 michael@0: #define mem_get_be16 mem_ops_wrap_symbol(mem_get_be16) michael@0: static unsigned MEM_VALUE_T mem_get_be16(const void *vmem) { michael@0: unsigned MEM_VALUE_T val; michael@0: const MAU_T *mem = (const MAU_T *)vmem; michael@0: michael@0: val = mem[0] << 8; michael@0: val |= mem[1]; michael@0: return val; michael@0: } michael@0: michael@0: #undef mem_get_be24 michael@0: #define mem_get_be24 mem_ops_wrap_symbol(mem_get_be24) michael@0: static unsigned MEM_VALUE_T mem_get_be24(const void *vmem) { michael@0: unsigned MEM_VALUE_T val; michael@0: const MAU_T *mem = (const MAU_T *)vmem; michael@0: michael@0: val = mem[0] << 16; michael@0: val |= mem[1] << 8; michael@0: val |= mem[2]; michael@0: return val; michael@0: } michael@0: michael@0: #undef mem_get_be32 michael@0: #define mem_get_be32 mem_ops_wrap_symbol(mem_get_be32) michael@0: static unsigned MEM_VALUE_T mem_get_be32(const void *vmem) { michael@0: unsigned MEM_VALUE_T val; michael@0: const MAU_T *mem = (const MAU_T *)vmem; michael@0: michael@0: val = mem[0] << 24; michael@0: val |= mem[1] << 16; michael@0: val |= mem[2] << 8; michael@0: val |= mem[3]; michael@0: return val; michael@0: } michael@0: michael@0: #undef mem_get_le16 michael@0: #define mem_get_le16 mem_ops_wrap_symbol(mem_get_le16) michael@0: static unsigned MEM_VALUE_T mem_get_le16(const void *vmem) { michael@0: unsigned MEM_VALUE_T val; michael@0: const MAU_T *mem = (const MAU_T *)vmem; michael@0: michael@0: val = mem[1] << 8; michael@0: val |= mem[0]; michael@0: return val; michael@0: } michael@0: michael@0: #undef mem_get_le24 michael@0: #define mem_get_le24 mem_ops_wrap_symbol(mem_get_le24) michael@0: static unsigned MEM_VALUE_T mem_get_le24(const void *vmem) { michael@0: unsigned MEM_VALUE_T val; michael@0: const MAU_T *mem = (const MAU_T *)vmem; michael@0: michael@0: val = mem[2] << 16; michael@0: val |= mem[1] << 8; michael@0: val |= mem[0]; michael@0: return val; michael@0: } michael@0: michael@0: #undef mem_get_le32 michael@0: #define mem_get_le32 mem_ops_wrap_symbol(mem_get_le32) michael@0: static unsigned MEM_VALUE_T mem_get_le32(const void *vmem) { michael@0: unsigned MEM_VALUE_T val; michael@0: const MAU_T *mem = (const MAU_T *)vmem; michael@0: michael@0: val = mem[3] << 24; michael@0: val |= mem[2] << 16; michael@0: val |= mem[1] << 8; michael@0: val |= mem[0]; michael@0: return val; michael@0: } michael@0: michael@0: #define mem_get_s_generic(end,sz) \ michael@0: static signed MEM_VALUE_T mem_get_s##end##sz(const void *vmem) {\ michael@0: const MAU_T *mem = (const MAU_T*)vmem;\ michael@0: signed MEM_VALUE_T val = mem_get_##end##sz(mem);\ michael@0: return (val << (MEM_VALUE_T_SZ_BITS - sz)) >> (MEM_VALUE_T_SZ_BITS - sz);\ michael@0: } michael@0: michael@0: #undef mem_get_sbe16 michael@0: #define mem_get_sbe16 mem_ops_wrap_symbol(mem_get_sbe16) michael@0: mem_get_s_generic(be, 16) michael@0: michael@0: #undef mem_get_sbe24 michael@0: #define mem_get_sbe24 mem_ops_wrap_symbol(mem_get_sbe24) michael@0: mem_get_s_generic(be, 24) michael@0: michael@0: #undef mem_get_sbe32 michael@0: #define mem_get_sbe32 mem_ops_wrap_symbol(mem_get_sbe32) michael@0: mem_get_s_generic(be, 32) michael@0: michael@0: #undef mem_get_sle16 michael@0: #define mem_get_sle16 mem_ops_wrap_symbol(mem_get_sle16) michael@0: mem_get_s_generic(le, 16) michael@0: michael@0: #undef mem_get_sle24 michael@0: #define mem_get_sle24 mem_ops_wrap_symbol(mem_get_sle24) michael@0: mem_get_s_generic(le, 24) michael@0: michael@0: #undef mem_get_sle32 michael@0: #define mem_get_sle32 mem_ops_wrap_symbol(mem_get_sle32) michael@0: mem_get_s_generic(le, 32) michael@0: michael@0: #undef mem_put_be16 michael@0: #define mem_put_be16 mem_ops_wrap_symbol(mem_put_be16) michael@0: static void mem_put_be16(void *vmem, MEM_VALUE_T val) { michael@0: MAU_T *mem = (MAU_T *)vmem; michael@0: michael@0: mem[0] = (val >> 8) & 0xff; michael@0: mem[1] = (val >> 0) & 0xff; michael@0: } michael@0: michael@0: #undef mem_put_be24 michael@0: #define mem_put_be24 mem_ops_wrap_symbol(mem_put_be24) michael@0: static void mem_put_be24(void *vmem, MEM_VALUE_T val) { michael@0: MAU_T *mem = (MAU_T *)vmem; michael@0: michael@0: mem[0] = (val >> 16) & 0xff; michael@0: mem[1] = (val >> 8) & 0xff; michael@0: mem[2] = (val >> 0) & 0xff; michael@0: } michael@0: michael@0: #undef mem_put_be32 michael@0: #define mem_put_be32 mem_ops_wrap_symbol(mem_put_be32) michael@0: static void mem_put_be32(void *vmem, MEM_VALUE_T val) { michael@0: MAU_T *mem = (MAU_T *)vmem; michael@0: michael@0: mem[0] = (val >> 24) & 0xff; michael@0: mem[1] = (val >> 16) & 0xff; michael@0: mem[2] = (val >> 8) & 0xff; michael@0: mem[3] = (val >> 0) & 0xff; michael@0: } michael@0: michael@0: #undef mem_put_le16 michael@0: #define mem_put_le16 mem_ops_wrap_symbol(mem_put_le16) michael@0: static void mem_put_le16(void *vmem, MEM_VALUE_T val) { michael@0: MAU_T *mem = (MAU_T *)vmem; michael@0: michael@0: mem[0] = (val >> 0) & 0xff; michael@0: mem[1] = (val >> 8) & 0xff; michael@0: } michael@0: michael@0: #undef mem_put_le24 michael@0: #define mem_put_le24 mem_ops_wrap_symbol(mem_put_le24) michael@0: static void mem_put_le24(void *vmem, MEM_VALUE_T val) { michael@0: MAU_T *mem = (MAU_T *)vmem; michael@0: michael@0: mem[0] = (val >> 0) & 0xff; michael@0: mem[1] = (val >> 8) & 0xff; michael@0: mem[2] = (val >> 16) & 0xff; michael@0: } michael@0: michael@0: #undef mem_put_le32 michael@0: #define mem_put_le32 mem_ops_wrap_symbol(mem_put_le32) michael@0: static void mem_put_le32(void *vmem, MEM_VALUE_T val) { michael@0: MAU_T *mem = (MAU_T *)vmem; michael@0: michael@0: mem[0] = (val >> 0) & 0xff; michael@0: mem[1] = (val >> 8) & 0xff; michael@0: mem[2] = (val >> 16) & 0xff; michael@0: mem[3] = (val >> 24) & 0xff; michael@0: }