Skip to content

Commit c53acbf

Browse files
author
Sriya Pratipati
committed
[libc] wmemcmp implementation
1 parent 3a0205a commit c53acbf

File tree

7 files changed

+121
-0
lines changed

7 files changed

+121
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ set(TARGET_LIBC_ENTRYPOINTS
365365
libc.src.wchar.wcslen
366366
libc.src.wchar.wctob
367367
libc.src.wchar.wcschr
368+
libc.src.wchar.wmemcmp
368369

369370
# sys/uio.h entrypoints
370371
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ functions:
3434
arguments:
3535
- type: const wchar_t *
3636
- type: wchar_t
37+
- name: wmemcmp
38+
standards:
39+
- stdc
40+
return_type: int
41+
arguments:
42+
- type: const wchar_t *
43+
- type: const wchar_t *
44+
- type: size_t

libc/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,15 @@ add_entrypoint_object(
4444
libc.hdr.wchar_macros
4545
libc.src.__support.wctype_utils
4646
)
47+
48+
add_entrypoint_object(
49+
wmemcmp
50+
SRCS
51+
wmemcmp.cpp
52+
HDRS
53+
wmemcmp.h
54+
DEPENDS
55+
libc.hdr.types.size_t
56+
libc.hdr.wchar_macros
57+
libc.src.__support.wctype_utils
58+
)

libc/src/wchar/wmemcmp.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Implementation of wmemcmp ------------------------------------------===//
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/wmemcmp.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+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(int, wmemcmp,
19+
(const wchar_t *s1, const wchar_t *s2, size_t n)) {
20+
for (size_t i = 0; i < n; ++s1, ++s2, ++i) {
21+
if (*s1 != *s2)
22+
return (int)(*s1 - *s2);
23+
}
24+
// If it reaches the end, all n values must be the same.
25+
return 0;
26+
}
27+
28+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wmemcmp.h

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

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ add_libc_test(
4242
DEPENDS
4343
libc.src.wchar.wcschr
4444
)
45+
46+
add_libc_test(
47+
wmemcmp_test
48+
SUITE
49+
libc_wchar_unittests
50+
SRCS
51+
wmemcmp_test.cpp
52+
DEPENDS
53+
libc.src.wchar.wmemcmp
54+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- Unittests for wmemcmp ----------------------------------------------===//
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/wmemcmp.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
TEST(LlvmLibcWMemcmpTest, CmpZeroByte) {
15+
// Comparing zero bytes should result in 0.
16+
const wchar_t *lhs = L"ab";
17+
const wchar_t *rhs = L"yz";
18+
EXPECT_EQ(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 0), 0);
19+
}
20+
21+
TEST(LlvmLibcWMemcmpTest, LhsRhsAreTheSame) {
22+
// Comparing strings of equal value should result in 0.
23+
const wchar_t *lhs = L"ab";
24+
const wchar_t *rhs = L"ab";
25+
EXPECT_EQ(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 2), 0);
26+
}
27+
28+
TEST(LlvmLibcWMemcmpTest, LhsBeforeRhsLexically) {
29+
// z after b, should result in a value less than 0.
30+
const wchar_t *lhs = L"ab";
31+
const wchar_t *rhs = L"az";
32+
EXPECT_LT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 2), 0);
33+
}
34+
35+
TEST(LlvmLibcWMemcmpTest, LhsAfterRhsLexically) {
36+
// z after b, should result in a value greater than 0.
37+
const wchar_t *lhs = L"az";
38+
const wchar_t *rhs = L"ab";
39+
EXPECT_GT(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 2), 0);
40+
}

0 commit comments

Comments
 (0)