qdev: Add new qdev_init_gpio_in_named_with_opaque()
[qemu.git] / qobject / qnum.c
blob60c395c1bce8a8fc17d3d9fa7c593a1f8a727bb2
1 /*
2 * QNum Module
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
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 "qemu-common.h"
19 /**
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);
29 qn->kind = QNUM_I64;
30 qn->u.i64 = value;
32 return qn;
35 /**
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);
45 qn->kind = QNUM_U64;
46 qn->u.u64 = value;
48 return qn;
51 /**
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;
62 qn->u.dbl = value;
64 return qn;
67 /**
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)
74 switch (qn->kind) {
75 case QNUM_I64:
76 *val = qn->u.i64;
77 return true;
78 case QNUM_U64:
79 if (qn->u.u64 > INT64_MAX) {
80 return false;
82 *val = qn->u.u64;
83 return true;
84 case QNUM_DOUBLE:
85 return false;
88 assert(0);
89 return false;
92 /**
93 * qnum_get_int(): Get an integer representation of the number
95 * assert() on failure.
97 int64_t qnum_get_int(const QNum *qn)
99 int64_t val;
100 bool success = qnum_get_try_int(qn, &val);
101 assert(success);
102 return 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)
112 switch (qn->kind) {
113 case QNUM_I64:
114 if (qn->u.i64 < 0) {
115 return false;
117 *val = qn->u.i64;
118 return true;
119 case QNUM_U64:
120 *val = qn->u.u64;
121 return true;
122 case QNUM_DOUBLE:
123 return false;
126 assert(0);
127 return false;
131 * qnum_get_uint(): Get an unsigned integer from the number
133 * assert() on failure.
135 uint64_t qnum_get_uint(const QNum *qn)
137 uint64_t val;
138 bool success = qnum_get_try_uint(qn, &val);
139 assert(success);
140 return 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)
150 switch (qn->kind) {
151 case QNUM_I64:
152 return qn->u.i64;
153 case QNUM_U64:
154 return qn->u.u64;
155 case QNUM_DOUBLE:
156 return qn->u.dbl;
159 assert(0);
160 return 0.0;
163 char *qnum_to_string(QNum *qn)
165 char *buffer;
166 int len;
168 switch (qn->kind) {
169 case QNUM_I64:
170 return g_strdup_printf("%" PRId64, qn->u.i64);
171 case QNUM_U64:
172 return g_strdup_printf("%" PRIu64, qn->u.u64);
173 case QNUM_DOUBLE:
174 /* FIXME: snprintf() is locale dependent; but JSON requires
175 * numbers to be formatted as if in the C locale. Dependence
176 * on C locale is a pervasive issue in QEMU. */
177 /* FIXME: This risks printing Inf or NaN, which are not valid
178 * JSON values. */
179 /* FIXME: the default precision of 6 for %f often causes
180 * rounding errors; we should be using DBL_DECIMAL_DIG (17),
181 * and only rounding to a shorter number if the result would
182 * still produce the same floating point value. */
183 buffer = g_strdup_printf("%f" , qn->u.dbl);
184 len = strlen(buffer);
185 while (len > 0 && buffer[len - 1] == '0') {
186 len--;
189 if (len && buffer[len - 1] == '.') {
190 buffer[len - 1] = 0;
191 } else {
192 buffer[len] = 0;
195 return buffer;
198 assert(0);
199 return NULL;
203 * qobject_to_qnum(): Convert a QObject into a QNum
205 QNum *qobject_to_qnum(const QObject *obj)
207 if (!obj || qobject_type(obj) != QTYPE_QNUM) {
208 return NULL;
210 return container_of(obj, QNum, base);
214 * qnum_is_equal(): Test whether the two QNums are equal
216 * Negative integers are never considered equal to unsigned integers,
217 * but positive integers in the range [0, INT64_MAX] are considered
218 * equal independently of whether the QNum's kind is i64 or u64.
220 * Doubles are never considered equal to integers.
222 bool qnum_is_equal(const QObject *x, const QObject *y)
224 QNum *num_x = qobject_to_qnum(x);
225 QNum *num_y = qobject_to_qnum(y);
227 switch (num_x->kind) {
228 case QNUM_I64:
229 switch (num_y->kind) {
230 case QNUM_I64:
231 /* Comparison in native int64_t type */
232 return num_x->u.i64 == num_y->u.i64;
233 case QNUM_U64:
234 /* Implicit conversion of x to uin64_t, so we have to
235 * check its sign before */
236 return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
237 case QNUM_DOUBLE:
238 return false;
240 abort();
241 case QNUM_U64:
242 switch (num_y->kind) {
243 case QNUM_I64:
244 return qnum_is_equal(y, x);
245 case QNUM_U64:
246 /* Comparison in native uint64_t type */
247 return num_x->u.u64 == num_y->u.u64;
248 case QNUM_DOUBLE:
249 return false;
251 abort();
252 case QNUM_DOUBLE:
253 switch (num_y->kind) {
254 case QNUM_I64:
255 case QNUM_U64:
256 return false;
257 case QNUM_DOUBLE:
258 /* Comparison in native double type */
259 return num_x->u.dbl == num_y->u.dbl;
261 abort();
264 abort();
268 * qnum_destroy_obj(): Free all memory allocated by a
269 * QNum object
271 void qnum_destroy_obj(QObject *obj)
273 assert(obj != NULL);
274 g_free(qobject_to_qnum(obj));