4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Marc-André Lureau <marcandre.lureau@redhat.com>
11 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
12 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qnum.h"
18 #include "qapi/qmp/qobject.h"
19 #include "qemu-common.h"
22 * qnum_from_int(): Create a new QNum from an int64_t
24 * Return strong reference.
26 QNum
*qnum_from_int(int64_t value
)
28 QNum
*qn
= g_new(QNum
, 1);
30 qobject_init(QOBJECT(qn
), QTYPE_QNUM
);
38 * qnum_from_uint(): Create a new QNum from an uint64_t
40 * Return strong reference.
42 QNum
*qnum_from_uint(uint64_t value
)
44 QNum
*qn
= g_new(QNum
, 1);
46 qobject_init(QOBJECT(qn
), QTYPE_QNUM
);
54 * qnum_from_double(): Create a new QNum from a double
56 * Return strong reference.
58 QNum
*qnum_from_double(double value
)
60 QNum
*qn
= g_new(QNum
, 1);
62 qobject_init(QOBJECT(qn
), QTYPE_QNUM
);
63 qn
->kind
= QNUM_DOUBLE
;
70 * qnum_get_try_int(): Get an integer representation of the number
72 * Return true on success.
74 bool qnum_get_try_int(const QNum
*qn
, int64_t *val
)
81 if (qn
->u
.u64
> INT64_MAX
) {
95 * qnum_get_int(): Get an integer representation of the number
97 * assert() on failure.
99 int64_t qnum_get_int(const QNum
*qn
)
102 bool success
= qnum_get_try_int(qn
, &val
);
108 * qnum_get_uint(): Get an unsigned integer from the number
110 * Return true on success.
112 bool qnum_get_try_uint(const QNum
*qn
, uint64_t *val
)
133 * qnum_get_uint(): Get an unsigned integer from the number
135 * assert() on failure.
137 uint64_t qnum_get_uint(const QNum
*qn
)
140 bool success
= qnum_get_try_uint(qn
, &val
);
146 * qnum_get_double(): Get a float representation of the number
148 * qnum_get_double() loses precision for integers beyond 53 bits.
150 double qnum_get_double(QNum
*qn
)
165 char *qnum_to_string(QNum
*qn
)
172 return g_strdup_printf("%" PRId64
, qn
->u
.i64
);
174 return g_strdup_printf("%" PRIu64
, qn
->u
.u64
);
176 /* FIXME: snprintf() is locale dependent; but JSON requires
177 * numbers to be formatted as if in the C locale. Dependence
178 * on C locale is a pervasive issue in QEMU. */
179 /* FIXME: This risks printing Inf or NaN, which are not valid
181 /* FIXME: the default precision of 6 for %f often causes
182 * rounding errors; we should be using DBL_DECIMAL_DIG (17),
183 * and only rounding to a shorter number if the result would
184 * still produce the same floating point value. */
185 buffer
= g_strdup_printf("%f" , qn
->u
.dbl
);
186 len
= strlen(buffer
);
187 while (len
> 0 && buffer
[len
- 1] == '0') {
191 if (len
&& buffer
[len
- 1] == '.') {
205 * qobject_to_qnum(): Convert a QObject into a QNum
207 QNum
*qobject_to_qnum(const QObject
*obj
)
209 if (!obj
|| qobject_type(obj
) != QTYPE_QNUM
) {
212 return container_of(obj
, QNum
, base
);
216 * qnum_is_equal(): Test whether the two QNums are equal
218 * Negative integers are never considered equal to unsigned integers,
219 * but positive integers in the range [0, INT64_MAX] are considered
220 * equal independently of whether the QNum's kind is i64 or u64.
222 * Doubles are never considered equal to integers.
224 bool qnum_is_equal(const QObject
*x
, const QObject
*y
)
226 QNum
*num_x
= qobject_to_qnum(x
);
227 QNum
*num_y
= qobject_to_qnum(y
);
229 switch (num_x
->kind
) {
231 switch (num_y
->kind
) {
233 /* Comparison in native int64_t type */
234 return num_x
->u
.i64
== num_y
->u
.i64
;
236 /* Implicit conversion of x to uin64_t, so we have to
237 * check its sign before */
238 return num_x
->u
.i64
>= 0 && num_x
->u
.i64
== num_y
->u
.u64
;
244 switch (num_y
->kind
) {
246 return qnum_is_equal(y
, x
);
248 /* Comparison in native uint64_t type */
249 return num_x
->u
.u64
== num_y
->u
.u64
;
255 switch (num_y
->kind
) {
260 /* Comparison in native double type */
261 return num_x
->u
.dbl
== num_y
->u
.dbl
;
270 * qnum_destroy_obj(): Free all memory allocated by a
273 void qnum_destroy_obj(QObject
*obj
)
276 g_free(qobject_to_qnum(obj
));