binary.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014-2017 Paul Sokolovsky
  7. * Copyright (c) 2014-2019 Damien P. George
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <stddef.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include "py/binary.h"
  33. #include "py/smallint.h"
  34. #include "py/objint.h"
  35. #include "py/runtime.h"
  36. // Helpers to work with binary-encoded data
  37. #ifndef alignof
  38. #define alignof(type) offsetof(struct { char c; type t; }, t)
  39. #endif
  40. size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) {
  41. size_t size = 0;
  42. int align = 1;
  43. switch (struct_type) {
  44. case '<':
  45. case '>':
  46. switch (val_type) {
  47. case 'b':
  48. case 'B':
  49. size = 1;
  50. break;
  51. case 'h':
  52. case 'H':
  53. size = 2;
  54. break;
  55. case 'i':
  56. case 'I':
  57. size = 4;
  58. break;
  59. case 'l':
  60. case 'L':
  61. size = 4;
  62. break;
  63. case 'q':
  64. case 'Q':
  65. size = 8;
  66. break;
  67. case 'P':
  68. case 'O':
  69. case 'S':
  70. size = sizeof(void *);
  71. break;
  72. case 'e':
  73. size = 2;
  74. break;
  75. case 'f':
  76. size = 4;
  77. break;
  78. case 'd':
  79. size = 8;
  80. break;
  81. }
  82. break;
  83. case '@': {
  84. // TODO:
  85. // The simplest heuristic for alignment is to align by value
  86. // size, but that doesn't work for "bigger than int" types,
  87. // for example, long long may very well have long alignment
  88. // So, we introduce separate alignment handling, but having
  89. // formal support for that is different from actually supporting
  90. // particular (or any) ABI.
  91. switch (val_type) {
  92. case BYTEARRAY_TYPECODE:
  93. case 'b':
  94. case 'B':
  95. align = size = 1;
  96. break;
  97. case 'h':
  98. case 'H':
  99. align = alignof(short);
  100. size = sizeof(short);
  101. break;
  102. case 'i':
  103. case 'I':
  104. align = alignof(int);
  105. size = sizeof(int);
  106. break;
  107. case 'l':
  108. case 'L':
  109. align = alignof(long);
  110. size = sizeof(long);
  111. break;
  112. case 'q':
  113. case 'Q':
  114. align = alignof(long long);
  115. size = sizeof(long long);
  116. break;
  117. case 'P':
  118. case 'O':
  119. case 'S':
  120. align = alignof(void *);
  121. size = sizeof(void *);
  122. break;
  123. case 'e':
  124. align = 2;
  125. size = 2;
  126. break;
  127. case 'f':
  128. align = alignof(float);
  129. size = sizeof(float);
  130. break;
  131. case 'd':
  132. align = alignof(double);
  133. size = sizeof(double);
  134. break;
  135. }
  136. }
  137. }
  138. if (size == 0) {
  139. mp_raise_ValueError(MP_ERROR_TEXT("bad typecode"));
  140. }
  141. if (palign != NULL) {
  142. *palign = align;
  143. }
  144. return size;
  145. }
  146. #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_FLOAT_USE_NATIVE_FLT16
  147. static inline float mp_decode_half_float(uint16_t hf) {
  148. union {
  149. uint16_t i;
  150. _Float16 f;
  151. } fpu = { .i = hf };
  152. return fpu.f;
  153. }
  154. static inline uint16_t mp_encode_half_float(float x) {
  155. union {
  156. uint16_t i;
  157. _Float16 f;
  158. } fp_sp = { .f = (_Float16)x };
  159. return fp_sp.i;
  160. }
  161. #elif MICROPY_PY_BUILTINS_FLOAT
  162. static float mp_decode_half_float(uint16_t hf) {
  163. union {
  164. uint32_t i;
  165. float f;
  166. } fpu;
  167. uint16_t m = hf & 0x3ff;
  168. int e = (hf >> 10) & 0x1f;
  169. if (e == 0x1f) {
  170. // Half-float is infinity.
  171. e = 0xff;
  172. } else if (e) {
  173. // Half-float is normal.
  174. e += 127 - 15;
  175. } else if (m) {
  176. // Half-float is subnormal, make it normal.
  177. e = 127 - 15;
  178. while (!(m & 0x400)) {
  179. m <<= 1;
  180. --e;
  181. }
  182. m -= 0x400;
  183. ++e;
  184. }
  185. fpu.i = ((hf & 0x8000) << 16) | (e << 23) | (m << 13);
  186. return fpu.f;
  187. }
  188. static uint16_t mp_encode_half_float(float x) {
  189. union {
  190. uint32_t i;
  191. float f;
  192. } fpu = { .f = x };
  193. uint16_t m = (fpu.i >> 13) & 0x3ff;
  194. if (fpu.i & (1 << 12)) {
  195. // Round up.
  196. ++m;
  197. }
  198. int e = (fpu.i >> 23) & 0xff;
  199. if (e == 0xff) {
  200. // Infinity.
  201. e = 0x1f;
  202. } else if (e != 0) {
  203. e -= 127 - 15;
  204. if (e < 0) {
  205. // Underflow: denormalized, or zero.
  206. if (e >= -11) {
  207. m = (m | 0x400) >> -e;
  208. if (m & 1) {
  209. m = (m >> 1) + 1;
  210. } else {
  211. m >>= 1;
  212. }
  213. } else {
  214. m = 0;
  215. }
  216. e = 0;
  217. } else if (e > 0x3f) {
  218. // Overflow: infinity.
  219. e = 0x1f;
  220. m = 0;
  221. }
  222. }
  223. uint16_t bits = ((fpu.i >> 16) & 0x8000) | (e << 10) | m;
  224. return bits;
  225. }
  226. #endif
  227. mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) {
  228. mp_int_t val = 0;
  229. switch (typecode) {
  230. case 'b':
  231. val = ((signed char *)p)[index];
  232. break;
  233. case BYTEARRAY_TYPECODE:
  234. case 'B':
  235. val = ((unsigned char *)p)[index];
  236. break;
  237. case 'h':
  238. val = ((short *)p)[index];
  239. break;
  240. case 'H':
  241. val = ((unsigned short *)p)[index];
  242. break;
  243. case 'i':
  244. return mp_obj_new_int(((int *)p)[index]);
  245. case 'I':
  246. return mp_obj_new_int_from_uint(((unsigned int *)p)[index]);
  247. case 'l':
  248. return mp_obj_new_int(((long *)p)[index]);
  249. case 'L':
  250. return mp_obj_new_int_from_uint(((unsigned long *)p)[index]);
  251. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  252. case 'q':
  253. return mp_obj_new_int_from_ll(((long long *)p)[index]);
  254. case 'Q':
  255. return mp_obj_new_int_from_ull(((unsigned long long *)p)[index]);
  256. #endif
  257. #if MICROPY_PY_BUILTINS_FLOAT
  258. case 'f':
  259. return mp_obj_new_float_from_f(((float *)p)[index]);
  260. case 'd':
  261. return mp_obj_new_float_from_d(((double *)p)[index]);
  262. #endif
  263. // Extension to CPython: array of objects
  264. case 'O':
  265. return ((mp_obj_t *)p)[index];
  266. // Extension to CPython: array of pointers
  267. case 'P':
  268. return mp_obj_new_int((mp_int_t)(uintptr_t)((void **)p)[index]);
  269. }
  270. return MP_OBJ_NEW_SMALL_INT(val);
  271. }
  272. // The long long type is guaranteed to hold at least 64 bits, and size is at
  273. // most 8 (for q and Q), so we will always be able to parse the given data
  274. // and fit it into a long long.
  275. long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const byte *src) {
  276. int delta;
  277. if (!big_endian) {
  278. delta = -1;
  279. src += size - 1;
  280. } else {
  281. delta = 1;
  282. }
  283. unsigned long long val = 0;
  284. if (is_signed && *src & 0x80) {
  285. val = -1;
  286. }
  287. for (uint i = 0; i < size; i++) {
  288. val <<= 8;
  289. val |= *src;
  290. src += delta;
  291. }
  292. return val;
  293. }
  294. #define is_signed(typecode) (typecode > 'Z')
  295. mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr) {
  296. byte *p = *ptr;
  297. size_t align;
  298. size_t size = mp_binary_get_size(struct_type, val_type, &align);
  299. if (struct_type == '@') {
  300. // Align p relative to p_base
  301. p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
  302. #if MP_ENDIANNESS_LITTLE
  303. struct_type = '<';
  304. #else
  305. struct_type = '>';
  306. #endif
  307. }
  308. *ptr = p + size;
  309. long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
  310. if (val_type == 'O') {
  311. return (mp_obj_t)(mp_uint_t)val;
  312. } else if (val_type == 'S') {
  313. const char *s_val = (const char *)(uintptr_t)(mp_uint_t)val;
  314. return mp_obj_new_str(s_val, strlen(s_val));
  315. #if MICROPY_PY_BUILTINS_FLOAT
  316. } else if (val_type == 'e') {
  317. return mp_obj_new_float_from_f(mp_decode_half_float(val));
  318. } else if (val_type == 'f') {
  319. union {
  320. uint32_t i;
  321. float f;
  322. } fpu = {val};
  323. return mp_obj_new_float_from_f(fpu.f);
  324. } else if (val_type == 'd') {
  325. union {
  326. uint64_t i;
  327. double f;
  328. } fpu = {val};
  329. return mp_obj_new_float_from_d(fpu.f);
  330. #endif
  331. } else if (is_signed(val_type)) {
  332. if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
  333. return mp_obj_new_int((mp_int_t)val);
  334. } else {
  335. return mp_obj_new_int_from_ll(val);
  336. }
  337. } else {
  338. if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
  339. return mp_obj_new_int_from_uint((mp_uint_t)val);
  340. } else {
  341. return mp_obj_new_int_from_ull(val);
  342. }
  343. }
  344. }
  345. void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
  346. if (MP_ENDIANNESS_LITTLE && !big_endian) {
  347. memcpy(dest, &val, val_sz);
  348. } else if (MP_ENDIANNESS_BIG && big_endian) {
  349. // only copy the least-significant val_sz bytes
  350. memcpy(dest, (byte *)&val + sizeof(mp_uint_t) - val_sz, val_sz);
  351. } else {
  352. const byte *src;
  353. if (MP_ENDIANNESS_LITTLE) {
  354. src = (const byte *)&val + val_sz;
  355. } else {
  356. src = (const byte *)&val + sizeof(mp_uint_t);
  357. }
  358. while (val_sz--) {
  359. *dest++ = *--src;
  360. }
  361. }
  362. }
  363. void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr) {
  364. byte *p = *ptr;
  365. size_t align;
  366. size_t size = mp_binary_get_size(struct_type, val_type, &align);
  367. if (struct_type == '@') {
  368. // Align p relative to p_base
  369. p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
  370. if (MP_ENDIANNESS_LITTLE) {
  371. struct_type = '<';
  372. } else {
  373. struct_type = '>';
  374. }
  375. }
  376. *ptr = p + size;
  377. mp_uint_t val;
  378. switch (val_type) {
  379. case 'O':
  380. val = (mp_uint_t)val_in;
  381. break;
  382. #if MICROPY_PY_BUILTINS_FLOAT
  383. case 'e':
  384. val = mp_encode_half_float(mp_obj_get_float_to_f(val_in));
  385. break;
  386. case 'f': {
  387. union {
  388. uint32_t i;
  389. float f;
  390. } fp_sp;
  391. fp_sp.f = mp_obj_get_float_to_f(val_in);
  392. val = fp_sp.i;
  393. break;
  394. }
  395. case 'd': {
  396. union {
  397. uint64_t i64;
  398. uint32_t i32[2];
  399. double f;
  400. } fp_dp;
  401. fp_dp.f = mp_obj_get_float_to_d(val_in);
  402. if (MP_BYTES_PER_OBJ_WORD == 8) {
  403. val = fp_dp.i64;
  404. } else {
  405. int be = struct_type == '>';
  406. mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]);
  407. p += sizeof(uint32_t);
  408. val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be];
  409. }
  410. break;
  411. }
  412. #endif
  413. default:
  414. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  415. if (mp_obj_is_exact_type(val_in, &mp_type_int)) {
  416. mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p);
  417. return;
  418. }
  419. #endif
  420. val = mp_obj_get_int(val_in);
  421. // zero/sign extend if needed
  422. if (MP_BYTES_PER_OBJ_WORD < 8 && size > sizeof(val)) {
  423. int c = (mp_int_t)val < 0 ? 0xff : 0x00;
  424. memset(p, c, size);
  425. if (struct_type == '>') {
  426. p += size - sizeof(val);
  427. }
  428. }
  429. break;
  430. }
  431. mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
  432. }
  433. void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in) {
  434. switch (typecode) {
  435. #if MICROPY_PY_BUILTINS_FLOAT
  436. case 'f':
  437. ((float *)p)[index] = mp_obj_get_float_to_f(val_in);
  438. break;
  439. case 'd':
  440. ((double *)p)[index] = mp_obj_get_float_to_d(val_in);
  441. break;
  442. #endif
  443. // Extension to CPython: array of objects
  444. case 'O':
  445. ((mp_obj_t *)p)[index] = val_in;
  446. break;
  447. default:
  448. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  449. if (mp_obj_is_exact_type(val_in, &mp_type_int)) {
  450. size_t size = mp_binary_get_size('@', typecode, NULL);
  451. mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
  452. size, (uint8_t *)p + index * size);
  453. return;
  454. }
  455. #endif
  456. mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
  457. }
  458. }
  459. void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val) {
  460. switch (typecode) {
  461. case 'b':
  462. ((signed char *)p)[index] = val;
  463. break;
  464. case BYTEARRAY_TYPECODE:
  465. case 'B':
  466. ((unsigned char *)p)[index] = val;
  467. break;
  468. case 'h':
  469. ((short *)p)[index] = val;
  470. break;
  471. case 'H':
  472. ((unsigned short *)p)[index] = val;
  473. break;
  474. case 'i':
  475. ((int *)p)[index] = val;
  476. break;
  477. case 'I':
  478. ((unsigned int *)p)[index] = val;
  479. break;
  480. case 'l':
  481. ((long *)p)[index] = val;
  482. break;
  483. case 'L':
  484. ((unsigned long *)p)[index] = val;
  485. break;
  486. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  487. case 'q':
  488. ((long long *)p)[index] = val;
  489. break;
  490. case 'Q':
  491. ((unsigned long long *)p)[index] = val;
  492. break;
  493. #endif
  494. #if MICROPY_PY_BUILTINS_FLOAT
  495. case 'f':
  496. ((float *)p)[index] = (float)val;
  497. break;
  498. case 'd':
  499. ((double *)p)[index] = (double)val;
  500. break;
  501. #endif
  502. // Extension to CPython: array of pointers
  503. case 'P':
  504. ((void **)p)[index] = (void *)(uintptr_t)val;
  505. break;
  506. }
  507. }