1efd5ed3624ccf4be0c9469d1a88aabd55d44e5a
[babeltrace.git] / types / float.c
1 /*
2 * float.c
3 *
4 * BabelTrace - Float Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
21 #include <endian.h>
22
23 static
24 struct definition *_float_definition_new(struct declaration *declaration,
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index);
27 static
28 void _float_definition_free(struct definition *definition);
29
30 static
31 void _float_declaration_free(struct declaration *declaration)
32 {
33 struct declaration_float *float_declaration =
34 container_of(declaration, struct declaration_float, p);
35
36 declaration_unref(&float_declaration->exp->p);
37 declaration_unref(&float_declaration->mantissa->p);
38 declaration_unref(&float_declaration->sign->p);
39 g_free(float_declaration);
40 }
41
42 struct declaration_float *
43 float_declaration_new(size_t mantissa_len,
44 size_t exp_len, int byte_order, size_t alignment)
45 {
46 struct declaration_float *float_declaration;
47 struct declaration *declaration;
48
49 float_declaration = g_new(struct declaration_float, 1);
50 declaration = &float_declaration->p;
51 declaration->id = CTF_TYPE_FLOAT;
52 declaration->alignment = alignment;
53 declaration->declaration_free = _float_declaration_free;
54 declaration->definition_new = _float_definition_new;
55 declaration->definition_free = _float_definition_free;
56 declaration->ref = 1;
57 float_declaration->byte_order = byte_order;
58
59 float_declaration->sign = integer_declaration_new(1,
60 byte_order, false, 1);
61 float_declaration->mantissa = integer_declaration_new(mantissa_len - 1,
62 byte_order, false, 1);
63 float_declaration->exp = integer_declaration_new(exp_len,
64 byte_order, true, 1);
65 return float_declaration;
66 }
67
68 static
69 struct definition *
70 _float_definition_new(struct declaration *declaration,
71 struct definition_scope *parent_scope,
72 GQuark field_name, int index)
73 {
74 struct declaration_float *float_declaration =
75 container_of(declaration, struct declaration_float, p);
76 struct definition_float *_float;
77 struct definition *tmp;
78
79 _float = g_new(struct definition_float, 1);
80 declaration_ref(&float_declaration->p);
81 _float->p.declaration = declaration;
82 _float->declaration = float_declaration;
83 if (float_declaration->byte_order == LITTLE_ENDIAN) {
84 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
85 parent_scope, g_quark_from_static_string("mantissa"), 0);
86 _float->mantissa = container_of(tmp, struct definition_integer, p);
87 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
88 parent_scope, g_quark_from_static_string("exp"), 1);
89 _float->exp = container_of(tmp, struct definition_integer, p);
90 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
91 parent_scope, g_quark_from_static_string("sign"), 2);
92 _float->sign = container_of(tmp, struct definition_integer, p);
93 } else {
94 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
95 parent_scope, g_quark_from_static_string("sign"), 0);
96 _float->sign = container_of(tmp, struct definition_integer, p);
97 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
98 parent_scope, g_quark_from_static_string("exp"), 1);
99 _float->exp = container_of(tmp, struct definition_integer, p);
100 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
101 parent_scope, g_quark_from_static_string("mantissa"), 2);
102 _float->mantissa = container_of(tmp, struct definition_integer, p);
103 }
104 _float->p.ref = 1;
105 _float->p.index = index;
106 _float->value = 0.0;
107 return &_float->p;
108 }
109
110 static
111 void _float_definition_free(struct definition *definition)
112 {
113 struct definition_float *_float =
114 container_of(definition, struct definition_float, p);
115
116 definition_unref(&_float->sign->p);
117 definition_unref(&_float->exp->p);
118 definition_unref(&_float->mantissa->p);
119 declaration_unref(_float->p.declaration);
120 g_free(_float);
121 }
This page took 0.031771 seconds and 4 git commands to generate.