-
Notifications
You must be signed in to change notification settings - Fork 208
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
Improve type_caster for floating-point types. #829
Merged
+58
−12
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e008be
Improve type_caster for floating-point types.
hpkfft b025460
Add dedicated detail::load_f32() function.
hpkfft 2057e51
Addressed feedback from code review.
hpkfft 59e449e
Update changelog.
hpkfft 44a3dca
Merge branch 'master' into floatingpoint
hpkfft b26f83b
Restore dead store.
hpkfft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -904,7 +904,7 @@ bool load_f64(PyObject *o, uint8_t flags, double *out) noexcept { | |
|
||
#if !defined(Py_LIMITED_API) | ||
if (NB_LIKELY(is_float)) { | ||
*out = (double) PyFloat_AS_DOUBLE(o); | ||
*out = PyFloat_AS_DOUBLE(o); | ||
return true; | ||
} | ||
|
||
|
@@ -915,7 +915,7 @@ bool load_f64(PyObject *o, uint8_t flags, double *out) noexcept { | |
double result = PyFloat_AsDouble(o); | ||
|
||
if (result != -1.0 || !PyErr_Occurred()) { | ||
*out = (double) result; | ||
*out = result; | ||
return true; | ||
} else { | ||
PyErr_Clear(); | ||
|
@@ -927,22 +927,31 @@ bool load_f64(PyObject *o, uint8_t flags, double *out) noexcept { | |
|
||
bool load_f32(PyObject *o, uint8_t flags, float *out) noexcept { | ||
bool is_float = PyFloat_CheckExact(o); | ||
bool convert = flags & (uint8_t) cast_flags::convert; | ||
|
||
#if !defined(Py_LIMITED_API) | ||
if (NB_LIKELY(is_float)) { | ||
*out = (float) PyFloat_AS_DOUBLE(o); | ||
return true; | ||
double d = PyFloat_AS_DOUBLE(o); | ||
float result = (float) d; | ||
if (convert || (double) result == d || d != d) { | ||
*out = result; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
is_float = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you re-enable this assigment? I am not sure that all compilers will understand that |
||
#endif | ||
|
||
if (is_float || (flags & (uint8_t) cast_flags::convert)) { | ||
double result = PyFloat_AsDouble(o); | ||
|
||
if (result != -1.0 || !PyErr_Occurred()) { | ||
*out = (float) result; | ||
return true; | ||
if (is_float || convert) { | ||
double d = PyFloat_AsDouble(o); | ||
if (d != -1.0 || !PyErr_Occurred()) { | ||
float result = (float) d; | ||
if (convert || (double) result == d || d != d) { | ||
*out = result; | ||
return true; | ||
} | ||
} else { | ||
PyErr_Clear(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does
result != result && d != d
accomplish thatresult != result
does not do?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intend for the caster to work for any floating-point type. The type
T
may not haveInf
. If that is the case, then a double precisionInf
would be converted toNaN
. So,result != result
butd == d
. This is a value-changing conversion, so we want it to fail ifnoconvert()
was specified. The same can happen ifd
is large. Then the conversion would overflow (depending on rounding mode), and althoughd
is finite,result
isNaN
.This is a possible scenario. Nvidia, Intel, Arm, Google, AMD, and Meta have "approved" an 8-bit floating-point specification E4M3 which does not have
Inf
but does haveNaN
. (E5M2 has bothInf
andNaN
.)https://www.opencompute.org/documents/ocp-8-bit-floating-point-specification-ofp8-revision-1-0-2023-12-01-pdf-1
Maybe we could just check
d != d
. But that would be wrong ifT
does not supportNaN
. I cannot immediately think of a system relevant to nanobind that has such a type, but I'd rather play it safe. TheNaN
comparison check is at the end of all the short-circuiting, so I think it won't affect performance in practical usage.