4 * Floating point read/write functions.
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * Reference: ISO C99 standard 5.2.4
21 #include <babeltrace/ctf/types.h>
23 #include <float.h> /* C99 floating point definitions */
24 #include <limits.h> /* C99 limits */
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).
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.
39 #if defined(__GNUC__) || defined(__MINGW32__) || defined(_MSC_VER)
40 #define HAS_TYPE_PRUNING
45 #error "Unsupported floating point radix"
51 #ifdef HAS_TYPE_PRUNING
52 unsigned long bits
[(sizeof(double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)];
54 unsigned char bits
[sizeof(double)];
58 union ldoubleIEEE754
{
60 #ifdef HAS_TYPE_PRUNING
61 unsigned long bits
[(sizeof(long double) + sizeof(unsigned long) - 1) / sizeof(unsigned long)];
63 unsigned char bits
[sizeof(long double)];
68 size_t sign_start
, exp_start
, mantissa_start
, len
;
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
)
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
);
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
);
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
);
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
);
102 void ctf_float_copy(struct stream_pos
*dest
, struct stream_pos
*src
,
103 const struct declaration_float
*float_declaration
)
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
);
110 double ctf_double_read(struct stream_pos
*srcp
,
111 const struct declaration_float
*float_declaration
)
113 union doubleIEEE754 u
;
114 struct declaration_float
*dest_declaration
= float_declaration_new(NULL
,
116 sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
,
118 __alignof__(double));
119 struct stream_pos destp
;
121 align_pos(srcp
, float_declaration
->p
.alignment
);
122 init_pos(&destp
, (char *) u
.bits
);
123 _ctf_float_copy(&destp
, dest_declaration
, srcp
, float_declaration
);
124 declaration_unref(&dest_declaration
->p
);
128 void ctf_double_write(struct stream_pos
*destp
,
129 const struct declaration_float
*float_declaration
,
132 union doubleIEEE754 u
;
133 struct declaration_float
*src_declaration
= float_declaration_new(NULL
,
135 sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
,
137 __alignof__(double));
138 struct stream_pos srcp
;
141 align_pos(destp
, float_declaration
->p
.alignment
);
142 init_pos(&srcp
, (char *) u
.bits
);
143 _ctf_float_copy(destp
, float_declaration
, &srcp
, src_declaration
);
144 declaration_unref(&src_declaration
->p
);
147 long double ctf_ldouble_read(struct stream_pos
*srcp
,
148 const struct declaration_float
*float_declaration
)
150 union ldoubleIEEE754 u
;
151 struct declaration_float
*dest_declaration
= float_declaration_new(NULL
,
153 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
,
155 __alignof__(long double));
156 struct stream_pos destp
;
158 align_pos(srcp
, float_declaration
->p
.alignment
);
159 init_pos(&destp
, (char *) u
.bits
);
160 _ctf_float_copy(&destp
, dest_declaration
, srcp
, float_declaration
);
161 declaration_unref(&dest_declaration
->p
);
165 void ctf_ldouble_write(struct stream_pos
*destp
,
166 const struct declaration_float
*float_declaration
,
169 union ldoubleIEEE754 u
;
170 struct declaration_float
*src_declaration
= float_declaration_new(NULL
,
172 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
,
174 __alignof__(long double));
175 struct stream_pos srcp
;
178 align_pos(destp
, float_declaration
->p
.alignment
);
179 init_pos(&srcp
, (char *) u
.bits
);
180 _ctf_float_copy(destp
, float_declaration
, &srcp
, src_declaration
);
181 declaration_unref(&src_declaration
->p
);
This page took 0.04219 seconds and 5 git commands to generate.