Relicense BabelTrace library to MIT (BSD-style)
[babeltrace.git] / types / float.c
CommitLineData
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>
448d3cc7 20#include <babeltrace/types.h>
0a46062b 21
bed864a7
MD
22void float_copy(struct stream_pos *dest, const struct format *fdest,
23 struct stream_pos *src, const struct format *fsrc,
24 const struct type_class *type_class)
0a46062b
MD
25{
26 struct type_class_float *float_class =
27 container_of(type_class, struct type_class_float, p);
28
29 if (fsrc->float_copy == fdest->float_copy) {
11d43b90 30 fsrc->float_copy(dest, src, float_class);
0a46062b
MD
31 } else {
32 double v;
33
11d43b90
MD
34 v = fsrc->double_read(src, float_class);
35 fdest->double_write(dest, float_class, v);
0a46062b
MD
36 }
37}
38
90b676d7
MD
39void float_type_free(struct type_class_float *float_class)
40{
d06d03db
MD
41 integer_type_free(float_class->exp);
42 integer_type_free(float_class->mantissa);
43 integer_type_free(float_class->sign);
90b676d7
MD
44 g_free(float_class);
45}
46
47static void _float_type_free(struct type_class *type_class)
48{
49 struct type_class_float *float_class =
50 container_of(type_class, struct type_class_float, p);
51 float_type_free(float_class);
52}
53
0a46062b
MD
54struct type_class_float *float_type_new(const char *name,
55 size_t mantissa_len,
56 size_t exp_len, int byte_order,
57 size_t alignment)
58{
59 struct type_class_float *float_class;
bed864a7
MD
60 struct type_class_integer *int_class;
61 struct type_class *type_class;
0a46062b
MD
62 int ret;
63
0a46062b 64 float_class = g_new(struct type_class_float, 1);
bed864a7
MD
65 type_class = &float_class->p;
66
67 type_class->name = g_quark_from_string(name);
68 type_class->alignment = alignment;
69 type_class->copy = float_copy;
70 type_class->free = _float_type_free;
0a46062b 71 float_class->byte_order = byte_order;
bed864a7 72
7fe00194
MD
73 float_class->sign = integer_type_new(NULL, 1,
74 byte_order, false, 1);
11d43b90
MD
75 if (!float_class->mantissa)
76 goto error_sign;
7fe00194
MD
77 float_class->mantissa = integer_type_new(NULL, mantissa_len - 1,
78 byte_order, false, 1);
bed864a7
MD
79 if (!float_class->mantissa)
80 goto error_mantissa;
7fe00194
MD
81 float_class->exp = integer_type_new(NULL, exp_len,
82 byte_order, true, 1);
bed864a7
MD
83 if (!float_class->exp)
84 goto error_exp;
85
0a46062b
MD
86 if (float_class->p.name) {
87 ret = ctf_register_type(&float_class->p);
bed864a7
MD
88 if (ret)
89 goto error_register;
0a46062b
MD
90 }
91 return float_class;
bed864a7
MD
92
93error_register:
7fe00194 94 integer_type_free(float_class->exp);
bed864a7 95error_exp:
7fe00194 96 integer_type_free(float_class->mantissa);
bed864a7 97error_mantissa:
7fe00194 98 integer_type_free(float_class->sign);
11d43b90 99error_sign:
bed864a7
MD
100 g_free(float_class);
101 return NULL;
0a46062b 102}
This page took 0.027267 seconds and 4 git commands to generate.