Finish float
[babeltrace.git] / types / float.c
CommitLineData
0a46062b
MD
1/*
2 * BabelTrace - Float Type Converter
3 *
4 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <babeltrace/compiler.h>
448d3cc7 22#include <babeltrace/types.h>
0a46062b 23
bed864a7
MD
24void float_copy(struct stream_pos *dest, const struct format *fdest,
25 struct stream_pos *src, const struct format *fsrc,
26 const struct type_class *type_class)
0a46062b
MD
27{
28 struct type_class_float *float_class =
29 container_of(type_class, struct type_class_float, p);
30
31 if (fsrc->float_copy == fdest->float_copy) {
11d43b90 32 fsrc->float_copy(dest, src, float_class);
0a46062b
MD
33 } else {
34 double v;
35
11d43b90
MD
36 v = fsrc->double_read(src, float_class);
37 fdest->double_write(dest, float_class, v);
0a46062b
MD
38 }
39}
40
90b676d7
MD
41void float_type_free(struct type_class_float *float_class)
42{
43 g_free(float_class);
44}
45
46static void _float_type_free(struct type_class *type_class)
47{
48 struct type_class_float *float_class =
49 container_of(type_class, struct type_class_float, p);
50 float_type_free(float_class);
51}
52
0a46062b
MD
53struct type_class_float *float_type_new(const char *name,
54 size_t mantissa_len,
55 size_t exp_len, int byte_order,
56 size_t alignment)
57{
58 struct type_class_float *float_class;
bed864a7
MD
59 struct type_class_bitfield *bitfield_class;
60 struct type_class_integer *int_class;
61 struct type_class *type_class;
0a46062b
MD
62 int ret;
63
0a46062b 64 float_class = g_new(struct type_class_float, 1);
bed864a7
MD
65 type_class = &float_class->p;
66
67 type_class->name = g_quark_from_string(name);
68 type_class->alignment = alignment;
69 type_class->copy = float_copy;
70 type_class->free = _float_type_free;
0a46062b 71 float_class->byte_order = byte_order;
bed864a7 72
11d43b90
MD
73 float_class->sign = bitfield_type_new(NULL, 1,
74 byte_order, false, 1);
75 if (!float_class->mantissa)
76 goto error_sign;
77 float_class->mantissa = bitfield_type_new(NULL, mantissa_len - 1,
bed864a7
MD
78 byte_order, false, 1);
79 if (!float_class->mantissa)
80 goto error_mantissa;
81 float_class->exp = bitfield_type_new(NULL, exp_len,
82 byte_order, true, 1);
83 if (!float_class->exp)
84 goto error_exp;
85
0a46062b
MD
86 if (float_class->p.name) {
87 ret = ctf_register_type(&float_class->p);
bed864a7
MD
88 if (ret)
89 goto error_register;
0a46062b
MD
90 }
91 return float_class;
bed864a7
MD
92
93error_register:
94 bitfield_type_free(float_class->exp);
95error_exp:
96 bitfield_type_free(float_class->mantissa);
97error_mantissa:
11d43b90
MD
98 bitfield_type_free(float_class->sign);
99error_sign:
bed864a7
MD
100 g_free(float_class);
101 return NULL;
0a46062b 102}
This page took 0.029581 seconds and 4 git commands to generate.