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)];
67 static struct declaration_float
*static_ldouble_declaration
;
70 size_t sign_start
, exp_start
, mantissa_start
, len
;
73 int _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
)
81 if (src_definition
->declaration
->byte_order
== LITTLE_ENDIAN
) {
82 ret
= ctf_integer_read(srcp
, &src_definition
->mantissa
->p
);
85 ret
= ctf_integer_read(srcp
, &src_definition
->exp
->p
);
88 ret
= ctf_integer_read(srcp
, &src_definition
->sign
->p
);
92 ret
= ctf_integer_read(srcp
, &src_definition
->sign
->p
);
95 ret
= ctf_integer_read(srcp
, &src_definition
->exp
->p
);
98 ret
= ctf_integer_read(srcp
, &src_definition
->mantissa
->p
);
103 dest_definition
->mantissa
->value
._unsigned
=
104 src_definition
->mantissa
->value
._unsigned
;
105 dest_definition
->exp
->value
._signed
=
106 src_definition
->exp
->value
._signed
;
107 dest_definition
->sign
->value
._unsigned
=
108 src_definition
->sign
->value
._unsigned
;
111 if (dest_definition
->declaration
->byte_order
== LITTLE_ENDIAN
) {
112 ret
= ctf_integer_write(destp
, &dest_definition
->mantissa
->p
);
115 ret
= ctf_integer_write(destp
, &dest_definition
->exp
->p
);
118 ret
= ctf_integer_write(destp
, &dest_definition
->sign
->p
);
122 ret
= ctf_integer_write(destp
, &dest_definition
->sign
->p
);
125 ret
= ctf_integer_write(destp
, &dest_definition
->exp
->p
);
128 ret
= ctf_integer_write(destp
, &dest_definition
->mantissa
->p
);
135 int ctf_float_read(struct stream_pos
*ppos
, struct definition
*definition
)
137 struct definition_float
*float_definition
=
138 container_of(definition
, struct definition_float
, p
);
139 const struct declaration_float
*float_declaration
=
140 float_definition
->declaration
;
141 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
142 union ldoubleIEEE754 u
;
143 struct definition
*tmpdef
=
144 static_ldouble_declaration
->p
.definition_new(&static_ldouble_declaration
->p
,
146 struct definition_float
*tmpfloat
=
147 container_of(tmpdef
, struct definition_float
, p
);
148 struct ctf_stream_pos destp
;
151 ctf_init_pos(&destp
, -1, O_RDWR
);
152 destp
.base
= (char *) u
.bits
;
154 ctf_align_pos(pos
, float_declaration
->p
.alignment
);
155 ret
= _ctf_float_copy(&destp
.parent
, tmpfloat
, ppos
, float_definition
);
156 float_definition
->value
= u
.v
;
157 definition_unref(tmpdef
);
161 int ctf_float_write(struct stream_pos
*ppos
, struct definition
*definition
)
163 struct definition_float
*float_definition
=
164 container_of(definition
, struct definition_float
, p
);
165 const struct declaration_float
*float_declaration
=
166 float_definition
->declaration
;
167 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
168 union ldoubleIEEE754 u
;
169 struct definition
*tmpdef
=
170 static_ldouble_declaration
->p
.definition_new(&static_ldouble_declaration
->p
,
172 struct definition_float
*tmpfloat
=
173 container_of(tmpdef
, struct definition_float
, p
);
174 struct ctf_stream_pos srcp
;
177 ctf_init_pos(&srcp
, -1, O_RDONLY
);
178 srcp
.base
= (char *) u
.bits
;
180 u
.v
= float_definition
->value
;
181 ctf_align_pos(pos
, float_declaration
->p
.alignment
);
182 ret
= _ctf_float_copy(ppos
, float_definition
, &srcp
.parent
, tmpfloat
);
183 definition_unref(tmpdef
);
187 void __attribute__((constructor
)) ctf_float_init(void)
189 static_ldouble_declaration
=
190 float_declaration_new(LDBL_MANT_DIG
,
191 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
,
193 __alignof__(long double));
196 void __attribute__((destructor
)) ctf_float_fini(void)
198 declaration_unref(&static_ldouble_declaration
->p
);