Thanks to visit codestin.com
Credit goes to github.com

Skip to content

py/parsenum.c: reduce code footprint of mp_parse_num_float. #16672

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

Merged

Conversation

yoctopuce
Copy link
Contributor

Summary

The existing mantissa parsing code uses a floating point variable to accumulate digits.

Using an mp_float_uint_t variable instead, and casting to mp_float_t at the very end is more efficient and reduces code size. In some cases, it also improves the rounding behaviour as extra digits are taken into account by the int-to-float conversion code.

Testing

The new code was unit-tested using a small program generating 100'000 random mantissa and exponents, parsing the resulting number as a float and comparing the result to the value obtained via bigint arithmetic.

  • when using MICROPY_FLOAT_IMPL_DOUBLE, with exponents between -200 and 200
    • the max relative error of the old code is around 5.7e-16
    • the max relative error of the new code is around 4.3e-16
    • CPython max relative error is around 3.6e-16
  • when using MICROPY_FLOAT_IMPL_FLOAT, with exponents between -20 and 50
    • the max relative error of the old code is around 4.3e-7
    • the max relative error of the new code is around 2.4e-7

The code size reduction was observed on binaries built using gcc-arm-none-eabi-9-2019-q4-major

Trade-offs and Alternatives

A further improvement in precision for positive exponents could be obtained by leveraging bigint arithmetic up to the computation of the final value:

        #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
        mp_obj_t mant_obj = mp_obj_new_int_from_uint(mantissa);
        #else
        mp_obj_t mant_obj = mp_obj_new_int_from_ulonglong(mantissa);
        #endif
        mp_binary_op_t combine_op = MP_BINARY_OP_MULTIPLY;
        if (exp_val < 0) {
            combine_op = MP_BINARY_OP_TRUE_DIVIDE;
            exp_val = -exp_val;
        }
        mp_obj_t exp_obj = MP_OBJ_NEW_SMALL_INT(exp_val);
        mp_obj_t pow_obj = mp_binary_op(MP_BINARY_OP_POWER, MP_OBJ_NEW_SMALL_INT(10), exp_obj);
        mp_obj_t res_obj = mp_binary_op(combine_op, mant_obj, pow_obj);
        dec_val = mp_obj_get_float(res_obj);

This would however increase the code size, and cause dynamic object allocation in case the mantissa is larger then 9 digits or the absolute value of the exponent is bigger than 9, so this idea was dropped.

Copy link

codecov bot commented Jan 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.54%. Comparing base (50fab08) to head (5fdd249).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #16672   +/-   ##
=======================================
  Coverage   98.53%   98.54%           
=======================================
  Files         169      169           
  Lines       21852    21855    +3     
=======================================
+ Hits        21532    21536    +4     
+ Misses        320      319    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Jan 30, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   -16 -0.002% standard
      stm32:   -56 -0.014% PYBV10
     mimxrt:  +920 +0.251% TEENSY40
        rp2:   -80 -0.009% RPI_PICO_W
       samd:   -16 -0.006% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:  -108 -0.024% VIRT_RV32

@yoctopuce yoctopuce force-pushed the mp_parse_num_float_improvement branch 2 times, most recently from c1539da to 1b2fc00 Compare January 30, 2025 13:54
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Feb 8, 2025
@yoctopuce yoctopuce force-pushed the mp_parse_num_float_improvement branch from 1b2fc00 to 148b097 Compare February 18, 2025 10:03
Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it's a very neat improvement.

The stm32 board PYBD_SF6 uses double precision floats, and it's always failed tests/float/float_parse.py and tests/float/float_parse_doubleprec.py due to minor parsing errors, ie:

FAILURE tests/results/float_float_parse.py
--- tests/results/float_float_parse.py.exp
+++ tests/results/float_float_parse.py.out
@@ -2,9 +2,9 @@
 0.0
 1.0000e+00
 1.0000e+20
-1.0
-1e+20
-True
+1.000000000000001
+1.000000000000001e+20
+False
 True
 True
 True

and

FAILURE tests/results/float_float_parse_doubleprec.py
--- tests/results/float_float_parse_doubleprec.py.exp
+++ tests/results/float_float_parse_doubleprec.py.out
@@ -1,9 +1,9 @@
-1e+300
-1e+200
-1.0
-1.0
-1e+100
-1e-100
+1.000000000000003e+300
+1.000000000000003e+200
+1.000000000000003
+1.000000000000003
+1.000000000000003e+100
+1.000000000000003e-100
 9.00000000000000e-301
 9.00000000000000e-201
 9.00000000000000e-01

All the above differences are resolved by this PR, very good!

I see that code size is increased for boards like TEENSY40 which use double precision. I assume this is because they now need to pull in 64-bit integer multiplication support. I guess that's necessary. If you want double precision floats then you want precision, and also probably have enough flash to fit the 64-bit integer arithmetic.

The mantissa parsing code uses a floating point variable to accumulate
digits.  Using an `mp_float_uint_t` variable instead and casting to
`mp_float_t` at the very end reduces code size.  In some cases, it also
improves the rounding behaviour as extra digits are taken into account
by the int-to-float conversion code.

An extra test case handles the special case where mantissa overflow occurs
while processing deferred trailing zeros.

Signed-off-by: Yoctopuce dev <[email protected]>
@dpgeorge dpgeorge force-pushed the mp_parse_num_float_improvement branch from 148b097 to 5fdd249 Compare February 28, 2025 02:37
@dpgeorge dpgeorge merged commit 5fdd249 into micropython:master Feb 28, 2025
64 checks passed
@yoctopuce yoctopuce deleted the mp_parse_num_float_improvement branch March 3, 2025 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants