Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 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" |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 16 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | 80d7112 | 2020-12-11 18:11:40 +0100 | [diff] [blame] | 17 | #include "qobject-internal.h" |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 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 | { |
| 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é Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 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 | { |
| 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é Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 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 | { |
| 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 | */ |
| 72 | bool 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é Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 78 | case QNUM_U64: |
| 79 | if (qn->u.u64 > INT64_MAX) { |
| 80 | return false; |
| 81 | } |
| 82 | *val = qn->u.u64; |
| 83 | return true; |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 84 | case QNUM_DOUBLE: |
| 85 | return false; |
| 86 | } |
| 87 | |
Pierrick Bouvier | 890000d | 2024-09-18 21:46:12 -0700 | [diff] [blame] | 88 | g_assert_not_reached(); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 89 | } |
| 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 | { |
| 98 | int64_t val; |
| 99 | bool success = qnum_get_try_int(qn, &val); |
| 100 | assert(success); |
| 101 | return val; |
| 102 | } |
| 103 | |
| 104 | /** |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 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 | { |
| 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 Bouvier | 890000d | 2024-09-18 21:46:12 -0700 | [diff] [blame] | 125 | g_assert_not_reached(); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 126 | } |
| 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 | { |
| 135 | uint64_t val; |
| 136 | bool success = qnum_get_try_uint(qn, &val); |
| 137 | assert(success); |
| 138 | return val; |
| 139 | } |
| 140 | |
| 141 | /** |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 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 | { |
| 148 | switch (qn->kind) { |
| 149 | case QNUM_I64: |
| 150 | return qn->u.i64; |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 151 | case QNUM_U64: |
| 152 | return qn->u.u64; |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 153 | case QNUM_DOUBLE: |
| 154 | return qn->u.dbl; |
| 155 | } |
| 156 | |
Pierrick Bouvier | 890000d | 2024-09-18 21:46:12 -0700 | [diff] [blame] | 157 | g_assert_not_reached(); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | char *qnum_to_string(QNum *qn) |
| 161 | { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 162 | switch (qn->kind) { |
| 163 | case QNUM_I64: |
| 164 | return g_strdup_printf("%" PRId64, qn->u.i64); |
Marc-André Lureau | 61a8f41 | 2017-06-07 20:36:01 +0400 | [diff] [blame] | 165 | case QNUM_U64: |
| 166 | return g_strdup_printf("%" PRIu64, qn->u.u64); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 167 | case QNUM_DOUBLE: |
Markus Armbruster | f917eed | 2020-12-10 17:14:48 +0100 | [diff] [blame] | 168 | /* 17 digits suffice for IEEE double */ |
| 169 | return g_strdup_printf("%.17g", qn->u.dbl); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 170 | } |
| 171 | |
Pierrick Bouvier | 890000d | 2024-09-18 21:46:12 -0700 | [diff] [blame] | 172 | g_assert_not_reached(); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Max Reitz | b38dd67 | 2017-11-14 19:01:25 +0100 | [diff] [blame] | 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 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 186 | QNum *num_x = qobject_to(QNum, x); |
| 187 | QNum *num_y = qobject_to(QNum, y); |
Max Reitz | b38dd67 | 2017-11-14 19:01:25 +0100 | [diff] [blame] | 188 | |
| 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é Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 230 | * qnum_destroy_obj(): Free all memory allocated by a |
| 231 | * QNum object |
| 232 | */ |
| 233 | void qnum_destroy_obj(QObject *obj) |
| 234 | { |
| 235 | assert(obj != NULL); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 236 | g_free(qobject_to(QNum, obj)); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 237 | } |
Marc-André Lureau | d709bbf | 2022-03-23 19:57:20 +0400 | [diff] [blame] | 238 | |
| 239 | void qnum_unref(QNum *q) |
| 240 | { |
| 241 | qobject_unref(q); |
| 242 | } |