ATLAS Offline Software
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  */
28 #ifndef CXXUTILS_GET_UNALIGNED_H
29 #define CXXUTILS_GET_UNALIGNED_H
30 
31 
32 #include "CxxUtils/restrict.h"
33 #include <cstring>
34 #include <stdint.h>
35 
36 
37 #ifdef __linux__
38 # include <endian.h>
39 #else
40 namespace CxxUtils {
41  inline uint16_t htole16 (uint16_t x) { return x; }
42  inline uint32_t htole32 (uint32_t x) { return x; }
43  inline uint64_t htole64 (uint64_t x) { return x; }
44 }
45 #endif
46 
47 
48 namespace CxxUtils {
49 
50 
61 inline
63 {
64  uint16_t ret;
65  memcpy (&ret, p, sizeof(ret));
66  p += sizeof(ret);
67  return htole16 (ret);
68 }
69 
70 
81 inline
83 {
84  uint32_t ret;
85  memcpy (&ret, p, sizeof(ret));
86  p += sizeof(ret);
87  return htole32 (ret);
88 }
89 
90 
101 inline
103 {
104  uint64_t ret;
105  memcpy (&ret, p, sizeof(ret));
106  p += sizeof(ret);
107  return htole64 (ret);
108 }
109 
110 
121 inline
123 {
124  union {
125  float f;
126  uint32_t i;
127  } cnv;
128  cnv.i = get_unaligned32 (p);
129  return cnv.f;
130 }
131 
132 
143 inline
145 {
146  union {
147  double f;
148  uint64_t i;
149  } cnv;
150  cnv.i = get_unaligned64 (p);
151  return cnv.f;
152 }
153 
154 
156 
157 
158 template <class T>
160 
161 
162 template <>
163 inline
165 {
166  return *p++;
167 }
168 
169 
170 template <>
171 inline
173 {
174  return get_unaligned16 (p);
175 }
176 
177 
178 template <>
179 inline
181 {
182  return get_unaligned32 (p);
183 }
184 
185 
186 template <>
187 inline
189 {
190  return get_unaligned64 (p);
191 }
192 
193 
194 template <>
195 inline
197 {
198  return get_unaligned_float (p);
199 }
200 
201 
202 template <>
203 inline
205 {
206  return get_unaligned_double (p);
207 }
208 
209 
210 template <>
211 inline
213 {
214  return get_unaligned<uint8_t> (p);
215 }
216 
217 
218 template <>
219 inline
221 {
222  return get_unaligned<uint16_t> (p);
223 }
224 
225 
226 template <>
227 inline
229 {
230  return get_unaligned<uint32_t> (p);
231 }
232 
233 
234 template <>
235 inline
237 {
238  return get_unaligned<uint64_t> (p);
239 }
240 
241 
242 } // namespace CxxUtils
243 
244 
245 #endif // not CXXUTILS_GET_UNALIGNED_H
CxxUtils::get_unaligned< int64_t >
int64_t get_unaligned< int64_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:236
CxxUtils::get_unaligned< double >
double get_unaligned< double >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:204
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:557
CxxUtils::get_unaligned< int32_t >
int32_t get_unaligned< int32_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:228
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
CxxUtils::get_unaligned64
uint64_t get_unaligned64(const uint8_t *ATH_RESTRICT &p)
Read a 8-byte little-endian value from a possibly unaligned pointer.
Definition: get_unaligned.h:102
CxxUtils::get_unaligned< int16_t >
int16_t get_unaligned< int16_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:220
CxxUtils::htole32
uint32_t htole32(uint32_t x)
Definition: get_unaligned.h:42
CxxUtils::get_unaligned
T get_unaligned(const uint8_t *ATH_RESTRICT &p)
Define templated versions of the above functions.
x
#define x
xAOD::int16_t
setScaleOne setStatusOne setSaturated int16_t
Definition: gFexGlobalRoI_v1.cxx:55
ATH_RESTRICT
#define ATH_RESTRICT
Definition: restrict.h:31
CxxUtils::get_unaligned_float
float get_unaligned_float(const uint8_t *ATH_RESTRICT &p)
Read little-endian float value from a possibly unaligned pointer.
Definition: get_unaligned.h:122
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
CxxUtils::get_unaligned< uint64_t >
uint64_t get_unaligned< uint64_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:188
lumiFormat.i
int i
Definition: lumiFormat.py:85
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::get_unaligned16
uint16_t get_unaligned16(const uint8_t *ATH_RESTRICT &p)
Read a 2-byte little-endian value from a possibly unaligned pointer.
Definition: get_unaligned.h:62
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
hist_file_dump.f
f
Definition: hist_file_dump.py:135
CxxUtils::get_unaligned_double
double get_unaligned_double(const uint8_t *ATH_RESTRICT &p)
Read little-endian float value from a possibly unaligned pointer.
Definition: get_unaligned.h:144
CxxUtils::htole64
uint64_t htole64(uint64_t x)
Definition: get_unaligned.h:43
restrict.h
Macro wrapping the nonstandard restrict keyword.
CxxUtils::get_unaligned< uint32_t >
uint32_t get_unaligned< uint32_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:180
CxxUtils::get_unaligned< uint16_t >
uint16_t get_unaligned< uint16_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:172
CxxUtils::get_unaligned< float >
float get_unaligned< float >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:196
CxxUtils::get_unaligned< uint8_t >
uint8_t get_unaligned< uint8_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:164
CxxUtils::get_unaligned32
uint32_t get_unaligned32(const uint8_t *ATH_RESTRICT &p)
Read a 4-byte little-endian value from a possibly unaligned pointer.
Definition: get_unaligned.h:82
CxxUtils::get_unaligned< int8_t >
int8_t get_unaligned< int8_t >(const uint8_t *ATH_RESTRICT &p)
Definition: get_unaligned.h:212
CxxUtils::htole16
uint16_t htole16(uint16_t x)
Definition: get_unaligned.h:41