Fix babeltrace-log incorrect timestamp type
[babeltrace.git] / formats / ctf / types / float.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Floating point 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 17 *
de0ba614 18 * Reference: ISO C99 standard 5.2.4
6dc2ca62
MD
19 */
20
d79865b9 21#include <babeltrace/ctf/types.h>
6dc2ca62 22#include <glib.h>
de0ba614 23#include <float.h> /* C99 floating point definitions */
a3dbc794 24#include <limits.h> /* C99 limits */
6dc2ca62
MD
25#include <endian.h>
26
de0ba614
MD
27/*
28 * This library is limited to binary representation of floating point values.
29 * Sign-extension of the exponents is assumed to keep the NaN, +inf, -inf
30 * values, but this should be double-checked (TODO).
31 */
32
6dc2ca62
MD
33/*
34 * Aliasing float/double and unsigned long is not strictly permitted by strict
f6625916 35 * aliasing, but in practice declaration prunning is well supported, and this permits
6dc2ca62
MD
36 * us to use per-word read/writes rather than per-byte.
37 */
38
39#if defined(__GNUC__) || defined(__MINGW32__) || defined(_MSC_VER)
40#define HAS_TYPE_PRUNING
41#endif
42
de0ba614
MD
43#if (FLT_RADIX != 2)
44
45#error "Unsupported floating point radix"
46
6dc2ca62 47#endif
6dc2ca62
MD
48
49union doubleIEEE754 {
50 double v;
51#ifdef HAS_TYPE_PRUNING
52 unsigned long bits[(sizeof(double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)];
53#else
54 unsigned char bits[sizeof(double)];
55#endif
56};
57
de0ba614
MD
58union ldoubleIEEE754 {
59 long double v;
60#ifdef HAS_TYPE_PRUNING
61 unsigned long bits[(sizeof(long double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)];
62#else
63 unsigned char bits[sizeof(long double)];
64#endif
65};
66
d11e9c49
MD
67static struct declaration_float *static_ldouble_declaration;
68
de0ba614
MD
69struct pos_len {
70 size_t sign_start, exp_start, mantissa_start, len;
71};
72
c5e74408
MD
73int _ctf_float_copy(struct stream_pos *destp,
74 struct definition_float *dest_definition,
75 struct stream_pos *srcp,
76 const struct definition_float *src_definition)
6dc2ca62 77{
c5e74408
MD
78 int ret;
79
11d43b90 80 /* Read */
d11e9c49 81 if (src_definition->declaration->byte_order == LITTLE_ENDIAN) {
c5e74408
MD
82 ret = ctf_integer_read(srcp, &src_definition->mantissa->p);
83 if (ret)
84 return ret;
85 ret = ctf_integer_read(srcp, &src_definition->exp->p);
86 if (ret)
87 return ret;
88 ret = ctf_integer_read(srcp, &src_definition->sign->p);
89 if (ret)
90 return ret;
de0ba614 91 } else {
c5e74408
MD
92 ret = ctf_integer_read(srcp, &src_definition->sign->p);
93 if (ret)
94 return ret;
95 ret = ctf_integer_read(srcp, &src_definition->exp->p);
96 if (ret)
97 return ret;
98 ret = ctf_integer_read(srcp, &src_definition->mantissa->p);
99 if (ret)
100 return ret;
6dc2ca62 101 }
d11e9c49
MD
102
103 dest_definition->mantissa->value._unsigned =
104 src_definition->mantissa->value._unsigned;
105 dest_definition->exp->value._signed =
106 src_definition->exp->value._signed;
107 dest_definition->sign->value._unsigned =
108 src_definition->sign->value._unsigned;
109
11d43b90 110 /* Write */
d11e9c49 111 if (dest_definition->declaration->byte_order == LITTLE_ENDIAN) {
c5e74408
MD
112 ret = ctf_integer_write(destp, &dest_definition->mantissa->p);
113 if (ret)
114 return ret;
115 ret = ctf_integer_write(destp, &dest_definition->exp->p);
116 if (ret)
117 return ret;
118 ret = ctf_integer_write(destp, &dest_definition->sign->p);
119 if (ret)
120 return ret;
de0ba614 121 } else {
c5e74408
MD
122 ret = ctf_integer_write(destp, &dest_definition->sign->p);
123 if (ret)
124 return ret;
125 ret = ctf_integer_write(destp, &dest_definition->exp->p);
126 if (ret)
127 return ret;
128 ret = ctf_integer_write(destp, &dest_definition->mantissa->p);
129 if (ret)
130 return ret;
6dc2ca62 131 }
c5e74408 132 return 0;
11d43b90 133}
de0ba614 134
c5e74408 135int ctf_float_read(struct stream_pos *ppos, struct definition *definition)
6dc2ca62 136{
d11e9c49
MD
137 struct definition_float *float_definition =
138 container_of(definition, struct definition_float, p);
139 const struct declaration_float *float_declaration =
140 float_definition->declaration;
141 struct ctf_stream_pos *pos = ctf_pos(ppos);
142 union ldoubleIEEE754 u;
143 struct definition *tmpdef =
144 static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p,
98df1c9f 145 NULL, 0, 0, NULL);
d11e9c49
MD
146 struct definition_float *tmpfloat =
147 container_of(tmpdef, struct definition_float, p);
46322b33 148 struct ctf_stream_pos destp;
c5e74408 149 int ret;
11d43b90 150
989c73bc 151 ctf_init_pos(&destp, -1, O_RDWR);
0f980a35 152 destp.base = (char *) u.bits;
d11e9c49
MD
153
154 ctf_align_pos(pos, float_declaration->p.alignment);
c5e74408 155 ret = _ctf_float_copy(&destp.parent, tmpfloat, ppos, float_definition);
d11e9c49
MD
156 float_definition->value = u.v;
157 definition_unref(tmpdef);
c5e74408 158 return ret;
de0ba614
MD
159}
160
c5e74408 161int ctf_float_write(struct stream_pos *ppos, struct definition *definition)
de0ba614 162{
d11e9c49
MD
163 struct definition_float *float_definition =
164 container_of(definition, struct definition_float, p);
165 const struct declaration_float *float_declaration =
166 float_definition->declaration;
167 struct ctf_stream_pos *pos = ctf_pos(ppos);
168 union ldoubleIEEE754 u;
169 struct definition *tmpdef =
170 static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p,
98df1c9f 171 NULL, 0, 0, NULL);
d11e9c49
MD
172 struct definition_float *tmpfloat =
173 container_of(tmpdef, struct definition_float, p);
46322b33 174 struct ctf_stream_pos srcp;
c5e74408 175 int ret;
11d43b90 176
8563e754 177 ctf_init_pos(&srcp, -1, O_RDONLY);
0f980a35 178 srcp.base = (char *) u.bits;
d11e9c49
MD
179
180 u.v = float_definition->value;
181 ctf_align_pos(pos, float_declaration->p.alignment);
c5e74408 182 ret = _ctf_float_copy(ppos, float_definition, &srcp.parent, tmpfloat);
d11e9c49 183 definition_unref(tmpdef);
c5e74408 184 return ret;
de0ba614 185}
6dc2ca62 186
d11e9c49 187void __attribute__((constructor)) ctf_float_init(void)
de0ba614 188{
d11e9c49 189 static_ldouble_declaration =
add40b62 190 float_declaration_new(LDBL_MANT_DIG,
11d43b90
MD
191 sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG,
192 BYTE_ORDER,
193 __alignof__(long double));
de0ba614
MD
194}
195
d11e9c49 196void __attribute__((destructor)) ctf_float_fini(void)
de0ba614 197{
d11e9c49 198 declaration_unref(&static_ldouble_declaration->p);
6dc2ca62 199}
This page took 0.034685 seconds and 4 git commands to generate.