f7f5f23df1135353374f2366ef3a257a3c564b93
4 * Integers read/write functions.
6 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <ctf/ctf-types.h>
24 #include <ctf/bitfield.h>
30 uint64_t _aligned_uint_read(struct stream_pos
*pos
,
31 const struct type_class_integer
*int_class
)
33 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
35 align_pos(pos
, int_class
->p
.alignment
);
36 assert(!(pos
->offset
% CHAR_BIT
));
37 switch (int_class
->len
) {
42 v
= *(const uint8_t *)pos
->base
;
43 move_pos(pos
, int_class
->len
);
50 v
= *(const uint16_t *)pos
->base
;
51 move_pos(pos
, int_class
->len
);
52 return rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
58 v
= *(const uint32_t *)pos
->base
;
59 move_pos(pos
, int_class
->len
);
60 return rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
66 v
= *(const uint64_t *)pos
->base
;
67 move_pos(pos
, int_class
->len
);
68 return rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
76 int64_t _aligned_int_read(struct stream_pos
*pos
,
77 const struct type_class_integer
*int_class
)
79 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
81 align_pos(pos
, int_class
->p
.alignment
);
82 assert(!(pos
->offset
% CHAR_BIT
));
83 switch (int_class
->len
) {
88 v
= *(const int8_t *)pos
->base
;
89 move_pos(pos
, int_class
->len
);
96 v
= *(const int16_t *)pos
->base
;
97 move_pos(pos
, int_class
->len
);
98 return rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
104 v
= *(const int32_t *)pos
->base
;
105 move_pos(pos
, int_class
->len
);
106 return rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
112 v
= *(const int64_t *)pos
->base
;
113 move_pos(pos
, int_class
->len
);
114 return rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
122 void _aligned_uint_write(struct stream_pos
*pos
,
123 const struct type_class_integer
*int_class
,
126 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
128 align_pos(pos
, int_class
->p
.alignment
);
129 assert(!(pos
->offset
% CHAR_BIT
));
133 switch (int_class
->len
) {
134 case 8: *(uint8_t *) get_pos_addr(pos
) = (uint8_t) v
;
137 *(uint16_t *) get_pos_addr(pos
) = rbo
?
138 GUINT16_SWAP_LE_BE((uint16_t) v
) :
142 *(uint32_t *) get_pos_addr(pos
) = rbo
?
143 GUINT32_SWAP_LE_BE((uint32_t) v
) :
147 *(uint64_t *) get_pos_addr(pos
) = rbo
?
148 GUINT64_SWAP_LE_BE(v
) : v
;
154 move_pos(pos
, int_class
->len
);
158 void _aligned_int_write(struct stream_pos
*pos
,
159 const struct type_class_integer
*int_class
,
162 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
164 align_pos(pos
, int_class
->p
.alignment
);
165 assert(!(pos
->offset
% CHAR_BIT
));
169 switch (int_class
->len
) {
170 case 8: *(int8_t *) get_pos_addr(pos
) = (int8_t) v
;
173 *(int16_t *) get_pos_addr(pos
) = rbo
?
174 GUINT16_SWAP_LE_BE((int16_t) v
) :
178 *(int32_t *) get_pos_addr(pos
) = rbo
?
179 GUINT32_SWAP_LE_BE((int32_t) v
) :
183 *(int64_t *) get_pos_addr(pos
) = rbo
?
184 GUINT64_SWAP_LE_BE(v
) : v
;
190 move_pos(pos
, int_class
->len
);
194 uint64_t ctf_uint_read(struct stream_pos
*pos
,
195 const struct type_class_bitfield
*int_class
)
199 align_pos(pos
, int_class
->p
.alignment
);
200 if (int_class
->byte_order
== LITTLE_ENDIAN
)
201 ctf_bitfield_read_le(pos
->base
, pos
->offset
,
204 ctf_bitfield_read_be(pos
->base
, pos
->offset
,
206 move_pos(pos
, int_class
->len
);
210 int64_t ctf_int_read(struct stream_pos
*pos
,
211 const struct type_class_bitfield
*int_class
)
215 align_pos(pos
, int_class
->p
.alignment
);
216 if (int_class
->byte_order
== LITTLE_ENDIAN
)
217 ctf_bitfield_read_le(pos
->base
, pos
->offset
,
220 ctf_bitfield_read_be(pos
->base
, pos
->offset
,
222 move_pos(pos
, int_class
->len
);
226 void ctf_uint_write(struct stream_pos
*pos
,
227 const struct type_class_bitfield
*int_class
,
230 align_pos(pos
, int_class
->p
.alignment
);
233 if (int_class
->byte_order
== LITTLE_ENDIAN
)
234 ctf_bitfield_write_le(pos
->base
, pos
->offset
,
237 ctf_bitfield_write_be(pos
->base
, pos
->offset
,
240 move_pos(pos
, int_class
->len
);
243 void ctf_int_write(struct stream_pos
*pos
,
244 const struct type_class_bitfield
*int_class
,
247 align_pos(pos
, int_class
->p
.alignment
);
250 if (int_class
->byte_order
== LITTLE_ENDIAN
)
251 ctf_bitfield_write_le(pos
->base
, pos
->offset
,
254 ctf_bitfield_write_be(pos
->base
, pos
->offset
,
257 move_pos(pos
, int_class
->len
);
This page took 0.034276 seconds and 4 git commands to generate.