Skip to content

[flang][runtime] Added simplified std::toupper implementation. #87850

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

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions flang/include/flang/Runtime/freestanding-tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "flang/Common/api-attrs.h"
#include "flang/Runtime/c-or-cpp.h"
#include <algorithm>
#include <cctype>
#include <cstring>

// The file defines a set of utilities/classes that might be
Expand Down Expand Up @@ -57,6 +58,11 @@
#define STD_STRCMP_UNSUPPORTED 1
#endif

#if !defined(STD_TOUPPER_UNSUPPORTED) && \
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
#define STD_TOUPPER_UNSUPPORTED 1
#endif

namespace Fortran::runtime {

#if STD_FILL_N_UNSUPPORTED
Expand Down Expand Up @@ -195,5 +201,18 @@ static inline RT_API_ATTRS int strcmp(const char *lhs, const char *rhs) {
using std::strcmp;
#endif // !STD_STRCMP_UNSUPPORTED

#if STD_TOUPPER_UNSUPPORTED
// Provides alternative implementation for std::toupper(), if
// it is not supported.
static inline RT_API_ATTRS int toupper(int ch) {
if (ch >= 'a' && ch <= 'z') {
return ch - 'a' + 'A';
}
return ch;
}
#else // !STD_TOUPPER_UNSUPPORTED
using std::toupper;
#endif // !STD_TOUPPER_UNSUPPORTED

} // namespace Fortran::runtime
#endif // FORTRAN_RUNTIME_FREESTANDING_TOOLS_H_
16 changes: 8 additions & 8 deletions flang/lib/Decimal/decimal-to-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "flang/Common/leading-zero-bit-count.h"
#include "flang/Decimal/binary-floating-point.h"
#include "flang/Decimal/decimal.h"
#include "flang/Runtime/freestanding-tools.h"
#include <cinttypes>
#include <cstring>
#include <ctype.h>
#include <utility>

namespace Fortran::decimal {
Expand Down Expand Up @@ -468,8 +468,8 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary(
++q;
}
}
if ((!limit || limit >= q + 3) && toupper(q[0]) == 'N' &&
toupper(q[1]) == 'A' && toupper(q[2]) == 'N') {
if ((!limit || limit >= q + 3) && runtime::toupper(q[0]) == 'N' &&
runtime::toupper(q[1]) == 'A' && runtime::toupper(q[2]) == 'N') {
// NaN
p = q + 3;
bool isQuiet{true};
Expand All @@ -493,11 +493,11 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary(
}
return {Real{NaN(isQuiet)}};
} else { // Inf?
if ((!limit || limit >= q + 3) && toupper(q[0]) == 'I' &&
toupper(q[1]) == 'N' && toupper(q[2]) == 'F') {
if ((!limit || limit >= q + 8) && toupper(q[3]) == 'I' &&
toupper(q[4]) == 'N' && toupper(q[5]) == 'I' &&
toupper(q[6]) == 'T' && toupper(q[7]) == 'Y') {
if ((!limit || limit >= q + 3) && runtime::toupper(q[0]) == 'I' &&
runtime::toupper(q[1]) == 'N' && runtime::toupper(q[2]) == 'F') {
if ((!limit || limit >= q + 8) && runtime::toupper(q[3]) == 'I' &&
runtime::toupper(q[4]) == 'N' && runtime::toupper(q[5]) == 'I' &&
runtime::toupper(q[6]) == 'T' && runtime::toupper(q[7]) == 'Y') {
p = q + 8;
} else {
p = q + 3;
Expand Down