This is an archive of the discontinued LLVM Phabricator instance.

[libc] Refactor string to float return values
ClosedPublic

Authored by michaelrj on Feb 22 2023, 3:22 PM.

Details

Summary

The internal implementation of the string to float function previously
used pointer arguments for returning several values. Additionally it
set errno in several unexpected places. Now all of that goes through
return structs. For readability I also moved the function away from raw
pointer arithmetic towards proper indexing. I also added support for
rounding modes.

Diff Detail

Event Timeline

michaelrj created this revision.Feb 22 2023, 3:22 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 22 2023, 3:22 PM
michaelrj requested review of this revision.Feb 22 2023, 3:22 PM
lntue added inline comments.Feb 22 2023, 8:44 PM
libc/test/src/__support/str_to_float_test.cpp
322–325

I think ASSERT_NE( 0, ...) for these would be better?

michaelrj marked an inline comment as done.

move away from ASSERT_FALSE(... == 0)

lntue added a comment.Feb 23 2023, 8:02 PM

Can you also update the new dependency in the bazel layout?

@llvm-project//libc/test/src/stdlib:atof_test                   FAILED TO BUILD
@llvm-project//libc/test/src/stdlib:strtod_test                 FAILED TO BUILD
@llvm-project//libc/test/src/stdlib:strtof_test                 FAILED TO BUILD
@llvm-project//libc/test/src/stdlib:strtold_test                FAILED TO BUILD
sivachandra added inline comments.Feb 23 2023, 8:43 PM
libc/src/__support/str_to_float.h
29

Bikeshed: s/FloatPair/FloatComponents ?

37

Looks like there are two types of uses for this type:

  1. The eisel_lemire function returns fail_output with error set to -1. Would cpp::optional<FloatPair> be more appropriate?
  2. The simple_decimal_conversion function returns actual error numbers in error. Would __llvm_libc::ErrorOr<FloatPair> be more appropriate?
40

s/FloatParseReturn/StrToFloatResult? Also, why can't we use StrToNumResult directly?

michaelrj updated this revision to Diff 500297.Feb 24 2023, 2:17 PM
michaelrj marked an inline comment as done.

clean up structs and fix bazel

michaelrj added inline comments.Feb 24 2023, 3:41 PM
libc/src/__support/str_to_float.h
29

I like FloatPair better, personally, but it doesn't really matter to me.

37

For 1 you're right, we can use optional instead.
For 2, we need to return not just the error, but also some additional information (specifically if we've got an infinity or a zero for the out of range value). For this reason I've left this struct in place.

sivachandra accepted this revision.Feb 24 2023, 4:10 PM
sivachandra added inline comments.
libc/src/__support/str_to_float.h
29

My concern is that FloatPair sounds like it holds two floating point values.

36

Can you combine mantissa and exponent into a FloatPair value? Can it be cpp::optional<FloatPair>?

This revision is now accepted and ready to land.Feb 24 2023, 4:10 PM
lntue added inline comments.Feb 27 2023, 6:30 AM
libc/src/__support/high_precision_decimal.h
390–391

nit: for performance purpose, this could be changed to:

return result + this->should_round_up(this->decimal_point, round)

which is well-defined behavior and branchless.

libc/src/__support/str_to_float.h
29

I agree that FloatPair name is a bit confusing, but other name that I can come up with is ExpMant, which might not be better,

michaelrj marked 6 inline comments as done.

reformat structs somewhat

libc/src/__support/str_to_float.h
29

I've changed it to ExpandedFloat since that seems more accurate to what it is and does.

36

I can combine them into a FloatPair, but I don't see any benefit to making it an optional FloatPair. Once we get to a function that passes back a FloatConvertResult the mantissa and exponent are well defined.

This revision was automatically updated to reflect the committed changes.