blob: dd8ea4956559afc2e344b5981296d70d0daaa496 [file] [log] [blame]
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001/*
2 * QNum Module
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Marc-André Lureau <marcandre.lureau@redhat.com>
10 *
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.
13 */
14
15#include "qemu/osdep.h"
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040016#include "qapi/qmp/qnum.h"
Markus Armbruster80d71122020-12-11 18:11:40 +010017#include "qobject-internal.h"
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040018
19/**
20 * qnum_from_int(): Create a new QNum from an int64_t
21 *
22 * Return strong reference.
23 */
24QNum *qnum_from_int(int64_t value)
25{
26 QNum *qn = g_new(QNum, 1);
27
28 qobject_init(QOBJECT(qn), QTYPE_QNUM);
29 qn->kind = QNUM_I64;
30 qn->u.i64 = value;
31
32 return qn;
33}
34
35/**
Marc-André Lureau61a8f412017-06-07 20:36:01 +040036 * qnum_from_uint(): Create a new QNum from an uint64_t
37 *
38 * Return strong reference.
39 */
40QNum *qnum_from_uint(uint64_t value)
41{
42 QNum *qn = g_new(QNum, 1);
43
44 qobject_init(QOBJECT(qn), QTYPE_QNUM);
45 qn->kind = QNUM_U64;
46 qn->u.u64 = value;
47
48 return qn;
49}
50
51/**
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040052 * qnum_from_double(): Create a new QNum from a double
53 *
54 * Return strong reference.
55 */
56QNum *qnum_from_double(double value)
57{
58 QNum *qn = g_new(QNum, 1);
59
60 qobject_init(QOBJECT(qn), QTYPE_QNUM);
61 qn->kind = QNUM_DOUBLE;
62 qn->u.dbl = value;
63
64 return qn;
65}
66
67/**
68 * qnum_get_try_int(): Get an integer representation of the number
69 *
70 * Return true on success.
71 */
72bool qnum_get_try_int(const QNum *qn, int64_t *val)
73{
74 switch (qn->kind) {
75 case QNUM_I64:
76 *val = qn->u.i64;
77 return true;
Marc-André Lureau61a8f412017-06-07 20:36:01 +040078 case QNUM_U64:
79 if (qn->u.u64 > INT64_MAX) {
80 return false;
81 }
82 *val = qn->u.u64;
83 return true;
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040084 case QNUM_DOUBLE:
85 return false;
86 }
87
Pierrick Bouvier890000d2024-09-18 21:46:12 -070088 g_assert_not_reached();
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040089}
90
91/**
92 * qnum_get_int(): Get an integer representation of the number
93 *
94 * assert() on failure.
95 */
96int64_t qnum_get_int(const QNum *qn)
97{
98 int64_t val;
99 bool success = qnum_get_try_int(qn, &val);
100 assert(success);
101 return val;
102}
103
104/**
Marc-André Lureau61a8f412017-06-07 20:36:01 +0400105 * qnum_get_uint(): Get an unsigned integer from the number
106 *
107 * Return true on success.
108 */
109bool qnum_get_try_uint(const QNum *qn, uint64_t *val)
110{
111 switch (qn->kind) {
112 case QNUM_I64:
113 if (qn->u.i64 < 0) {
114 return false;
115 }
116 *val = qn->u.i64;
117 return true;
118 case QNUM_U64:
119 *val = qn->u.u64;
120 return true;
121 case QNUM_DOUBLE:
122 return false;
123 }
124
Pierrick Bouvier890000d2024-09-18 21:46:12 -0700125 g_assert_not_reached();
Marc-André Lureau61a8f412017-06-07 20:36:01 +0400126}
127
128/**
129 * qnum_get_uint(): Get an unsigned integer from the number
130 *
131 * assert() on failure.
132 */
133uint64_t qnum_get_uint(const QNum *qn)
134{
135 uint64_t val;
136 bool success = qnum_get_try_uint(qn, &val);
137 assert(success);
138 return val;
139}
140
141/**
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400142 * qnum_get_double(): Get a float representation of the number
143 *
144 * qnum_get_double() loses precision for integers beyond 53 bits.
145 */
146double qnum_get_double(QNum *qn)
147{
148 switch (qn->kind) {
149 case QNUM_I64:
150 return qn->u.i64;
Marc-André Lureau61a8f412017-06-07 20:36:01 +0400151 case QNUM_U64:
152 return qn->u.u64;
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400153 case QNUM_DOUBLE:
154 return qn->u.dbl;
155 }
156
Pierrick Bouvier890000d2024-09-18 21:46:12 -0700157 g_assert_not_reached();
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400158}
159
160char *qnum_to_string(QNum *qn)
161{
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400162 switch (qn->kind) {
163 case QNUM_I64:
164 return g_strdup_printf("%" PRId64, qn->u.i64);
Marc-André Lureau61a8f412017-06-07 20:36:01 +0400165 case QNUM_U64:
166 return g_strdup_printf("%" PRIu64, qn->u.u64);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400167 case QNUM_DOUBLE:
Markus Armbrusterf917eed2020-12-10 17:14:48 +0100168 /* 17 digits suffice for IEEE double */
169 return g_strdup_printf("%.17g", qn->u.dbl);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400170 }
171
Pierrick Bouvier890000d2024-09-18 21:46:12 -0700172 g_assert_not_reached();
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400173}
174
175/**
Max Reitzb38dd672017-11-14 19:01:25 +0100176 * qnum_is_equal(): Test whether the two QNums are equal
177 *
178 * Negative integers are never considered equal to unsigned integers,
179 * but positive integers in the range [0, INT64_MAX] are considered
180 * equal independently of whether the QNum's kind is i64 or u64.
181 *
182 * Doubles are never considered equal to integers.
183 */
184bool qnum_is_equal(const QObject *x, const QObject *y)
185{
Max Reitz7dc847e2018-02-24 16:40:29 +0100186 QNum *num_x = qobject_to(QNum, x);
187 QNum *num_y = qobject_to(QNum, y);
Max Reitzb38dd672017-11-14 19:01:25 +0100188
189 switch (num_x->kind) {
190 case QNUM_I64:
191 switch (num_y->kind) {
192 case QNUM_I64:
193 /* Comparison in native int64_t type */
194 return num_x->u.i64 == num_y->u.i64;
195 case QNUM_U64:
196 /* Implicit conversion of x to uin64_t, so we have to
197 * check its sign before */
198 return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
199 case QNUM_DOUBLE:
200 return false;
201 }
202 abort();
203 case QNUM_U64:
204 switch (num_y->kind) {
205 case QNUM_I64:
206 return qnum_is_equal(y, x);
207 case QNUM_U64:
208 /* Comparison in native uint64_t type */
209 return num_x->u.u64 == num_y->u.u64;
210 case QNUM_DOUBLE:
211 return false;
212 }
213 abort();
214 case QNUM_DOUBLE:
215 switch (num_y->kind) {
216 case QNUM_I64:
217 case QNUM_U64:
218 return false;
219 case QNUM_DOUBLE:
220 /* Comparison in native double type */
221 return num_x->u.dbl == num_y->u.dbl;
222 }
223 abort();
224 }
225
226 abort();
227}
228
229/**
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400230 * qnum_destroy_obj(): Free all memory allocated by a
231 * QNum object
232 */
233void qnum_destroy_obj(QObject *obj)
234{
235 assert(obj != NULL);
Max Reitz7dc847e2018-02-24 16:40:29 +0100236 g_free(qobject_to(QNum, obj));
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400237}
Marc-André Lureaud709bbf2022-03-23 19:57:20 +0400238
239void qnum_unref(QNum *q)
240{
241 qobject_unref(q);
242}