CTF float/types compile fixes
[babeltrace.git] / formats / ctf / types / integer.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Integers read/write functions.
5 *
ccd7e1c8 6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62 7 *
ccd7e1c8
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
de0ba614 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
6dc2ca62
MD
17 */
18
4c8bfb7e
MD
19#include <babeltrace/ctf/types.h>
20#include <babeltrace/bitfield.h>
6dc2ca62
MD
21#include <stdint.h>
22#include <glib.h>
23#include <endian.h>
24
7fe00194
MD
25static
26uint64_t _aligned_uint_read(struct stream_pos *pos,
bed864a7 27 const struct type_class_integer *int_class)
6dc2ca62 28{
bed864a7 29 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 30
bed864a7
MD
31 align_pos(pos, int_class->p.alignment);
32 assert(!(pos->offset % CHAR_BIT));
33 switch (int_class->len) {
6dc2ca62
MD
34 case 8:
35 {
36 uint8_t v;
37
bed864a7
MD
38 v = *(const uint8_t *)pos->base;
39 move_pos(pos, int_class->len);
6dc2ca62
MD
40 return v;
41 }
42 case 16:
43 {
44 uint16_t v;
45
bed864a7
MD
46 v = *(const uint16_t *)pos->base;
47 move_pos(pos, int_class->len);
6dc2ca62
MD
48 return rbo ? GUINT16_SWAP_LE_BE(v) : v;
49 }
50 case 32:
51 {
52 uint32_t v;
53
bed864a7
MD
54 v = *(const uint32_t *)pos->base;
55 move_pos(pos, int_class->len);
6dc2ca62
MD
56 return rbo ? GUINT32_SWAP_LE_BE(v) : v;
57 }
58 case 64:
59 {
60 uint64_t v;
61
bed864a7
MD
62 v = *(const uint64_t *)pos->base;
63 move_pos(pos, int_class->len);
6dc2ca62
MD
64 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
65 }
66 default:
67 assert(0);
68 }
69}
70
7fe00194
MD
71static
72int64_t _aligned_int_read(struct stream_pos *pos,
bed864a7 73 const struct type_class_integer *int_class)
6dc2ca62 74{
bed864a7 75 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 76
bed864a7
MD
77 align_pos(pos, int_class->p.alignment);
78 assert(!(pos->offset % CHAR_BIT));
79 switch (int_class->len) {
6dc2ca62
MD
80 case 8:
81 {
82 int8_t v;
83
bed864a7
MD
84 v = *(const int8_t *)pos->base;
85 move_pos(pos, int_class->len);
6dc2ca62
MD
86 return v;
87 }
88 case 16:
89 {
90 int16_t v;
91
bed864a7
MD
92 v = *(const int16_t *)pos->base;
93 move_pos(pos, int_class->len);
6dc2ca62
MD
94 return rbo ? GUINT16_SWAP_LE_BE(v) : v;
95 }
96 case 32:
97 {
98 int32_t v;
99
bed864a7
MD
100 v = *(const int32_t *)pos->base;
101 move_pos(pos, int_class->len);
6dc2ca62
MD
102 return rbo ? GUINT32_SWAP_LE_BE(v) : v;
103 }
104 case 64:
105 {
106 int64_t v;
107
bed864a7
MD
108 v = *(const int64_t *)pos->base;
109 move_pos(pos, int_class->len);
6dc2ca62
MD
110 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
111 }
112 default:
113 assert(0);
114 }
115}
116
7fe00194
MD
117static
118void _aligned_uint_write(struct stream_pos *pos,
bed864a7
MD
119 const struct type_class_integer *int_class,
120 uint64_t v)
6dc2ca62 121{
bed864a7 122 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 123
bed864a7
MD
124 align_pos(pos, int_class->p.alignment);
125 assert(!(pos->offset % CHAR_BIT));
126 if (pos->dummy)
6dc2ca62
MD
127 goto end;
128
bed864a7
MD
129 switch (int_class->len) {
130 case 8: *(uint8_t *) get_pos_addr(pos) = (uint8_t) v;
6dc2ca62
MD
131 break;
132 case 16:
bed864a7
MD
133 *(uint16_t *) get_pos_addr(pos) = rbo ?
134 GUINT16_SWAP_LE_BE((uint16_t) v) :
6dc2ca62
MD
135 (uint16_t) v;
136 break;
137 case 32:
bed864a7
MD
138 *(uint32_t *) get_pos_addr(pos) = rbo ?
139 GUINT32_SWAP_LE_BE((uint32_t) v) :
6dc2ca62
MD
140 (uint32_t) v;
141 break;
142 case 64:
bed864a7
MD
143 *(uint64_t *) get_pos_addr(pos) = rbo ?
144 GUINT64_SWAP_LE_BE(v) : v;
6dc2ca62
MD
145 break;
146 default:
147 assert(0);
148 }
149end:
bed864a7 150 move_pos(pos, int_class->len);
6dc2ca62
MD
151}
152
7fe00194
MD
153static
154void _aligned_int_write(struct stream_pos *pos,
bed864a7
MD
155 const struct type_class_integer *int_class,
156 int64_t v)
6dc2ca62 157{
bed864a7 158 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 159
bed864a7
MD
160 align_pos(pos, int_class->p.alignment);
161 assert(!(pos->offset % CHAR_BIT));
162 if (pos->dummy)
6dc2ca62
MD
163 goto end;
164
bed864a7
MD
165 switch (int_class->len) {
166 case 8: *(int8_t *) get_pos_addr(pos) = (int8_t) v;
6dc2ca62
MD
167 break;
168 case 16:
bed864a7
MD
169 *(int16_t *) get_pos_addr(pos) = rbo ?
170 GUINT16_SWAP_LE_BE((int16_t) v) :
6dc2ca62
MD
171 (int16_t) v;
172 break;
173 case 32:
bed864a7
MD
174 *(int32_t *) get_pos_addr(pos) = rbo ?
175 GUINT32_SWAP_LE_BE((int32_t) v) :
6dc2ca62
MD
176 (int32_t) v;
177 break;
178 case 64:
bed864a7
MD
179 *(int64_t *) get_pos_addr(pos) = rbo ?
180 GUINT64_SWAP_LE_BE(v) : v;
6dc2ca62
MD
181 break;
182 default:
183 assert(0);
184 }
185end:
bed864a7
MD
186 move_pos(pos, int_class->len);
187 return;
6dc2ca62 188}
7fe00194
MD
189
190uint64_t ctf_uint_read(struct stream_pos *pos,
4c8bfb7e 191 const struct type_class_integer *int_class)
7fe00194 192{
47e0f2e2 193 uint64_t v = 0;
7fe00194
MD
194
195 align_pos(pos, int_class->p.alignment);
196 if (int_class->byte_order == LITTLE_ENDIAN)
47e0f2e2
MD
197 bt_bitfield_read_le(pos->base, unsigned long, pos->offset,
198 int_class->len, &v);
7fe00194 199 else
47e0f2e2
MD
200 bt_bitfield_read_be(pos->base, unsigned long, pos->offset,
201 int_class->len, &v);
7fe00194
MD
202 move_pos(pos, int_class->len);
203 return v;
204}
205
206int64_t ctf_int_read(struct stream_pos *pos,
4c8bfb7e 207 const struct type_class_integer *int_class)
7fe00194 208{
47e0f2e2 209 int64_t v = 0;
7fe00194
MD
210
211 align_pos(pos, int_class->p.alignment);
212 if (int_class->byte_order == LITTLE_ENDIAN)
47e0f2e2
MD
213 bt_bitfield_read_le(pos->base, unsigned long, pos->offset,
214 int_class->len, &v);
7fe00194 215 else
47e0f2e2
MD
216 bt_bitfield_read_be(pos->base, unsigned long, pos->offset,
217 int_class->len, &v);
7fe00194
MD
218 move_pos(pos, int_class->len);
219 return v;
220}
221
222void ctf_uint_write(struct stream_pos *pos,
4c8bfb7e 223 const struct type_class_integer *int_class,
7fe00194
MD
224 uint64_t v)
225{
226 align_pos(pos, int_class->p.alignment);
227 if (pos->dummy)
228 goto end;
229 if (int_class->byte_order == LITTLE_ENDIAN)
47e0f2e2
MD
230 bt_bitfield_write_le(pos->base, unsigned long, pos->offset,
231 int_class->len, v);
7fe00194 232 else
47e0f2e2
MD
233 bt_bitfield_write_be(pos->base, unsigned long, pos->offset,
234 int_class->len, v);
7fe00194
MD
235end:
236 move_pos(pos, int_class->len);
237}
238
239void ctf_int_write(struct stream_pos *pos,
4c8bfb7e 240 const struct type_class_integer *int_class,
7fe00194
MD
241 int64_t v)
242{
243 align_pos(pos, int_class->p.alignment);
244 if (pos->dummy)
245 goto end;
246 if (int_class->byte_order == LITTLE_ENDIAN)
47e0f2e2
MD
247 bt_bitfield_write_le(pos->base, unsigned long, pos->offset,
248 int_class->len, v);
7fe00194 249 else
47e0f2e2
MD
250 bt_bitfield_write_be(pos->base, unsigned long, pos->offset,
251 int_class->len, v);
7fe00194
MD
252end:
253 move_pos(pos, int_class->len);
254}
This page took 0.033687 seconds and 4 git commands to generate.