Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 30-bit stuffed float #1516

Closed
wants to merge 6 commits into from

Conversation

@dpgeorge
Copy link
Member

commented Oct 17, 2015

The first 3 patches here make the float object representation configurable. The 4th patch adds a new object representation "C" which has 30-bit floats stuffed into the object word, so that floats don't need allocating on the heap.

See #1504 for discussion.

The first 3 patches are solid and don't change any functionality, they just make the object model more configurable.

The 4th patch is useful for ports that have little RAM but still want to use floats, and accept that 30-bit precision is enough. Unfortunately this "C" representation adds about 1400 bytes to the code size, due to the more complex way of packing, testing and unpacking qstrs. [In an alternative implementation that I tried, floats only had 29-bits and qstrs were much more efficient to pack/test/unpack, but 29-bit floats is really starting to degrade precision.]

dpgeorge added 5 commits Aug 20, 2015
py: Add object repr "C", where 30-bit floats are stuffed in obj word.
This new object representation puts floats into the object word instead
of on the heap, at the expense of reducing their precision to 30 bits.
It only makes sense when the word size is 32-bits.
py: With obj repr "C", change raw str accessor from macro to function.
This saves around 1000 bytes (Thumb2 arch) because in repr "C" it is
costly to check and extract a qstr.  So making such check/extract a
function instead of a macro saves lots of code space.
@dpgeorge

This comment has been minimized.

Copy link
Member Author

commented Oct 18, 2015

Ok, added a new commit (5th one above) which reduces code size of representation "C" by about 1k. So now, with just floats enabled (no complex numbers, no math/cmath modules), going from repr "A" to repr "C" costs less than 200 bytes code space.

@pfalcon

This comment has been minimized.

Copy link
Contributor

commented Oct 18, 2015

Nice! Didn't look into details yet, but from #1504, I guess it indeed would be a good exercise to come up with descriptive names for object representations rather than A, B, C. I'm not sure this will be possible though. E.g., this one could called exactly "float stuffing", but readable identifier would be rather long. And I'm not even sure how describe A and B difference in couple of words. Still, may be nice to go thru the exercise.

@dpgeorge

This comment has been minimized.

Copy link
Member Author

commented Oct 18, 2015

The amount of code that is changed is rather minimal, so should be easy to review.

I guess it indeed would be a good exercise to come up with descriptive names for object representations

MICROPY_OBJ_REPR_STUFFED_FLOAT_32BIT would be suitable, and the actual identifier is not used very much in the code so isn't that bad having it long.

@pfalcon

This comment has been minimized.

Copy link

commented on 6478fb2 Oct 19, 2015

+1

@pfalcon

This comment has been minimized.

Copy link

commented on f81f6b4 Oct 19, 2015

+1

@pfalcon

This comment has been minimized.

Copy link
Contributor

commented Oct 20, 2015

MICROPY_OBJ_REPR_STUFFED_FLOAT_32BIT would be suitable

Well, this is useful if we can do similar for "A" and "B" at the same time, otherwise having "A", "B", and "STUFFED_FLOAT_32BIT" doesn't feel to consistent. I guess, we should skip this for now after all.

@pfalcon

This comment has been minimized.

Copy link
Contributor

commented Oct 20, 2015

@dpgeorge : I believe Travis failed due to lack of some header, or something.

@pfalcon

This comment has been minimized.

Copy link

commented on 85c848f Oct 20, 2015

+1, though need to duplicate float accessors even is object representation doesn't touch float is not ideal.

This comment has been minimized.

Copy link
Owner Author

replied Oct 20, 2015

Yes, I was thinking to break up the obj configuration into pieces, which are selected by a given repr (eg I want 31-bit small int and float on the heap). So that would eliminate duplication. But at the moment with only 3 reprs it's not clear how to do this so it scales, so I'd rather eat the duplication for now.

This comment has been minimized.

Copy link
Owner Author

replied Oct 20, 2015

BTW, I think the compiler is smart enough to inline mp_obj_float_get so that this commit makes no changes to generated code.

@pfalcon

This comment has been minimized.

Copy link

commented on 5683c7b Oct 20, 2015

+1

@pfalcon

This comment has been minimized.

Copy link

commented on 364efff Oct 20, 2015

+1

@pfalcon

This comment has been minimized.

Copy link
Contributor

commented Oct 20, 2015

Looks good, thanks! And given Microbit announcement, if you think it would be nice to changelog entry that mainline already has changes to support it, let's merge it for 1.5!

@dpgeorge

This comment has been minimized.

Copy link
Member Author

commented Oct 20, 2015

Merged, ending in commit 04353cc.

@dpgeorge dpgeorge closed this Oct 20, 2015

@dpgeorge dpgeorge deleted the dpgeorge:stuffed-float branch Oct 20, 2015

@dpgeorge

This comment has been minimized.

Copy link
Member Author

commented Oct 20, 2015

And these changes have now been pushed to microbit repo: bbcmicrobit/micropython@ed61dff

tannewt added a commit to tannewt/circuitpython that referenced this pull request Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.