File tree Expand file tree Collapse file tree 8 files changed +104
-0
lines changed Expand file tree Collapse file tree 8 files changed +104
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ set(TARGET_LIBC_ENTRYPOINTS
51
51
libc.src.string.mempcpy
52
52
libc.src.string.memrchr
53
53
libc.src.string.memset
54
+ libc.src.string.memset_explicit
54
55
libc.src.string.rindex
55
56
libc.src.string.stpcpy
56
57
libc.src.string.stpncpy
Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ set(TARGET_LIBC_ENTRYPOINTS
51
51
libc.src.string.mempcpy
52
52
libc.src.string.memrchr
53
53
libc.src.string.memset
54
+ libc.src.string.memset_explicit
54
55
libc.src.string.rindex
55
56
libc.src.string.stpcpy
56
57
libc.src.string.stpncpy
Original file line number Diff line number Diff line change @@ -234,6 +234,11 @@ def StdC : StandardSpec<"stdc"> {
234
234
RetValSpec<VoidPtr>,
235
235
[ArgSpec<VoidPtr>, ArgSpec<IntType>, ArgSpec<SizeTType>]
236
236
>,
237
+ FunctionSpec<
238
+ "memset_explicit",
239
+ RetValSpec<VoidPtr>,
240
+ [ArgSpec<VoidPtr>, ArgSpec<IntType>, ArgSpec<SizeTType>]
241
+ >,
237
242
FunctionSpec<
238
243
"strcpy",
239
244
RetValSpec<CharPtr>,
Original file line number Diff line number Diff line change @@ -441,6 +441,17 @@ add_entrypoint_object(
441
441
.memory_utils.inline_memcpy
442
442
)
443
443
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
+
444
455
# Helper to define a function with multiple implementations
445
456
# - Computes flags to satisfy required/rejected features and arch,
446
457
# - Declares an entry point,
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -418,6 +418,16 @@ add_libc_test(
418
418
libc.src.string.strxfrm
419
419
)
420
420
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
+
421
431
# Tests all implementations that can run on the target CPU.
422
432
function (add_libc_multi_impl_test name )
423
433
get_property (fq_implementations GLOBAL PROPERTY ${name} _implementations )
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments