Move to babeltrace namespace
[babeltrace.git] / formats / ctf / types / float.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Floating point 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 21 *
de0ba614 22 * Reference: ISO C99 standard 5.2.4
6dc2ca62
MD
23 */
24
d79865b9 25#include <babeltrace/ctf/types.h>
6dc2ca62 26#include <glib.h>
de0ba614 27#include <float.h> /* C99 floating point definitions */
6dc2ca62
MD
28#include <endian.h>
29
de0ba614
MD
30/*
31 * This library is limited to binary representation of floating point values.
32 * Sign-extension of the exponents is assumed to keep the NaN, +inf, -inf
33 * values, but this should be double-checked (TODO).
34 */
35
6dc2ca62
MD
36/*
37 * Aliasing float/double and unsigned long is not strictly permitted by strict
38 * aliasing, but in practice type prunning is well supported, and this permits
39 * us to use per-word read/writes rather than per-byte.
40 */
41
42#if defined(__GNUC__) || defined(__MINGW32__) || defined(_MSC_VER)
43#define HAS_TYPE_PRUNING
44#endif
45
de0ba614
MD
46#if (FLT_RADIX != 2)
47
48#error "Unsupported floating point radix"
49
6dc2ca62 50#endif
6dc2ca62
MD
51
52union doubleIEEE754 {
53 double v;
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
de0ba614
MD
61union ldoubleIEEE754 {
62 long double v;
63#ifdef HAS_TYPE_PRUNING
64 unsigned long bits[(sizeof(long double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)];
65#else
66 unsigned char bits[sizeof(long double)];
67#endif
68};
69
70struct pos_len {
71 size_t sign_start, exp_start, mantissa_start, len;
72};
73
74void ctf_float_copy(unsigned char *destp, const struct ctf_float *dest,
75 const unsigned char *src, const struct ctf_float *src)
6dc2ca62 76{
de0ba614 77 struct pos_len destpos, srcpos;
dd617f8a
MD
78 union {
79 unsigned long long u;
80 long long s;
81 } tmp;
de0ba614
MD
82
83 destpos.len = dest.exp_len + dest.mantissa_len;
84 if (dest.byte_order == LITTLE_ENDIAN) {
85 destpos.sign_start = destpos.len - 1;
86 destpos.exp_start = destpos.sign_start - dest->exp_len;
87 destpos.mantissa_start = 0;
88 } else {
89 destpos.sign_start = 0;
90 destpos.exp_start = 1;
91 destpos.mantissa_start = destpos.exp_start + dest->exp_len;
6dc2ca62 92 }
de0ba614
MD
93
94 srcpos.len = src.exp_len + src.mantissa_len;
95 if (src.byte_order == LITTLE_ENDIAN) {
96 srcpos.sign_start = srcpos.len - 1;
97 srcpos.exp_start = srcpos.sign_start - src->exp_len;
98 srcpos.mantissa_start = 0;
99 } else {
100 srcpos.sign_start = 0;
101 srcpos.exp_start = 1;
102 srcpos.mantissa_start = srcpos.exp_start + src->exp_len;
6dc2ca62 103 }
de0ba614
MD
104
105 /* sign */
d79865b9
MD
106 tmp.u = ctf_bitfield_unsigned_read(ptr, srcpos.sign_start, 1,
107 src->byte_order);
108 ctf_bitfield_unsigned_write(&u.bits, destpos.sign_start, 1,
109 dest->byte_order, tmp.u);
de0ba614 110
6ca23e32 111 /* mantissa (without leading 1). No sign extend. */
d79865b9
MD
112 tmp.u = ctf_bitfield_unsigned_read(ptr, srcpos.mantissa_start,
113 src->mantissa_len - 1,
114 src->byte_order);
115 ctf_bitfield_unsigned_write(&u.bits, destpos.mantissa_start,
116 dest->mantissa_len - 1, dest->byte_order,
117 tmp.u);
de0ba614
MD
118
119 /* exponent, with sign-extend. */
d79865b9
MD
120 tmp.s = ctf_bitfield_signed_read(ptr, srcpos.exp_start, src->exp_len,
121 src->byte_order);
122 ctf_bitfield_signed_write(&u.bits, destpos.exp_start, dest->exp_len,
123 dest->byte_order, tmp.s);
6dc2ca62
MD
124}
125
de0ba614 126double ctf_double_read(const unsigned char *ptr, const struct ctf_float *src)
6dc2ca62 127{
de0ba614
MD
128 union doubleIEEE754 u;
129 struct ctf_float dest = {
130 .exp_len = sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
131 .mantissa_len = DBL_MANT_DIG,
132 .byte_order = BYTE_ORDER,
133 };
134
d79865b9 135 ctf_float_copy(&u.bits, &dest, ptr, src);
de0ba614
MD
136 return u.v;
137}
138
139size_t ctf_double_write(unsigned char *ptr, const struct ctf_float *dest,
140 double v)
141{
142 union doubleIEEE754 u;
143 struct ctf_float src = {
144 .exp_len = sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
145 .mantissa_len = DBL_MANT_DIG,
146 .byte_order = BYTE_ORDER,
147 };
6dc2ca62
MD
148
149 if (!ptr)
150 goto end;
de0ba614 151 u.v = v;
d79865b9 152 ctf_float_copy(ptr, dest, &u.bits, &src);
de0ba614
MD
153end:
154 return len;
155}
6dc2ca62 156
de0ba614
MD
157long double ctf_ldouble_read(const unsigned char *ptr,
158 const struct ctf_float *src)
159{
160 union ldoubleIEEE754 u;
161 struct ctf_float dest = {
162 .exp_len = sizeof(double) * CHAR_BIT - LDBL_MANT_DIG,
163 .mantissa_len = LDBL_MANT_DIG,
164 .byte_order = BYTE_ORDER,
165 };
166
d79865b9 167 ctf_float_copy(&u.bits, &dest, ptr, src);
de0ba614
MD
168 return u.v;
169}
170
171size_t ctf_ldouble_write(unsigned char *ptr, const struct ctf_float *dest,
172 long double v)
173{
174 union ldoubleIEEE754 u;
175 struct ctf_float src = {
176 .exp_len = sizeof(double) * CHAR_BIT - LDBL_MANT_DIG,
177 .mantissa_len = LDBL_MANT_DIG,
178 .byte_order = BYTE_ORDER,
179 };
180
181 if (!ptr)
182 goto end;
183 u.v = v;
d79865b9 184 ctf_float_copy(ptr, dest, &u.bits, &src);
6dc2ca62
MD
185end:
186 return len;
187}
This page took 0.030423 seconds and 4 git commands to generate.