spandsp 3.1.1
unaligned.h
Go to the documentation of this file.
1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * unaligned.h - Cross platform unaligned data access
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2006, 2022 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26/*! \file */
27
28#if !defined(_SPANDSP_UNALIGNED_H_)
29#define _SPANDSP_UNALIGNED_H_
30
31#if defined(__cplusplus)
32extern "C"
33{
34#endif
35
36#if defined(_MSC_VER)
37#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
38
39PACK(struct __dealign_uint16 { uint16_t datum; };)
40PACK(struct __dealign_uint32 { uint32_t datum; };)
41PACK(struct __dealign_uint64 { uint64_t datum; };)
42#endif
43
44#if defined(__GNUC__) || defined(__clang__)
45struct __dealign_uint16 { uint16_t datum; } __attribute__((packed));
46struct __dealign_uint32 { uint32_t datum; } __attribute__((packed));
47struct __dealign_uint64 { uint64_t datum; } __attribute__((packed));
48#endif
49
50#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
51/* If we just tell GCC what's going on, we can trust it to behave optimally */
52static __inline__ uint64_t get_unaligned_uint64(const void *p)
53{
54 const struct __dealign_uint64 *pp = (const struct __dealign_uint64 *) p;
55
56 return pp->datum;
57}
58/*- End of function --------------------------------------------------------*/
59
60static __inline__ void put_unaligned_uint64(void *p, uint32_t datum)
61{
62 struct __dealign_uint64 *pp = (struct __dealign_uint64 *) p;
63
64 pp->datum = datum;
65}
66/*- End of function --------------------------------------------------------*/
67
68static __inline__ uint32_t get_unaligned_uint32(const void *p)
69{
70 const struct __dealign_uint32 *pp = (const struct __dealign_uint32 *) p;
71
72 return pp->datum;
73}
74/*- End of function --------------------------------------------------------*/
75
76static __inline__ void put_unaligned_uint32(void *p, uint32_t datum)
77{
78 struct __dealign_uint32 *pp = (struct __dealign_uint32 *) p;
79
80 pp->datum = datum;
81}
82/*- End of function --------------------------------------------------------*/
83
84static __inline__ uint16_t get_unaligned_uint16(const void *p)
85{
86 const struct __dealign_uint16 *pp = (const struct __dealign_uint16 *) p;
87
88 return pp->datum;
89}
90/*- End of function --------------------------------------------------------*/
91
92static __inline__ void put_unaligned_uint16(void *p, uint16_t datum)
93{
94 struct __dealign_uint16 *pp = (struct __dealign_uint16 *) p;
95
96 pp->datum = datum;
97}
98/*- End of function --------------------------------------------------------*/
99
100#elif defined(SOLARIS) && defined(__sparc__)
101
102static __inline__ uint64_t get_unaligned_uint64(const void *p)
103{
104 const uint8_t *cp = p;
105
106 return (cp[0] << 56) | (cp[1] << 48) | (cp[2] << 40) | (cp[3] << 32) | (cp[4] << 24) | (cp[5] << 16) | (cp[6] << 8) | cp[7];
107}
108/*- End of function --------------------------------------------------------*/
109
110static __inline__ void put_unaligned_uint64(void *p, uint64_t datum)
111{
112 const uint8_t *cp = p;
113
114 cp[0] = datum >> 56;
115 cp[1] = datum >> 48;
116 cp[2] = datum >> 40;
117 cp[3] = datum >> 32;
118 cp[4] = datum >> 24;
119 cp[5] = datum >> 16;
120 cp[6] = datum >> 8;
121 cp[7] = datum;
122}
123/*- End of function --------------------------------------------------------*/
124
125static __inline__ uint32_t get_unaligned_uint32(const void *p)
126{
127 const uint8_t *cp = p;
128
129 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
130}
131/*- End of function --------------------------------------------------------*/
132
133static __inline__ void put_unaligned_uint32(void *p, uint32_t datum)
134{
135 const uint8_t *cp = p;
136
137 cp[0] = datum >> 24;
138 cp[1] = datum >> 16;
139 cp[2] = datum >> 8;
140 cp[3] = datum;
141}
142/*- End of function --------------------------------------------------------*/
143
144static __inline__ uint16_t get_unaligned_uint16(const void *p)
145{
146 const uint8_t *cp = p;
147
148 return (cp[0] << 8) | cp[1];
149}
150/*- End of function --------------------------------------------------------*/
151
152static __inline__ void put_unaligned_uint16(void *p, uint16_t datum)
153{
154 uint8_t *cp = p;
155
156 cp[0] = datum >> 8;
157 cp[1] = datum;
158}
159/*- End of function --------------------------------------------------------*/
160
161#else
162
163/* The generic case. Assume we can handle direct load/store. */
164#define get_unaligned_uint64(p) (*((uint64_t *) (p)))
165#define put_unaligned_uint64(p,d) do { uint64_t *__P = (p); *__P = d; } while(0)
166#define get_unaligned_uint32(p) (*((uint32_t *) (p)))
167#define put_unaligned_uint32(p,d) do { uint32_t *__P = (p); *__P = d; } while(0)
168#define get_unaligned_uint16(p) (*((uint16_t *) (p)))
169#define put_unaligned_uint16(p,d) do { uint16_t *__P = (p); *__P = d; } while(0)
170#endif
171
172/* Network (big endian) byte order access. Building the value from single bytes
173 gives the same result whatever the host byte order is. */
174static __inline__ uint16_t get_net_unaligned_uint16(const void *p)
175{
176 const uint8_t *cp = (const uint8_t *) p;
177
178 return (uint16_t) ((cp[0] << 8) | cp[1]);
179}
180/*- End of function --------------------------------------------------------*/
181
182static __inline__ void put_net_unaligned_uint16(void *p, uint16_t datum)
183{
184 uint8_t *cp = (uint8_t *) p;
185
186 cp[0] = (uint8_t) (datum >> 8);
187 cp[1] = (uint8_t) datum;
188}
189/*- End of function --------------------------------------------------------*/
190
191static __inline__ uint32_t get_net_unaligned_uint32(const void *p)
192{
193 const uint8_t *cp = (const uint8_t *) p;
194
195 return ((uint32_t) cp[0] << 24) | ((uint32_t) cp[1] << 16) | ((uint32_t) cp[2] << 8) | cp[3];
196}
197/*- End of function --------------------------------------------------------*/
198
199static __inline__ void put_net_unaligned_uint32(void *p, uint32_t datum)
200{
201 uint8_t *cp = (uint8_t *) p;
202
203 cp[0] = (uint8_t) (datum >> 24);
204 cp[1] = (uint8_t) (datum >> 16);
205 cp[2] = (uint8_t) (datum >> 8);
206 cp[3] = (uint8_t) datum;
207}
208/*- End of function --------------------------------------------------------*/
209
210static __inline__ uint64_t get_net_unaligned_uint64(const void *p)
211{
212 const uint8_t *cp = (const uint8_t *) p;
213
214 return ((uint64_t) get_net_unaligned_uint32(cp) << 32) | get_net_unaligned_uint32(cp + 4);
215}
216/*- End of function --------------------------------------------------------*/
217
218static __inline__ void put_net_unaligned_uint64(void *p, uint64_t datum)
219{
220 uint8_t *cp = (uint8_t *) p;
221
222 put_net_unaligned_uint32(cp, (uint32_t) (datum >> 32));
223 put_net_unaligned_uint32(cp + 4, (uint32_t) datum);
224}
225/*- End of function --------------------------------------------------------*/
226
227#if defined(__cplusplus)
228}
229#endif
230
231#endif
232/*- End of file ------------------------------------------------------------*/