|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <corecrt_internal_simd.h>
|
| #include <stdlib.h>
|
| #include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
| #pragma warning(disable: 4752)
|
|
|
|
|
|
|
| namespace
|
| {
|
| enum strnlen_mode
|
| {
|
| bounded,
|
| unbounded,
|
| };
|
| }
|
|
|
|
|
|
|
|
|
| template <strnlen_mode Mode>
|
| static __forceinline bool __cdecl last_reached(
|
| void const* const it,
|
| void const* const last
|
| ) throw()
|
| {
|
| return it == last;
|
| }
|
|
|
| template <>
|
| __forceinline bool __cdecl last_reached<unbounded>(
|
| void const* const it,
|
| void const* const last
|
| ) throw()
|
| {
|
| UNREFERENCED_PARAMETER(it);
|
| UNREFERENCED_PARAMETER(last);
|
|
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
| template <strnlen_mode Mode, typename Element>
|
| _Check_return_
|
| _When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string)))
|
| _When_(maximum_count <= _String_length_(string), _Post_satisfies_(return == maximum_count))
|
| static __forceinline size_t __cdecl common_strnlen_c(
|
| Element const* const string,
|
| size_t const maximum_count
|
| ) throw()
|
| {
|
| Element const* const last = string + maximum_count;
|
| Element const* it = string;
|
|
|
| for (; !last_reached<Mode>(it, last) && *it != '\0'; ++it)
|
| {
|
| }
|
|
|
| return static_cast<size_t>(it - string);
|
| }
|
|
|
| #ifdef _CRT_SIMD_SUPPORT_AVAILABLE
|
|
|
| template <strnlen_mode Mode, __crt_simd_isa Isa, typename Element>
|
| _Check_return_
|
| _When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string)))
|
| _When_(maximum_count <= _String_length_(string), _Post_satisfies_(return == maximum_count))
|
| static __forceinline size_t __cdecl common_strnlen_simd(
|
| Element const* const string,
|
| size_t const maximum_count
|
| ) throw()
|
| {
|
| using traits = __crt_simd_traits<Isa, Element>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| uintptr_t const string_integer = reinterpret_cast<uintptr_t>(string);
|
| if (string_integer % traits::element_size != 0)
|
| {
|
|
|
|
|
|
|
| return common_strnlen_c<Mode>(string, maximum_count);
|
| }
|
|
|
|
|
| uintptr_t const prefix_forward_offset = string_integer % traits::pack_size;
|
| uintptr_t const prefix_reverse_offset = prefix_forward_offset == 0
|
| ? 0
|
| : traits::pack_size - prefix_forward_offset;
|
|
|
| size_t const prefix_count = __min(maximum_count, prefix_reverse_offset / traits::element_size);
|
| size_t const prefix_result = common_strnlen_c<bounded>(string, prefix_count);
|
| if (prefix_result != prefix_count)
|
| {
|
| return prefix_result;
|
| }
|
|
|
| Element const* it = string + prefix_result;
|
|
|
|
|
| __crt_simd_cleanup_guard<Isa> const simd_cleanup;
|
|
|
| traits::pack_type const zero = traits::get_zero_pack();
|
|
|
| size_t const middle_and_suffix_count = maximum_count - prefix_count;
|
| size_t const suffix_count = middle_and_suffix_count % traits::pack_size;
|
| size_t const middle_count = middle_and_suffix_count - suffix_count;
|
|
|
| Element const* const middle_last = it + middle_count;
|
| while (!last_reached<Mode>(it, middle_last))
|
| {
|
| auto const element_it = reinterpret_cast<traits::pack_type const*>(it);
|
|
|
| bool const element_has_terminator = traits::compute_byte_mask(traits::compare_equals(*element_it, zero)) != 0;
|
| if (element_has_terminator)
|
| {
|
| break;
|
| }
|
|
|
| it += traits::elements_per_pack;
|
| }
|
|
|
|
|
| Element const* const suffix_last = string + maximum_count;
|
| for (; !last_reached<Mode>(it, suffix_last) && *it != '\0'; ++it)
|
| {
|
| }
|
|
|
|
|
| return static_cast<size_t>(it - string);
|
| }
|
|
|
| #endif
|
|
|
| template <strnlen_mode Mode, typename Element>
|
| _Check_return_
|
| _When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string)))
|
| _When_(maximum_count <= _String_length_(string), _Post_satisfies_(return == maximum_count))
|
| static __forceinline size_t __cdecl common_strnlen(
|
| Element const* const string,
|
| size_t const maximum_count
|
| ) throw()
|
| {
|
| #ifdef _CRT_SIMD_SUPPORT_AVAILABLE
|
| if (__isa_available >= __ISA_AVAILABLE_AVX2)
|
| {
|
| return common_strnlen_simd<Mode, __crt_simd_isa::avx2>(string, maximum_count);
|
| }
|
| else if (__isa_available >= __ISA_AVAILABLE_SSE2)
|
| {
|
| return common_strnlen_simd<Mode, __crt_simd_isa::sse2>(string, maximum_count);
|
| }
|
| #endif
|
|
|
| return common_strnlen_c<Mode>(string, maximum_count);
|
| }
|
|
|
| #if !defined(_M_ARM64) && !defined(_M_ARM64EC) && !defined(_M_HYBRID_X86_ARM64)
|
|
|
| extern "C" size_t __cdecl strnlen(
|
| char const* const string,
|
| size_t const maximum_count
|
| )
|
| {
|
| return common_strnlen<bounded>(reinterpret_cast<uint8_t const*>(string), maximum_count);
|
| }
|
|
|
| extern "C" size_t __cdecl wcsnlen(
|
| wchar_t const* const string,
|
| size_t const maximum_count
|
| )
|
| {
|
| return common_strnlen<bounded>(reinterpret_cast<uint16_t const*>(string), maximum_count);
|
| }
|
|
|
| #pragma function(wcslen)
|
|
|
| extern "C" size_t __cdecl wcslen(
|
| wchar_t const* const string
|
| )
|
| {
|
| return common_strnlen<unbounded>(reinterpret_cast<uint16_t const*>(string), _CRT_UNBOUNDED_BUFFER_SIZE);
|
| }
|
|
|
| #endif
|
|
|