Skip to content

Commit ed1b24b

Browse files
authored
[flang][runtime] Added simplified std::toupper implementation. (#87850)
1 parent 312b929 commit ed1b24b

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

flang/include/flang/Runtime/freestanding-tools.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flang/Common/api-attrs.h"
1313
#include "flang/Runtime/c-or-cpp.h"
1414
#include <algorithm>
15+
#include <cctype>
1516
#include <cstring>
1617

1718
// The file defines a set of utilities/classes that might be
@@ -57,6 +58,11 @@
5758
#define STD_STRCMP_UNSUPPORTED 1
5859
#endif
5960

61+
#if !defined(STD_TOUPPER_UNSUPPORTED) && \
62+
(defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
63+
#define STD_TOUPPER_UNSUPPORTED 1
64+
#endif
65+
6066
namespace Fortran::runtime {
6167

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

204+
#if STD_TOUPPER_UNSUPPORTED
205+
// Provides alternative implementation for std::toupper(), if
206+
// it is not supported.
207+
static inline RT_API_ATTRS int toupper(int ch) {
208+
if (ch >= 'a' && ch <= 'z') {
209+
return ch - 'a' + 'A';
210+
}
211+
return ch;
212+
}
213+
#else // !STD_TOUPPER_UNSUPPORTED
214+
using std::toupper;
215+
#endif // !STD_TOUPPER_UNSUPPORTED
216+
198217
} // namespace Fortran::runtime
199218
#endif // FORTRAN_RUNTIME_FREESTANDING_TOOLS_H_

flang/lib/Decimal/decimal-to-binary.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#include "flang/Common/leading-zero-bit-count.h"
1212
#include "flang/Decimal/binary-floating-point.h"
1313
#include "flang/Decimal/decimal.h"
14+
#include "flang/Runtime/freestanding-tools.h"
1415
#include <cinttypes>
1516
#include <cstring>
16-
#include <ctype.h>
1717
#include <utility>
1818

1919
namespace Fortran::decimal {
@@ -468,8 +468,8 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary(
468468
++q;
469469
}
470470
}
471-
if ((!limit || limit >= q + 3) && toupper(q[0]) == 'N' &&
472-
toupper(q[1]) == 'A' && toupper(q[2]) == 'N') {
471+
if ((!limit || limit >= q + 3) && runtime::toupper(q[0]) == 'N' &&
472+
runtime::toupper(q[1]) == 'A' && runtime::toupper(q[2]) == 'N') {
473473
// NaN
474474
p = q + 3;
475475
bool isQuiet{true};
@@ -493,11 +493,11 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary(
493493
}
494494
return {Real{NaN(isQuiet)}};
495495
} else { // Inf?
496-
if ((!limit || limit >= q + 3) && toupper(q[0]) == 'I' &&
497-
toupper(q[1]) == 'N' && toupper(q[2]) == 'F') {
498-
if ((!limit || limit >= q + 8) && toupper(q[3]) == 'I' &&
499-
toupper(q[4]) == 'N' && toupper(q[5]) == 'I' &&
500-
toupper(q[6]) == 'T' && toupper(q[7]) == 'Y') {
496+
if ((!limit || limit >= q + 3) && runtime::toupper(q[0]) == 'I' &&
497+
runtime::toupper(q[1]) == 'N' && runtime::toupper(q[2]) == 'F') {
498+
if ((!limit || limit >= q + 8) && runtime::toupper(q[3]) == 'I' &&
499+
runtime::toupper(q[4]) == 'N' && runtime::toupper(q[5]) == 'I' &&
500+
runtime::toupper(q[6]) == 'T' && runtime::toupper(q[7]) == 'Y') {
501501
p = q + 8;
502502
} else {
503503
p = q + 3;

0 commit comments

Comments
 (0)