gmp-utils.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (C) 2019-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "gmp-utils.h"
  14. /* See gmp-utils.h. */
  15. std::string
  16. gmp_string_printf (const char *fmt, ...)
  17. {
  18. va_list vp;
  19. va_start (vp, fmt);
  20. int size = gmp_vsnprintf (NULL, 0, fmt, vp);
  21. va_end (vp);
  22. std::string str (size, '\0');
  23. /* C++11 and later guarantee std::string uses contiguous memory and
  24. always includes the terminating '\0'. */
  25. va_start (vp, fmt);
  26. gmp_vsprintf (&str[0], fmt, vp);
  27. va_end (vp);
  28. return str;
  29. }
  30. /* See gmp-utils.h. */
  31. void
  32. gdb_mpz::read (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order,
  33. bool unsigned_p)
  34. {
  35. mpz_import (val, 1 /* count */, -1 /* order */, buf.size () /* size */,
  36. byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */,
  37. 0 /* nails */, buf.data () /* op */);
  38. if (!unsigned_p)
  39. {
  40. /* The value was imported as if it was a positive value,
  41. as mpz_import does not handle signs. If the original value
  42. was in fact negative, we need to adjust VAL accordingly. */
  43. gdb_mpz max;
  44. mpz_ui_pow_ui (max.val, 2, buf.size () * HOST_CHAR_BIT - 1);
  45. if (mpz_cmp (val, max.val) >= 0)
  46. mpz_submul_ui (val, max.val, 2);
  47. }
  48. }
  49. /* See gmp-utils.h. */
  50. void
  51. gdb_mpz::write (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
  52. bool unsigned_p) const
  53. {
  54. this->safe_export
  55. (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, unsigned_p);
  56. }
  57. /* See gmp-utils.h. */
  58. void
  59. gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
  60. int endian, bool unsigned_p) const
  61. {
  62. gdb_assert (buf.size () > 0);
  63. if (mpz_sgn (val) == 0)
  64. {
  65. /* Our value is zero, so no need to call mpz_export to do the work,
  66. especially since mpz_export's documentation explicitly says
  67. that the function is a noop in this case. Just write zero to
  68. BUF ourselves. */
  69. memset (buf.data (), 0, buf.size ());
  70. return;
  71. }
  72. /* Determine the maximum range of values that our buffer can hold,
  73. and verify that VAL is within that range. */
  74. gdb_mpz lo, hi;
  75. const size_t max_usable_bits = buf.size () * HOST_CHAR_BIT;
  76. if (unsigned_p)
  77. {
  78. lo = 0;
  79. mpz_ui_pow_ui (hi.val, 2, max_usable_bits);
  80. mpz_sub_ui (hi.val, hi.val, 1);
  81. }
  82. else
  83. {
  84. mpz_ui_pow_ui (lo.val, 2, max_usable_bits - 1);
  85. mpz_neg (lo.val, lo.val);
  86. mpz_ui_pow_ui (hi.val, 2, max_usable_bits - 1);
  87. mpz_sub_ui (hi.val, hi.val, 1);
  88. }
  89. if (mpz_cmp (val, lo.val) < 0 || mpz_cmp (val, hi.val) > 0)
  90. error (_("Cannot export value %s as %zu-bits %s integer"
  91. " (must be between %s and %s)"),
  92. this->str ().c_str (),
  93. max_usable_bits,
  94. unsigned_p ? _("unsigned") : _("signed"),
  95. lo.str ().c_str (),
  96. hi.str ().c_str ());
  97. gdb_mpz exported_val (val);
  98. if (mpz_cmp_ui (exported_val.val, 0) < 0)
  99. {
  100. /* mpz_export does not handle signed values, so create a positive
  101. value whose bit representation as an unsigned of the same length
  102. would be the same as our negative value. */
  103. gdb_mpz neg_offset;
  104. mpz_ui_pow_ui (neg_offset.val, 2, buf.size () * HOST_CHAR_BIT);
  105. mpz_add (exported_val.val, exported_val.val, neg_offset.val);
  106. }
  107. /* Do the export into a buffer allocated by GMP itself; that way,
  108. we can detect cases where BUF is not large enough to export
  109. our value, and thus avoid a buffer overlow. Normally, this should
  110. never happen, since we verified earlier that the buffer is large
  111. enough to accomodate our value, but doing this allows us to be
  112. extra safe with the export.
  113. After verification that the export behaved as expected, we will
  114. copy the data over to BUF. */
  115. size_t word_countp;
  116. gdb::unique_xmalloc_ptr<void> exported
  117. (mpz_export (NULL, &word_countp, -1 /* order */, buf.size () /* size */,
  118. endian, 0 /* nails */, exported_val.val));
  119. gdb_assert (word_countp == 1);
  120. memcpy (buf.data (), exported.get (), buf.size ());
  121. }
  122. /* See gmp-utils.h. */
  123. gdb_mpz
  124. gdb_mpq::get_rounded () const
  125. {
  126. /* Work with a positive number so as to make the "floor" rounding
  127. always round towards zero. */
  128. gdb_mpq abs_val (val);
  129. mpq_abs (abs_val.val, abs_val.val);
  130. /* Convert our rational number into a quotient and remainder,
  131. with "floor" rounding, which in our case means rounding
  132. towards zero. */
  133. gdb_mpz quotient, remainder;
  134. mpz_fdiv_qr (quotient.val, remainder.val,
  135. mpq_numref (abs_val.val), mpq_denref (abs_val.val));
  136. /* Multiply the remainder by 2, and see if it is greater or equal
  137. to abs_val's denominator. If yes, round to the next integer. */
  138. mpz_mul_ui (remainder.val, remainder.val, 2);
  139. if (mpz_cmp (remainder.val, mpq_denref (abs_val.val)) >= 0)
  140. mpz_add_ui (quotient.val, quotient.val, 1);
  141. /* Re-apply the sign if needed. */
  142. if (mpq_sgn (val) < 0)
  143. mpz_neg (quotient.val, quotient.val);
  144. return quotient;
  145. }
  146. /* See gmp-utils.h. */
  147. void
  148. gdb_mpq::read_fixed_point (gdb::array_view<const gdb_byte> buf,
  149. enum bfd_endian byte_order, bool unsigned_p,
  150. const gdb_mpq &scaling_factor)
  151. {
  152. gdb_mpz vz;
  153. vz.read (buf, byte_order, unsigned_p);
  154. mpq_set_z (val, vz.val);
  155. mpq_mul (val, val, scaling_factor.val);
  156. }
  157. /* See gmp-utils.h. */
  158. void
  159. gdb_mpq::write_fixed_point (gdb::array_view<gdb_byte> buf,
  160. enum bfd_endian byte_order, bool unsigned_p,
  161. const gdb_mpq &scaling_factor) const
  162. {
  163. gdb_mpq unscaled (val);
  164. mpq_div (unscaled.val, unscaled.val, scaling_factor.val);
  165. gdb_mpz unscaled_z = unscaled.get_rounded ();
  166. unscaled_z.write (buf, byte_order, unsigned_p);
  167. }
  168. /* A wrapper around xrealloc that we can then register with GMP
  169. as the "realloc" function. */
  170. static void *
  171. xrealloc_for_gmp (void *ptr, size_t old_size, size_t new_size)
  172. {
  173. return xrealloc (ptr, new_size);
  174. }
  175. /* A wrapper around xfree that we can then register with GMP
  176. as the "free" function. */
  177. static void
  178. xfree_for_gmp (void *ptr, size_t size)
  179. {
  180. xfree (ptr);
  181. }
  182. void _initialize_gmp_utils ();
  183. void
  184. _initialize_gmp_utils ()
  185. {
  186. /* Tell GMP to use GDB's memory management routines. */
  187. mp_set_memory_functions (xmalloc, xrealloc_for_gmp, xfree_for_gmp);
  188. }