| 1 | /* |
| 2 | * Common Trace Format |
| 3 | * |
| 4 | * Floating point read/write functions. |
| 5 | * |
| 6 | * Copyright 2010 - 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 | * Reference: ISO C99 standard 5.2.4 |
| 19 | */ |
| 20 | |
| 21 | #include <babeltrace/ctf/types.h> |
| 22 | #include <glib.h> |
| 23 | #include <float.h> /* C99 floating point definitions */ |
| 24 | #include <limits.h> /* C99 limits */ |
| 25 | #include <endian.h> |
| 26 | |
| 27 | /* |
| 28 | * This library is limited to binary representation of floating point values. |
| 29 | * Sign-extension of the exponents is assumed to keep the NaN, +inf, -inf |
| 30 | * values, but this should be double-checked (TODO). |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Aliasing float/double and unsigned long is not strictly permitted by strict |
| 35 | * aliasing, but in practice declaration prunning is well supported, and this permits |
| 36 | * us to use per-word read/writes rather than per-byte. |
| 37 | */ |
| 38 | |
| 39 | #if defined(__GNUC__) || defined(__MINGW32__) || defined(_MSC_VER) |
| 40 | #define HAS_TYPE_PRUNING |
| 41 | #endif |
| 42 | |
| 43 | #if (FLT_RADIX != 2) |
| 44 | |
| 45 | #error "Unsupported floating point radix" |
| 46 | |
| 47 | #endif |
| 48 | |
| 49 | union doubleIEEE754 { |
| 50 | double v; |
| 51 | #ifdef HAS_TYPE_PRUNING |
| 52 | unsigned long bits[(sizeof(double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)]; |
| 53 | #else |
| 54 | unsigned char bits[sizeof(double)]; |
| 55 | #endif |
| 56 | }; |
| 57 | |
| 58 | union ldoubleIEEE754 { |
| 59 | long double v; |
| 60 | #ifdef HAS_TYPE_PRUNING |
| 61 | unsigned long bits[(sizeof(long double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)]; |
| 62 | #else |
| 63 | unsigned char bits[sizeof(long double)]; |
| 64 | #endif |
| 65 | }; |
| 66 | |
| 67 | static struct declaration_float *static_ldouble_declaration; |
| 68 | |
| 69 | struct pos_len { |
| 70 | size_t sign_start, exp_start, mantissa_start, len; |
| 71 | }; |
| 72 | |
| 73 | void _ctf_float_copy(struct stream_pos *destp, |
| 74 | struct definition_float *dest_definition, |
| 75 | struct stream_pos *srcp, |
| 76 | const struct definition_float *src_definition) |
| 77 | { |
| 78 | /* Read */ |
| 79 | if (src_definition->declaration->byte_order == LITTLE_ENDIAN) { |
| 80 | ctf_integer_read(srcp, &src_definition->mantissa->p); |
| 81 | ctf_integer_read(srcp, &src_definition->exp->p); |
| 82 | ctf_integer_read(srcp, &src_definition->sign->p); |
| 83 | } else { |
| 84 | ctf_integer_read(srcp, &src_definition->sign->p); |
| 85 | ctf_integer_read(srcp, &src_definition->exp->p); |
| 86 | ctf_integer_read(srcp, &src_definition->mantissa->p); |
| 87 | } |
| 88 | |
| 89 | dest_definition->mantissa->value._unsigned = |
| 90 | src_definition->mantissa->value._unsigned; |
| 91 | dest_definition->exp->value._signed = |
| 92 | src_definition->exp->value._signed; |
| 93 | dest_definition->sign->value._unsigned = |
| 94 | src_definition->sign->value._unsigned; |
| 95 | |
| 96 | /* Write */ |
| 97 | if (dest_definition->declaration->byte_order == LITTLE_ENDIAN) { |
| 98 | ctf_integer_write(destp, &dest_definition->mantissa->p); |
| 99 | ctf_integer_write(destp, &dest_definition->exp->p); |
| 100 | ctf_integer_write(destp, &dest_definition->sign->p); |
| 101 | } else { |
| 102 | ctf_integer_write(destp, &dest_definition->sign->p); |
| 103 | ctf_integer_write(destp, &dest_definition->exp->p); |
| 104 | ctf_integer_write(destp, &dest_definition->mantissa->p); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void ctf_float_read(struct stream_pos *ppos, struct definition *definition) |
| 109 | { |
| 110 | struct definition_float *float_definition = |
| 111 | container_of(definition, struct definition_float, p); |
| 112 | const struct declaration_float *float_declaration = |
| 113 | float_definition->declaration; |
| 114 | struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 115 | union ldoubleIEEE754 u; |
| 116 | struct definition *tmpdef = |
| 117 | static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p, |
| 118 | NULL, 0, 0); |
| 119 | struct definition_float *tmpfloat = |
| 120 | container_of(tmpdef, struct definition_float, p); |
| 121 | struct ctf_stream_pos destp; |
| 122 | |
| 123 | ctf_init_pos(&destp, -1, O_WRONLY); |
| 124 | destp.base = (char *) u.bits; |
| 125 | |
| 126 | ctf_align_pos(pos, float_declaration->p.alignment); |
| 127 | _ctf_float_copy(&destp.parent, tmpfloat, ppos, float_definition); |
| 128 | float_definition->value = u.v; |
| 129 | definition_unref(tmpdef); |
| 130 | } |
| 131 | |
| 132 | void ctf_float_write(struct stream_pos *ppos, struct definition *definition) |
| 133 | { |
| 134 | struct definition_float *float_definition = |
| 135 | container_of(definition, struct definition_float, p); |
| 136 | const struct declaration_float *float_declaration = |
| 137 | float_definition->declaration; |
| 138 | struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 139 | union ldoubleIEEE754 u; |
| 140 | struct definition *tmpdef = |
| 141 | static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p, |
| 142 | NULL, 0, 0); |
| 143 | struct definition_float *tmpfloat = |
| 144 | container_of(tmpdef, struct definition_float, p); |
| 145 | struct ctf_stream_pos srcp; |
| 146 | |
| 147 | ctf_init_pos(&srcp, -1, O_RDONLY); |
| 148 | srcp.base = (char *) u.bits; |
| 149 | |
| 150 | u.v = float_definition->value; |
| 151 | ctf_align_pos(pos, float_declaration->p.alignment); |
| 152 | _ctf_float_copy(ppos, float_definition, &srcp.parent, tmpfloat); |
| 153 | definition_unref(tmpdef); |
| 154 | } |
| 155 | |
| 156 | void __attribute__((constructor)) ctf_float_init(void) |
| 157 | { |
| 158 | static_ldouble_declaration = |
| 159 | float_declaration_new(LDBL_MANT_DIG, |
| 160 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, |
| 161 | BYTE_ORDER, |
| 162 | __alignof__(long double)); |
| 163 | } |
| 164 | |
| 165 | void __attribute__((destructor)) ctf_float_fini(void) |
| 166 | { |
| 167 | declaration_unref(&static_ldouble_declaration->p); |
| 168 | } |