Finish float
[babeltrace.git] / formats / ctf / types / integer.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Integers read/write functions.
5 *
de0ba614 6 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62 7 *
de0ba614
MD
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.
12 *
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.
17 *
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
6dc2ca62
MD
21 */
22
23#include <ctf/ctf-types.h>
24#include <stdint.h>
25#include <glib.h>
26#include <endian.h>
27
bed864a7
MD
28uint64_t ctf_uint_read(struct stream_pos *pos,
29 const struct type_class_integer *int_class)
6dc2ca62 30{
bed864a7 31 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 32
bed864a7
MD
33 align_pos(pos, int_class->p.alignment);
34 assert(!(pos->offset % CHAR_BIT));
35 switch (int_class->len) {
6dc2ca62
MD
36 case 8:
37 {
38 uint8_t v;
39
bed864a7
MD
40 v = *(const uint8_t *)pos->base;
41 move_pos(pos, int_class->len);
6dc2ca62
MD
42 return v;
43 }
44 case 16:
45 {
46 uint16_t v;
47
bed864a7
MD
48 v = *(const uint16_t *)pos->base;
49 move_pos(pos, int_class->len);
6dc2ca62
MD
50 return rbo ? GUINT16_SWAP_LE_BE(v) : v;
51 }
52 case 32:
53 {
54 uint32_t v;
55
bed864a7
MD
56 v = *(const uint32_t *)pos->base;
57 move_pos(pos, int_class->len);
6dc2ca62
MD
58 return rbo ? GUINT32_SWAP_LE_BE(v) : v;
59 }
60 case 64:
61 {
62 uint64_t v;
63
bed864a7
MD
64 v = *(const uint64_t *)pos->base;
65 move_pos(pos, int_class->len);
6dc2ca62
MD
66 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
67 }
68 default:
69 assert(0);
70 }
71}
72
bed864a7
MD
73int64_t ctf_int_read(struct stream_pos *pos,
74 const struct type_class_integer *int_class)
6dc2ca62 75{
bed864a7 76 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 77
bed864a7
MD
78 align_pos(pos, int_class->p.alignment);
79 assert(!(pos->offset % CHAR_BIT));
80 switch (int_class->len) {
6dc2ca62
MD
81 case 8:
82 {
83 int8_t v;
84
bed864a7
MD
85 v = *(const int8_t *)pos->base;
86 move_pos(pos, int_class->len);
6dc2ca62
MD
87 return v;
88 }
89 case 16:
90 {
91 int16_t v;
92
bed864a7
MD
93 v = *(const int16_t *)pos->base;
94 move_pos(pos, int_class->len);
6dc2ca62
MD
95 return rbo ? GUINT16_SWAP_LE_BE(v) : v;
96 }
97 case 32:
98 {
99 int32_t v;
100
bed864a7
MD
101 v = *(const int32_t *)pos->base;
102 move_pos(pos, int_class->len);
6dc2ca62
MD
103 return rbo ? GUINT32_SWAP_LE_BE(v) : v;
104 }
105 case 64:
106 {
107 int64_t v;
108
bed864a7
MD
109 v = *(const int64_t *)pos->base;
110 move_pos(pos, int_class->len);
6dc2ca62
MD
111 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
112 }
113 default:
114 assert(0);
115 }
116}
117
bed864a7
MD
118void ctf_uint_write(struct stream_pos *pos,
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
bed864a7
MD
153void ctf_int_write(struct stream_pos *pos,
154 const struct type_class_integer *int_class,
155 int64_t v)
6dc2ca62 156{
bed864a7 157 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 158
bed864a7
MD
159 align_pos(pos, int_class->p.alignment);
160 assert(!(pos->offset % CHAR_BIT));
161 if (pos->dummy)
6dc2ca62
MD
162 goto end;
163
bed864a7
MD
164 switch (int_class->len) {
165 case 8: *(int8_t *) get_pos_addr(pos) = (int8_t) v;
6dc2ca62
MD
166 break;
167 case 16:
bed864a7
MD
168 *(int16_t *) get_pos_addr(pos) = rbo ?
169 GUINT16_SWAP_LE_BE((int16_t) v) :
6dc2ca62
MD
170 (int16_t) v;
171 break;
172 case 32:
bed864a7
MD
173 *(int32_t *) get_pos_addr(pos) = rbo ?
174 GUINT32_SWAP_LE_BE((int32_t) v) :
6dc2ca62
MD
175 (int32_t) v;
176 break;
177 case 64:
bed864a7
MD
178 *(int64_t *) get_pos_addr(pos) = rbo ?
179 GUINT64_SWAP_LE_BE(v) : v;
6dc2ca62
MD
180 break;
181 default:
182 assert(0);
183 }
184end:
bed864a7
MD
185 move_pos(pos, int_class->len);
186 return;
6dc2ca62 187}
This page took 0.046404 seconds and 4 git commands to generate.