a5917beb14a44a5fc826adcf1ed5d303b2fc0144
[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
22 static
23 struct definition *_float_definition_new(struct declaration *declaration,
24 struct definition_scope *parent_scope,
25 GQuark field_name, int index);
26 static
27 void _float_definition_free(struct definition *definition);
28
29 static
30 void _float_declaration_free(struct declaration *declaration)
31 {
32 struct declaration_float *float_declaration =
33 container_of(declaration, struct declaration_float, p);
34
35 declaration_unref(&float_declaration->exp->p);
36 declaration_unref(&float_declaration->mantissa->p);
37 declaration_unref(&float_declaration->sign->p);
38 g_free(float_declaration);
39 }
40
41 struct declaration_float *
42 float_declaration_new(size_t mantissa_len,
43 size_t exp_len, int byte_order, size_t alignment)
44 {
45 struct declaration_float *float_declaration;
46 struct declaration *declaration;
47
48 float_declaration = g_new(struct declaration_float, 1);
49 declaration = &float_declaration->p;
50 declaration->id = CTF_TYPE_FLOAT;
51 declaration->alignment = alignment;
52 declaration->declaration_free = _float_declaration_free;
53 declaration->definition_new = _float_definition_new;
54 declaration->definition_free = _float_definition_free;
55 declaration->ref = 1;
56 float_declaration->byte_order = byte_order;
57
58 float_declaration->sign = integer_declaration_new(1,
59 byte_order, false, 1);
60 float_declaration->mantissa = integer_declaration_new(mantissa_len - 1,
61 byte_order, false, 1);
62 float_declaration->exp = integer_declaration_new(exp_len,
63 byte_order, true, 1);
64 return float_declaration;
65 }
66
67 static
68 struct definition *
69 _float_definition_new(struct declaration *declaration,
70 struct definition_scope *parent_scope,
71 GQuark field_name, int index)
72 {
73 struct declaration_float *float_declaration =
74 container_of(declaration, struct declaration_float, p);
75 struct definition_float *_float;
76 struct definition *tmp;
77
78 _float = g_new(struct definition_float, 1);
79 declaration_ref(&float_declaration->p);
80 _float->p.declaration = declaration;
81 _float->declaration = float_declaration;
82 if (float_declaration->byte_order == LITTLE_ENDIAN) {
83 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
84 parent_scope, g_quark_from_static_string("mantissa"), 0);
85 _float->mantissa = container_of(tmp, struct definition_integer, p);
86 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
87 parent_scope, g_quark_from_static_string("exp"), 1);
88 _float->exp = container_of(tmp, struct definition_integer, p);
89 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
90 parent_scope, g_quark_from_static_string("sign"), 2);
91 _float->sign = container_of(tmp, struct definition_integer, p);
92 } else {
93 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
94 parent_scope, g_quark_from_static_string("sign"), 0);
95 _float->sign = container_of(tmp, struct definition_integer, p);
96 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
97 parent_scope, g_quark_from_static_string("exp"), 1);
98 _float->exp = container_of(tmp, struct definition_integer, p);
99 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
100 parent_scope, g_quark_from_static_string("mantissa"), 2);
101 _float->mantissa = container_of(tmp, struct definition_integer, p);
102 }
103 _float->p.ref = 1;
104 _float->p.index = index;
105 _float->value = 0.0;
106 return &_float->p;
107 }
108
109 static
110 void _float_definition_free(struct definition *definition)
111 {
112 struct definition_float *_float =
113 container_of(definition, struct definition_float, p);
114
115 definition_unref(&_float->sign->p);
116 definition_unref(&_float->exp->p);
117 definition_unref(&_float->mantissa->p);
118 declaration_unref(_float->p.declaration);
119 g_free(_float);
120 }
This page took 0.030661 seconds and 3 git commands to generate.