1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 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 */ 10 11 12 /* \file 13 * \brief Provides portable memory access primitives for operating on aligned 14 * data 15 * 16 * This file is split from mem_ops.h for easier maintenance. See mem_ops.h 17 * for a more detailed description of these primitives. 18 */ 19 #ifndef INCLUDED_BY_MEM_OPS_H 20 #error Include mem_ops.h, not mem_ops_aligned.h directly. 21 #endif 22 23 /* Architectures that provide instructions for doing this byte swapping 24 * could redefine these macros. 25 */ 26 #define swap_endian_16(val,raw) do {\ 27 val = ((raw>>8) & 0x00ff) \ 28 | ((raw<<8) & 0xff00);\ 29 } while(0) 30 #define swap_endian_32(val,raw) do {\ 31 val = ((raw>>24) & 0x000000ff) \ 32 | ((raw>>8) & 0x0000ff00) \ 33 | ((raw<<8) & 0x00ff0000) \ 34 | ((raw<<24) & 0xff000000); \ 35 } while(0) 36 #define swap_endian_16_se(val,raw) do {\ 37 swap_endian_16(val,raw);\ 38 val = ((val << 16) >> 16);\ 39 } while(0) 40 #define swap_endian_32_se(val,raw) swap_endian_32(val,raw) 41 42 #define mem_get_ne_aligned_generic(end,sz) \ 43 static unsigned MEM_VALUE_T mem_get_##end##sz##_aligned(const void *vmem) {\ 44 const uint##sz##_t *mem = (const uint##sz##_t *)vmem;\ 45 return *mem;\ 46 } 47 48 #define mem_get_sne_aligned_generic(end,sz) \ 49 static signed MEM_VALUE_T mem_get_s##end##sz##_aligned(const void *vmem) {\ 50 const int##sz##_t *mem = (const int##sz##_t *)vmem;\ 51 return *mem;\ 52 } 53 54 #define mem_get_se_aligned_generic(end,sz) \ 55 static unsigned MEM_VALUE_T mem_get_##end##sz##_aligned(const void *vmem) {\ 56 const uint##sz##_t *mem = (const uint##sz##_t *)vmem;\ 57 unsigned MEM_VALUE_T val, raw = *mem;\ 58 swap_endian_##sz(val,raw);\ 59 return val;\ 60 } 61 62 #define mem_get_sse_aligned_generic(end,sz) \ 63 static signed MEM_VALUE_T mem_get_s##end##sz##_aligned(const void *vmem) {\ 64 const int##sz##_t *mem = (const int##sz##_t *)vmem;\ 65 unsigned MEM_VALUE_T val, raw = *mem;\ 66 swap_endian_##sz##_se(val,raw);\ 67 return val;\ 68 } 69 70 #define mem_put_ne_aligned_generic(end,sz) \ 71 static void mem_put_##end##sz##_aligned(void *vmem, MEM_VALUE_T val) {\ 72 uint##sz##_t *mem = (uint##sz##_t *)vmem;\ 73 *mem = (uint##sz##_t)val;\ 74 } 75 76 #define mem_put_se_aligned_generic(end,sz) \ 77 static void mem_put_##end##sz##_aligned(void *vmem, MEM_VALUE_T val) {\ 78 uint##sz##_t *mem = (uint##sz##_t *)vmem, raw;\ 79 swap_endian_##sz(raw,val);\ 80 *mem = (uint##sz##_t)raw;\ 81 } 82 83 #include "config.h" 84 #if CONFIG_BIG_ENDIAN 85 #define mem_get_be_aligned_generic(sz) mem_get_ne_aligned_generic(be,sz) 86 #define mem_get_sbe_aligned_generic(sz) mem_get_sne_aligned_generic(be,sz) 87 #define mem_get_le_aligned_generic(sz) mem_get_se_aligned_generic(le,sz) 88 #define mem_get_sle_aligned_generic(sz) mem_get_sse_aligned_generic(le,sz) 89 #define mem_put_be_aligned_generic(sz) mem_put_ne_aligned_generic(be,sz) 90 #define mem_put_le_aligned_generic(sz) mem_put_se_aligned_generic(le,sz) 91 #else 92 #define mem_get_be_aligned_generic(sz) mem_get_se_aligned_generic(be,sz) 93 #define mem_get_sbe_aligned_generic(sz) mem_get_sse_aligned_generic(be,sz) 94 #define mem_get_le_aligned_generic(sz) mem_get_ne_aligned_generic(le,sz) 95 #define mem_get_sle_aligned_generic(sz) mem_get_sne_aligned_generic(le,sz) 96 #define mem_put_be_aligned_generic(sz) mem_put_se_aligned_generic(be,sz) 97 #define mem_put_le_aligned_generic(sz) mem_put_ne_aligned_generic(le,sz) 98 #endif 99 100 #undef mem_get_be16_aligned 101 #define mem_get_be16_aligned mem_ops_wrap_symbol(mem_get_be16_aligned) 102 mem_get_be_aligned_generic(16); 103 104 #undef mem_get_be32_aligned 105 #define mem_get_be32_aligned mem_ops_wrap_symbol(mem_get_be32_aligned) 106 mem_get_be_aligned_generic(32); 107 108 #undef mem_get_le16_aligned 109 #define mem_get_le16_aligned mem_ops_wrap_symbol(mem_get_le16_aligned) 110 mem_get_le_aligned_generic(16); 111 112 #undef mem_get_le32_aligned 113 #define mem_get_le32_aligned mem_ops_wrap_symbol(mem_get_le32_aligned) 114 mem_get_le_aligned_generic(32); 115 116 #undef mem_get_sbe16_aligned 117 #define mem_get_sbe16_aligned mem_ops_wrap_symbol(mem_get_sbe16_aligned) 118 mem_get_sbe_aligned_generic(16); 119 120 #undef mem_get_sbe32_aligned 121 #define mem_get_sbe32_aligned mem_ops_wrap_symbol(mem_get_sbe32_aligned) 122 mem_get_sbe_aligned_generic(32); 123 124 #undef mem_get_sle16_aligned 125 #define mem_get_sle16_aligned mem_ops_wrap_symbol(mem_get_sle16_aligned) 126 mem_get_sle_aligned_generic(16); 127 128 #undef mem_get_sle32_aligned 129 #define mem_get_sle32_aligned mem_ops_wrap_symbol(mem_get_sle32_aligned) 130 mem_get_sle_aligned_generic(32); 131 132 #undef mem_put_be16_aligned 133 #define mem_put_be16_aligned mem_ops_wrap_symbol(mem_put_be16_aligned) 134 mem_put_be_aligned_generic(16); 135 136 #undef mem_put_be32_aligned 137 #define mem_put_be32_aligned mem_ops_wrap_symbol(mem_put_be32_aligned) 138 mem_put_be_aligned_generic(32); 139 140 #undef mem_put_le16_aligned 141 #define mem_put_le16_aligned mem_ops_wrap_symbol(mem_put_le16_aligned) 142 mem_put_le_aligned_generic(16); 143 144 #undef mem_put_le32_aligned 145 #define mem_put_le32_aligned mem_ops_wrap_symbol(mem_put_le32_aligned) 146 mem_put_le_aligned_generic(32); 147 148 #undef mem_get_ne_aligned_generic 149 #undef mem_get_se_aligned_generic 150 #undef mem_get_sne_aligned_generic 151 #undef mem_get_sse_aligned_generic 152 #undef mem_put_ne_aligned_generic 153 #undef mem_put_se_aligned_generic 154 #undef swap_endian_16 155 #undef swap_endian_32 156 #undef swap_endian_16_se 157 #undef swap_endian_32_se 158