Commit | Line | Data |
---|---|---|
6dc2ca62 MD |
1 | /* |
2 | * Common Trace Format | |
3 | * | |
4 | * Floating point read/write functions. | |
5 | * | |
ccd7e1c8 | 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
6dc2ca62 | 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: | |
de0ba614 | 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. | |
6dc2ca62 | 17 | * |
de0ba614 | 18 | * Reference: ISO C99 standard 5.2.4 |
6dc2ca62 MD |
19 | */ |
20 | ||
d79865b9 | 21 | #include <babeltrace/ctf/types.h> |
6dc2ca62 | 22 | #include <glib.h> |
de0ba614 | 23 | #include <float.h> /* C99 floating point definitions */ |
a3dbc794 | 24 | #include <limits.h> /* C99 limits */ |
6dc2ca62 MD |
25 | #include <endian.h> |
26 | ||
de0ba614 MD |
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 | ||
6dc2ca62 MD |
33 | /* |
34 | * Aliasing float/double and unsigned long is not strictly permitted by strict | |
f6625916 | 35 | * aliasing, but in practice declaration prunning is well supported, and this permits |
6dc2ca62 MD |
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 | ||
de0ba614 MD |
43 | #if (FLT_RADIX != 2) |
44 | ||
45 | #error "Unsupported floating point radix" | |
46 | ||
6dc2ca62 | 47 | #endif |
6dc2ca62 MD |
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 | ||
de0ba614 MD |
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 | ||
11d43b90 | 71 | void _ctf_float_copy(struct stream_pos *destp, |
f6625916 | 72 | const struct declaration_float *dest_declaration, |
11d43b90 | 73 | struct stream_pos *srcp, |
f6625916 | 74 | const struct declaration_float *src_declaration) |
6dc2ca62 | 75 | { |
11d43b90 MD |
76 | uint8_t sign; |
77 | int64_t exp; | |
78 | uint64_t mantissa; | |
de0ba614 | 79 | |
11d43b90 | 80 | /* Read */ |
f6625916 MD |
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); | |
de0ba614 | 85 | } else { |
f6625916 MD |
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); | |
6dc2ca62 | 89 | } |
11d43b90 | 90 | /* Write */ |
f6625916 MD |
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); | |
de0ba614 | 95 | } else { |
f6625916 MD |
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); | |
6dc2ca62 | 99 | } |
11d43b90 | 100 | } |
de0ba614 | 101 | |
11d43b90 | 102 | void ctf_float_copy(struct stream_pos *dest, struct stream_pos *src, |
f6625916 | 103 | const struct declaration_float *float_declaration) |
11d43b90 | 104 | { |
f6625916 MD |
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); | |
6dc2ca62 MD |
108 | } |
109 | ||
11d43b90 | 110 | double ctf_double_read(struct stream_pos *srcp, |
f6625916 | 111 | const struct declaration_float *float_declaration) |
6dc2ca62 | 112 | { |
de0ba614 | 113 | union doubleIEEE754 u; |
f6625916 | 114 | struct declaration_float *dest_declaration = float_declaration_new(NULL, |
11d43b90 MD |
115 | DBL_MANT_DIG, |
116 | sizeof(double) * CHAR_BIT - DBL_MANT_DIG, | |
117 | BYTE_ORDER, | |
118 | __alignof__(double)); | |
119 | struct stream_pos destp; | |
120 | ||
f6625916 | 121 | align_pos(srcp, float_declaration->p.alignment); |
47e0f2e2 | 122 | init_pos(&destp, (char *) u.bits); |
f6625916 MD |
123 | _ctf_float_copy(&destp, dest_declaration, srcp, float_declaration); |
124 | declaration_unref(&dest_declaration->p); | |
de0ba614 MD |
125 | return u.v; |
126 | } | |
127 | ||
11d43b90 | 128 | void ctf_double_write(struct stream_pos *destp, |
f6625916 | 129 | const struct declaration_float *float_declaration, |
11d43b90 | 130 | double v) |
de0ba614 MD |
131 | { |
132 | union doubleIEEE754 u; | |
f6625916 | 133 | struct declaration_float *src_declaration = float_declaration_new(NULL, |
11d43b90 MD |
134 | DBL_MANT_DIG, |
135 | sizeof(double) * CHAR_BIT - DBL_MANT_DIG, | |
136 | BYTE_ORDER, | |
137 | __alignof__(double)); | |
138 | struct stream_pos srcp; | |
139 | ||
de0ba614 | 140 | u.v = v; |
f6625916 | 141 | align_pos(destp, float_declaration->p.alignment); |
47e0f2e2 | 142 | init_pos(&srcp, (char *) u.bits); |
f6625916 MD |
143 | _ctf_float_copy(destp, float_declaration, &srcp, src_declaration); |
144 | declaration_unref(&src_declaration->p); | |
de0ba614 | 145 | } |
6dc2ca62 | 146 | |
11d43b90 | 147 | long double ctf_ldouble_read(struct stream_pos *srcp, |
f6625916 | 148 | const struct declaration_float *float_declaration) |
de0ba614 MD |
149 | { |
150 | union ldoubleIEEE754 u; | |
f6625916 | 151 | struct declaration_float *dest_declaration = float_declaration_new(NULL, |
11d43b90 MD |
152 | LDBL_MANT_DIG, |
153 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, | |
154 | BYTE_ORDER, | |
155 | __alignof__(long double)); | |
156 | struct stream_pos destp; | |
157 | ||
f6625916 | 158 | align_pos(srcp, float_declaration->p.alignment); |
47e0f2e2 | 159 | init_pos(&destp, (char *) u.bits); |
f6625916 MD |
160 | _ctf_float_copy(&destp, dest_declaration, srcp, float_declaration); |
161 | declaration_unref(&dest_declaration->p); | |
de0ba614 MD |
162 | return u.v; |
163 | } | |
164 | ||
11d43b90 | 165 | void ctf_ldouble_write(struct stream_pos *destp, |
f6625916 | 166 | const struct declaration_float *float_declaration, |
11d43b90 | 167 | long double v) |
de0ba614 MD |
168 | { |
169 | union ldoubleIEEE754 u; | |
f6625916 | 170 | struct declaration_float *src_declaration = float_declaration_new(NULL, |
11d43b90 MD |
171 | LDBL_MANT_DIG, |
172 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, | |
173 | BYTE_ORDER, | |
174 | __alignof__(long double)); | |
175 | struct stream_pos srcp; | |
176 | ||
de0ba614 | 177 | u.v = v; |
f6625916 | 178 | align_pos(destp, float_declaration->p.alignment); |
47e0f2e2 | 179 | init_pos(&srcp, (char *) u.bits); |
f6625916 MD |
180 | _ctf_float_copy(destp, float_declaration, &srcp, src_declaration); |
181 | declaration_unref(&src_declaration->p); | |
6dc2ca62 | 182 | } |