[libc] Add tolower, toupper implementation.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D85326

GitOrigin-RevId: 128bf458ab8c5bdbb02e5b13769a618b357d5ae2
diff --git a/config/linux/aarch64/entrypoints.txt b/config/linux/aarch64/entrypoints.txt
index 468f2cc..e1f54bf 100644
--- a/config/linux/aarch64/entrypoints.txt
+++ b/config/linux/aarch64/entrypoints.txt
@@ -12,6 +12,8 @@
     libc.src.ctype.isspace
     libc.src.ctype.isupper
     libc.src.ctype.isxdigit
+    libc.src.ctype.tolower
+    libc.src.ctype.toupper
     
     # errno.h entrypoints
     libc.src.errno.__errno_location
diff --git a/config/linux/api.td b/config/linux/api.td
index 71b592d..1519247 100644
--- a/config/linux/api.td
+++ b/config/linux/api.td
@@ -100,6 +100,8 @@
     "isspace",
     "isupper",
     "isxdigit",
+    "tolower",
+    "toupper",
   ];
 }
 
diff --git a/config/linux/x86_64/entrypoints.txt b/config/linux/x86_64/entrypoints.txt
index 031812c..db76c35 100644
--- a/config/linux/x86_64/entrypoints.txt
+++ b/config/linux/x86_64/entrypoints.txt
@@ -15,6 +15,8 @@
     libc.src.ctype.isspace
     libc.src.ctype.isupper
     libc.src.ctype.isxdigit
+    libc.src.ctype.tolower
+    libc.src.ctype.toupper
 
     # errno.h entrypoints
     libc.src.errno.__errno_location
diff --git a/spec/stdc.td b/spec/stdc.td
index 892fbb2..ac240ff 100644
--- a/spec/stdc.td
+++ b/spec/stdc.td
@@ -106,6 +106,16 @@
               RetValSpec<IntType>,
               [ArgSpec<IntType>]
           >,
+          FunctionSpec<
+              "tolower",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
+          FunctionSpec<
+              "toupper",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
       ]
   >;
      
diff --git a/src/ctype/CMakeLists.txt b/src/ctype/CMakeLists.txt
index 4356eab..da8c440 100644
--- a/src/ctype/CMakeLists.txt
+++ b/src/ctype/CMakeLists.txt
@@ -66,6 +66,8 @@
     islower.cpp
   HDRS
     islower.h
+  DEPENDS
+    .ctype_utils
 )
 
 add_entrypoint_object(
@@ -100,6 +102,8 @@
     isupper.cpp
   HDRS
     isupper.h
+  DEPENDS
+    .ctype_utils
 )
 
 add_entrypoint_object(
@@ -111,3 +115,23 @@
   DEPENDS
     .ctype_utils
 )
+
+add_entrypoint_object(
+  tolower
+  SRCS
+    tolower.cpp
+  HDRS
+    tolower.h
+  DEPENDS
+    .ctype_utils
+)
+
+add_entrypoint_object(
+  toupper
+  SRCS
+    toupper.cpp
+  HDRS
+    toupper.h
+  DEPENDS
+    .ctype_utils
+)
diff --git a/src/ctype/ctype_utils.h b/src/ctype/ctype_utils.h
index 787a19e..6238bd3 100644
--- a/src/ctype/ctype_utils.h
+++ b/src/ctype/ctype_utils.h
@@ -18,14 +18,18 @@
 // of a function call by inlining them.
 // ------------------------------------------------------
 
-static inline int isdigit(unsigned ch) { return (ch - '0') < 10; }
-
 static inline int isalpha(unsigned ch) { return (ch | 32) - 'a' < 26; }
 
+static inline int isdigit(unsigned ch) { return (ch - '0') < 10; }
+
 static inline int isalnum(unsigned ch) { return isalpha(ch) || isdigit(ch); }
 
 static inline int isgraph(unsigned ch) { return 0x20 < ch && ch < 0x7f; }
 
+static inline int islower(unsigned ch) { return (ch - 'a') < 26; }
+
+static inline int isupper(unsigned ch) { return (ch - 'A') < 26; }
+
 } // namespace internal
 } // namespace __llvm_libc
 
diff --git a/src/ctype/islower.cpp b/src/ctype/islower.cpp
index df21355..ae1291b 100644
--- a/src/ctype/islower.cpp
+++ b/src/ctype/islower.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/ctype/islower.h"
+#include "src/ctype/ctype_utils.h"
 
 #include "src/__support/common.h"
 
@@ -14,9 +15,6 @@
 
 // TODO: Currently restricted to default locale.
 // These should be extended using locale information.
-int LLVM_LIBC_ENTRYPOINT(islower)(int c) {
-  const unsigned ch = c;
-  return (ch - 'a') < 26;
-}
+int LLVM_LIBC_ENTRYPOINT(islower)(int c) { return internal::islower(c); }
 
 } // namespace __llvm_libc
