objint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdlib.h>
  27. #include <assert.h>
  28. #include <string.h>
  29. #include "py/parsenum.h"
  30. #include "py/smallint.h"
  31. #include "py/objint.h"
  32. #include "py/objstr.h"
  33. #include "py/runtime.h"
  34. #include "py/binary.h"
  35. #if MICROPY_PY_BUILTINS_FLOAT
  36. #include <math.h>
  37. #endif
  38. // This dispatcher function is expected to be independent of the implementation of long int
  39. static mp_obj_t mp_obj_int_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  40. (void)type_in;
  41. mp_arg_check_num(n_args, n_kw, 0, 2, false);
  42. switch (n_args) {
  43. case 0:
  44. return MP_OBJ_NEW_SMALL_INT(0);
  45. case 1: {
  46. mp_buffer_info_t bufinfo;
  47. mp_obj_t o = mp_unary_op(MP_UNARY_OP_INT_MAYBE, args[0]);
  48. if (o != MP_OBJ_NULL) {
  49. return o;
  50. } else if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ)) {
  51. // a textual representation, parse it
  52. return mp_parse_num_integer(bufinfo.buf, bufinfo.len, 0, NULL);
  53. #if MICROPY_PY_BUILTINS_FLOAT
  54. } else if (mp_obj_is_float(args[0])) {
  55. return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
  56. #endif
  57. } else {
  58. mp_raise_TypeError_int_conversion(args[0]);
  59. }
  60. }
  61. case 2:
  62. default: {
  63. // should be a string, parse it
  64. size_t l;
  65. const char *s = mp_obj_str_get_data(args[0], &l);
  66. return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]), NULL);
  67. }
  68. }
  69. }
  70. #if MICROPY_PY_BUILTINS_FLOAT
  71. typedef enum {
  72. MP_FP_CLASS_FIT_SMALLINT,
  73. MP_FP_CLASS_FIT_LONGINT,
  74. MP_FP_CLASS_OVERFLOW
  75. } mp_fp_as_int_class_t;
  76. static mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
  77. union {
  78. mp_float_t f;
  79. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  80. uint32_t i;
  81. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  82. uint32_t i[2];
  83. #endif
  84. } u = {val};
  85. uint32_t e;
  86. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  87. e = u.i;
  88. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  89. e = u.i[MP_ENDIANNESS_LITTLE];
  90. #endif
  91. #define MP_FLOAT_SIGN_SHIFT_I32 ((MP_FLOAT_FRAC_BITS + MP_FLOAT_EXP_BITS) % 32)
  92. #define MP_FLOAT_EXP_SHIFT_I32 (MP_FLOAT_FRAC_BITS % 32)
  93. if (e & (1U << MP_FLOAT_SIGN_SHIFT_I32)) {
  94. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  95. e |= u.i[MP_ENDIANNESS_BIG] != 0;
  96. #endif
  97. if ((e & ~(1U << MP_FLOAT_SIGN_SHIFT_I32)) == 0) {
  98. // handle case of -0 (when sign is set but rest of bits are zero)
  99. e = 0;
  100. } else {
  101. e += ((1U << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32;
  102. }
  103. } else {
  104. e &= ~((1U << MP_FLOAT_EXP_SHIFT_I32) - 1);
  105. }
  106. // 8 * sizeof(uintptr_t) counts the number of bits for a small int
  107. // TODO provide a way to configure this properly
  108. if (e <= ((8 * sizeof(uintptr_t) + MP_FLOAT_EXP_BIAS - 3) << MP_FLOAT_EXP_SHIFT_I32)) {
  109. return MP_FP_CLASS_FIT_SMALLINT;
  110. }
  111. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
  112. if (e <= (((sizeof(long long) * MP_BITS_PER_BYTE) + MP_FLOAT_EXP_BIAS - 2) << MP_FLOAT_EXP_SHIFT_I32)) {
  113. return MP_FP_CLASS_FIT_LONGINT;
  114. }
  115. #endif
  116. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
  117. return MP_FP_CLASS_FIT_LONGINT;
  118. #else
  119. return MP_FP_CLASS_OVERFLOW;
  120. #endif
  121. }
  122. #undef MP_FLOAT_SIGN_SHIFT_I32
  123. #undef MP_FLOAT_EXP_SHIFT_I32
  124. mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
  125. mp_float_union_t u = {val};
  126. // IEEE-754: if biased exponent is all 1 bits...
  127. if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
  128. // ...then number is Inf (positive or negative) if fraction is 0, else NaN.
  129. if (u.p.frc == 0) {
  130. mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("can't convert inf to int"));
  131. } else {
  132. mp_raise_ValueError(MP_ERROR_TEXT("can't convert NaN to int"));
  133. }
  134. } else {
  135. mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
  136. if (icl == MP_FP_CLASS_FIT_SMALLINT) {
  137. return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
  138. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
  139. } else {
  140. mp_obj_int_t *o = mp_obj_int_new_mpz();
  141. mpz_set_from_float(&o->mpz, val);
  142. return MP_OBJ_FROM_PTR(o);
  143. }
  144. #else
  145. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
  146. } else if (icl == MP_FP_CLASS_FIT_LONGINT) {
  147. return mp_obj_new_int_from_ll((long long)val);
  148. #endif
  149. } else {
  150. mp_raise_ValueError(MP_ERROR_TEXT("float too big"));
  151. }
  152. #endif
  153. }
  154. }
  155. #endif
  156. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
  157. typedef mp_longint_impl_t fmt_int_t;
  158. typedef unsigned long long fmt_uint_t;
  159. #else
  160. typedef mp_int_t fmt_int_t;
  161. typedef mp_uint_t fmt_uint_t;
  162. #endif
  163. void mp_obj_int_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  164. (void)kind;
  165. // The size of this buffer is rather arbitrary. If it's not large
  166. // enough, a dynamic one will be allocated.
  167. char stack_buf[sizeof(fmt_int_t) * 4];
  168. char *buf = stack_buf;
  169. size_t buf_size = sizeof(stack_buf);
  170. size_t fmt_size;
  171. char *str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, self_in, 10, NULL, '\0', '\0');
  172. mp_print_str(print, str);
  173. if (buf != stack_buf) {
  174. m_del(char, buf, buf_size);
  175. }
  176. }
  177. static const uint8_t log_base2_floor[] = {
  178. 0, 1, 1, 2,
  179. 2, 2, 2, 3,
  180. 3, 3, 3, 3,
  181. 3, 3, 3, 4,
  182. /* if needed, these are the values for higher bases
  183. 4, 4, 4, 4,
  184. 4, 4, 4, 4,
  185. 4, 4, 4, 4,
  186. 4, 4, 4, 5
  187. */
  188. };
  189. size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma) {
  190. assert(2 <= base && base <= 16);
  191. size_t num_digits = num_bits / log_base2_floor[base - 1] + 1;
  192. size_t num_commas = comma ? num_digits / 3 : 0;
  193. size_t prefix_len = prefix ? strlen(prefix) : 0;
  194. return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
  195. }
  196. // This routine expects you to pass in a buffer and size (in *buf and *buf_size).
  197. // If, for some reason, this buffer is too small, then it will allocate a
  198. // buffer and return the allocated buffer and size in *buf and *buf_size. It
  199. // is the callers responsibility to free this allocated buffer.
  200. //
  201. // The resulting formatted string will be returned from this function and the
  202. // formatted size will be in *fmt_size.
  203. char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in,
  204. int base, const char *prefix, char base_char, char comma) {
  205. fmt_int_t num;
  206. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
  207. // Only have small ints; get the integer value to format.
  208. num = MP_OBJ_SMALL_INT_VALUE(self_in);
  209. #else
  210. if (mp_obj_is_small_int(self_in)) {
  211. // A small int; get the integer value to format.
  212. num = MP_OBJ_SMALL_INT_VALUE(self_in);
  213. } else {
  214. assert(mp_obj_is_exact_type(self_in, &mp_type_int));
  215. // Not a small int.
  216. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
  217. const mp_obj_int_t *self = self_in;
  218. // Get the value to format; mp_obj_get_int truncates to mp_int_t.
  219. num = self->val;
  220. #else
  221. // Delegate to the implementation for the long int.
  222. return mp_obj_int_formatted_impl(buf, buf_size, fmt_size, self_in, base, prefix, base_char, comma);
  223. #endif
  224. }
  225. #endif
  226. char sign = '\0';
  227. if (num < 0) {
  228. num = -num;
  229. sign = '-';
  230. }
  231. size_t needed_size = mp_int_format_size(sizeof(fmt_int_t) * 8, base, prefix, comma);
  232. if (needed_size > *buf_size) {
  233. *buf = m_new(char, needed_size);
  234. *buf_size = needed_size;
  235. }
  236. char *str = *buf;
  237. char *b = str + needed_size;
  238. *(--b) = '\0';
  239. char *last_comma = b;
  240. if (num == 0) {
  241. *(--b) = '0';
  242. } else {
  243. do {
  244. // The cast to fmt_uint_t is because num is positive and we want unsigned arithmetic
  245. int c = (fmt_uint_t)num % base;
  246. num = (fmt_uint_t)num / base;
  247. if (c >= 10) {
  248. c += base_char - 10;
  249. } else {
  250. c += '0';
  251. }
  252. *(--b) = c;
  253. if (comma && num != 0 && b > str && (last_comma - b) == 3) {
  254. *(--b) = comma;
  255. last_comma = b;
  256. }
  257. }
  258. while (b > str && num != 0);
  259. }
  260. if (prefix) {
  261. size_t prefix_len = strlen(prefix);
  262. char *p = b - prefix_len;
  263. if (p > str) {
  264. b = p;
  265. while (*prefix) {
  266. *p++ = *prefix++;
  267. }
  268. }
  269. }
  270. if (sign && b > str) {
  271. *(--b) = sign;
  272. }
  273. *fmt_size = *buf + needed_size - b - 1;
  274. return b;
  275. }
  276. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
  277. int mp_obj_int_sign(mp_obj_t self_in) {
  278. mp_int_t val = mp_obj_get_int(self_in);
  279. if (val < 0) {
  280. return -1;
  281. } else if (val > 0) {
  282. return 1;
  283. } else {
  284. return 0;
  285. }
  286. }
  287. // This is called for operations on SMALL_INT that are not handled by mp_unary_op
  288. mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
  289. return MP_OBJ_NULL; // op not supported
  290. }
  291. // This is called for operations on SMALL_INT that are not handled by mp_binary_op
  292. mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  293. return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
  294. }
  295. // This is called only with strings whose value doesn't fit in SMALL_INT
  296. mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) {
  297. mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("long int not supported in this build"));
  298. return mp_const_none;
  299. }
  300. // This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
  301. mp_obj_t mp_obj_new_int_from_ll(long long val) {
  302. mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
  303. return mp_const_none;
  304. }
  305. // This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
  306. mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
  307. mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
  308. return mp_const_none;
  309. }
  310. mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
  311. // SMALL_INT accepts only signed numbers, so make sure the input
  312. // value fits completely in the small-int positive range.
  313. if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
  314. return MP_OBJ_NEW_SMALL_INT(value);
  315. }
  316. mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
  317. return mp_const_none;
  318. }
  319. mp_obj_t mp_obj_new_int(mp_int_t value) {
  320. if (MP_SMALL_INT_FITS(value)) {
  321. return MP_OBJ_NEW_SMALL_INT(value);
  322. }
  323. mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
  324. return mp_const_none;
  325. }
  326. mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
  327. return MP_OBJ_SMALL_INT_VALUE(self_in);
  328. }
  329. mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
  330. return MP_OBJ_SMALL_INT_VALUE(self_in);
  331. }
  332. #endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
  333. // This dispatcher function is expected to be independent of the implementation of long int
  334. // It handles the extra cases for integer-like arithmetic
  335. mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  336. if (rhs_in == mp_const_false) {
  337. // false acts as 0
  338. return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));
  339. } else if (rhs_in == mp_const_true) {
  340. // true acts as 0
  341. return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1));
  342. } else if (op == MP_BINARY_OP_MULTIPLY) {
  343. if (mp_obj_is_str_or_bytes(rhs_in) || mp_obj_is_type(rhs_in, &mp_type_tuple) || mp_obj_is_type(rhs_in, &mp_type_list)) {
  344. // multiply is commutative for these types, so delegate to them
  345. return mp_binary_op(op, rhs_in, lhs_in);
  346. }
  347. }
  348. return MP_OBJ_NULL; // op not supported
  349. }
  350. // this is a classmethod
  351. static mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
  352. // TODO: Support signed param (assumes signed=False at the moment)
  353. (void)n_args;
  354. // get the buffer info
  355. mp_buffer_info_t bufinfo;
  356. mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
  357. const byte *buf = (const byte *)bufinfo.buf;
  358. int delta = 1;
  359. if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
  360. buf += bufinfo.len - 1;
  361. delta = -1;
  362. }
  363. mp_uint_t value = 0;
  364. size_t len = bufinfo.len;
  365. for (; len--; buf += delta) {
  366. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  367. if (value > (MP_SMALL_INT_MAX >> 8)) {
  368. // Result will overflow a small-int so construct a big-int
  369. return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
  370. }
  371. #endif
  372. value = (value << 8) | *buf;
  373. }
  374. return mp_obj_new_int_from_uint(value);
  375. }
  376. static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
  377. static MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
  378. static mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
  379. // TODO: Support signed param (assumes signed=False)
  380. (void)n_args;
  381. mp_int_t len = mp_obj_get_int(args[1]);
  382. if (len < 0) {
  383. mp_raise_ValueError(NULL);
  384. }
  385. bool big_endian = args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
  386. vstr_t vstr;
  387. vstr_init_len(&vstr, len);
  388. byte *data = (byte *)vstr.buf;
  389. memset(data, 0, len);
  390. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  391. if (!mp_obj_is_small_int(args[0])) {
  392. mp_obj_int_to_bytes_impl(args[0], big_endian, len, data);
  393. } else
  394. #endif
  395. {
  396. mp_int_t val = MP_OBJ_SMALL_INT_VALUE(args[0]);
  397. size_t l = MIN((size_t)len, sizeof(val));
  398. mp_binary_set_int(l, big_endian, data + (big_endian ? (len - l) : 0), val);
  399. }
  400. return mp_obj_new_bytes_from_vstr(&vstr);
  401. }
  402. static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 3, 4, int_to_bytes);
  403. static const mp_rom_map_elem_t int_locals_dict_table[] = {
  404. { MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) },
  405. { MP_ROM_QSTR(MP_QSTR_to_bytes), MP_ROM_PTR(&int_to_bytes_obj) },
  406. };
  407. static MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table);
  408. MP_DEFINE_CONST_OBJ_TYPE(
  409. mp_type_int,
  410. MP_QSTR_int,
  411. MP_TYPE_FLAG_NONE,
  412. make_new, mp_obj_int_make_new,
  413. print, mp_obj_int_print,
  414. unary_op, mp_obj_int_unary_op,
  415. binary_op, mp_obj_int_binary_op,
  416. locals_dict, &int_locals_dict
  417. );