1 /* Unaligned memory access functionality.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2001.
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18 Red Hat elfutils is an included package of the Open Invention Network.
19 An included package of the Open Invention Network is a package for which
20 Open Invention Network licensees cross-license their patents. No patent
21 license is granted, either expressly or impliedly, by designation as an
22 included package. Should you wish to participate in the Open Invention
23 Network licensing program, please visit www.openinventionnetwork.com
24 <http://www.openinventionnetwork.com>. */
26 #ifndef _MEMORY_ACCESS_H
27 #define _MEMORY_ACCESS_H 1
35 /* When loading this file we require the macro MACHINE_ENCODING to be
36 defined to signal the endianness of the architecture which is
38 #ifndef MACHINE_ENCODING
39 # error "MACHINE_ENCODING needs to be defined"
41 #if MACHINE_ENCODING != __BIG_ENDIAN && MACHINE_ENCODING != __LITTLE_ENDIAN
42 # error "MACHINE_ENCODING must signal either big or little endian"
46 /* We use simple memory access functions in case the hardware allows it.
47 The caller has to make sure we don't have alias problems. */
50 # define read_2ubyte_unaligned(Addr) \
51 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
52 ? bswap_16 (*((const uint16_t *) (Addr))) \
53 : *((const uint16_t *) (Addr)))
54 # define read_2sbyte_unaligned(Addr) \
55 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
56 ? (int16_t) bswap_16 (*((const int16_t *) (Addr))) \
57 : *((const int16_t *) (Addr)))
59 # define read_4ubyte_unaligned_noncvt(Addr) \
60 *((const uint32_t *) (Addr))
61 # define read_4ubyte_unaligned(Addr) \
62 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
63 ? bswap_32 (*((const uint32_t *) (Addr))) \
64 : *((const uint32_t *) (Addr)))
65 # define read_4sbyte_unaligned(Addr) \
66 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
67 ? (int32_t) bswap_32 (*((const int32_t *) (Addr))) \
68 : *((const int32_t *) (Addr)))
70 # define read_8ubyte_unaligned(Addr) \
71 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
72 ? bswap_64 (*((const uint64_t *) (Addr))) \
73 : *((const uint64_t *) (Addr)))
74 # define read_8sbyte_unaligned(Addr) \
75 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
76 ? (int64_t) bswap_64 (*((const int64_t *) (Addr))) \
77 : *((const int64_t *) (Addr)))
90 } __attribute__ ((packed));
92 static inline uint16_t
93 read_2ubyte_unaligned (const void *p)
95 const union unaligned *up = p;
96 if (MACHINE_ENCODING != __BYTE_ORDER)
97 return bswap_16 (up->u2);
100 static inline int16_t
101 read_2sbyte_unaligned (const void *p)
103 const union unaligned *up = p;
104 if (MACHINE_ENCODING != __BYTE_ORDER)
105 return (int16_t) bswap_16 (up->u2);
109 static inline uint32_t
110 read_4ubyte_unaligned_noncvt (const void *p)
112 const union unaligned *up = p;
115 static inline uint32_t
116 read_4ubyte_unaligned (const void *p)
118 const union unaligned *up = p;
119 if (MACHINE_ENCODING != __BYTE_ORDER)
120 return bswap_32 (up->u4);
123 static inline int32_t
124 read_4sbyte_unaligned (const void *p)
126 const union unaligned *up = p;
127 if (MACHINE_ENCODING != __BYTE_ORDER)
128 return (int32_t) bswap_32 (up->u4);
132 static inline uint64_t
133 read_8ubyte_unaligned (const void *p)
135 const union unaligned *up = p;
136 if (MACHINE_ENCODING != __BYTE_ORDER)
137 return bswap_64 (up->u8);
140 static inline int64_t
141 read_8sbyte_unaligned (const void *p)
143 const union unaligned *up = p;
144 if (MACHINE_ENCODING != __BYTE_ORDER)
145 return (int64_t) bswap_64 (up->u8);
149 #endif /* allow unaligned */
152 #define read_2ubyte_unaligned_inc(Addr) \
153 ({ uint16_t t_ = read_2ubyte_unaligned (Addr); \
154 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
156 #define read_2sbyte_unaligned_inc(Addr) \
157 ({ int16_t t_ = read_2sbyte_unaligned (Addr); \
158 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
161 #define read_4ubyte_unaligned_inc(Addr) \
162 ({ uint32_t t_ = read_4ubyte_unaligned (Addr); \
163 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
165 #define read_4sbyte_unaligned_inc(Addr) \
166 ({ int32_t t_ = read_4sbyte_unaligned (Addr); \
167 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
170 #define read_8ubyte_unaligned_inc(Addr) \
171 ({ uint64_t t_ = read_8ubyte_unaligned (Addr); \
172 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
174 #define read_8sbyte_unaligned_inc(Addr) \
175 ({ int64_t t_ = read_8sbyte_unaligned (Addr); \
176 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
179 #endif /* memory-access.h */