roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
store.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include <cstring>
6#include <limits>
7
8#include "roo_io/base/byte.h"
10#include "roo_io/data/ieee754.h"
11
12namespace roo_io {
13
14// Basic, unsigned.
15
16// Stores an unsigned 8-bit int to the specified memory address.
17inline void StoreU8(uint8_t v, byte* target) { target[0] = (byte)v; }
18
19// Stores a big-endian unsigned 16-bit int to the specified memory address.
20inline void StoreBeU16(uint16_t v, byte* target) {
21 target[0] = (byte)(v >> 8);
22 target[1] = (byte)(v >> 0);
23}
24
25// Stores a little-endian unsigned 16-bit int to the specified memory address.
26inline void StoreLeU16(uint16_t v, byte* target) {
27 target[0] = (byte)(v >> 0);
28 target[1] = (byte)(v >> 8);
29}
30
31// Stores a big-endian unsigned 24-bit int to the specified memory address.
32inline void StoreBeU24(uint32_t v, byte* target) {
33 target[0] = (byte)(v >> 16);
34 target[1] = (byte)(v >> 8);
35 target[2] = (byte)(v >> 0);
36}
37
38// Stores a little-endian unsigned 24-bit int to the specified memory address.
39inline void StoreLeU24(uint32_t v, byte* target) {
40 target[0] = (byte)(v >> 0);
41 target[1] = (byte)(v >> 8);
42 target[2] = (byte)(v >> 16);
43}
44
45// Stores a big-endian unsigned 32-bit int to the specified memory address.
46inline void StoreBeU32(uint32_t v, byte* target) {
47 target[0] = (byte)(v >> 24);
48 target[1] = (byte)(v >> 16);
49 target[2] = (byte)(v >> 8);
50 target[3] = (byte)(v >> 0);
51}
52
53// Stores a little-endian unsigned 32-bit int to the specified memory address.
54inline void StoreLeU32(uint32_t v, byte* target) {
55 target[0] = (byte)(v >> 0);
56 target[1] = (byte)(v >> 8);
57 target[2] = (byte)(v >> 16);
58 target[3] = (byte)(v >> 24);
59}
60
61// Stores a big-endian unsigned 64-bit int to the specified memory address.
62inline void StoreBeU64(uint64_t v, byte* target) {
63 target[0] = (byte)(v >> 56);
64 target[1] = (byte)(v >> 48);
65 target[2] = (byte)(v >> 40);
66 target[3] = (byte)(v >> 32);
67 target[4] = (byte)(v >> 24);
68 target[5] = (byte)(v >> 16);
69 target[6] = (byte)(v >> 8);
70 target[7] = (byte)(v >> 0);
71}
72
73// Stores a little-endian unsigned 64-bit int to the specified memory address.
74inline void StoreLeU64(uint64_t v, byte* target) {
75 target[0] = (byte)(v >> 0);
76 target[1] = (byte)(v >> 8);
77 target[2] = (byte)(v >> 16);
78 target[3] = (byte)(v >> 24);
79 target[4] = (byte)(v >> 32);
80 target[5] = (byte)(v >> 40);
81 target[6] = (byte)(v >> 48);
82 target[7] = (byte)(v >> 56);
83}
84
85// Basic, signed.
86
87// Stores an unsigned 8-bit int to the specified memory address.
88inline void StoreS8(int8_t v, byte* target) { StoreU8(v, target); }
89
90// Stores a big-endian signed 16-bit int to the specified memory address.
91inline void StoreBeS16(uint16_t v, byte* target) { StoreBeU16(v, target); }
92
93// Stores a little-endian signed 16-bit int to the specified memory address.
94inline void StoreLeS16(uint16_t v, byte* target) { StoreLeU16(v, target); }
95
96// Stores a big-endian signed 24-bit int to the specified memory address.
97inline void StoreBeS24(uint32_t v, byte* target) { StoreBeU24(v, target); }
98
99// Stores a little-endian signed 24-bit int to the specified memory address.
100inline void StoreLeS24(uint32_t v, byte* target) { StoreLeU24(v, target); }
101
102// Stores a big-endian signed 32-bit int to the specified memory address.
103inline void StoreBeS32(uint32_t v, byte* target) { StoreBeU32(v, target); }
104
105// Stores a little-endian signed 32-bit int to the specified memory address.
106inline void StoreLeS32(uint32_t v, byte* target) { StoreLeU32(v, target); }
107
108// Stores a big-endian signed 64-bit int to the specified memory address.
109inline void StoreBeS64(uint64_t v, byte* target) { StoreBeU64(v, target); }
110
111// Stores a little-endian signed 64-bit int to the specified memory address.
112inline void StoreLeS64(uint64_t v, byte* target) { StoreLeU64(v, target); }
113
114#if ROO_IO_IEEE754
115// Stores a big-endian IEEE754 float to the specified memory address.
116inline void StoreBeFloat(float v, byte* target) {
117 static_assert(sizeof(float) == sizeof(uint32_t),
118 "StoreBeFloat requires 32-bit float.");
119 static_assert(std::numeric_limits<float>::is_iec559,
120 "StoreBeFloat requires IEEE754 float.");
122 memcpy(&bits, &v, sizeof(bits));
124}
125
126// Stores a little-endian IEEE754 float to the specified memory address.
127inline void StoreLeFloat(float v, byte* target) {
128 static_assert(sizeof(float) == sizeof(uint32_t),
129 "StoreLeFloat requires 32-bit float.");
130 static_assert(std::numeric_limits<float>::is_iec559,
131 "StoreLeFloat requires IEEE754 float.");
133 memcpy(&bits, &v, sizeof(bits));
135}
136
137// Stores a big-endian IEEE754 double to the specified memory address.
138inline void StoreBeDouble(double v, byte* target) {
139 static_assert(sizeof(double) == sizeof(uint64_t),
140 "StoreBeDouble requires 64-bit double.");
141 static_assert(std::numeric_limits<double>::is_iec559,
142 "StoreBeDouble requires IEEE754 double.");
144 memcpy(&bits, &v, sizeof(bits));
146}
147
148// Stores a little-endian IEEE754 double to the specified memory address.
149inline void StoreLeDouble(double v, byte* target) {
150 static_assert(sizeof(double) == sizeof(uint64_t),
151 "StoreLeDouble requires 64-bit double.");
152 static_assert(std::numeric_limits<double>::is_iec559,
153 "StoreLeDouble requires IEEE754 double.");
155 memcpy(&bits, &v, sizeof(bits));
157}
158#endif // ROO_IO_IEEE754
159
160// Arbitrary types, native encoding.
161
162// Stores a platform-native (implementation-dependent) datum to the specified
163// memory address. T must be default-constructible and have trivial destructor.
164template <typename T>
165inline void StoreHostNative(const T& v, byte* target) {
166 memcpy((char*)target, (const char*)&v, sizeof(v));
167}
168
169// Variants that can be used in code templated on byte order.
170
171// Stores an unsigned 16-bit int to the specified memory address.
172template <ByteOrder byte_order>
173inline void StoreU16(uint16_t v, byte* target);
174
175// Stores an unsigned 24-bit int to the specified memory address.
176template <ByteOrder byte_order>
177inline void StoreU24(uint32_t v, byte* target);
178
179// Stores an unsigned 32-bit int to the specified memory address.
180template <ByteOrder byte_order>
181inline void StoreU32(uint32_t v, byte* target);
182
183// Stores an unsigned 64-bit int to the specified memory address.
184template <ByteOrder byte_order>
185inline void StoreU64(uint64_t v, byte* target);
186
187// Stores a signed 16-bit int to the specified memory address.
188template <ByteOrder byte_order>
189inline void StoreS16(int16_t v, byte* target);
190
191// Stores a signed 24-bit int to the specified memory address.
192template <ByteOrder byte_order>
193inline void StoreS24(int32_t v, byte* target);
194
195// Stores a signed 32-bit int to the specified memory address.
196template <ByteOrder byte_order>
197inline void StoreS32(int32_t v, byte* target);
198
199// Stores a signed 64-bit int to the specified memory address.
200template <ByteOrder byte_order>
201inline void StoreS64(int64_t v, byte* target);
202
203#if ROO_IO_IEEE754
204// Stores an IEEE754 float to the specified memory address.
205template <ByteOrder byte_order>
206inline void StoreFloat(float v, byte* target);
207
208// Stores an IEEE754 double to the specified memory address.
209template <ByteOrder byte_order>
210inline void StoreDouble(double v, byte* target);
211#endif // ROO_IO_IEEE754
212
213// Templated on both byte order and the type.
214
215// Stores an integer to the specified memory address.
216template <ByteOrder byte_order, typename IntegerType>
217inline void StoreInteger(IntegerType v, byte* target);
218
219// Implementation details.
220
221template <>
224}
225
226template <>
229}
230
231template <>
234}
235
236template <>
239}
240
241template <>
244}
245
246template <>
249}
250
251template <>
254}
255
256template <>
259}
260
261template <>
264}
265
266template <>
269}
270
271template <>
274}
275
276template <>
279}
280
281template <>
284}
285
286template <>
289}
290
291template <>
294}
295
296template <>
299}
300
301#if ROO_IO_IEEE754
302template <>
303inline void StoreFloat<kBigEndian>(float v, byte* target) {
305}
306
307template <>
308inline void StoreFloat<kLittleEndian>(float v, byte* target) {
310}
311
312template <>
313inline void StoreDouble<kBigEndian>(double v, byte* target) {
315}
316
317template <>
318inline void StoreDouble<kLittleEndian>(double v, byte* target) {
320}
321#endif // ROO_IO_IEEE754
322
323template <>
327
328template <>
332
333template <>
337
338template <>
342
343template <>
347
348template <>
352
353template <>
357
358template <>
362
363template <>
367
368template <>
372
373} // namespace roo_io
Definition byte.h:6
void StoreS8(int8_t v, byte *target)
Definition store.h:88
void StoreInteger< kLittleEndian, uint64_t >(uint64_t v, byte *target)
Definition store.h:369
void StoreLeS32(uint32_t v, byte *target)
Definition store.h:106
void StoreBeU24(uint32_t v, byte *target)
Definition store.h:32
void StoreS24< kBigEndian >(int32_t v, byte *target)
Definition store.h:272
void StoreU24< kLittleEndian >(uint32_t v, byte *target)
Definition store.h:237
void StoreBeS24(uint32_t v, byte *target)
Definition store.h:97
void StoreU32< kBigEndian >(uint32_t v, byte *target)
Definition store.h:242
void StoreHostNative(const T &v, byte *target)
Definition store.h:165
void StoreU16(uint16_t v, byte *target)
void StoreBeS16(uint16_t v, byte *target)
Definition store.h:91
void StoreInteger< kBigEndian, uint64_t >(uint64_t v, byte *target)
Definition store.h:364
void StoreS24< kLittleEndian >(int32_t v, byte *target)
Definition store.h:277
void StoreBeU16(uint16_t v, byte *target)
Definition store.h:20
void StoreInteger< kBigEndian, uint16_t >(uint16_t v, byte *target)
Definition store.h:344
void StoreS16< kBigEndian >(int16_t v, byte *target)
Definition store.h:262
void StoreInteger< kBigEndian, uint8_t >(uint8_t v, byte *target)
Definition store.h:324
void StoreS64(int64_t v, byte *target)
void StoreU8(uint8_t v, byte *target)
Definition store.h:17
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
void StoreS32(int32_t v, byte *target)
void StoreInteger< kBigEndian, int8_t >(int8_t v, byte *target)
Definition store.h:334
void StoreBeU64(uint64_t v, byte *target)
Definition store.h:62
void StoreU24< kBigEndian >(uint32_t v, byte *target)
Definition store.h:232
void StoreInteger< kBigEndian, uint32_t >(uint32_t v, byte *target)
Definition store.h:354
void StoreS64< kLittleEndian >(int64_t v, byte *target)
Definition store.h:297
void StoreU32< kLittleEndian >(uint32_t v, byte *target)
Definition store.h:247
void StoreS64< kBigEndian >(int64_t v, byte *target)
Definition store.h:292
void StoreInteger< kLittleEndian, int8_t >(int8_t v, byte *target)
Definition store.h:339
void StoreLeU24(uint32_t v, byte *target)
Definition store.h:39
void StoreLeS64(uint64_t v, byte *target)
Definition store.h:112
void StoreLeU64(uint64_t v, byte *target)
Definition store.h:74
void StoreS32< kLittleEndian >(int32_t v, byte *target)
Definition store.h:287
void StoreLeS24(uint32_t v, byte *target)
Definition store.h:100
void StoreU16< kBigEndian >(uint16_t v, byte *target)
Definition store.h:222
void StoreBeS64(uint64_t v, byte *target)
Definition store.h:109
void StoreLeU16(uint16_t v, byte *target)
Definition store.h:26
void StoreS16< kLittleEndian >(int16_t v, byte *target)
Definition store.h:267
void StoreS16(int16_t v, byte *target)
void StoreU64(uint64_t v, byte *target)
void StoreInteger< kLittleEndian, uint32_t >(uint32_t v, byte *target)
Definition store.h:359
void StoreU24(uint32_t v, byte *target)
void StoreLeS16(uint16_t v, byte *target)
Definition store.h:94
void StoreBeS32(uint32_t v, byte *target)
Definition store.h:103
void StoreS24(int32_t v, byte *target)
void StoreU64< kBigEndian >(uint64_t v, byte *target)
Definition store.h:252
void StoreU64< kLittleEndian >(uint64_t v, byte *target)
Definition store.h:257
void StoreS32< kBigEndian >(int32_t v, byte *target)
Definition store.h:282
void StoreInteger(IntegerType v, byte *target)
roo::byte byte
Definition byte.h:8
void StoreBeU32(uint32_t v, byte *target)
Definition store.h:46
void StoreLeU32(uint32_t v, byte *target)
Definition store.h:54
void StoreU16< kLittleEndian >(uint16_t v, byte *target)
Definition store.h:227
void StoreU32(uint32_t v, byte *target)
void StoreInteger< kLittleEndian, uint8_t >(uint8_t v, byte *target)
Definition store.h:329
void StoreInteger< kLittleEndian, uint16_t >(uint16_t v, byte *target)
Definition store.h:349