diff --git a/src/ctype/isupper.cpp b/src/ctype/isupper.cpp
index 57aed96..9b0e523 100644
--- a/src/ctype/isupper.cpp
+++ b/src/ctype/isupper.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/ctype/isupper.h"
+#include "src/ctype/ctype_utils.h"
 
 #include "src/__support/common.h"
 
@@ -14,9 +15,6 @@
 
 // TODO: Currently restricted to default locale.
 // These should be extended using locale information.
-int LLVM_LIBC_ENTRYPOINT(isupper)(int c) {
-  const unsigned ch = c;
-  return (ch - 'A') < 26;
-}
+int LLVM_LIBC_ENTRYPOINT(isupper)(int c) { return internal::isupper(c); }
 
 } // namespace __llvm_libc
diff --git a/src/ctype/tolower.cpp b/src/ctype/tolower.cpp
new file mode 100644
index 0000000..6fd3006
--- /dev/null
+++ b/src/ctype/tolower.cpp
@@ -0,0 +1,24 @@
+//===-- Implementation of tolower------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/ctype/tolower.h"
+#include "src/ctype/ctype_utils.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+// TODO: Currently restricted to default locale.
+// These should be extended using locale information.
+int LLVM_LIBC_ENTRYPOINT(tolower)(int c) {
+  if (internal::isupper(c))
+    return c + 'a' - 'A';
+  return c;
+}
+
+} // namespace __llvm_libc
diff --git a/src/ctype/tolower.h b/src/ctype/tolower.h
new file mode 100644
index 0000000..97e675c
--- /dev/null
+++ b/src/ctype/tolower.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for tolower -------------------------*-C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_CTYPE_TOLOWER_H
+#define LLVM_LIBC_SRC_CTYPE_TOLOWER_H
+
+namespace __llvm_libc {
+
+int tolower(int c);
+
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_TOLOWER_H
diff --git a/src/ctype/toupper.cpp b/src/ctype/toupper.cpp
new file mode 100644
index 0000000..8d591c2
--- /dev/null
+++ b/src/ctype/toupper.cpp
@@ -0,0 +1,24 @@
+//===-- Implementation of toupper------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/ctype/toupper.h"
+#include "src/ctype/ctype_utils.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+// TODO: Currently restricted to default locale.
+// These should be extended using locale information.
+int LLVM_LIBC_ENTRYPOINT(toupper)(int c) {
+  if (internal::islower(c))
+    return c + 'A' - 'a';
+  return c;
+}
+
+} // namespace __llvm_libc
diff --git a/src/ctype/toupper.h b/src/ctype/toupper.h
new file mode 100644
index 0000000..a21b0cf
--- /dev/null
+++ b/src/ctype/toupper.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for toupper -------------------------*-C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_CTYPE_TOUPPER_H
+#define LLVM_LIBC_SRC_CTYPE_TOUPPER_H
+
+namespace __llvm_libc {
+
+int toupper(int c);
+
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_TOUPPER_H
diff --git a/test/src/ctype/CMakeLists.txt b/test/src/ctype/CMakeLists.txt
index 0d77134..4141708 100644
--- a/test/src/ctype/CMakeLists.txt
+++ b/test/src/ctype/CMakeLists.txt
@@ -119,3 +119,23 @@
   DEPENDS
     libc.src.ctype.isxdigit
 )
+
+add_libc_unittest(
+  tolower
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    tolower_test.cpp
+  DEPENDS
+    libc.src.ctype.tolower
+)
+
+add_libc_unittest(
+  toupper
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    toupper_test.cpp
+  DEPENDS
+    libc.src.ctype.toupper
+)
diff --git a/test/src/ctype/tolower_test.cpp b/test/src/ctype/tolower_test.cpp
new file mode 100644
index 0000000..096ef96
--- /dev/null
+++ b/test/src/ctype/tolower_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for tolower----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/ctype/tolower.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(ToLower, DefaultLocale) {
+  for (int ch = 0; ch < 255; ++ch) {
+    // This follows pattern 'A' + 32 = 'a'.
+    if ('A' <= ch && ch <= 'Z')
+      EXPECT_EQ(__llvm_libc::tolower(ch), ch + 32);
+    else
+      EXPECT_EQ(__llvm_libc::tolower(ch), ch);
+  }
+}
diff --git a/test/src/ctype/toupper_test.cpp b/test/src/ctype/toupper_test.cpp
new file mode 100644
index 0000000..4d6a1b3
--- /dev/null
+++ b/test/src/ctype/toupper_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for toupper----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/ctype/toupper.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(ToUpper, DefaultLocale) {
+  for (int ch = 0; ch < 255; ++ch) {
+    // This follows pattern 'a' - 32 = 'A'.
+    if ('a' <= ch && ch <= 'z')
+      EXPECT_EQ(__llvm_libc::toupper(ch), ch - 32);
+    else
+      EXPECT_EQ(__llvm_libc::toupper(ch), ch);
+  }
+}