forked from MRtrix3/mrtrix3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_set.h
368 lines (326 loc) · 8.72 KB
/
get_set.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Copyright 2008 Brain Research Institute, Melbourne, Australia
Written by J-Donald Tournier, 27/06/08.
This file is part of MRtrix.
MRtrix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MRtrix is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MRtrix. If not, see <http://www.gnu.org/licenses/>.
31-10-2008 J-Donald Tournier <[email protected]>
* replace get::ValueType and put::ValueType() methods with template
* get<ValueType>() & put<ValueType>() methods
* add get/put template specialisations for bool, int8 and uint8
* remove obsolete ArrayXX classes
* move MR::ByteOrder namespace & methods from lib/mrtrix.h to here
*/
#ifndef __get_set_h__
#define __get_set_h__
/** \defgroup Binary Binary access functions
* \brief functions to provide easy access to binary data. */
#include <atomic>
#include "mrtrix.h"
#define BITMASK 0x01U << 7
#ifdef BYTE_ORDER_IS_BIG_ENDIAN
#define MRTRIX_IS_BIG_ENDIAN true
#define TO_LE(v) swap(v)
#define TO_BE(v) v
#else
#define MRTRIX_IS_BIG_ENDIAN false
#define TO_LE(v) v
#define TO_BE(v) swap(v)
#endif
namespace MR
{
/** \addtogroup Binary
* @{ */
namespace ByteOrder
{
namespace
{
template <typename ValueType> inline void swap (ValueType& a, ValueType& b) throw ()
{
ValueType c (a);
a = b;
b = c;
}
}
inline int16_t swap (int16_t v)
{
union {
int16_t v;
uint8_t i[2];
} val = { v };
swap (val.i[0], val.i[1]);
return val.v;
}
inline uint16_t swap (uint16_t v)
{
union {
uint16_t v;
uint8_t i[2];
} val = { v };
swap (val.i[0], val.i[1]);
return val.v;
}
inline int32_t swap (int32_t v)
{
union {
int32_t v;
uint8_t i[4];
} val = { v };
swap (val.i[0], val.i[3]);
swap (val.i[1], val.i[2]);
return val.v;
}
inline uint32_t swap (uint32_t v)
{
union {
uint32_t v;
uint8_t i[4];
} val = { v };
swap (val.i[0], val.i[3]);
swap (val.i[1], val.i[2]);
return val.v;
}
inline int64_t swap (int64_t v)
{
union {
int64_t v;
uint8_t i[8];
} val = { v };
swap (val.i[0], val.i[7]);
swap (val.i[1], val.i[6]);
swap (val.i[2], val.i[5]);
swap (val.i[3], val.i[4]);
return val.v;
}
inline uint64_t swap (uint64_t v)
{
union {
uint64_t v;
uint8_t i[8];
} val = { v };
swap (val.i[0], val.i[7]);
swap (val.i[1], val.i[6]);
swap (val.i[2], val.i[5]);
swap (val.i[3], val.i[4]);
return val.v;
}
inline float32 swap (float32 v)
{
union {
float32 v;
uint8_t i[4];
} val = { v };
swap (val.i[0], val.i[3]);
swap (val.i[1], val.i[2]);
return val.v;
}
inline float64 swap (float64 v)
{
union {
float64 v;
uint32_t i[2];
} val = { v };
uint32_t t = swap (val.i[0]);
val.i[0] = swap (val.i[1]);
val.i[1] = t;
return val.v;
}
inline int16_t LE (int16_t v)
{
return TO_LE (v);
}
inline int16_t BE (int16_t v)
{
return TO_BE (v);
}
inline uint16_t LE (uint16_t v)
{
return TO_LE (v);
}
inline uint16_t BE (uint16_t v)
{
return TO_BE (v);
}
inline int32_t LE (int32_t v)
{
return TO_LE (v);
}
inline int32_t BE (int32_t v)
{
return TO_BE (v);
}
inline uint32_t LE (uint32_t v)
{
return TO_LE (v);
}
inline uint32_t BE (uint32_t v)
{
return TO_BE (v);
}
inline int64_t LE (int64_t v)
{
return TO_LE (v);
}
inline int64_t BE (int64_t v)
{
return TO_BE (v);
}
inline uint64_t LE (uint64_t v)
{
return TO_LE (v);
}
inline uint64_t BE (uint64_t v)
{
return TO_BE (v);
}
inline float32 LE (float32 v)
{
return TO_LE (v);
}
inline float32 BE (float32 v)
{
return TO_BE (v);
}
inline float64 LE (float64 v)
{
return TO_LE (v);
}
inline float64 BE (float64 v)
{
return TO_BE (v);
}
inline cfloat LE (cfloat v)
{
TO_LE (v.real());
TO_LE (v.imag());
return v;
}
inline cfloat BE (cfloat v)
{
TO_BE (v.real());
TO_BE (v.imag());
return v;
}
inline cdouble LE (cdouble v)
{
TO_LE (v.real());
TO_LE (v.imag());
return v;
}
inline cdouble BE (cdouble v)
{
TO_BE (v.real());
TO_BE (v.imag());
return v;
}
template <typename ValueType> inline ValueType swap (const ValueType value, bool is_big_endian)
{
return is_big_endian ? BE (value) : LE (value);
}
}
template <typename ValueType> inline ValueType getLE (const void* address)
{
return ByteOrder::LE (*((ValueType*) address));
}
template <typename ValueType> inline ValueType getBE (const void* address)
{
return ByteOrder::BE (*((ValueType*) address));
}
template <typename ValueType> inline ValueType get (const void* address, bool is_big_endian = MRTRIX_IS_BIG_ENDIAN)
{
return ByteOrder::swap (*((ValueType*) address), is_big_endian);
}
template <typename ValueType> inline void putLE (const ValueType value, void* address)
{
*((ValueType*) address) = ByteOrder::LE (value);
}
template <typename ValueType> inline void putBE (const ValueType value, void* address)
{
*((ValueType*) address) = ByteOrder::BE (value);
}
template <typename ValueType> inline void put (const ValueType value, void* address, bool is_big_endian = MRTRIX_IS_BIG_ENDIAN)
{
*((ValueType*) address) = ByteOrder::swap (value, is_big_endian);
}
template <typename ValueType> inline ValueType getLE (const void* data, size_t i)
{
return ByteOrder::LE (((ValueType*) data)[i]);
}
template <typename ValueType> inline ValueType getBE (const void* data, size_t i)
{
return ByteOrder::BE (((ValueType*) data)[i]);
}
template <typename ValueType> inline ValueType get (const void* data, size_t i, bool is_big_endian = MRTRIX_IS_BIG_ENDIAN)
{
return ByteOrder::swap (*((ValueType*) data)[i], is_big_endian);
}
template <typename ValueType> inline void putLE (const ValueType value, void* data, size_t i)
{
((ValueType*) data)[i] = ByteOrder::LE (value);
}
template <typename ValueType> inline void putBE (const ValueType value, void* data, size_t i)
{
((ValueType*) data)[i] = ByteOrder::BE (value);
}
template <typename ValueType> inline void put (const ValueType value, void* data, size_t i, bool is_big_endian = MRTRIX_IS_BIG_ENDIAN)
{
*((ValueType*) data)[i] = ByteOrder::swap (value, is_big_endian);
}
//! \cond skip
template <> inline int8_t get<int8_t> (const void* address, bool)
{
return *((int8_t*) address);
}
template <> inline int8_t get<int8_t> (const void* data, size_t i, bool)
{
return ((int8_t*) data)[i];
}
template <> inline void put<int8_t> (const int8_t value, void* address, bool)
{
*((int8_t*) address) = value;
}
template <> inline void put<int8_t> (const int8_t value, void* data, size_t i, bool)
{
((int8_t*) data)[i] = value;
}
template <> inline uint8_t get<uint8_t> (const void* address, bool)
{
return *((uint8_t*) address);
}
template <> inline uint8_t get<uint8_t> (const void* data, size_t i, bool)
{
return ((uint8_t*) data)[i];
}
template <> inline void put<uint8_t> (const uint8_t value, void* address, bool)
{
*((uint8_t*) address) = value;
}
template <> inline void put<uint8_t> (const uint8_t value, void* data, size_t i, bool)
{
((uint8_t*) data)[i] = value;
}
template <> inline bool get<bool> (const void* data, size_t i, bool)
{
return (((uint8_t*) data)[i/8]) & (BITMASK >> i%8);
}
template <> inline void put<bool> (const bool value, void* data, size_t i, bool)
{
std::atomic<uint8_t>* at = reinterpret_cast<std::atomic<uint8_t>*> (((uint8_t*) data) + (i/8));
uint8_t prev = *at, new_value;
do {
if (value) new_value = prev | (BITMASK >> i%8);
else new_value = prev & ~(BITMASK >> i%8);
} while (!at->compare_exchange_weak (prev, new_value));
}
//! \endcond
/** @} */
}
#endif