Skip to content

Commit 4b3dcda

Browse files
committed
wmempcpy implemented
1 parent 882e733 commit 4b3dcda

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ set(TARGET_LIBC_ENTRYPOINTS
367367
libc.src.wchar.wmemset
368368
libc.src.wchar.wcschr
369369
libc.src.wchar.wmemcmp
370+
libc.src.wchar.wmempcpy
370371

371372
# sys/uio.h entrypoints
372373
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,13 @@ functions:
5050
- type: const wchar_t *
5151
- type: const wchar_t *
5252
- type: size_t
53+
- name: wmempcpy
54+
standards:
55+
- stdc
56+
return_type: wchar_t *
57+
arguments:
58+
- type: wchar_t *
59+
- type: const wchar_t *
60+
- type: size_t
61+
62+

libc/src/wchar/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,16 @@ add_entrypoint_object(
6868
libc.hdr.wchar_macros
6969
libc.src.__support.wctype_utils
7070
)
71+
72+
add_entrypoint_object(
73+
wmempcpy
74+
SRCS
75+
wmempcpy.cpp
76+
HDRS
77+
wmempcpy.h
78+
DEPENDS
79+
libc.hdr.types.size_t
80+
libc.hdr.wchar_macros
81+
libc.src.__support.wctype_utils
82+
libc.src.string.memory_utils.inline_memcpy
83+
)

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
@@ -64,3 +64,13 @@ add_libc_test(
6464
DEPENDS
6565
libc.src.wchar.wmemcmp
6666
)
67+
68+
add_libc_test(
69+
wmempcpy_test
70+
SUITE
71+
libc_wchar_unittests
72+
SRCS
73+
wmempcpy_test.cpp
74+
DEPENDS
75+
libc.src.wchar.wmempcpy
76+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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(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(LlvmLibcWmempcpyTest, ZeroCount) {
27+
const wchar_t *src = L"12345";
28+
wchar_t dest[10];
29+
void *result = LIBC_NAMESPACE::wmempcpy(dest, src, 0);
30+
ASSERT_EQ(static_cast<wchar_t *>(result), dest);
31+
}
32+
33+
TEST(LlvmLibcWMempcpyTest, BoundaryCheck) {
34+
const wchar_t *src = L"12345";
35+
wchar_t dest[4] = {};
36+
void *result = LIBC_NAMESPACE::wmempcpy(dest + 1, src + 1, 2);
37+
38+
ASSERT_TRUE(dest[0] == 0);
39+
ASSERT_TRUE(dest[1] == src[1]);
40+
ASSERT_TRUE(dest[2] == src[2]);
41+
ASSERT_TRUE(dest[3] == 0);
42+
43+
ASSERT_EQ(static_cast<wchar_t *>(result), dest + 3);
44+
}

0 commit comments

Comments
 (0)