Rename "type" to "declaration"
[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 static
26 void _float_definition_free(struct definition *definition);
27
28 void float_copy(struct stream_pos *destp,
29 const struct format *fdest,
30 struct stream_pos *srcp,
31 const struct format *fsrc,
32 struct definition *definition)
33 {
34 struct definition_float *_float =
35 container_of(definition, struct definition_float, p);
36 struct declaration_float *float_declaration = _float->declaration;
37
38 if (fsrc->float_copy == fdest->float_copy) {
39 fsrc->float_copy(destp, srcp, float_declaration);
40 } else {
41 double v;
42
43 v = fsrc->double_read(srcp, float_declaration);
44 fdest->double_write(destp, float_declaration, v);
45 }
46 }
47
48 static
49 void _float_declaration_free(struct declaration *declaration)
50 {
51 struct declaration_float *float_declaration =
52 container_of(declaration, struct declaration_float, p);
53
54 declaration_unref(&float_declaration->exp->p);
55 declaration_unref(&float_declaration->mantissa->p);
56 declaration_unref(&float_declaration->sign->p);
57 g_free(float_declaration);
58 }
59
60 struct declaration_float *
61 float_declaration_new(const char *name, size_t mantissa_len,
62 size_t exp_len, int byte_order, size_t alignment)
63 {
64 struct declaration_float *float_declaration;
65 struct declaration *declaration;
66
67 float_declaration = g_new(struct declaration_float, 1);
68 declaration = &float_declaration->p;
69 declaration->id = CTF_TYPE_FLOAT;
70 declaration->name = g_quark_from_string(name);
71 declaration->alignment = alignment;
72 declaration->copy = float_copy;
73 declaration->declaration_free = _float_declaration_free;
74 declaration->definition_new = _float_definition_new;
75 declaration->definition_free = _float_definition_free;
76 declaration->ref = 1;
77 float_declaration->byte_order = byte_order;
78
79 float_declaration->sign = integer_declaration_new(NULL, 1,
80 byte_order, false, 1);
81 float_declaration->mantissa = integer_declaration_new(NULL, mantissa_len - 1,
82 byte_order, false, 1);
83 float_declaration->exp = integer_declaration_new(NULL, exp_len,
84 byte_order, true, 1);
85 return float_declaration;
86 }
87
88 static
89 struct definition *
90 _float_definition_new(struct declaration *declaration,
91 struct definition_scope *parent_scope)
92 {
93 struct declaration_float *float_declaration =
94 container_of(declaration, struct declaration_float, p);
95 struct definition_float *_float;
96
97 _float = g_new(struct definition_float, 1);
98 declaration_ref(&float_declaration->p);
99 _float->p.declaration= declaration;
100 _float->declaration= float_declaration;
101 _float->p.ref = 1;
102 _float->value = 0.0;
103 return &_float->p;
104 }
105
106 static
107 void _float_definition_free(struct definition *definition)
108 {
109 struct definition_float *_float =
110 container_of(definition, struct definition_float, p);
111
112 declaration_unref(_float->p.declaration);
113 g_free(_float);
114 }
This page took 0.032924 seconds and 5 git commands to generate.