f15c2856001f608cde70c093af8f77e7e9779786
[babeltrace.git] / formats / ctf / types / float.c
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 type 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 type_float *dest_type,
73 struct stream_pos *srcp,
74 const struct type_float *src_type)
75 {
76 uint8_t sign;
77 int64_t exp;
78 uint64_t mantissa;
79
80 /* Read */
81 if (src_type->byte_order == LITTLE_ENDIAN) {
82 mantissa = ctf_uint_read(srcp, src_type->mantissa);
83 exp = ctf_int_read(srcp, src_type->exp);
84 sign = ctf_uint_read(srcp, src_type->sign);
85 } else {
86 sign = ctf_uint_read(srcp, src_type->sign);
87 exp = ctf_int_read(srcp, src_type->exp);
88 mantissa = ctf_uint_read(srcp, src_type->mantissa);
89 }
90 /* Write */
91 if (dest_type->byte_order == LITTLE_ENDIAN) {
92 ctf_uint_write(destp, dest_type->mantissa, mantissa);
93 ctf_int_write(destp, dest_type->exp, exp);
94 ctf_uint_write(destp, dest_type->sign, sign);
95 } else {
96 ctf_uint_write(destp, dest_type->sign, sign);
97 ctf_int_write(destp, dest_type->exp, exp);
98 ctf_uint_write(destp, dest_type->mantissa, mantissa);
99 }
100 }
101
102 void ctf_float_copy(struct stream_pos *dest, struct stream_pos *src,
103 const struct type_float *float_type)
104 {
105 align_pos(src, float_type->p.alignment);
106 align_pos(dest, float_type->p.alignment);
107 _ctf_float_copy(dest, float_type, src, float_type);
108 }
109
110 double ctf_double_read(struct stream_pos *srcp,
111 const struct type_float *float_type)
112 {
113 union doubleIEEE754 u;
114 struct type_float *dest_type = float_type_new(NULL,
115 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_type->p.alignment);
122 init_pos(&destp, (char *) u.bits);
123 _ctf_float_copy(&destp, dest_type, srcp, float_type);
124 type_unref(&dest_type->p);
125 return u.v;
126 }
127
128 void ctf_double_write(struct stream_pos *destp,
129 const struct type_float *float_type,
130 double v)
131 {
132 union doubleIEEE754 u;
133 struct type_float *src_type = float_type_new(NULL,
134 DBL_MANT_DIG,
135 sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
136 BYTE_ORDER,
137 __alignof__(double));
138 struct stream_pos srcp;
139
140 u.v = v;
141 align_pos(destp, float_type->p.alignment);
142 init_pos(&srcp, (char *) u.bits);
143 _ctf_float_copy(destp, float_type, &srcp, src_type);
144 type_unref(&src_type->p);
145 }
146
147 long double ctf_ldouble_read(struct stream_pos *srcp,
148 const struct type_float *float_type)
149 {
150 union ldoubleIEEE754 u;
151 struct type_float *dest_type = float_type_new(NULL,
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
158 align_pos(srcp, float_type->p.alignment);
159 init_pos(&destp, (char *) u.bits);
160 _ctf_float_copy(&destp, dest_type, srcp, float_type);
161 type_unref(&dest_type->p);
162 return u.v;
163 }
164
165 void ctf_ldouble_write(struct stream_pos *destp,
166 const struct type_float *float_type,
167 long double v)
168 {
169 union ldoubleIEEE754 u;
170 struct type_float *src_type = float_type_new(NULL,
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
177 u.v = v;
178 align_pos(destp, float_type->p.alignment);
179 init_pos(&srcp, (char *) u.bits);
180 _ctf_float_copy(destp, float_type, &srcp, src_type);
181 type_unref(&src_type->p);
182 }
This page took 0.050118 seconds and 3 git commands to generate.