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/qmp/qnum.h"
17 #include "qobject-internal.h"
20 * qnum_from_int(): Create a new QNum from an int64_t
22 * Return strong reference.
24 QNum
*qnum_from_int(int64_t value
)
26 QNum
*qn
= g_new(QNum
, 1);
28 qobject_init(QOBJECT(qn
), QTYPE_QNUM
);
36 * qnum_from_uint(): Create a new QNum from an uint64_t
38 * Return strong reference.
40 QNum
*qnum_from_uint(uint64_t value
)
42 QNum
*qn
= g_new(QNum
, 1);
44 qobject_init(QOBJECT(qn
), QTYPE_QNUM
);
52 * qnum_from_double(): Create a new QNum from a double
54 * Return strong reference.
56 QNum
*qnum_from_double(double value
)
58 QNum
*qn
= g_new(QNum
, 1);
60 qobject_init(QOBJECT(qn
), QTYPE_QNUM
);
61 qn
->kind
= QNUM_DOUBLE
;
68 * qnum_get_try_int(): Get an integer representation of the number
70 * Return true on success.
72 bool qnum_get_try_int(const QNum
*qn
, int64_t *val
)
79 if (qn
->u
.u64
> INT64_MAX
) {
93 * qnum_get_int(): Get an integer representation of the number
95 * assert() on failure.
97 int64_t qnum_get_int(const QNum
*qn
)
100 bool success
= qnum_get_try_int(qn
, &val
);
106 * qnum_get_uint(): Get an unsigned integer from the number
108 * Return true on success.
110 bool qnum_get_try_uint(const QNum
*qn
, uint64_t *val
)
131 * qnum_get_uint(): Get an unsigned integer from the number
133 * assert() on failure.
135 uint64_t qnum_get_uint(const QNum
*qn
)
138 bool success
= qnum_get_try_uint(qn
, &val
);
144 * qnum_get_double(): Get a float representation of the number
146 * qnum_get_double() loses precision for integers beyond 53 bits.
148 double qnum_get_double(QNum
*qn
)
163 char *qnum_to_string(QNum
*qn
)
167 return g_strdup_printf("%" PRId64
, qn
->u
.i64
);
169 return g_strdup_printf("%" PRIu64
, qn
->u
.u64
);
171 /* 17 digits suffice for IEEE double */
172 return g_strdup_printf("%.17g", qn
->u
.dbl
);
180 * qnum_is_equal(): Test whether the two QNums are equal
182 * Negative integers are never considered equal to unsigned integers,
183 * but positive integers in the range [0, INT64_MAX] are considered
184 * equal independently of whether the QNum's kind is i64 or u64.
186 * Doubles are never considered equal to integers.
188 bool qnum_is_equal(const QObject
*x
, const QObject
*y
)
190 QNum
*num_x
= qobject_to(QNum
, x
);
191 QNum
*num_y
= qobject_to(QNum
, y
);
193 switch (num_x
->kind
) {
195 switch (num_y
->kind
) {
197 /* Comparison in native int64_t type */
198 return num_x
->u
.i64
== num_y
->u
.i64
;
200 /* Implicit conversion of x to uin64_t, so we have to
201 * check its sign before */
202 return num_x
->u
.i64
>= 0 && num_x
->u
.i64
== num_y
->u
.u64
;
208 switch (num_y
->kind
) {
210 return qnum_is_equal(y
, x
);
212 /* Comparison in native uint64_t type */
213 return num_x
->u
.u64
== num_y
->u
.u64
;
219 switch (num_y
->kind
) {
224 /* Comparison in native double type */
225 return num_x
->u
.dbl
== num_y
->u
.dbl
;
234 * qnum_destroy_obj(): Free all memory allocated by a
237 void qnum_destroy_obj(QObject
*obj
)
240 g_free(qobject_to(QNum
, obj
));
243 void qnum_unref(QNum
*q
)