Skip to content

Commit 97e20ef

Browse files
committed
[UCRT] Fix GCC/Clang build of SSE2/AVX code
On GCC the code using extended ISA cannot be inlined and must either be marked with a function attribute or compiled with the proper target options ('-mavx2' on the command line or '#pragma GCC target("avx2")' inside the code)
1 parent 51e6ce3 commit 97e20ef

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

sdk/lib/ucrt/inc/corecrt_internal_simd.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@
1818

1919
#if defined _CRT_SIMD_SUPPORT_AVAILABLE
2020

21+
#if defined(__clang__)
22+
#define _UCRT_ENABLE_EXTENDED_ISA \
23+
_Pragma("clang attribute push(__attribute__((target(\"sse2,avx,avx2\"))), apply_to=function)")
24+
#define _UCRT_RESTORE_DEFAULT_ISA \
25+
_Pragma("clang attribute pop")
26+
#elif defined(__GNUC__)
27+
#define _UCRT_ENABLE_EXTENDED_ISA \
28+
_Pragma("GCC push_options") \
29+
_Pragma("GCC target(\"avx2\")")
30+
#define _UCRT_RESTORE_DEFAULT_ISA \
31+
_Pragma("GCC pop_options")
32+
#else
33+
#define _UCRT_ENABLE_EXTENDED_ISA
34+
#define _UCRT_RESTORE_DEFAULT_ISA
35+
#endif
36+
37+
_UCRT_ENABLE_EXTENDED_ISA
38+
2139
extern "C" int __isa_available;
2240

2341
enum class __crt_simd_isa
@@ -155,4 +173,6 @@
155173
}
156174
};
157175

176+
_UCRT_RESTORE_DEFAULT_ISA
177+
158178
#endif // _CRT_SIMD_SUPPORT_AVAILABLE

sdk/lib/ucrt/string/strnlen.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ static __forceinline size_t __cdecl common_strnlen_c(
7878

7979
#ifdef _CRT_SIMD_SUPPORT_AVAILABLE
8080

81+
_UCRT_ENABLE_EXTENDED_ISA
82+
8183
template <strnlen_mode Mode, __crt_simd_isa Isa, typename Element>
8284
_Check_return_
8385
_When_(maximum_count > _String_length_(string), _Post_satisfies_(return == _String_length_(string)))
8486
_When_(maximum_count <= _String_length_(string), _Post_satisfies_(return == maximum_count))
85-
static __forceinline size_t __cdecl common_strnlen_simd(
87+
static __inline size_t __cdecl common_strnlen_simd(
8688
Element const* const string,
8789
size_t const maximum_count
8890
) throw()
@@ -168,6 +170,8 @@ static __forceinline size_t __cdecl common_strnlen_c(
168170
return static_cast<size_t>(it - string);
169171
}
170172

173+
_UCRT_RESTORE_DEFAULT_ISA
174+
171175
#endif // _CRT_SIMD_SUPPORT_AVAILABLE
172176

173177
template <strnlen_mode Mode, typename Element>

0 commit comments

Comments
 (0)