Skip to content

Commit 57a3373

Browse files
[libc][c23] add memset_explicit (#83577)
1 parent 4f85f62 commit 57a3373

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ set(TARGET_LIBC_ENTRYPOINTS
5151
libc.src.string.mempcpy
5252
libc.src.string.memrchr
5353
libc.src.string.memset
54+
libc.src.string.memset_explicit
5455
libc.src.string.rindex
5556
libc.src.string.stpcpy
5657
libc.src.string.stpncpy

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ set(TARGET_LIBC_ENTRYPOINTS
5151
libc.src.string.mempcpy
5252
libc.src.string.memrchr
5353
libc.src.string.memset
54+
libc.src.string.memset_explicit
5455
libc.src.string.rindex
5556
libc.src.string.stpcpy
5657
libc.src.string.stpncpy

libc/spec/stdc.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ def StdC : StandardSpec<"stdc"> {
234234
RetValSpec<VoidPtr>,
235235
[ArgSpec<VoidPtr>, ArgSpec<IntType>, ArgSpec<SizeTType>]
236236
>,
237+
FunctionSpec<
238+
"memset_explicit",
239+
RetValSpec<VoidPtr>,
240+
[ArgSpec<VoidPtr>, ArgSpec<IntType>, ArgSpec<SizeTType>]
241+
>,
237242
FunctionSpec<
238243
"strcpy",
239244
RetValSpec<CharPtr>,

libc/src/string/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,17 @@ add_entrypoint_object(
441441
.memory_utils.inline_memcpy
442442
)
443443

444+
add_entrypoint_object(
445+
memset_explicit
446+
SRCS
447+
memset_explicit.cpp
448+
HDRS
449+
memset_explicit.h
450+
DEPENDS
451+
.string_utils
452+
.memory_utils.inline_memset
453+
)
454+
444455
# Helper to define a function with multiple implementations
445456
# - Computes flags to satisfy required/rejected features and arch,
446457
# - Declares an entry point,

libc/src/string/memset_explicit.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of memset_explicit ---------------------------------===//
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/string/memset_explicit.h"
10+
#include "src/__support/common.h"
11+
#include "src/string/memory_utils/inline_memset.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
[[gnu::noinline]] LLVM_LIBC_FUNCTION(void *, memset_explicit,
16+
(void *dst, int value, size_t count)) {
17+
// Use the inline memset function to set the memory.
18+
inline_memset(dst, static_cast<uint8_t>(value), count);
19+
// avoid dead store elimination
20+
// The asm itself should also be sufficient to behave as a compiler barrier.
21+
asm("" : : "r"(dst) : "memory");
22+
return dst;
23+
}
24+
25+
} // namespace LIBC_NAMESPACE

libc/src/string/memset_explicit.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for memset_explicit ---------------*- C++ -*-===//
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_STRING_MEMSET_EXPLICIT_H
10+
#define LLVM_LIBC_SRC_STRING_MEMSET_EXPLICIT_H
11+
12+
#include <stddef.h> // size_t
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
[[gnu::noinline]] void *memset_explicit(void *ptr, int value, size_t count);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STRING_MEMSET_EXPLICIT_H

libc/test/src/string/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ add_libc_test(
418418
libc.src.string.strxfrm
419419
)
420420

421+
add_libc_test(
422+
memset_explicit_test
423+
SUITE
424+
libc-string-tests
425+
SRCS
426+
memset_explicit_test.cpp
427+
DEPENDS
428+
libc.src.string.memset_explicit
429+
)
430+
421431
# Tests all implementations that can run on the target CPU.
422432
function(add_libc_multi_impl_test name)
423433
get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- Unittests for memset_explicit -------------------------------------===//
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 "memory_utils/memory_check_utils.h"
10+
#include "src/string/memset_explicit.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
// Apply the same tests as memset
16+
17+
static inline void Adaptor(cpp::span<char> p1, uint8_t value, size_t size) {
18+
LIBC_NAMESPACE::memset_explicit(p1.begin(), value, size);
19+
}
20+
21+
TEST(LlvmLibcmemsetExplicitTest, SizeSweep) {
22+
static constexpr size_t kMaxSize = 400;
23+
Buffer DstBuffer(kMaxSize);
24+
for (size_t size = 0; size < kMaxSize; ++size) {
25+
const char value = size % 10;
26+
auto dst = DstBuffer.span().subspan(0, size);
27+
ASSERT_TRUE((CheckMemset<Adaptor>(dst, value, size)));
28+
}
29+
}
30+
31+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)