Commit | Line | Data |
---|---|---|
0a46062b | 1 | /* |
ccd7e1c8 | 2 | * float.c |
0a46062b | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Float Type Converter |
0a46062b | 5 | * |
ccd7e1c8 | 6 | * Copyright 2010 - 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 | |
4c8bfb7e MD |
22 | void float_copy(struct stream_pos *destp, |
23 | const struct format *fdest, | |
24 | struct stream_pos *srcp, | |
25 | const struct format *fsrc, | |
bed864a7 | 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) { | |
4c8bfb7e | 32 | fsrc->float_copy(destp, srcp, float_class); |
0a46062b MD |
33 | } else { |
34 | double v; | |
35 | ||
4c8bfb7e MD |
36 | v = fsrc->double_read(srcp, float_class); |
37 | fdest->double_write(destp, float_class, v); | |
0a46062b MD |
38 | } |
39 | } | |
40 | ||
90b676d7 MD |
41 | void float_type_free(struct type_class_float *float_class) |
42 | { | |
d06d03db MD |
43 | integer_type_free(float_class->exp); |
44 | integer_type_free(float_class->mantissa); | |
45 | integer_type_free(float_class->sign); | |
90b676d7 MD |
46 | g_free(float_class); |
47 | } | |
48 | ||
49 | static void _float_type_free(struct type_class *type_class) | |
50 | { | |
51 | struct type_class_float *float_class = | |
52 | container_of(type_class, struct type_class_float, p); | |
53 | float_type_free(float_class); | |
54 | } | |
55 | ||
0a46062b MD |
56 | struct type_class_float *float_type_new(const char *name, |
57 | size_t mantissa_len, | |
58 | size_t exp_len, int byte_order, | |
59 | size_t alignment) | |
60 | { | |
61 | struct type_class_float *float_class; | |
bed864a7 MD |
62 | struct type_class_integer *int_class; |
63 | struct type_class *type_class; | |
0a46062b MD |
64 | int ret; |
65 | ||
0a46062b | 66 | float_class = g_new(struct type_class_float, 1); |
bed864a7 MD |
67 | type_class = &float_class->p; |
68 | ||
69 | type_class->name = g_quark_from_string(name); | |
70 | type_class->alignment = alignment; | |
71 | type_class->copy = float_copy; | |
72 | type_class->free = _float_type_free; | |
4c8bfb7e | 73 | type_class->ref = 1; |
0a46062b | 74 | float_class->byte_order = byte_order; |
bed864a7 | 75 | |
7fe00194 MD |
76 | float_class->sign = integer_type_new(NULL, 1, |
77 | byte_order, false, 1); | |
11d43b90 MD |
78 | if (!float_class->mantissa) |
79 | goto error_sign; | |
7fe00194 MD |
80 | float_class->mantissa = integer_type_new(NULL, mantissa_len - 1, |
81 | byte_order, false, 1); | |
bed864a7 MD |
82 | if (!float_class->mantissa) |
83 | goto error_mantissa; | |
7fe00194 MD |
84 | float_class->exp = integer_type_new(NULL, exp_len, |
85 | byte_order, true, 1); | |
bed864a7 MD |
86 | if (!float_class->exp) |
87 | goto error_exp; | |
88 | ||
0a46062b MD |
89 | if (float_class->p.name) { |
90 | ret = ctf_register_type(&float_class->p); | |
bed864a7 MD |
91 | if (ret) |
92 | goto error_register; | |
0a46062b MD |
93 | } |
94 | return float_class; | |
bed864a7 MD |
95 | |
96 | error_register: | |
7fe00194 | 97 | integer_type_free(float_class->exp); |
bed864a7 | 98 | error_exp: |
7fe00194 | 99 | integer_type_free(float_class->mantissa); |
bed864a7 | 100 | error_mantissa: |
7fe00194 | 101 | integer_type_free(float_class->sign); |
11d43b90 | 102 | error_sign: |
bed864a7 MD |
103 | g_free(float_class); |
104 | return NULL; | |
0a46062b | 105 | } |