On 5/8/2025 12:01 PM, Anatoly Burakov wrote:
Add support for parsing floating point numbers in cmdline library, as well
as unit tests for the new functionality. Use C library for parsing.
Signed-off-by: Anatoly Burakov <anatoly.bura...@intel.com>
---
Notes:
v6 -> v7:
- Fixed a bug in float compare in unit tests where a bigger epsilon
value than necessary was "needed" because we were comparing float
result to a double result
v5 -> v6:
- Small refactor to reduce amount of noise
- Use strtof for parsing single precision floats
- More unit tests
v4 -> v5:
- Reworked to use standard C library functions as much as possible,
keeping near-100% compatibility with earlier versions (the only
difference is that strings like "+4" are now considered valid)
v3 -> v4:
- Removed unnecessary check for integer overflow when parsing negative
floats (as we convert to double before changing sign)
- Make naming of float exponent states more consistent
v2 -> v3:
- Fixed a bug where a free-standing negative exponent ("1e-") would
attempt to be
parsed, and added unit tests for this case
- Added support for floats in dpdk-cmdline-gen script
- Added documentation updates to call out float support
app/test/test_cmdline_num.c | 284 ++++++++++++++++++++++++-
buildtools/dpdk-cmdline-gen.py | 24 ++-
doc/guides/prog_guide/cmdline.rst | 3 +
doc/guides/rel_notes/release_25_07.rst | 5 +
lib/cmdline/cmdline_parse_num.c | 67 +++++-
lib/cmdline/cmdline_parse_num.h | 4 +-
6 files changed, 368 insertions(+), 19 deletions(-)
...but forgot to remove the comment:
+static int
+float_cmp(double expected, void *actual_p, enum cmdline_numtype type)
+{
+ double eps;
+ double actual_d;
+
+ if (type == RTE_FLOAT_SINGLE) {
+ /* read as float, convert to double */
+ actual_d = (double)*(float *)actual_p;
+ /* downcast expected to float as well */
+ expected = (double)(float)expected;
+ /* FLT_EPSILON is too small for some tests */
Please fix this on apply if there is no v8, thanks :)
+ eps = FLT_EPSILON;
+ } else {
+ /* read as double */
+ actual_d = *(double *)actual_p;
+ eps = DBL_EPSILON;
+ }
+ /* compare using epsilon value */
+ if (fabs(expected - actual_d) < eps)
+ return 0;
+ /* not equal */
+ return expected < actual_d ? -1 : 1;
+}
+
--
Thanks,
Anatoly