Commit | Line | Data |
---|---|---|
0a46062b | 1 | /* |
ccd7e1c8 | 2 | * float.c |
0a46062b | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Float Type Converter |
0a46062b | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
0a46062b | 7 | * |
ccd7e1c8 MD |
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: | |
0a46062b | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
0a46062b MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 20 | #include <babeltrace/format.h> |
0a46062b | 21 | |
c054553d | 22 | static |
e19c3d69 | 23 | struct declaration *_float_declaration_new(struct type *type, |
c054553d MD |
24 | struct declaration_scope *parent_scope); |
25 | static | |
e19c3d69 | 26 | void _float_declaration_free(struct declaration *declaration); |
c054553d | 27 | |
4c8bfb7e MD |
28 | void float_copy(struct stream_pos *destp, |
29 | const struct format *fdest, | |
30 | struct stream_pos *srcp, | |
31 | const struct format *fsrc, | |
e19c3d69 | 32 | struct declaration *declaration) |
0a46062b | 33 | { |
e19c3d69 MD |
34 | struct declaration_float *_float = |
35 | container_of(declaration, struct declaration_float, p); | |
36 | struct type_float *float_type = _float->type; | |
0a46062b MD |
37 | |
38 | if (fsrc->float_copy == fdest->float_copy) { | |
e19c3d69 | 39 | fsrc->float_copy(destp, srcp, float_type); |
0a46062b MD |
40 | } else { |
41 | double v; | |
42 | ||
e19c3d69 MD |
43 | v = fsrc->double_read(srcp, float_type); |
44 | fdest->double_write(destp, float_type, v); | |
0a46062b MD |
45 | } |
46 | } | |
47 | ||
c054553d | 48 | static |
e19c3d69 | 49 | void _float_type_free(struct type *type) |
90b676d7 | 50 | { |
e19c3d69 MD |
51 | struct type_float *float_type = |
52 | container_of(type, struct type_float, p); | |
c054553d | 53 | |
e19c3d69 MD |
54 | type_unref(&float_type->exp->p); |
55 | type_unref(&float_type->mantissa->p); | |
56 | type_unref(&float_type->sign->p); | |
57 | g_free(float_type); | |
90b676d7 MD |
58 | } |
59 | ||
e19c3d69 MD |
60 | struct type_float * |
61 | float_type_new(const char *name, size_t mantissa_len, | |
62 | size_t exp_len, int byte_order, size_t alignment) | |
0a46062b | 63 | { |
e19c3d69 MD |
64 | struct type_float *float_type; |
65 | struct type *type; | |
0a46062b | 66 | |
e19c3d69 MD |
67 | float_type = g_new(struct type_float, 1); |
68 | type = &float_type->p; | |
69 | type->name = g_quark_from_string(name); | |
70 | type->alignment = alignment; | |
71 | type->copy = float_copy; | |
72 | type->type_free = _float_type_free; | |
73 | type->declaration_new = _float_declaration_new; | |
74 | type->declaration_free = _float_declaration_free; | |
75 | type->ref = 1; | |
76 | float_type->byte_order = byte_order; | |
77 | ||
78 | float_type->sign = integer_type_new(NULL, 1, | |
79 | byte_order, false, 1); | |
e19c3d69 MD |
80 | float_type->mantissa = integer_type_new(NULL, mantissa_len - 1, |
81 | byte_order, false, 1); | |
e19c3d69 MD |
82 | float_type->exp = integer_type_new(NULL, exp_len, |
83 | byte_order, true, 1); | |
e19c3d69 | 84 | return float_type; |
0a46062b | 85 | } |
c054553d MD |
86 | |
87 | static | |
e19c3d69 MD |
88 | struct declaration * |
89 | _float_declaration_new(struct type *type, | |
90 | struct declaration_scope *parent_scope) | |
c054553d | 91 | { |
e19c3d69 MD |
92 | struct type_float *float_type = |
93 | container_of(type, struct type_float, p); | |
94 | struct declaration_float *_float; | |
c054553d | 95 | |
e19c3d69 | 96 | _float = g_new(struct declaration_float, 1); |
6ee5115e MD |
97 | type_ref(&float_type->p); |
98 | _float->p.type= type; | |
99 | _float->type= float_type; | |
c054553d MD |
100 | _float->p.ref = 1; |
101 | _float->value = 0.0; | |
102 | return &_float->p; | |
103 | } | |
104 | ||
105 | static | |
e19c3d69 | 106 | void _float_declaration_free(struct declaration *declaration) |
c054553d | 107 | { |
e19c3d69 MD |
108 | struct declaration_float *_float = |
109 | container_of(declaration, struct declaration_float, p); | |
c054553d | 110 | |
e19c3d69 | 111 | type_unref(_float->p.type); |
c054553d MD |
112 | g_free(_float); |
113 | } |