{"action":"created","issue":{"url":"https://api.github.com/repos/micropython/micropython/issues/1504","labels_url":"https://api.github.com/repos/micropython/micropython/issues/1504/labels{/name}","comments_url":"https://api.github.com/repos/micropython/micropython/issues/1504/comments","events_url":"https://api.github.com/repos/micropython/micropython/issues/1504/events","html_url":"https://github.com/micropython/micropython/issues/1504","id":110966011,"number":1504,"title":"RFC: configurablility of object representation, especially with respect to floats","user":{"login":"dpgeorge","id":6187689,"avatar_url":"https://avatars.githubusercontent.com/u/6187689?v=3","gravatar_id":"","url":"https://api.github.com/users/dpgeorge","html_url":"https://github.com/dpgeorge","followers_url":"https://api.github.com/users/dpgeorge/followers","following_url":"https://api.github.com/users/dpgeorge/following{/other_user}","gists_url":"https://api.github.com/users/dpgeorge/gists{/gist_id}","starred_url":"https://api.github.com/users/dpgeorge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpgeorge/subscriptions","organizations_url":"https://api.github.com/users/dpgeorge/orgs","repos_url":"https://api.github.com/users/dpgeorge/repos","events_url":"https://api.github.com/users/dpgeorge/events{/privacy}","received_events_url":"https://api.github.com/users/dpgeorge/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/micropython/micropython/labels/rfc","name":"rfc","color":"FF66CC"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-12T12:37:30Z","updated_at":"2015-10-12T12:42:09Z","closed_at":null,"body":"There is a need to make a more comprehensive selection of object models (ie how uPy objects are represented in a machine word).  They would be configurable at compile time.  In particular there is a need to have more flexibility with floating point representation so floats can be used without the heap.  There are 3 driving use-cases: ESA, microbit, and floats in interrupts on pyboard.\r\n\r\nI foresee the following:\r\n\r\n1. Floats always stuffed in an object word.\r\n\r\n2. Floats stuffed and on the heap.\r\n\r\n3. 64-bit NaN boxing.\r\n\r\nFloat stuffing is similar to int stuffing, except that we need to truncate the mantissa to fit.  It depends on whether the word size is 32-bit or 64-bit, and what precision float you have.  To keep it simple for now, here is the scheme I came up with for single precision float stuffing on a 32-bit machine:\r\n```\r\niiiiiiii iiiiiiii iiiiiiii iiiiiii1 small int, i's are the 31-bit signed value\r\n01111111 11qqqqqq qqqqqqqq qqqqqq10 str object with 20-bit qstr value\r\n11111111 11qqqqqq qqqqqqqq qqqqqq10 bytes object with 20-bit qstr value (optional)\r\ns1111111 10000000 00000000 00000010 +/- inf (lower 2 bits -> 0)\r\nx1111111 101xxxxx xxxxxxxx xxxxxx10 nan (ok as-is, but could put lower 2 bits -> 0)\r\nseeeeeee efffffff ffffffff ffffff10 30-bit fp, e != 0xff (lower 2 bits -> 0)\r\npppppppp pppppppp pppppppp pppppp00 ptr (4 byte alignment)\r\n```\r\nThe trick is to truncate the last 2 bits of the single precision float (so it's stored as 30-bits) and then use 30-bit nan boxing within that 30-bit float to store qstr strings.\r\n\r\nWith float stuffing like this, no heap is needed for floating point objects.  Downside is that all floats are stored in 30-bit precision.  This is a loss in precision of about 1 part per million so not a great loss, and would be a good option for some ports that are low on RAM.\r\n\r\nOf course, the big advantage of no heap for floats is that you can do floating point operations within an interrupt.\r\n\r\nAnd we can actually keep 32-bit precision outside interrupts by the following trick: we allow 2 different float objects, one is stuffed and the other is the existing heap version.  When extracting a float value from an object you just need to check if it's stuffed or a pointer, it's pretty easy and relatively efficient.  When creating a float object, if the heap is locked then return a stuff float, otherwise allocate on the heap.  So that's option 2 above, stuffed + heap.\r\n\r\nThe 64-bit NaN boxing option, option 3, is just standard boxing (see eg JavaScript, Lua).  On a 32-bit machine the object size becomes 64-bit.\r\n\r\nWe could support 64-bit stuffing (ie truncate to 62-bit on a 64-bit machine) and that might be useful for testing etc.\r\n\r\nImplementation of stuff floats is relatively easy (I already have a working patch).  64-bit NaN boxing is a bit harder.\r\n\r\nOne issue will be configuring all this and providing clear config options.  At the moment we just use `MICROPY_OBJ_REPR` with current options being `MICROPY_OBJ_REPR_A` and `MICROPY_OBJ_REPR_B` (latter is for 16-bit systems, but works on 32-bit as well).  Maybe we need more descriptive names?\r\n\r\nComments welcome!\r\n "},"comment":{"url":"https://api.github.com/repos/micropython/micropython/issues/comments/147385414","html_url":"https://github.com/micropython/micropython/issues/1504#issuecomment-147385414","issue_url":"https://api.github.com/repos/micropython/micropython/issues/1504","id":147385414,"user":{"login":"danicampora","id":7749335,"avatar_url":"https://avatars.githubusercontent.com/u/7749335?v=3","gravatar_id":"","url":"https://api.github.com/users/danicampora","html_url":"https://github.com/danicampora","followers_url":"https://api.github.com/users/danicampora/followers","following_url":"https://api.github.com/users/danicampora/following{/other_user}","gists_url":"https://api.github.com/users/danicampora/gists{/gist_id}","starred_url":"https://api.github.com/users/danicampora/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danicampora/subscriptions","organizations_url":"https://api.github.com/users/danicampora/orgs","repos_url":"https://api.github.com/users/danicampora/repos","events_url":"https://api.github.com/users/danicampora/events{/privacy}","received_events_url":"https://api.github.com/users/danicampora/received_events","type":"User","site_admin":false},"created_at":"2015-10-12T12:42:09Z","updated_at":"2015-10-12T12:42:09Z","body":"I like option 2."}}