[libc-commits] [libc] c80c452 - [libc] Implemented wmempcpy (#142067)

via libc-commits libc-commits at lists.llvm.org
Mon Jun 2 15:00:50 PDT 2025


Author: Uzair Nawaz
Date: 2025-06-02T15:00:46-07:00
New Revision: c80c4525251eed6c833fb56bc0c956bfe590570f

URL: https://github.com/llvm/llvm-project/commit/c80c4525251eed6c833fb56bc0c956bfe590570f
DIFF: https://github.com/llvm/llvm-project/commit/c80c4525251eed6c833fb56bc0c956bfe590570f.diff

LOG: [libc] Implemented wmempcpy (#142067)

Implemented wmempcpy and added tests

Added: 
    libc/src/wchar/wmempcpy.cpp
    libc/src/wchar/wmempcpy.h
    libc/test/src/wchar/wmempcpy_test.cpp

Modified: 
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/wchar.yaml
    libc/src/wchar/CMakeLists.txt
    libc/test/src/wchar/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9574b12c64e77..a8fa26eb3d6c4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -369,6 +369,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wcspbrk
     libc.src.wchar.wcsspn
     libc.src.wchar.wmemcmp
+    libc.src.wchar.wmempcpy
     libc.src.wchar.wmemcpy
     libc.src.wchar.wcscpy
 

diff  --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index d8ff908a5c27f..5d1e63d1d8933 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -64,6 +64,14 @@ functions:
       - type: const wchar_t *
       - type: const wchar_t *
       - type: size_t
+  - name: wmempcpy
+    standards:
+      - gnu
+    return_type: wchar_t * 
+    arguments: 
+      - type: wchar_t *
+      - type: const wchar_t *
+      - type: size_t
   - name: wmemcpy
     standards:
       - stdc

diff  --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index c1dc3ce95765e..988c362e53187 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -105,6 +105,19 @@ add_entrypoint_object(
     libc.src.__support.wctype_utils
 )
 
+add_entrypoint_object(
+  wmempcpy
+  SRCS
+    wmempcpy.cpp
+  HDRS
+    wmempcpy.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.wchar_macros
+    libc.src.__support.wctype_utils
+    libc.src.string.memory_utils.inline_memcpy
+)
+
 add_entrypoint_object(
   wcscpy
   SRCS

diff  --git a/libc/src/wchar/wmempcpy.cpp b/libc/src/wchar/wmempcpy.cpp
new file mode 100644
index 0000000000000..d8b89c0a88d05
--- /dev/null
+++ b/libc/src/wchar/wmempcpy.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of wmempcpy ----------------------------------------===//
+//
+// 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/wmempcpy.h"
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(wchar_t *, wmempcpy,
+                   (wchar_t *__restrict to, const wchar_t *__restrict from,
+                    size_t size)) {
+  inline_memcpy(to, from, size * sizeof(wchar_t));
+  return reinterpret_cast<wchar_t *>(to) + size;
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/wchar/wmempcpy.h b/libc/src/wchar/wmempcpy.h
new file mode 100644
index 0000000000000..580674b1be807
--- /dev/null
+++ b/libc/src/wchar/wmempcpy.h
@@ -0,0 +1,23 @@
+//===-- Implementation header for wmempcpy---------------------------------===//
+//
+// 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_WMEMPCPY_H
+#define LLVM_LIBC_SRC_WCHAR_WMEMPCPY_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 *wmempcpy(wchar_t *__restrict from, const wchar_t *__restrict s2,
+                  size_t n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WMEMPCPY_H

diff  --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 5bc48e8aa49ec..4d73c43cbf8e1 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -85,6 +85,16 @@ add_libc_test(
     libc.src.wchar.wmemcmp
 )
 
+add_libc_test(
+  wmempcpy_test
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    wmempcpy_test.cpp
+  DEPENDS
+    libc.src.wchar.wmempcpy
+)
+
 add_libc_test(
   wmemcpy_test
   SUITE

diff  --git a/libc/test/src/wchar/wmempcpy_test.cpp b/libc/test/src/wchar/wmempcpy_test.cpp
new file mode 100644
index 0000000000000..000e4b3899090
--- /dev/null
+++ b/libc/test/src/wchar/wmempcpy_test.cpp
@@ -0,0 +1,50 @@
+//===-- Unittests for wmempcpy --------------------------------------------===//
+//
+// 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/wmempcpy.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWMempcpyTest, Simple) {
+  const wchar_t *src = L"12345";
+  wchar_t dest[10] = {};
+  void *result = LIBC_NAMESPACE::wmempcpy(dest, src, 6);
+  ASSERT_EQ(static_cast<wchar_t *>(result), dest + 6);
+
+  ASSERT_TRUE(dest[0] == src[0]);
+  ASSERT_TRUE(dest[1] == src[1]);
+  ASSERT_TRUE(dest[2] == src[2]);
+  ASSERT_TRUE(dest[3] == src[3]);
+  ASSERT_TRUE(dest[4] == src[4]);
+  ASSERT_TRUE(dest[5] == src[5]);
+}
+
+TEST(LlvmLibcWmempcpyTest, ZeroCount) {
+  const wchar_t *src = L"12345";
+  wchar_t dest[5] = {};
+  void *result = LIBC_NAMESPACE::wmempcpy(dest, src, 0);
+  ASSERT_EQ(static_cast<wchar_t *>(result), dest);
+
+  ASSERT_TRUE(dest[0] == 0);
+  ASSERT_TRUE(dest[1] == 0);
+  ASSERT_TRUE(dest[2] == 0);
+  ASSERT_TRUE(dest[3] == 0);
+  ASSERT_TRUE(dest[4] == 0);
+}
+
+TEST(LlvmLibcWMempcpyTest, BoundaryCheck) {
+  const wchar_t *src = L"12345";
+  wchar_t dest[4] = {};
+  void *result = LIBC_NAMESPACE::wmempcpy(dest + 1, src + 1, 2);
+
+  ASSERT_TRUE(dest[0] == 0);
+  ASSERT_TRUE(dest[1] == src[1]);
+  ASSERT_TRUE(dest[2] == src[2]);
+  ASSERT_TRUE(dest[3] == 0);
+
+  ASSERT_EQ(static_cast<wchar_t *>(result), dest + 3);
+}


        


More information about the libc-commits mailing list