Skip to content

Conversation

EmbedEngineer
Copy link

@EmbedEngineer EmbedEngineer commented Sep 4, 2020

Summary of changes

Added width modifier and prepend zeros if the width modifier is given feature to printing integer, hexadecimal and floating point values. So e.g. printf("%04X", i); works now the same as the std version.

Added precision specifier for printing floating point values, as it already existed for strings. If no precision specifier is given MBED_CONF_PLATFORM_MINIMAL_PRINTF_SET_FLOATING_POINT_MAX_DECIMALS will be used.

This PR also fixes #13783.

Impact of changes

Prepending zeros and width modifier work with integer, hexadecimal and floating point values when using minimal_printf.
Also the precision modifier works for floating point variables. If no precision specifier is given MBED_CONF_PLATFORM_MINIMAL_PRINTF_SET_FLOATING_POINT_MAX_DECIMALS is used.

Migration actions required

Documentation

None


Pull request type

  • Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
  • Feature update (New feature / Functionality change / New API)
  • Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

  • [] No Tests required for this change (E.g docs only update)
  • Covered by existing mbed-os tests (Greentea or Unittest)
  • [X ] Tests / results supplied as part of this PR

Reviewers


@mergify mergify bot added the needs: work label Sep 4, 2020
@ciarmcom ciarmcom requested review from a team September 4, 2020 11:00
@ciarmcom
Copy link
Member

ciarmcom commented Sep 4, 2020

@EmbedEngineer, thank you for your changes.
@ARMmbed/mbed-os-core @ARMmbed/mbed-os-maintainers please review.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 4, 2020

Please review astyle failures.

@LDong-Arm
Copy link
Contributor

@EmbedEngineer Thanks for the contribution, the width modifier is something I've personally wanted for while.
@pan- FYI, if this gets in we should be able to switch our ble-cliapp back to minimal-printf (i.e. default) from std.

Though any feature added increases the size of minimal printf - shall we make this feature configurable? @evedon @hugueskamba

@hugueskamba hugueskamba changed the title added width modifier and prepending zeros for hexadecimal output and … added width modifier and prepending zeros for hexadecimal output and decimal precision for floating point Sep 4, 2020
@pan-
Copy link
Member

pan- commented Sep 4, 2020

@EmbedEngineer That could be a great addition, do you have numbers in term of code size ? How much do we pay to print hexadecimal values with the width modifier ?

Copy link
Collaborator

@hugueskamba hugueskamba left a comment

Choose a reason for hiding this comment

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

Thank you for the changes.
How does the change affect code size?
Perhaps it should be a configurable option as we do not want to increase code size for existing users.

@mergify mergify bot removed the needs: review label Sep 7, 2020
@evedon
Copy link
Contributor

evedon commented Sep 7, 2020

Thanks for the PR. Like the others, I'd like to know the impact on code size. In addition could you add test for this? See https://github.com/ARMmbed/mbed-os/blob/master/platform/tests/TESTS/mbed_platform/minimal-printf/compliance/main.cpp

@EmbedEngineer
Copy link
Author

@pan- @hugueskamba @evedon considering the size, I got 90 bytes more with 0s and arm gcc 9

Copy link
Contributor

@LDong-Arm LDong-Arm left a comment

Choose a reason for hiding this comment

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

Thanks @EmbedEngineer, the code looks good in general

@LDong-Arm
Copy link
Contributor

LDong-Arm commented Sep 7, 2020

Thanks for the PR. Like the others, I'd like to know the impact on code size. In addition could you add test for this? See https://github.com/ARMmbed/mbed-os/blob/master/platform/tests/TESTS/mbed_platform/minimal-printf/compliance/main.cpp

@EmbedEngineer This is the test for minimal-printf. Could you please extend test_printf_x() & test_snprintf_x() (hexadecimals) and test_printf_f() & test_snprintf_f() (floating points) to test widths and decimal precisions? It should be enough to copy some existing code and replace the format strings, expected values, etc.

Then the test can be run with

mbed test -t GCC_ARM -m detect -n platform-tests-tests-mbed_platform-minimal-printf

if you have a board connected.

Thanks for the effort in so far!

@EmbedEngineer
Copy link
Author

@LDong-Arm thanks.
I will work on the tests.

@EmbedEngineer
Copy link
Author

@LDong-Arm @evedon I finished the tests so far. Could you please review them?

Copy link
Collaborator

@hugueskamba hugueskamba left a comment

Choose a reason for hiding this comment

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

Thank you.
platform/source/minimal-printf/README.md also needs updating.

@EmbedEngineer
Copy link
Author

EmbedEngineer commented Sep 8, 2020

Thank you.
platform/source/minimal-printf/README.md also needs updating.

Done. Unfortunately I can't adapt the code sizes since I neither have an ARMC6 nor an IAR workbench compiler.

@LDong-Arm
Copy link
Contributor

LDong-Arm commented Sep 8, 2020

@EmbedEngineer I tried it locally, and it uses an extra 1KB of flash with GCC and ARM compilers. I compared the latest master branch as of now vs this branch rebased onto latest master. All builds were clean builds.

If I got things right, then it shows we need an extra configuration to conditionally enable/disable width and precision supports.

The test looks good to me 👍

@EmbedEngineer
Copy link
Author

EmbedEngineer commented Sep 8, 2020

@EmbedEngineer I tried it locally, and it uses an extra 1KB of flash with GCC and ARM compilers. I compared the latest master branch as of now vs this branch rebased onto latest master. All builds were clean builds.

If I got things right, then it shows we need an extra configuration to conditionally enable/disable width and precision supports.

The test looks good to me +1

