Skip to content

Commit 55d7681

Browse files
author
Sriya Pratipati
committed
[libc] wmemcpy implementation
Implemented wmemcpy and tests for the function.
1 parent 882e733 commit 55d7681

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
@@ -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.wmemcpy
370371

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

libc/include/wchar.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ functions:
5050
- type: const wchar_t *
5151
- type: const wchar_t *
5252
- type: size_t
53+
- name: wmemcpy
54+
standards:
55+
- stdc
56+
return_type: wchar_t *
57+
arguments:
58+
- type: __restricted wchar_t *
59+
- type: const __ restricted wchar_t *
60+
- type: size_t

libc/src/wchar/CMakeLists.txt

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

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
@@ -64,3 +64,13 @@ add_libc_test(
6464
DEPENDS
6565
libc.src.wchar.wmemcmp
6666
)
67+
68+
add_libc_test(
69+
wmemcpy_test
70+
SUITE
71+
libc_wchar_unittests
72+
SRCS
73+
wmemcpy_test.cpp
74+
DEPENDS
75+
libc.src.wchar.wmemcpy
76+
)
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] = {};
54+
const wchar_t *src = L"abcde";
55+
LIBC_NAMESPACE::wmemcpy(dest, src, 0);
56+
ASSERT_TRUE(src[0] != dest[0]);
57+
ASSERT_TRUE(src[1] != dest[1]);
58+
ASSERT_TRUE(src[2] != dest[2]);
59+
ASSERT_TRUE(src[3] != dest[3]);
60+
ASSERT_TRUE(src[4] != dest[4]);
61+
}

0 commit comments

Comments
 (0)