| 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 | struct pos_len { |
| 68 | size_t sign_start, exp_start, mantissa_start, len; |
| 69 | }; |
| 70 | |
| 71 | void _ctf_float_copy(struct stream_pos *destp, |
| 72 | const struct declaration_float *dest_declaration, |
| 73 | struct stream_pos *srcp, |
| 74 | const struct declaration_float *src_declaration) |
| 75 | { |
| 76 | uint8_t sign; |
| 77 | int64_t exp; |
| 78 | uint64_t mantissa; |
| 79 | |
| 80 | /* Read */ |
| 81 | if (src_declaration->byte_order == LITTLE_ENDIAN) { |
| 82 | mantissa = ctf_uint_read(srcp, src_declaration->mantissa); |
| 83 | exp = ctf_int_read(srcp, src_declaration->exp); |
| 84 | sign = ctf_uint_read(srcp, src_declaration->sign); |
| 85 | } else { |
| 86 | sign = ctf_uint_read(srcp, src_declaration->sign); |
| 87 | exp = ctf_int_read(srcp, src_declaration->exp); |
| 88 | mantissa = ctf_uint_read(srcp, src_declaration->mantissa); |
| 89 | } |
| 90 | /* Write */ |
| 91 | if (dest_declaration->byte_order == LITTLE_ENDIAN) { |
| 92 | ctf_uint_write(destp, dest_declaration->mantissa, mantissa); |
| 93 | ctf_int_write(destp, dest_declaration->exp, exp); |
| 94 | ctf_uint_write(destp, dest_declaration->sign, sign); |
| 95 | } else { |
| 96 | ctf_uint_write(destp, dest_declaration->sign, sign); |
| 97 | ctf_int_write(destp, dest_declaration->exp, exp); |
| 98 | ctf_uint_write(destp, dest_declaration->mantissa, mantissa); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void ctf_float_copy(struct stream_pos *dest, struct stream_pos *src, |
| 103 | const struct declaration_float *float_declaration) |
| 104 | { |
| 105 | align_pos(src, float_declaration->p.alignment); |
| 106 | align_pos(dest, float_declaration->p.alignment); |
| 107 | _ctf_float_copy(dest, float_declaration, src, float_declaration); |
| 108 | } |
| 109 | |
| 110 | double ctf_double_read(struct stream_pos *srcp, |
| 111 | const struct declaration_float *float_declaration) |
| 112 | { |
| 113 | union doubleIEEE754 u; |
| 114 | struct declaration_float *dest_declaration = |
| 115 | float_declaration_new(DBL_MANT_DIG, |
| 116 | sizeof(double) * CHAR_BIT - DBL_MANT_DIG, |
| 117 | BYTE_ORDER, |
| 118 | __alignof__(double)); |
| 119 | struct stream_pos destp; |
| 120 | |
| 121 | align_pos(srcp, float_declaration->p.alignment); |
| 122 | init_pos(&destp, -1); |
| 123 | destp.base = (char *) u.bits; |
| 124 | _ctf_float_copy(&destp, dest_declaration, srcp, float_declaration); |
| 125 | declaration_unref(&dest_declaration->p); |
| 126 | return u.v; |
| 127 | } |
| 128 | |
| 129 | void ctf_double_write(struct stream_pos *destp, |
| 130 | const struct declaration_float *float_declaration, |
| 131 | double v) |
| 132 | { |
| 133 | union doubleIEEE754 u; |
| 134 | struct declaration_float *src_declaration = |
| 135 | float_declaration_new(DBL_MANT_DIG, |
| 136 | sizeof(double) * CHAR_BIT - DBL_MANT_DIG, |
| 137 | BYTE_ORDER, |
| 138 | __alignof__(double)); |
| 139 | struct stream_pos srcp; |
| 140 | |
| 141 | u.v = v; |
| 142 | align_pos(destp, float_declaration->p.alignment); |
| 143 | init_pos(&srcp, -1); |
| 144 | srcp.base = (char *) u.bits; |
| 145 | _ctf_float_copy(destp, float_declaration, &srcp, src_declaration); |
| 146 | declaration_unref(&src_declaration->p); |
| 147 | } |
| 148 | |
| 149 | long double ctf_ldouble_read(struct stream_pos *srcp, |
| 150 | const struct declaration_float *float_declaration) |
| 151 | { |
| 152 | union ldoubleIEEE754 u; |
| 153 | struct declaration_float *dest_declaration = |
| 154 | float_declaration_new(LDBL_MANT_DIG, |
| 155 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, |
| 156 | BYTE_ORDER, |
| 157 | __alignof__(long double)); |
| 158 | struct stream_pos destp; |
| 159 | |
| 160 | align_pos(srcp, float_declaration->p.alignment); |
| 161 | init_pos(&destp, -1); |
| 162 | destp.base = (char *) u.bits; |
| 163 | _ctf_float_copy(&destp, dest_declaration, srcp, float_declaration); |
| 164 | declaration_unref(&dest_declaration->p); |
| 165 | return u.v; |
| 166 | } |
| 167 | |
| 168 | void ctf_ldouble_write(struct stream_pos *destp, |
| 169 | const struct declaration_float *float_declaration, |
| 170 | long double v) |
| 171 | { |
| 172 | union ldoubleIEEE754 u; |
| 173 | struct declaration_float *src_declaration = |
| 174 | float_declaration_new(LDBL_MANT_DIG, |
| 175 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, |
| 176 | BYTE_ORDER, |
| 177 | __alignof__(long double)); |
| 178 | struct stream_pos srcp; |
| 179 | |
| 180 | u.v = v; |
| 181 | align_pos(destp, float_declaration->p.alignment); |
| 182 | init_pos(&srcp, -1); |
| 183 | srcp.base = (char *) u.bits; |
| 184 | _ctf_float_copy(destp, float_declaration, &srcp, src_declaration); |
| 185 | declaration_unref(&src_declaration->p); |
| 186 | } |