@LDong-Arm That's very interesting because I get 88 bytes more when i do the same thing with mbed compile -t GCC_ARM -m K64F -c which results in ./BUILD/K64F/GCC_ARM/mbed-os-example-blinky.bin with 42982 Flash and 12072 RAM compared to 42894 Flash and 12072 RAM on the master without floating points enabled. If I activate floating points I got 140 bytes flash (45706 -> 45846) more.

Thank you for looking over the test!

@EmbedEngineer
Copy link
Author

@LDong-Arm Could you please recheck your size calculations, because no matter what I do, I cannot find the 1kB of Flash usage. It always stays 88 bytes w/o floating points and 140 bytes with floating points, which makes kind of sense for me, since I only added about 20 lines of code.

@LDong-Arm
Copy link
Contributor

@LDong-Arm Could you please recheck your size calculations, because no matter what I do, I cannot find the 1kB of Flash usage. It always stays 88 bytes w/o floating points and 140 bytes with floating points, which makes kind of sense for me, since I only added about 20 lines of code.

Sorry my bad, I checked again and the size increase is only 190 bytes with the Arm Compiler and floating point not enabled.

Copy link
Contributor

@LDong-Arm LDong-Arm left a comment

Choose a reason for hiding this comment

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

The implementation looks good to me. But anyone thinks we need a config to enable/disable this?

*/
static void mbed_minimal_formatted_string_hexadecimal(char *buffer, size_t length, int *result, MBED_UNSIGNED_STORAGE value, FILE *stream, bool upper)
static void mbed_minimal_formatted_string_hexadecimal(
char *buffer,
Copy link
Contributor

Choose a reason for hiding this comment

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

why was this changed to have arg on a new separate line? As the rest of the functions have it the way it was, should this be reverted?

Copy link
Author

Choose a reason for hiding this comment

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

it was a change request from @hugueskamba since the line got too long

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry about the contradictory comments but this change should be reverted for consistency with the rest of the file.

@evedon
Copy link
Contributor

evedon commented Dec 2, 2020

@EmbedEngineer That's would be great. Thanks.

@EmbedEngineer
Copy link
Author

@evedon so far I think I'm finished

evedon
evedon previously requested changes Dec 7, 2020
Copy link
Contributor

@evedon evedon left a comment

Choose a reason for hiding this comment

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

Unfortunately, by default the tests do not run with floating points enabled. You need the following command:

mbed test -t gcc_arm -m k64f -n *minimal-printf* -vv --app-config platform/tests/TESTS/mbed_platform/minimal-printf/compliance/test_config.json

You will find that the current tests don't pass for floating point values. Look at my suggestion for a fix.

@mergify mergify bot dismissed evedon’s stale review December 8, 2020 08:23

Pull request has been modified.

evedon
evedon previously approved these changes Dec 8, 2020
Copy link
Contributor

@evedon evedon left a comment

Choose a reason for hiding this comment

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

Great! the implementation is correct now. Thanks for the work on this PR.
I'd suggest that you squash your commits and that @0xc0170 kicks off CI

@evedon
Copy link
Contributor

evedon commented Dec 8, 2020

I just noticed that the PR description needs updating, specifically the "Impact" section. Also this PR fixes #13783 so this should be added in the description

@mergify mergify bot dismissed evedon’s stale review December 8, 2020 14:32

Pull request has been modified.

@EmbedEngineer EmbedEngineer force-pushed the enhance-minimal-printf branch from ac250ae to c4f3450 Compare December 8, 2020 15:08
@EmbedEngineer
Copy link
Author

@evedon I updated the pull request description and squashed the commits.

…floating point output as well as decimal precision for floating point
@EmbedEngineer EmbedEngineer force-pushed the enhance-minimal-printf branch from c4f3450 to 03cf621 Compare December 8, 2020 20:10
@EmbedEngineer
Copy link
Author

EmbedEngineer commented Dec 9, 2020

@evedon I think I'm finally done. Please let me know if there is anything else to do on this PR. Thank you for your input. I appreciated it very much.

@mergify mergify bot added needs: CI and removed needs: work labels Dec 9, 2020
@evedon
Copy link
Contributor

evedon commented Dec 9, 2020

Code change approved but the PR description still needs updating as width modifier does not need to be an even number. Also update the "Impact" section to say what has been fixed; it is useful when we make the release notes.
Thanks

@EmbedEngineer
Copy link
Author

@evedon Done!

@mbed-ci
Copy link

mbed-ci commented Dec 9, 2020

Jenkins CI Test : ✔️ SUCCESS

Build Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_cmake-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-cloud-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-greentea-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-greentea-ARM ✔️
jenkins-ci/mbed-os-ci_build-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-cloud-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-test ✔️
jenkins-ci/mbed-os-ci_greentea-test ✔️

@caoddx
Copy link
Contributor

caoddx commented Jan 19, 2021

I did not find the test case of printf %f in the test log

It seems that the test_config.json file didn't work.
(platform/tests/TESTS/mbed_platform/minimal-printf/compliance/test_config.json)

@LDong-Arm
Copy link
Contributor

LDong-Arm commented Jan 20, 2021

I did not find the test case of printf %f in the test log

It seems that the test_config.json file didn't work.
(platform/tests/TESTS/mbed_platform/minimal-printf/compliance/test_config.json)

This test_config.json is not used in CI which tests the default configuration. You can run it locally with

mbed test -t <TOOLCHAIN> -m <TARGET> -n platform-tests-tests-mbed_platform-minimal-printf --app-config platform/tests/TESTS/mbed_platform/minimal-printf/compliance/test_config.json

Btw, thanks for raising the issue in minimal-printf!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minimal-printf doesn't display decimals between -1 to 0 correctly

10 participants