Skip to content

Commit 226d06b

Browse files
sribee8Sriya Pratipati
andauthored
[libc] wmemcpy implementation (#142070)
Implemented wmemcpy and tests for the function. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 66a357f commit 226d06b

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-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.wmemcpy
372373

373374
# sys/uio.h entrypoints
374375
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ functions:
6464
- type: const wchar_t *
6565
- type: const wchar_t *
6666
- type: size_t
67+
- name: wmemcpy
68+
standards:
69+
- stdc
70+
return_type: wchar_t *
71+
arguments:
72+
- type: __restricted wchar_t *
73+
- type: const __ restricted wchar_t *
74+
- type: size_t

libc/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,15 @@ add_entrypoint_object(
9191
libc.hdr.wchar_macros
9292
libc.src.__support.wctype_utils
9393
)
94+
95+
add_entrypoint_object(
96+
wmemcpy
97+
SRCS
98+
wmemcpy.cpp
99+
HDRS
100+
wmemcpy.h
101+
DEPENDS
102+
libc.hdr.types.size_t
103+
libc.hdr.wchar_macros
104+
libc.src.__support.wctype_utils
105+
)

libc/src/wchar/wmemcpy.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Implementation of wmemcpy -----------------------------------------===//
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/wmemcpy.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/__support/macros/config.h"
15+
#include "src/string/memory_utils/inline_memcpy.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
LLVM_LIBC_FUNCTION(wchar_t *, wmemcpy,
20+
(wchar_t *__restrict s1, const wchar_t *__restrict s2,
21+
size_t n)) {
22+
inline_memcpy(s1, s2, n * sizeof(wchar_t));
23+
return s1;
24+
}
25+
26+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wmemcpy.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation header for wmemcpy ---------------------------------===//
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_WMEMCPY_H
10+
#define LLVM_LIBC_SRC_WCHAR_WMEMCPY_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 *wmemcpy(wchar_t *__restrict s1, const wchar_t *__restrict s2,
19+
size_t n);
20+
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SRC_WCHAR_WMEMCPY_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,13 @@ add_libc_test(
8484
DEPENDS
8585
libc.src.wchar.wmemcmp
8686
)
87+
88+
add_libc_test(
89+
wmemcpy_test
90+
SUITE
91+
libc_wchar_unittests
92+
SRCS
93+
wmemcpy_test.cpp
94+
DEPENDS
95+
libc.src.wchar.wmemcpy
96+
)

libc/test/src/wchar/wmemcpy_test.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===-- Unittests for wmemcpy ---------------------------------------------===//
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 "hdr/types/size_t.h"
10+
#include "hdr/types/wchar_t.h"
11+
#include "src/wchar/wmemcpy.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
TEST(LlvmLibcWMemcpyTest, CopyIntoEmpty) {
15+
wchar_t dest[10] = {};
16+
const wchar_t *src = L"abcde";
17+
LIBC_NAMESPACE::wmemcpy(dest, src, 6);
18+
ASSERT_TRUE(src[0] == dest[0]);
19+
ASSERT_TRUE(src[1] == dest[1]);
20+
ASSERT_TRUE(src[2] == dest[2]);
21+
ASSERT_TRUE(src[3] == dest[3]);
22+
ASSERT_TRUE(src[4] == dest[4]);
23+
ASSERT_TRUE(src[5] == dest[5]);
24+
}
25+
26+
TEST(LlvmLibcWMemcpyTest, CopyFullString) {
27+
// After copying, strings should be the same.
28+
wchar_t dest[10] = {};
29+
const wchar_t *src = L"abcde";
30+
LIBC_NAMESPACE::wmemcpy(dest, src, 6);
31+
ASSERT_TRUE(src[0] == dest[0]);
32+
ASSERT_TRUE(src[1] == dest[1]);
33+
ASSERT_TRUE(src[2] == dest[2]);
34+
ASSERT_TRUE(src[3] == dest[3]);
35+
ASSERT_TRUE(src[4] == dest[4]);
36+
ASSERT_TRUE(src[5] == dest[5]);
37+
}
38+
39+
TEST(LlvmLibcWMemcpyTest, CopyPartialString) {
40+
// After copying, only first two characters should be the same.
41+
wchar_t dest[10] = {};
42+
const wchar_t *src = L"abcde";
43+
LIBC_NAMESPACE::wmemcpy(dest, src, 2);
44+
ASSERT_TRUE(src[0] == dest[0]);
45+
ASSERT_TRUE(src[1] == dest[1]);
46+
ASSERT_TRUE(src[2] != dest[2]);
47+
ASSERT_TRUE(src[3] != dest[3]);
48+
ASSERT_TRUE(src[4] != dest[4]);
49+
}
50+
51+
TEST(LlvmLibcWMemcpyTest, CopyZeroCharacters) {
52+
// Copying 0 characters should not change the string
53+
wchar_t dest[10] = {L'1', L'2', L'3', L'4', L'5', L'\0'};
54+
const wchar_t *src = L"abcde";
55+
LIBC_NAMESPACE::wmemcpy(dest, src, 0);
56+
ASSERT_TRUE(L'1' == dest[0]);
57+
ASSERT_TRUE(L'2' == dest[1]);
58+
ASSERT_TRUE(L'3' == dest[2]);
59+
ASSERT_TRUE(L'4' == dest[3]);
60+
ASSERT_TRUE(L'5' == dest[4]);
61+
}

0 commit comments

Comments
 (0)