Commit | Line | Data |
---|---|---|
6dc2ca62 MD |
1 | /* |
2 | * Common Trace Format | |
3 | * | |
4 | * Floating point read/write functions. | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6dc2ca62 | 9 | * |
ccd7e1c8 MD |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
de0ba614 | 16 | * |
ccd7e1c8 MD |
17 | * The above copyright notice and this permission notice shall be included in |
18 | * all copies or substantial portions of the Software. | |
6dc2ca62 | 19 | * |
de0ba614 | 20 | * Reference: ISO C99 standard 5.2.4 |
6dc2ca62 MD |
21 | */ |
22 | ||
d79865b9 | 23 | #include <babeltrace/ctf/types.h> |
6dc2ca62 | 24 | #include <glib.h> |
de0ba614 | 25 | #include <float.h> /* C99 floating point definitions */ |
a3dbc794 | 26 | #include <limits.h> /* C99 limits */ |
9b0422a0 | 27 | #include <babeltrace/endian.h> |
6dc2ca62 | 28 | |
de0ba614 MD |
29 | /* |
30 | * This library is limited to binary representation of floating point values. | |
f72803ca MD |
31 | * We use hardware support for conversion between 32 and 64-bit floating |
32 | * point values. | |
de0ba614 MD |
33 | */ |
34 | ||
6dc2ca62 MD |
35 | /* |
36 | * Aliasing float/double and unsigned long is not strictly permitted by strict | |
f6625916 | 37 | * aliasing, but in practice declaration prunning is well supported, and this permits |
6dc2ca62 MD |
38 | * us to use per-word read/writes rather than per-byte. |
39 | */ | |
40 | ||
41 | #if defined(__GNUC__) || defined(__MINGW32__) || defined(_MSC_VER) | |
42 | #define HAS_TYPE_PRUNING | |
43 | #endif | |
44 | ||
de0ba614 MD |
45 | #if (FLT_RADIX != 2) |
46 | ||
47 | #error "Unsupported floating point radix" | |
48 | ||
6dc2ca62 | 49 | #endif |
6dc2ca62 MD |
50 | |
51 | union doubleIEEE754 { | |
f72803ca MD |
52 | double vd; |
53 | float vf; | |
6dc2ca62 MD |
54 | #ifdef HAS_TYPE_PRUNING |
55 | unsigned long bits[(sizeof(double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)]; | |
56 | #else | |
57 | unsigned char bits[sizeof(double)]; | |
58 | #endif | |
59 | }; | |
60 | ||
f72803ca MD |
61 | static struct declaration_float *static_float_declaration, |
62 | *static_double_declaration; | |
d11e9c49 | 63 | |
de0ba614 MD |
64 | struct pos_len { |
65 | size_t sign_start, exp_start, mantissa_start, len; | |
66 | }; | |
67 | ||
c5e74408 MD |
68 | int _ctf_float_copy(struct stream_pos *destp, |
69 | struct definition_float *dest_definition, | |
70 | struct stream_pos *srcp, | |
71 | const struct definition_float *src_definition) | |
6dc2ca62 | 72 | { |
c5e74408 MD |
73 | int ret; |
74 | ||
f72803ca MD |
75 | /* We only support copy of same-size floats for now */ |
76 | assert(src_definition->declaration->sign->len == | |
77 | dest_definition->declaration->sign->len); | |
78 | assert(src_definition->declaration->exp->len == | |
79 | dest_definition->declaration->exp->len); | |
80 | assert(src_definition->declaration->mantissa->len == | |
81 | dest_definition->declaration->mantissa->len); | |
11d43b90 | 82 | /* Read */ |
d11e9c49 | 83 | if (src_definition->declaration->byte_order == LITTLE_ENDIAN) { |
c5e74408 MD |
84 | ret = ctf_integer_read(srcp, &src_definition->mantissa->p); |
85 | if (ret) | |
86 | return ret; | |
87 | ret = ctf_integer_read(srcp, &src_definition->exp->p); | |
88 | if (ret) | |
89 | return ret; | |
90 | ret = ctf_integer_read(srcp, &src_definition->sign->p); | |
91 | if (ret) | |
92 | return ret; | |
de0ba614 | 93 | } else { |
c5e74408 MD |
94 | ret = ctf_integer_read(srcp, &src_definition->sign->p); |
95 | if (ret) | |
96 | return ret; | |
97 | ret = ctf_integer_read(srcp, &src_definition->exp->p); | |
98 | if (ret) | |
99 | return ret; | |
100 | ret = ctf_integer_read(srcp, &src_definition->mantissa->p); | |
101 | if (ret) | |
102 | return ret; | |
6dc2ca62 | 103 | } |
d11e9c49 MD |
104 | |
105 | dest_definition->mantissa->value._unsigned = | |
106 | src_definition->mantissa->value._unsigned; | |
107 | dest_definition->exp->value._signed = | |
108 | src_definition->exp->value._signed; | |
109 | dest_definition->sign->value._unsigned = | |
110 | src_definition->sign->value._unsigned; | |
111 | ||
11d43b90 | 112 | /* Write */ |
d11e9c49 | 113 | if (dest_definition->declaration->byte_order == LITTLE_ENDIAN) { |
c5e74408 MD |
114 | ret = ctf_integer_write(destp, &dest_definition->mantissa->p); |
115 | if (ret) | |
116 | return ret; | |
117 | ret = ctf_integer_write(destp, &dest_definition->exp->p); | |
118 | if (ret) | |
119 | return ret; | |
120 | ret = ctf_integer_write(destp, &dest_definition->sign->p); | |
121 | if (ret) | |
122 | return ret; | |
de0ba614 | 123 | } else { |
c5e74408 MD |
124 | ret = ctf_integer_write(destp, &dest_definition->sign->p); |
125 | if (ret) | |
126 | return ret; | |
127 | ret = ctf_integer_write(destp, &dest_definition->exp->p); | |
128 | if (ret) | |
129 | return ret; | |
130 | ret = ctf_integer_write(destp, &dest_definition->mantissa->p); | |
131 | if (ret) | |
132 | return ret; | |
6dc2ca62 | 133 | } |
c5e74408 | 134 | return 0; |
11d43b90 | 135 | } |
de0ba614 | 136 | |
c5e74408 | 137 | int ctf_float_read(struct stream_pos *ppos, struct definition *definition) |
6dc2ca62 | 138 | { |
d11e9c49 MD |
139 | struct definition_float *float_definition = |
140 | container_of(definition, struct definition_float, p); | |
141 | const struct declaration_float *float_declaration = | |
142 | float_definition->declaration; | |
143 | struct ctf_stream_pos *pos = ctf_pos(ppos); | |
f72803ca MD |
144 | union doubleIEEE754 u; |
145 | struct definition *tmpdef; | |
146 | struct definition_float *tmpfloat; | |
46322b33 | 147 | struct ctf_stream_pos destp; |
aee35fcc | 148 | struct mmap_align mma; |
c5e74408 | 149 | int ret; |
11d43b90 | 150 | |
f72803ca MD |
151 | switch (float_declaration->mantissa->len + 1) { |
152 | case FLT_MANT_DIG: | |
153 | tmpdef = static_float_declaration->p.definition_new( | |
154 | &static_float_declaration->p, | |
155 | NULL, 0, 0, "__tmpfloat"); | |
156 | break; | |
157 | case DBL_MANT_DIG: | |
158 | tmpdef = static_double_declaration->p.definition_new( | |
159 | &static_double_declaration->p, | |
160 | NULL, 0, 0, "__tmpfloat"); | |
161 | break; | |
162 | default: | |
163 | return -EINVAL; | |
164 | } | |
165 | tmpfloat = container_of(tmpdef, struct definition_float, p); | |
86b06ede | 166 | memset(&destp, 0, sizeof(destp)); |
989c73bc | 167 | ctf_init_pos(&destp, -1, O_RDWR); |
aee35fcc MD |
168 | mmap_align_set_addr(&mma, (char *) u.bits); |
169 | destp.base_mma = &mma; | |
f72803ca | 170 | destp.packet_size = sizeof(u) * CHAR_BIT; |
d11e9c49 | 171 | ctf_align_pos(pos, float_declaration->p.alignment); |
c5e74408 | 172 | ret = _ctf_float_copy(&destp.parent, tmpfloat, ppos, float_definition); |
f72803ca MD |
173 | switch (float_declaration->mantissa->len + 1) { |
174 | case FLT_MANT_DIG: | |
175 | float_definition->value = u.vf; | |
176 | break; | |
177 | case DBL_MANT_DIG: | |
178 | float_definition->value = u.vd; | |
179 | break; | |
180 | default: | |
181 | return -EINVAL; | |
182 | } | |
d11e9c49 | 183 | definition_unref(tmpdef); |
c5e74408 | 184 | return ret; |
de0ba614 MD |
185 | } |
186 | ||
c5e74408 | 187 | int ctf_float_write(struct stream_pos *ppos, struct definition *definition) |
de0ba614 | 188 | { |
d11e9c49 MD |
189 | struct definition_float *float_definition = |
190 | container_of(definition, struct definition_float, p); | |
191 | const struct declaration_float *float_declaration = | |
192 | float_definition->declaration; | |
193 | struct ctf_stream_pos *pos = ctf_pos(ppos); | |
f72803ca MD |
194 | union doubleIEEE754 u; |
195 | struct definition *tmpdef; | |
196 | struct definition_float *tmpfloat; | |
46322b33 | 197 | struct ctf_stream_pos srcp; |
aee35fcc | 198 | struct mmap_align mma; |
c5e74408 | 199 | int ret; |
11d43b90 | 200 | |
f72803ca MD |
201 | switch (float_declaration->mantissa->len + 1) { |
202 | case FLT_MANT_DIG: | |
203 | tmpdef = static_float_declaration->p.definition_new( | |
204 | &static_float_declaration->p, | |
205 | NULL, 0, 0, "__tmpfloat"); | |
206 | break; | |
207 | case DBL_MANT_DIG: | |
208 | tmpdef = static_double_declaration->p.definition_new( | |
209 | &static_double_declaration->p, | |
210 | NULL, 0, 0, "__tmpfloat"); | |
211 | break; | |
212 | default: | |
213 | return -EINVAL; | |
214 | } | |
215 | tmpfloat = container_of(tmpdef, struct definition_float, p); | |
8563e754 | 216 | ctf_init_pos(&srcp, -1, O_RDONLY); |
aee35fcc MD |
217 | mmap_align_set_addr(&mma, (char *) u.bits); |
218 | srcp.base_mma = &mma; | |
f72803ca MD |
219 | srcp.packet_size = sizeof(u) * CHAR_BIT; |
220 | switch (float_declaration->mantissa->len + 1) { | |
221 | case FLT_MANT_DIG: | |
222 | u.vf = float_definition->value; | |
223 | break; | |
224 | case DBL_MANT_DIG: | |
225 | u.vd = float_definition->value; | |
226 | break; | |
227 | default: | |
228 | return -EINVAL; | |
229 | } | |
d11e9c49 | 230 | ctf_align_pos(pos, float_declaration->p.alignment); |
c5e74408 | 231 | ret = _ctf_float_copy(ppos, float_definition, &srcp.parent, tmpfloat); |
d11e9c49 | 232 | definition_unref(tmpdef); |
c5e74408 | 233 | return ret; |
de0ba614 | 234 | } |
6dc2ca62 | 235 | |
d11e9c49 | 236 | void __attribute__((constructor)) ctf_float_init(void) |
de0ba614 | 237 | { |
f72803ca MD |
238 | static_float_declaration = |
239 | float_declaration_new(FLT_MANT_DIG, | |
240 | sizeof(float) * CHAR_BIT - FLT_MANT_DIG, | |
241 | BYTE_ORDER, | |
242 | __alignof__(float)); | |
243 | static_double_declaration = | |
244 | float_declaration_new(DBL_MANT_DIG, | |
245 | sizeof(double) * CHAR_BIT - DBL_MANT_DIG, | |
11d43b90 | 246 | BYTE_ORDER, |
f72803ca | 247 | __alignof__(double)); |
de0ba614 MD |
248 | } |
249 | ||
d11e9c49 | 250 | void __attribute__((destructor)) ctf_float_fini(void) |
de0ba614 | 251 | { |
f72803ca MD |
252 | declaration_unref(&static_float_declaration->p); |
253 | declaration_unref(&static_double_declaration->p); | |
6dc2ca62 | 254 | } |