ATLAS Offline Software
Loading...
Searching...
No Matches
get_unaligned.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration.
4 */
26
27
28#ifndef CXXUTILS_GET_UNALIGNED_H
29#define CXXUTILS_GET_UNALIGNED_H
30
31
32#include "CxxUtils/restrict.h"
33#include <cstring>
34#include <cstdint>
35#include <bit>
36
37
38#ifdef __linux__
39# include <endian.h>
40#else
41namespace CxxUtils {
42 inline uint16_t le16toh (uint16_t x) { return x; }
43 inline uint32_t le32toh (uint32_t x) { return x; }
44 inline uint64_t le64toh (uint64_t x) { return x; }
45}
46#endif
47
48
49namespace CxxUtils {
50
51
62inline
63uint16_t get_unaligned16 (const uint8_t* ATH_RESTRICT & p)
64{
65 uint16_t ret;
66 memcpy (&ret, p, sizeof(ret));
67 p += sizeof(ret);
68 return le16toh (ret);
69}
70
71
82inline
83uint32_t get_unaligned32 (const uint8_t* ATH_RESTRICT & p)
84{
85 uint32_t ret;
86 memcpy (&ret, p, sizeof(ret));
87 p += sizeof(ret);
88 return le32toh (ret);
89}
90
91
102inline
103uint64_t get_unaligned64 (const uint8_t* ATH_RESTRICT & p)
104{
105 uint64_t ret;
106 memcpy (&ret, p, sizeof(ret));
107 p += sizeof(ret);
108 return le64toh (ret);
109}
110
111
122inline
123float get_unaligned_float (const uint8_t* ATH_RESTRICT & p)
124{
125 return std::bit_cast<float> (get_unaligned32 (p));
126}
127
128
139inline
140double get_unaligned_double (const uint8_t* ATH_RESTRICT & p)
141{
142 return std::bit_cast<double> (get_unaligned64 (p));
143}
144
145
147
148
149template <class T>
150T get_unaligned (const uint8_t* ATH_RESTRICT & p);
151
152
153template <>
154inline
155uint8_t get_unaligned<uint8_t> (const uint8_t* ATH_RESTRICT & p)
156{
157 return *p++;
158}
159
160
161template <>
162inline
163uint16_t get_unaligned<uint16_t> (const uint8_t* ATH_RESTRICT & p)
164{
165 return get_unaligned16 (p);
166}
167
168
169template <>
170inline
171uint32_t get_unaligned<uint32_t> (const uint8_t* ATH_RESTRICT & p)
172{
173 return get_unaligned32 (p);
174}
175
176
177template <>
178inline
179uint64_t get_unaligned<uint64_t> (const uint8_t* ATH_RESTRICT & p)
180{
181 return get_unaligned64 (p);
182}
183
184
185template <>
186inline
187float get_unaligned<float> (const uint8_t* ATH_RESTRICT & p)
188{
189 return get_unaligned_float (p);
190}
191
192
193template <>
194inline
195double get_unaligned<double> (const uint8_t* ATH_RESTRICT & p)
196{
197 return get_unaligned_double (p);
198}
199
200
201template <>
202inline
203int8_t get_unaligned<int8_t> (const uint8_t* ATH_RESTRICT & p)
204{
205 return get_unaligned<uint8_t> (p);
206}
207
208
209template <>
210inline
211int16_t get_unaligned<int16_t> (const uint8_t* ATH_RESTRICT & p)
212{
213 return get_unaligned<uint16_t> (p);
214}
215
216
217template <>
218inline
219int32_t get_unaligned<int32_t> (const uint8_t* ATH_RESTRICT & p)
220{
221 return get_unaligned<uint32_t> (p);
222}
223
224
225template <>
226inline
227int64_t get_unaligned<int64_t> (const uint8_t* ATH_RESTRICT & p)
228{
229 return get_unaligned<uint64_t> (p);
230}
231
232
233} // namespace CxxUtils
234
235
236#endif // not CXXUTILS_GET_UNALIGNED_H
#define x
float get_unaligned< float >(const uint8_t *ATH_RESTRICT &p)
uint32_t le32toh(uint32_t x)
double get_unaligned_double(const uint8_t *ATH_RESTRICT &p)
Read a little-endian double value from a possibly unaligned pointer.
uint32_t get_unaligned32(const uint8_t *ATH_RESTRICT &p)
Read a 4-byte little-endian value from a possibly unaligned pointer.
int16_t get_unaligned< int16_t >(const uint8_t *ATH_RESTRICT &p)
T get_unaligned(const uint8_t *ATH_RESTRICT &p)
Define templated versions of the above functions.
uint16_t get_unaligned16(const uint8_t *ATH_RESTRICT &p)
Read a 2-byte little-endian value from a possibly unaligned pointer.
uint32_t get_unaligned< uint32_t >(const uint8_t *ATH_RESTRICT &p)
uint16_t le16toh(uint16_t x)
uint8_t get_unaligned< uint8_t >(const uint8_t *ATH_RESTRICT &p)
int64_t get_unaligned< int64_t >(const uint8_t *ATH_RESTRICT &p)
uint64_t get_unaligned64(const uint8_t *ATH_RESTRICT &p)
Read an 8-byte little-endian value from a possibly unaligned pointer.
uint16_t get_unaligned< uint16_t >(const uint8_t *ATH_RESTRICT &p)
float get_unaligned_float(const uint8_t *ATH_RESTRICT &p)
Read a little-endian float value from a possibly unaligned pointer.
int32_t get_unaligned< int32_t >(const uint8_t *ATH_RESTRICT &p)
int8_t get_unaligned< int8_t >(const uint8_t *ATH_RESTRICT &p)
double get_unaligned< double >(const uint8_t *ATH_RESTRICT &p)
uint64_t get_unaligned< uint64_t >(const uint8_t *ATH_RESTRICT &p)
uint64_t le64toh(uint64_t x)
Macro wrapping the nonstandard restrict keyword.
#define ATH_RESTRICT
Definition restrict.h:31