2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
6 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include <ftypes-int.h>
13 #include <wsutil/array.h>
16 double_fvalue_new(fvalue_t
*fv
)
18 fv
->value
.floating
= 0.0;
22 double_fvalue_set_floating(fvalue_t
*fv
, double value
)
24 fv
->value
.floating
= value
;
28 value_get_floating(fvalue_t
*fv
)
30 return fv
->value
.floating
;
34 val_from_uinteger64(fvalue_t
*fv
, const char *s _U_
, uint64_t value
, char **err_msg _U_
)
36 fv
->value
.floating
= (double)value
;
41 val_from_sinteger64(fvalue_t
*fv
, const char *s _U_
, int64_t value
, char **err_msg _U_
)
43 fv
->value
.floating
= (double)value
;
48 val_from_double(fvalue_t
*fv
, const char *s _U_
, double floating
, char **err_msg _U_
)
50 fv
->value
.floating
= floating
;
55 float_val_to_repr(wmem_allocator_t
*scope
, const fvalue_t
*fv
, ftrepr_t rtype
, int field_display _U_
)
57 char *buf
= wmem_alloc(scope
, G_ASCII_DTOSTR_BUF_SIZE
);
58 if (rtype
== FTREPR_DFILTER
)
59 g_ascii_dtostr(buf
, G_ASCII_DTOSTR_BUF_SIZE
, fv
->value
.floating
);
61 g_ascii_formatd(buf
, G_ASCII_DTOSTR_BUF_SIZE
, "%." G_STRINGIFY(FLT_DIG
) "g", fv
->value
.floating
);
66 double_val_to_repr(wmem_allocator_t
*scope
, const fvalue_t
*fv
, ftrepr_t rtype
, int field_display _U_
)
68 char *buf
= wmem_alloc(scope
, G_ASCII_DTOSTR_BUF_SIZE
);
69 if (rtype
== FTREPR_DFILTER
)
70 g_ascii_dtostr(buf
, G_ASCII_DTOSTR_BUF_SIZE
, fv
->value
.floating
);
72 g_ascii_formatd(buf
, G_ASCII_DTOSTR_BUF_SIZE
, "%." G_STRINGIFY(DBL_DIG
) "g", fv
->value
.floating
);
77 double_val_to_double(const fvalue_t
*fv
, double *repr
)
79 *repr
= fv
->value
.floating
;
84 val_unary_minus(fvalue_t
* dst
, const fvalue_t
*src
, char **err_ptr _U_
)
86 dst
->value
.floating
= -src
->value
.floating
;
91 val_add(fvalue_t
* dst
, const fvalue_t
*a
, const fvalue_t
*b
, char **err_ptr _U_
)
93 dst
->value
.floating
= a
->value
.floating
+ b
->value
.floating
;
98 val_subtract(fvalue_t
* dst
, const fvalue_t
*a
, const fvalue_t
*b
, char **err_ptr _U_
)
100 dst
->value
.floating
= a
->value
.floating
- b
->value
.floating
;
104 static enum ft_result
105 val_multiply(fvalue_t
* dst
, const fvalue_t
*a
, const fvalue_t
*b
, char **err_ptr _U_
)
107 dst
->value
.floating
= a
->value
.floating
* b
->value
.floating
;
111 static enum ft_result
112 val_divide(fvalue_t
* dst
, const fvalue_t
*a
, const fvalue_t
*b
, char **err_ptr _U_
)
114 dst
->value
.floating
= a
->value
.floating
/ b
->value
.floating
;
118 static enum ft_result
119 cmp_order(const fvalue_t
*a
, const fvalue_t
*b
, int *cmp
)
121 if (a
->value
.floating
< b
->value
.floating
)
123 else if (a
->value
.floating
> b
->value
.floating
)
131 val_is_zero(const fvalue_t
*fv_a
)
133 return fv_a
->value
.floating
== 0;
137 val_is_negative(const fvalue_t
*fv_a
)
139 return fv_a
->value
.floating
< 0;
143 val_hash(const fvalue_t
*fv
)
145 return g_double_hash(&fv
->value
.floating
);
149 ftype_register_double(void)
152 static const ftype_t float_type
= {
153 FT_FLOAT
, /* ftype */
155 double_fvalue_new
, /* new_value */
156 NULL
, /* copy_value */
157 NULL
, /* free_value */
158 NULL
, /* val_from_literal */
159 NULL
, /* val_from_string */
160 NULL
, /* val_from_charconst */
161 val_from_uinteger64
, /* val_from_uinteger64 */
162 val_from_sinteger64
, /* val_from_sinteger64 */
163 val_from_double
, /* val_from_double */
164 float_val_to_repr
, /* val_to_string_repr */
166 NULL
, /* val_to_uinteger64 */
167 NULL
, /* val_to_sinteger64 */
168 double_val_to_double
, /* val_to_double */
170 { .set_value_floating
= double_fvalue_set_floating
}, /* union set_value */
171 { .get_value_floating
= value_get_floating
}, /* union get_value */
174 NULL
, /* cmp_contains */
175 NULL
, /* cmp_matches */
178 val_is_zero
, /* is_zero */
179 val_is_negative
, /* is_negative */
182 NULL
, /* bitwise_and */
183 val_unary_minus
, /* unary_minus */
185 val_subtract
, /* subtract */
186 val_multiply
, /* multiply */
187 val_divide
, /* divide */
191 static const ftype_t double_type
= {
192 FT_DOUBLE
, /* ftype */
194 double_fvalue_new
, /* new_value */
195 NULL
, /* copy_value */
196 NULL
, /* free_value */
197 NULL
, /* val_from_literal */
198 NULL
, /* val_from_string */
199 NULL
, /* val_from_charconst */
200 val_from_uinteger64
, /* val_from_uinteger64 */
201 val_from_sinteger64
, /* val_from_sinteger64 */
202 val_from_double
, /* val_from_double */
203 double_val_to_repr
, /* val_to_string_repr */
205 NULL
, /* val_to_uinteger64 */
206 NULL
, /* val_to_sinteger64 */
207 double_val_to_double
, /* val_to_double */
209 { .set_value_floating
= double_fvalue_set_floating
}, /* union set_value */
210 { .get_value_floating
= value_get_floating
}, /* union get_value */
213 NULL
, /* cmp_contains */
214 NULL
, /* cmp_matches */
217 val_is_zero
, /* is_zero */
218 val_is_negative
, /* is_negative */
221 NULL
, /* bitwise_and */
222 val_unary_minus
, /* unary_minus */
224 val_subtract
, /* subtract */
225 val_multiply
, /* multiply */
226 val_divide
, /* divide */
230 ftype_register(FT_FLOAT
, &float_type
);
231 ftype_register(FT_DOUBLE
, &double_type
);
235 ftype_register_pseudofields_double(int proto
)
237 static int hf_ft_float
;
238 static int hf_ft_double
;
240 static hf_register_info hf_ftypes
[] = {
242 { "FT_FLOAT", "_ws.ftypes.float",
243 FT_FLOAT
, BASE_NONE
, NULL
, 0x00,
247 { "FT_DOUBLE", "_ws.ftypes.double",
248 FT_DOUBLE
, BASE_NONE
, NULL
, 0x00,
253 proto_register_field_array(proto
, hf_ftypes
, array_length(hf_ftypes
));
257 * Editor modelines - https://www.wireshark.org/tools/modelines.html
262 * indent-tabs-mode: t
265 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
266 * :indentSize=8:tabSize=8:noTabs=false: