Line | Count | Source |
1 | | /* |
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" |
16 | | #include "qobject/qnum.h" |
17 | | #include "qobject-internal.h" |
18 | | |
19 | | /** |
20 | | * qnum_from_int(): Create a new QNum from an int64_t |
21 | | * |
22 | | * Return strong reference. |
23 | | */ |
24 | | QNum *qnum_from_int(int64_t value) |
25 | 302k | { |
26 | 302k | QNum *qn = g_new(QNum, 1); |
27 | | |
28 | 302k | qobject_init(QOBJECT(qn), QTYPE_QNUM); |
29 | 302k | qn->kind = QNUM_I64; |
30 | 302k | qn->u.i64 = value; |
31 | | |
32 | 302k | return qn; |
33 | 302k | } |
34 | | |
35 | | /** |
36 | | * qnum_from_uint(): Create a new QNum from an uint64_t |
37 | | * |
38 | | * Return strong reference. |
39 | | */ |
40 | | QNum *qnum_from_uint(uint64_t value) |
41 | 18.6M | { |
42 | 18.6M | QNum *qn = g_new(QNum, 1); |
43 | | |
44 | 18.6M | qobject_init(QOBJECT(qn), QTYPE_QNUM); |
45 | 18.6M | qn->kind = QNUM_U64; |
46 | 18.6M | qn->u.u64 = value; |
47 | | |
48 | 18.6M | return qn; |
49 | 18.6M | } |
50 | | |
51 | | /** |
52 | | * qnum_from_double(): Create a new QNum from a double |
53 | | * |
54 | | * Return strong reference. |
55 | | */ |
56 | | QNum *qnum_from_double(double value) |
57 | 0 | { |
58 | 0 | QNum *qn = g_new(QNum, 1); |
59 | |
|
60 | 0 | qobject_init(QOBJECT(qn), QTYPE_QNUM); |
61 | 0 | qn->kind = QNUM_DOUBLE; |
62 | 0 | qn->u.dbl = value; |
63 | |
|
64 | 0 | return qn; |
65 | 0 | } |
66 | | |
67 | | /** |
68 | | * qnum_get_try_int(): Get an integer representation of the number |
69 | | * |
70 | | * Return true on success. |
71 | | */ |
72 | | bool qnum_get_try_int(const QNum *qn, int64_t *val) |
73 | 8.09k | { |
74 | 8.09k | switch (qn->kind) { |
75 | 8.09k | case QNUM_I64: |
76 | 8.09k | *val = qn->u.i64; |
77 | 8.09k | return true; |
78 | 0 | case QNUM_U64: |
79 | 0 | if (qn->u.u64 > INT64_MAX) { |
80 | 0 | return false; |
81 | 0 | } |
82 | 0 | *val = qn->u.u64; |
83 | 0 | return true; |
84 | 0 | case QNUM_DOUBLE: |
85 | 0 | return false; |
86 | 8.09k | } |
87 | | |
88 | 8.09k | g_assert_not_reached(); |
89 | 0 | } |
90 | | |
91 | | /** |
92 | | * qnum_get_int(): Get an integer representation of the number |
93 | | * |
94 | | * assert() on failure. |
95 | | */ |
96 | | int64_t qnum_get_int(const QNum *qn) |
97 | 0 | { |
98 | 0 | int64_t val; |
99 | 0 | bool success = qnum_get_try_int(qn, &val); |
100 | 0 | assert(success); |
101 | 0 | return val; |
102 | 0 | } |
103 | | |
104 | | /** |
105 | | * qnum_get_uint(): Get an unsigned integer from the number |
106 | | * |
107 | | * Return true on success. |
108 | | */ |
109 | | bool qnum_get_try_uint(const QNum *qn, uint64_t *val) |
110 | 18.6M | { |
111 | 18.6M | switch (qn->kind) { |
112 | 1.82k | case QNUM_I64: |
113 | 1.82k | if (qn->u.i64 < 0) { |
114 | 24 | return false; |
115 | 24 | } |
116 | 1.80k | *val = qn->u.i64; |
117 | 1.80k | return true; |
118 | 18.6M | case QNUM_U64: |
119 | 18.6M | *val = qn->u.u64; |
120 | 18.6M | return true; |
121 | 0 | case QNUM_DOUBLE: |
122 | 0 | return false; |
123 | 18.6M | } |
124 | | |
125 | 18.6M | g_assert_not_reached(); |
126 | 0 | } |
127 | | |
128 | | /** |
129 | | * qnum_get_uint(): Get an unsigned integer from the number |
130 | | * |
131 | | * assert() on failure. |
132 | | */ |
133 | | uint64_t qnum_get_uint(const QNum *qn) |
134 | 362 | { |
135 | 362 | uint64_t val; |
136 | 362 | bool success = qnum_get_try_uint(qn, &val); |
137 | 362 | assert(success); |
138 | 362 | return val; |
139 | 362 | } |
140 | | |
141 | | /** |
142 | | * qnum_get_double(): Get a float representation of the number |
143 | | * |
144 | | * qnum_get_double() loses precision for integers beyond 53 bits. |
145 | | */ |
146 | | double qnum_get_double(QNum *qn) |
147 | 0 | { |
148 | 0 | switch (qn->kind) { |
149 | 0 | case QNUM_I64: |
150 | 0 | return qn->u.i64; |
151 | 0 | case QNUM_U64: |
152 | 0 | return qn->u.u64; |
153 | 0 | case QNUM_DOUBLE: |
154 | 0 | return qn->u.dbl; |
155 | 0 | } |
156 | | |
157 | 0 | g_assert_not_reached(); |
158 | 0 | } |
159 | | |
160 | | char *qnum_to_string(QNum *qn) |
161 | 0 | { |
162 | 0 | switch (qn->kind) { |
163 | 0 | case QNUM_I64: |
164 | 0 | return g_strdup_printf("%" PRId64, qn->u.i64); |
165 | 0 | case QNUM_U64: |
166 | 0 | return g_strdup_printf("%" PRIu64, qn->u.u64); |
167 | 0 | case QNUM_DOUBLE: |
168 | | /* 17 digits suffice for IEEE double */ |
169 | 0 | return g_strdup_printf("%.17g", qn->u.dbl); |
170 | 0 | } |
171 | | |
172 | 0 | g_assert_not_reached(); |
173 | 0 | } |
174 | | |
175 | | /** |
176 | | * 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 | | */ |
184 | | bool qnum_is_equal(const QObject *x, const QObject *y) |
185 | 0 | { |
186 | 0 | QNum *num_x = qobject_to(QNum, x); |
187 | 0 | QNum *num_y = qobject_to(QNum, y); |
188 | |
|
189 | 0 | switch (num_x->kind) { |
190 | 0 | case QNUM_I64: |
191 | 0 | switch (num_y->kind) { |
192 | 0 | case QNUM_I64: |
193 | | /* Comparison in native int64_t type */ |
194 | 0 | return num_x->u.i64 == num_y->u.i64; |
195 | 0 | case QNUM_U64: |
196 | | /* Implicit conversion of x to uin64_t, so we have to |
197 | | * check its sign before */ |
198 | 0 | return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64; |
199 | 0 | case QNUM_DOUBLE: |
200 | 0 | return false; |
201 | 0 | } |
202 | 0 | abort(); |
203 | 0 | case QNUM_U64: |
204 | 0 | switch (num_y->kind) { |
205 | 0 | case QNUM_I64: |
206 | 0 | return qnum_is_equal(y, x); |
207 | 0 | case QNUM_U64: |
208 | | /* Comparison in native uint64_t type */ |
209 | 0 | return num_x->u.u64 == num_y->u.u64; |
210 | 0 | case QNUM_DOUBLE: |
211 | 0 | return false; |
212 | 0 | } |
213 | 0 | abort(); |
214 | 0 | case QNUM_DOUBLE: |
215 | 0 | switch (num_y->kind) { |
216 | 0 | case QNUM_I64: |
217 | 0 | case QNUM_U64: |
218 | 0 | return false; |
219 | 0 | case QNUM_DOUBLE: |
220 | | /* Comparison in native double type */ |
221 | 0 | return num_x->u.dbl == num_y->u.dbl; |
222 | 0 | } |
223 | 0 | abort(); |
224 | 0 | } |
225 | | |
226 | 0 | abort(); |
227 | 0 | } |
228 | | |
229 | | /** |
230 | | * qnum_destroy_obj(): Free all memory allocated by a |
231 | | * QNum object |
232 | | */ |
233 | | void qnum_destroy_obj(QObject *obj) |
234 | 18.9M | { |
235 | 18.9M | assert(obj != NULL); |
236 | 18.9M | g_free(qobject_to(QNum, obj)); |
237 | 18.9M | } |
238 | | |
239 | | void qnum_unref(QNum *q) |
240 | 0 | { |
241 | | qobject_unref(q); |
242 | 0 | } |