| 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 | void float_copy(struct stream_pos *destp, |
| 30 | const struct format *fdest, |
| 31 | struct stream_pos *srcp, |
| 32 | const struct format *fsrc, |
| 33 | struct definition *definition) |
| 34 | { |
| 35 | struct definition_float *_float = |
| 36 | container_of(definition, struct definition_float, p); |
| 37 | struct declaration_float *float_declaration = _float->declaration; |
| 38 | |
| 39 | if (fsrc->float_copy == fdest->float_copy) { |
| 40 | fsrc->float_copy(destp, srcp, float_declaration); |
| 41 | } else { |
| 42 | double v; |
| 43 | |
| 44 | v = fsrc->double_read(srcp, float_declaration); |
| 45 | fdest->double_write(destp, float_declaration, v); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static |
| 50 | void _float_declaration_free(struct declaration *declaration) |
| 51 | { |
| 52 | struct declaration_float *float_declaration = |
| 53 | container_of(declaration, struct declaration_float, p); |
| 54 | |
| 55 | declaration_unref(&float_declaration->exp->p); |
| 56 | declaration_unref(&float_declaration->mantissa->p); |
| 57 | declaration_unref(&float_declaration->sign->p); |
| 58 | g_free(float_declaration); |
| 59 | } |
| 60 | |
| 61 | struct declaration_float * |
| 62 | float_declaration_new(const char *name, size_t mantissa_len, |
| 63 | size_t exp_len, int byte_order, size_t alignment) |
| 64 | { |
| 65 | struct declaration_float *float_declaration; |
| 66 | struct declaration *declaration; |
| 67 | |
| 68 | float_declaration = g_new(struct declaration_float, 1); |
| 69 | declaration = &float_declaration->p; |
| 70 | declaration->id = CTF_TYPE_FLOAT; |
| 71 | declaration->name = g_quark_from_string(name); |
| 72 | declaration->alignment = alignment; |
| 73 | declaration->copy = float_copy; |
| 74 | declaration->declaration_free = _float_declaration_free; |
| 75 | declaration->definition_new = _float_definition_new; |
| 76 | declaration->definition_free = _float_definition_free; |
| 77 | declaration->ref = 1; |
| 78 | float_declaration->byte_order = byte_order; |
| 79 | |
| 80 | float_declaration->sign = integer_declaration_new(NULL, 1, |
| 81 | byte_order, false, 1); |
| 82 | float_declaration->mantissa = integer_declaration_new(NULL, mantissa_len - 1, |
| 83 | byte_order, false, 1); |
| 84 | float_declaration->exp = integer_declaration_new(NULL, exp_len, |
| 85 | byte_order, true, 1); |
| 86 | return float_declaration; |
| 87 | } |
| 88 | |
| 89 | static |
| 90 | struct definition * |
| 91 | _float_definition_new(struct declaration *declaration, |
| 92 | struct definition_scope *parent_scope, |
| 93 | GQuark field_name, int index) |
| 94 | { |
| 95 | struct declaration_float *float_declaration = |
| 96 | container_of(declaration, struct declaration_float, p); |
| 97 | struct definition_float *_float; |
| 98 | |
| 99 | _float = g_new(struct definition_float, 1); |
| 100 | declaration_ref(&float_declaration->p); |
| 101 | _float->p.declaration = declaration; |
| 102 | _float->declaration = float_declaration; |
| 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 | declaration_unref(_float->p.declaration); |
| 116 | g_free(_float); |
| 117 | } |