qnum.c 5.1 KB

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