Skip to content

[libc] Implemented wmemset and added tests #141691

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 8 commits into from
May 29, 2025
Merged

Conversation

uzairnawaz
Copy link
Contributor

Implemented and tests the wmemset function in libc.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc label May 27, 2025
@llvmbot
Copy link
Member

llvmbot commented May 27, 2025

@llvm/pr-subscribers-libc

Author: Uzair Nawaz (uzairnawaz)

Changes

Implemented and tests the wmemset function in libc.


Full diff: https://github.com/llvm/llvm-project/pull/141691.diff

7 Files Affected:

  • (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
  • (modified) libc/include/wchar.yaml (+8)
  • (modified) libc/src/wchar/CMakeLists.txt (+12)
  • (added) libc/src/wchar/wmemset.cpp (+25)
  • (added) libc/src/wchar/wmemset.h (+23)
  • (modified) libc/test/src/wchar/CMakeLists.txt (+12)
  • (added) libc/test/src/wchar/wmemset_test.cpp (+89)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9f447dd0d35d2..6a0dafd20e828 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -364,6 +364,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.btowc
     libc.src.wchar.wcslen
     libc.src.wchar.wctob
+    libc.src.wchar.wmemset
 
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 0ac9aa29f0a18..ad1b2e552f219 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -27,3 +27,11 @@ functions:
     return_type: wint_t
     arguments:
       - type: int
+  - name: wmemset
+    standards:
+      - stdc
+    return_type: wchar_t*
+    arguments:
+      - type: wchar_t*
+      - type: wchar_t
+      - type: size_t
\ No newline at end of file
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 703db75b5b194..6cc6001f82db6 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -33,3 +33,15 @@ add_entrypoint_object(
     libc.hdr.wchar_macros
     libc.src.__support.wctype_utils
 )
+
+add_entrypoint_object(
+  wmemset
+  SRCS
+    wmemset.cpp
+  HDRS
+    wmemset.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.__support.wctype_utils
+)
diff --git a/libc/src/wchar/wmemset.cpp b/libc/src/wchar/wmemset.cpp
new file mode 100644
index 0000000000000..4b0c36db045b0
--- /dev/null
+++ b/libc/src/wchar/wmemset.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of wmemset -----------------------------------------===//
+//
+// 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/wchar/wmemset.h"
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(wchar_t*, wmemset, (wchar_t *s, wchar_t c, size_t n)) {
+    for (size_t i = 0; i < n; i++) {
+        s[i] = c;
+    }
+    return s;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
diff --git a/libc/src/wchar/wmemset.h b/libc/src/wchar/wmemset.h
new file mode 100644
index 0000000000000..b7067aaa188e6
--- /dev/null
+++ b/libc/src/wchar/wmemset.h
@@ -0,0 +1,23 @@
+//===-- Implementation header for wmemset ----------------------------------===//
+//
+// 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_WCHAR_WMEMSET_H
+#define LLVM_LIBC_SRC_WCHAR_WMEMSET_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n);
+
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WMEMSET_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index d41e328fc9d90..e6eb51c8e8183 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -32,3 +32,15 @@ add_libc_test(
   DEPENDS
     libc.src.wchar.wctob
 )
+
+add_libc_test(
+  wmemset_test 
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    wmemset_test.cpp
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.wchar.wmemset
+)
\ No newline at end of file
diff --git a/libc/test/src/wchar/wmemset_test.cpp b/libc/test/src/wchar/wmemset_test.cpp
new file mode 100644
index 0000000000000..19b8e1d95f8d0
--- /dev/null
+++ b/libc/test/src/wchar/wmemset_test.cpp
@@ -0,0 +1,89 @@
+//===-- Unittests for wmemset ---------------------------------------------===//
+//
+// 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 "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/wchar/wmemset.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
+  wchar_t* str = new wchar_t[5];
+  for (int i = 0; i < 5; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, 'B', 3);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  EXPECT_TRUE(str[1] == (wchar_t)'B');
+  EXPECT_TRUE(str[2] == (wchar_t)'B');
+  EXPECT_TRUE(str[3] == (wchar_t)'B');
+  EXPECT_TRUE(str[4] == (wchar_t)'A');
+}
+
+TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {
+  const int str_size = 1000;
+  wchar_t* str = new wchar_t[str_size];
+  for (int i = 0; i < str_size; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, 'B', str_size - 2);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  for (int i = 1; i < str_size - 1; i++) {
+    EXPECT_TRUE(str[i] == (wchar_t)'B');
+  }
+  EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
+}
+
+TEST(LlvmLibcWMemsetTest, WChar_Size_Small) {
+  // ensure we can handle 32 bit values 
+  wchar_t* str = new wchar_t[5];
+  const wchar_t magic = INT32_MAX;
+  
+  for (int i = 0; i < 5; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, magic, 3);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  EXPECT_TRUE(str[1] == magic);
+  EXPECT_TRUE(str[2] == magic);
+  EXPECT_TRUE(str[3] == magic);
+  EXPECT_TRUE(str[4] == (wchar_t)'A');
+}
+
+TEST(LlvmLibcWMemsetTest, WChar_Size_Large) {
+  // ensure we can handle 32 bit values 
+  const int str_size = 1000;
+  const wchar_t magic = INT32_MAX;
+  wchar_t* str = new wchar_t[str_size];
+  for (int i = 0; i < str_size; i++) {
+    str[i] = 'A';
+  }
+
+  wchar_t* output = LIBC_NAMESPACE::wmemset(str + 1, magic, str_size - 2);
+
+  EXPECT_EQ(output, str + 1);
+
+  EXPECT_TRUE(str[0] == (wchar_t)'A');
+  for (int i = 1; i < str_size - 1; i++) {
+    EXPECT_TRUE(str[i] == magic);
+  }
+  EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');
+}
+
+

@michaelrj-google michaelrj-google self-requested a review May 27, 2025 23:59
Copy link

github-actions bot commented May 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

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

Looks good! Needs a couple changes but this is a good start.

arguments:
- type: wchar_t*
- type: wchar_t
- type: size_t
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: fix missing newline

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(wchar_t*, wmemset, (wchar_t *s, wchar_t c, size_t n)) {
for (size_t i = 0; i < n; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

libc.hdr.types.size_t
libc.hdr.types.wchar_t
libc.src.wchar.wmemset
)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: fix missing newline

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(wchar_t *, wmemset, (wchar_t * s, wchar_t c, size_t n)) {
for (size_t i = 0; i < n; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The LLVM coding standards recommend skipping the braces for one line for loops: https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements

}

TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {
const int str_size = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make this constexpr

Suggested change
const int str_size = 1000;
constexpr int STR_SIZE = 1000;

#include "test/UnitTest/Test.h"

TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {
wchar_t *str = new wchar_t[5];
Copy link
Contributor

Choose a reason for hiding this comment

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

If you use new here that puts the data on the heap. When possible, it's preferred to put test variables on the stack to simplify memory management. To do that you define the variable as a local array instead of just a pointer.

Same recommendation on all these tests, but for simplicity I'm just marking this one.

Suggested change
wchar_t *str = new wchar_t[5];
wchar_t str[5];

TEST(LlvmLibcWMemsetTest, WChar_Size_Small) {
// ensure we can handle 32 bit values
wchar_t *str = new wchar_t[5];
const wchar_t magic = INT32_MAX;
Copy link
Contributor

Choose a reason for hiding this comment

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

it's not necessarily safe to assume wchar_t is 32 bits. Instead it's better to use the provided WCHAR_MAX macro.

It would also be good to give magic a more descriptive name.

Suggested change
const wchar_t magic = INT32_MAX;
const wchar_t magic = WCHAR_MAX;

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

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

LGTM, good job! Planning to merge this tomorrow after the presubmits are done.

@michaelrj-google
Copy link
Contributor

The test failure is unrelated, merging.

@michaelrj-google michaelrj-google merged commit 6cf53da into llvm:main May 29, 2025
15 of 16 checks passed
Copy link

@uzairnawaz Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@michaelrj-google michaelrj-google mentioned this pull request May 29, 2025
4 tasks
svkeerthy pushed a commit that referenced this pull request May 29, 2025
Implemented and tests the wmemset function in libc.
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
Implemented and tests the wmemset function in libc.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Implemented and tests the wmemset function in libc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants