Skip to content
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

Add fmin/fmax functions to math.h #303

Merged
merged 7 commits into from
Feb 13, 2024
Merged

Add fmin/fmax functions to math.h #303

merged 7 commits into from
Feb 13, 2024

Conversation

mlund
Copy link
Contributor

@mlund mlund commented Feb 11, 2024

This adds min and max functions for float and double.
Closes #299.

@mlund mlund marked this pull request as ready for review February 12, 2024 05:52
@@ -0,0 +1,22 @@
#include <math.h>

template <typename T> static inline bool _is_nan(const T x) { return x != x; }
Copy link
Member

Choose a reason for hiding this comment

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

May as well actually implement the macro in <math.h>:

#define isnan(arg) __builtin_isfpclass(arg, __FPCLASS_SNAN | __FPCLASS_QNAN)

Copy link
Contributor Author

@mlund mlund Feb 13, 2024

Choose a reason for hiding this comment

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

Or use the existing __builtin_isnan over __builtin_isfpclass? I just noticed that std::numeric_limits<float>::has_quiet_NaN == false and NAN is not defined. Is nan at all supported?
Update: also std::numeric_limits<float>::has_infinity == false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, I now notice that __builtin_fmin and __builtin_fmax along with several other math functions are already defined and seem to be correct. Perhaps better to use these? Are these covered by existing tests in the separate test suite?

Copy link
Member

@mysterymath mysterymath Feb 13, 2024

Choose a reason for hiding this comment

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

Yeah, good point. __builtin_isnan lowers to the same thing and is clearer.

By contrast, __builtin_fmin doesn't help us; it lowers to a compiler intrinsic that most naturally lowers to a call to fmin. By comparison, __builtin_isnan lowers to a call to __unordsf2 in compiler_rt, plus a bit of additional logic.

mos-platform/common/c/math.cc Outdated Show resolved Hide resolved
mos-platform/common/c/math.cc Outdated Show resolved Hide resolved
Copy link
Member

@mysterymath mysterymath left a comment

Choose a reason for hiding this comment

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

LGTM!

@mysterymath mysterymath merged commit 777e89e into llvm-mos:main Feb 13, 2024
@mlund mlund deleted the fminmax branch February 13, 2024 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement libm min and max functions
2 participants