Skip to content

Commit c80c452

Browse files
authored
[libc] Implemented wmempcpy (#142067)
Implemented wmempcpy and added tests
1 parent 43bb68b commit c80c452

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ set(TARGET_LIBC_ENTRYPOINTS
369369
libc.src.wchar.wcspbrk
370370
libc.src.wchar.wcsspn
371371
libc.src.wchar.wmemcmp
372+
libc.src.wchar.wmempcpy
372373
libc.src.wchar.wmemcpy
373374
libc.src.wchar.wcscpy
374375

libc/include/wchar.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ functions:
6464
- type: const wchar_t *
6565
- type: const wchar_t *
6666
- type: size_t
67+
- name: wmempcpy
68+
standards:
69+
- gnu
70+
return_type: wchar_t *
71+
arguments:
72+
- type: wchar_t *
73+
- type: const wchar_t *
74+
- type: size_t
6775
- name: wmemcpy
6876
standards:
6977
- stdc

libc/src/wchar/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ add_entrypoint_object(
105105
libc.src.__support.wctype_utils
106106
)
107107

108+
add_entrypoint_object(
109+
wmempcpy
110+
SRCS
111+
wmempcpy.cpp
112+
HDRS
113+
wmempcpy.h
114+
DEPENDS
115+
libc.hdr.types.size_t
116+
libc.hdr.wchar_macros
117+
libc.src.__support.wctype_utils
118+
libc.src.string.memory_utils.inline_memcpy
119+
)
120+
108121
add_entrypoint_object(
109122
wcscpy
110123
SRCS

libc/src/wchar/wmempcpy.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of wmempcpy ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/wchar/wmempcpy.h"
10+
11+
#include "hdr/types/size_t.h"
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/common.h"
14+
#include "src/string/memory_utils/inline_memcpy.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(wchar_t *, wmempcpy,
19+
(wchar_t *__restrict to, const wchar_t *__restrict from,
20+
size_t size)) {
21+
inline_memcpy(to, from, size * sizeof(wchar_t));
22+
return reinterpret_cast<wchar_t *>(to) + size;
23+
}
24+
25+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wmempcpy.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation header for wmempcpy---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_WCHAR_WMEMPCPY_H
10+
#define LLVM_LIBC_SRC_WCHAR_WMEMPCPY_H
11+
12+
#include "hdr/types/size_t.h"
13+
#include "hdr/types/wchar_t.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
wchar_t *wmempcpy(wchar_t *__restrict from, const wchar_t *__restrict s2,
19+
size_t n);
20+
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SRC_WCHAR_WMEMPCPY_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ add_libc_test(
8585
libc.src.wchar.wmemcmp
8686
)
8787

88+
add_libc_test(
89+
wmempcpy_test
90+
SUITE
91+
libc_wchar_unittests
92+
SRCS
93+
wmempcpy_test.cpp
94+
DEPENDS
95+
libc.src.wchar.wmempcpy
96+
)
97+
8898
add_libc_test(
8999
wmemcpy_test
90100
SUITE

libc/test/src/wchar/wmempcpy_test.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- Unittests for wmempcpy --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/wchar/wmempcpy.h"
10+
#include "test/UnitTest/Test.h"
11+
12+
TEST(LlvmLibcWMempcpyTest, Simple) {
13+
const wchar_t *src = L"12345";
14+
wchar_t dest[10] = {};
15+
void *result = LIBC_NAMESPACE::wmempcpy(dest, src, 6);
16+
ASSERT_EQ(static_cast<wchar_t *>(result), dest + 6);
17+
18+
ASSERT_TRUE(dest[0] == src[0]);
19+
ASSERT_TRUE(dest[1] == src[1]);
20+
ASSERT_TRUE(dest[2] == src[2]);
21+
ASSERT_TRUE(dest[3] == src[3]);
22+
ASSERT_TRUE(dest[4] == src[4]);
23+
ASSERT_TRUE(dest[5] == src[5]);
24+
}
25+
26+
TEST(LlvmLibcWmempcpyTest, ZeroCount) {
27+
const wchar_t *src = L"12345";
28+
wchar_t dest[5] = {};
29+
void *result = LIBC_NAMESPACE::wmempcpy(dest, src, 0);
30+
ASSERT_EQ(static_cast<wchar_t *>(result), dest);
31+
32+
ASSERT_TRUE(dest[0] == 0);
33+
ASSERT_TRUE(dest[1] == 0);
34+
ASSERT_TRUE(dest[2] == 0);
35+
ASSERT_TRUE(dest[3] == 0);
36+
ASSERT_TRUE(dest[4] == 0);
37+
}
38+
39+
TEST(LlvmLibcWMempcpyTest, BoundaryCheck) {
40+
const wchar_t *src = L"12345";
41+
wchar_t dest[4] = {};
42+
void *result = LIBC_NAMESPACE::wmempcpy(dest + 1, src + 1, 2);
43+
44+
ASSERT_TRUE(dest[0] == 0);
45+
ASSERT_TRUE(dest[1] == src[1]);
46+
ASSERT_TRUE(dest[2] == src[2]);
47+
ASSERT_TRUE(dest[3] == 0);
48+
49+
ASSERT_EQ(static_cast<wchar_t *>(result), dest + 3);
50+
}

0 commit comments

Comments
 (0)