diff --git a/stl/inc/__msvc_int128.hpp b/stl/inc/__msvc_int128.hpp index bcb3b6e0cab..e648ab53f61 100644 --- a/stl/inc/__msvc_int128.hpp +++ b/stl/inc/__msvc_int128.hpp @@ -213,7 +213,7 @@ struct int64_t __k = 0; int64_t __t _ZERO_OR_NO_INIT; - for (int __i = 0; __i < static_cast(__n); ++__i) { + for (int __i = 0; __i < __n; ++__i) { const auto _Prod = static_cast(__qhat) * static_cast(__v[__i]); __t = __u[__i + __j] - __k - static_cast(_Prod); __u[__i + __j] = static_cast(__t); @@ -226,7 +226,7 @@ struct if (__t < 0) { --__q[__j]; __k = 0; - for (int __i = 0; __i < static_cast(__n); ++__i) { + for (int __i = 0; __i < __n; ++__i) { __t = __u[__i + __j] + __k + __v[__i]; __u[__i + __j] = static_cast(__t); __k = __t >> 32; diff --git a/stl/inc/random b/stl/inc/random index 32be63fa7df..2acfaa5656b 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -4140,16 +4140,13 @@ private: _Ty _Rx0; _Ty _Rs; uniform_real<_Ty> _Dist(-1, 1); - for (;;) { // get a point inside unit circle + do { // get a point inside unit circle _Vx1 = _Dist(_Eng); _Vx2 = _Dist(_Eng); _Rs = _Vx1 * _Vx1 + _Vx2 * _Vx2; // very small _Rs will overflow on pow(_Rx0, -_Ty{4} / _Par0._Nx) - if (_Rs < _Ty{1} && _Rs > _Ty{1e-12}) { - break; - } - } + } while (_Rs >= _Ty{1} || _Rs <= _Ty{1e-12}); _Rx0 = _STD sqrt(_Rs); return _Vx1 * _STD sqrt(_Par0._Nx * (_STD pow(_Rx0, -_Ty{4} / _Par0._Nx) - _Ty{1}) / _Rs); } diff --git a/stl/inc/xcharconv_ryu.h b/stl/inc/xcharconv_ryu.h index d4f7ca635d1..27a3513f7ef 100644 --- a/stl/inc/xcharconv_ryu.h +++ b/stl/inc/xcharconv_ryu.h @@ -703,7 +703,7 @@ _NODISCARD pair<_CharT*, errc> __d2fixed_buffered_n(_CharT* _First, _CharT* cons _NODISCARD inline to_chars_result __d2exp_buffered_n(char* _First, char* const _Last, const double __d, uint32_t __precision) { - char* const _Original_first = _First; + const char* const _Original_first = _First; const uint64_t __bits = __double_to_bits(__d); @@ -1832,11 +1832,11 @@ _NODISCARD inline __floating_decimal_64 __d2d(const uint64_t __ieeeMantissa, con // Step 4: Find the shortest decimal representation in the interval of valid representations. int32_t __removed = 0; - uint8_t __lastRemovedDigit = 0; uint64_t _Output; // On average, we remove ~2 digits. if (__vmIsTrailingZeros || __vrIsTrailingZeros) { - // General case, which happens rarely (~0.7%). + uint8_t __lastRemovedDigit = 0; + // General case, which happens rarely (~0.7%). for (;;) { const uint64_t __vpDiv10 = __div10(__vp); const uint64_t __vmDiv10 = __div10(__vm); @@ -2331,8 +2331,7 @@ _NODISCARD pair<_CharT*, errc> __d2s_buffered_n(_CharT* const _First, _CharT* co } __floating_decimal_64 __v; - const bool __isSmallInt = __d2d_small_int(__ieeeMantissa, __ieeeExponent, &__v); - if (__isSmallInt) { + if (const bool __isSmallInt = __d2d_small_int(__ieeeMantissa, __ieeeExponent, &__v)) { // For small integers in the range [1, 2^53), __v.__mantissa might contain trailing (decimal) zeros. // For scientific notation we need to move these zeros into the exponent. // (This is not needed for fixed-point notation, so it might be beneficial to trim diff --git a/stl/inc/xpolymorphic_allocator.h b/stl/inc/xpolymorphic_allocator.h index 278843e3ba5..cfee23382f1 100644 --- a/stl/inc/xpolymorphic_allocator.h +++ b/stl/inc/xpolymorphic_allocator.h @@ -149,9 +149,9 @@ namespace pmr { } private: - virtual void* do_allocate(size_t _Bytes, size_t _Align) = 0; - virtual void do_deallocate(void* _Ptr, size_t _Bytes, size_t _Align) = 0; - virtual bool do_is_equal(const memory_resource& _That) const noexcept = 0; + virtual void* do_allocate(size_t _Bytes, size_t _Align) = 0; + virtual void do_deallocate(void* _Ptr, size_t _Bytes, size_t _Align) = 0; + _NODISCARD virtual bool do_is_equal(const memory_resource& _That) const noexcept = 0; }; _NODISCARD inline bool operator==(const memory_resource& _Left, const memory_resource& _Right) noexcept { @@ -269,11 +269,12 @@ namespace pmr { } template - _CXX17_DEPRECATE_POLYMORPHIC_ALLOCATOR_DESTROY void destroy(_Uty* const _Ptr) noexcept /* strengthened */ { + _CXX17_DEPRECATE_POLYMORPHIC_ALLOCATOR_DESTROY void destroy(_Uty* const _Ptr) noexcept + /* strengthened */ { _Destroy_in_place(*_Ptr); } - _NODISCARD polymorphic_allocator select_on_container_copy_construction() const noexcept /* strengthened */ { + _NODISCARD polymorphic_allocator select_on_container_copy_construction() noexcept /* strengthened */ { // don't propagate on copy return {}; } diff --git a/stl/src/_tolower.cpp b/stl/src/_tolower.cpp index 16bec174d3d..1d0581781a0 100644 --- a/stl/src/_tolower.cpp +++ b/stl/src/_tolower.cpp @@ -84,7 +84,7 @@ _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL _Tolower(int c, const _Ctypevec* ploc) // convert wide char to lowercase size = __crtLCMapStringA(locale_name, LCMAP_LOWERCASE, reinterpret_cast(inbuffer), size, - reinterpret_cast(outbuffer), 3, codepage, TRUE); + reinterpret_cast(outbuffer), 3, static_cast(codepage), TRUE); if (size == 0) { return c; diff --git a/stl/src/_toupper.cpp b/stl/src/_toupper.cpp index 70ef52d7c77..33fac7b1ddb 100644 --- a/stl/src/_toupper.cpp +++ b/stl/src/_toupper.cpp @@ -83,7 +83,7 @@ _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL _Toupper(int c, const _Ctypevec* ploc) // convert wide char to uppercase size = __crtLCMapStringA(locale_name, LCMAP_UPPERCASE, reinterpret_cast(inbuffer), size, - reinterpret_cast(outbuffer), 3, codepage, TRUE); + reinterpret_cast(outbuffer), 3, static_cast(codepage), TRUE); if (size == 0) { return c; diff --git a/stl/src/cond.cpp b/stl/src/cond.cpp index 46d82ae1132..321c8e0e182 100644 --- a/stl/src/cond.cpp +++ b/stl/src/cond.cpp @@ -12,8 +12,9 @@ #include "primitives.hpp" struct _Cnd_internal_imp_t { // condition variable implementation for ConcRT - typename std::_Aligned_storage::type cv; + std::_Aligned_storage_t + cv; [[nodiscard]] Concurrency::details::stl_condition_variable_interface* _get_cv() noexcept { // get pointer to implementation diff --git a/stl/src/cthread.cpp b/stl/src/cthread.cpp index 20512fc6580..d8e931c951e 100644 --- a/stl/src/cthread.cpp +++ b/stl/src/cthread.cpp @@ -87,7 +87,7 @@ void _Thrd_yield() { // surrender remainder of timeslice // TRANSITION, ABI: _Thrd_equal() is preserved for binary compatibility _CRTIMP2_PURE int _Thrd_equal(_Thrd_t thr0, _Thrd_t thr1) { // return 1 if thr0 and thr1 identify same thread - return thr0._Id == thr1._Id; + return static_cast(thr0._Id == thr1._Id); } // TRANSITION, ABI: _Thrd_current() is preserved for binary compatibility diff --git a/stl/src/iosptrs.cpp b/stl/src/iosptrs.cpp index 218ca0b672b..b385dc5d8e4 100644 --- a/stl/src/iosptrs.cpp +++ b/stl/src/iosptrs.cpp @@ -42,9 +42,8 @@ _MRTIMP2 void __cdecl _Atexit(void(__cdecl* pf)()) { // add to wrapup list struct _Init_atexit { // controller for atexit processing __CLR_OR_THIS_CALL ~_Init_atexit() noexcept { // process wrapup functions while (atcount_cdecl < _Nats) { - const auto pf = reinterpret_cast( - DecodePointer(reinterpret_cast(atfuns_cdecl[atcount_cdecl++]))); - if (pf) { + if (const auto pf = reinterpret_cast( + DecodePointer(reinterpret_cast(atfuns_cdecl[atcount_cdecl++])))) { pf(); } } diff --git a/stl/src/locale.cpp b/stl/src/locale.cpp index e8802cf8c07..cd65be58673 100644 --- a/stl/src/locale.cpp +++ b/stl/src/locale.cpp @@ -79,7 +79,7 @@ locale::_Locimp* __CLRCALL_OR_CDECL locale::_Locimp::_Makeloc( void __CLRCALL_PURE_OR_CDECL locale::_Locimp::_Locimp_ctor( locale::_Locimp* _This, const locale::_Locimp& imp) { // construct a _Locimp from a copy - if (&imp == _This->_Clocptr) { + if (&imp == _Clocptr) { _BEGIN_LOCINFO(_Lobj) _Makeloc(_Lobj, locale::all, _This, nullptr); _END_LOCINFO() @@ -107,7 +107,7 @@ void __CLRCALL_PURE_OR_CDECL locale::_Locimp::_Locimp_ctor( void __CLRCALL_PURE_OR_CDECL locale::_Locimp::_Locimp_Addfac( _Locimp* _This, locale::facet* ptrfac, size_t id) { // add a facet to a locale _BEGIN_LOCK(_LOCK_LOCALE) - const size_t MINCAT = 40; // minimum number of facets in a locale + constexpr size_t MINCAT = 40; // minimum number of facets in a locale if (_This->_Facetcount <= id) { // make facet vector larger size_t count = id + 1; @@ -115,7 +115,7 @@ void __CLRCALL_PURE_OR_CDECL locale::_Locimp::_Locimp_Addfac( count = MINCAT; } - locale::facet** ptrnewvec = + const auto ptrnewvec = static_cast(_realloc_crt(_This->_Facetvec, count * sizeof(locale::facet**))); if (ptrnewvec == nullptr) { // report no memory _Xbad_alloc(); diff --git a/stl/src/locale0.cpp b/stl/src/locale0.cpp index e24d96bbeaf..2c36200dc38 100644 --- a/stl/src/locale0.cpp +++ b/stl/src/locale0.cpp @@ -146,7 +146,7 @@ _MRTIMP2_PURE locale __CLRCALL_PURE_OR_CDECL locale::empty() { // make empty tra } _MRTIMP2_PURE locale::_Locimp* __CLRCALL_PURE_OR_CDECL locale::_Init(bool _Do_incref) { // setup global and "C" locales - locale::_Locimp* ptr = nullptr; + locale::_Locimp* ptr; _BEGIN_LOCK(_LOCK_LOCALE) // prevent double initialization diff --git a/stl/src/multprec.cpp b/stl/src/multprec.cpp index 0fb8c9d7c25..e1b12ac44f5 100644 --- a/stl/src/multprec.cpp +++ b/stl/src/multprec.cpp @@ -52,8 +52,8 @@ static void mul( void __CLRCALL_PURE_OR_CDECL _MP_Mul( _MP_arr w, unsigned long long u0, unsigned long long v0) noexcept { // multiply multi-word value by multi-word value - constexpr int m = 2; - constexpr int n = 2; + constexpr unsigned int m = 2; + constexpr unsigned int n = 2; unsigned long long u[2]; unsigned long long v[2]; u[0] = u0 & mask; @@ -63,16 +63,16 @@ void __CLRCALL_PURE_OR_CDECL _MP_Mul( // Knuth, vol. 2, p. 268, Algorithm M // M1: [Initialize.] - for (int i = 0; i < m + n + 1; ++i) { + for (unsigned int i = 0; i < m + n + 1; ++i) { w[i] = 0; } - for (int j = 0; j < n; ++j) { // M2: [Zero multiplier?] + for (unsigned int j = 0; j < n; ++j) { // M2: [Zero multiplier?] if (v[j] == 0) { w[j + m] = 0; } else { // multiply by non-zero value unsigned long long k = 0; - int i; + unsigned int i; // M3: [Initialize i.] for (i = 0; i < m; ++i) { // M4: [Multiply and add.] w[i + j] = u[i] * v[j] + w[i + j] + k; @@ -170,7 +170,6 @@ void __CLRCALL_PURE_OR_CDECL _MP_Rem( } // D5: [Test remainder.] if (k != 0) { // D6: [Add back.] - --qh; add(u + j, n + 1, v, n); } // D7: [Loop on j.] diff --git a/stl/src/mutex.cpp b/stl/src/mutex.cpp index 031fb02b9c8..f4021ab7e3f 100644 --- a/stl/src/mutex.cpp +++ b/stl/src/mutex.cpp @@ -38,8 +38,9 @@ extern "C" _CRTIMP2 void __cdecl __set_stl_sync_api_mode(__stl_sync_api_modes_en struct _Mtx_internal_imp_t { // ConcRT mutex int type; - typename std::_Aligned_storage::type cs; + std::_Aligned_storage_t + cs; long thread_id; int count; Concurrency::details::stl_critical_section_interface* _get_cs() { // get pointer to implementation diff --git a/stl/src/nothrow.cpp b/stl/src/nothrow.cpp index d12dcdcf336..360c615f367 100644 --- a/stl/src/nothrow.cpp +++ b/stl/src/nothrow.cpp @@ -14,5 +14,5 @@ #include _STD_BEGIN -const nothrow_t nothrow = nothrow_t(); // define nothrow +constexpr nothrow_t nothrow = nothrow_t(); // define nothrow _STD_END diff --git a/stl/src/pplerror.cpp b/stl/src/pplerror.cpp index bfdec568cc8..732a1f533f8 100644 --- a/stl/src/pplerror.cpp +++ b/stl/src/pplerror.cpp @@ -46,12 +46,10 @@ namespace Concurrency { #else // defined(_CRT_APP) || defined(UNDOCKED_WINDOWS_UCRT) -namespace Concurrency { - namespace details { +namespace Concurrency::details { - _CRTIMP2 void __thiscall _ExceptionHolder::ReportUnhandledError() {} + _CRTIMP2 void __thiscall _ExceptionHolder::ReportUnhandledError() {} - } // namespace details -} // namespace Concurrency +} // namespace Concurrency::details #endif // defined(_CRT_APP) || defined(UNDOCKED_WINDOWS_UCRT) diff --git a/stl/src/primitives.hpp b/stl/src/primitives.hpp index 176dcbf02cc..130717d9312 100644 --- a/stl/src/primitives.hpp +++ b/stl/src/primitives.hpp @@ -8,132 +8,130 @@ #include "awint.hpp" -namespace Concurrency { - namespace details { - class __declspec(novtable) stl_critical_section_interface { - public: - virtual void lock() = 0; - virtual bool try_lock() = 0; - virtual bool try_lock_for(unsigned int) = 0; - virtual void unlock() = 0; - virtual void destroy() = 0; - }; - - class __declspec(novtable) stl_condition_variable_interface { - public: - virtual void wait(stl_critical_section_interface*) = 0; - virtual bool wait_for(stl_critical_section_interface*, unsigned int) = 0; - virtual void notify_one() = 0; - virtual void notify_all() = 0; - virtual void destroy() = 0; - }; - - class stl_critical_section_win7 final : public stl_critical_section_interface { - public: - stl_critical_section_win7() { - InitializeSRWLock(&m_srw_lock); - } +namespace Concurrency::details { + class __declspec(novtable) stl_critical_section_interface { + public: + virtual void lock() = 0; + virtual bool try_lock() = 0; + virtual bool try_lock_for(unsigned int) = 0; + virtual void unlock() = 0; + virtual void destroy() = 0; + }; + + class __declspec(novtable) stl_condition_variable_interface { + public: + virtual void wait(stl_critical_section_interface*) = 0; + virtual bool wait_for(stl_critical_section_interface*, unsigned int) = 0; + virtual void notify_one() = 0; + virtual void notify_all() = 0; + virtual void destroy() = 0; + }; + + class stl_critical_section_win7 final : public stl_critical_section_interface { + public: + stl_critical_section_win7() { + InitializeSRWLock(&m_srw_lock); + } - ~stl_critical_section_win7() = delete; - stl_critical_section_win7(const stl_critical_section_win7&) = delete; - stl_critical_section_win7& operator=(const stl_critical_section_win7&) = delete; + ~stl_critical_section_win7() = delete; + stl_critical_section_win7(const stl_critical_section_win7&) = delete; + stl_critical_section_win7& operator=(const stl_critical_section_win7&) = delete; - void destroy() override {} + void destroy() override {} - void lock() override { - AcquireSRWLockExclusive(&m_srw_lock); - } + void lock() override { + AcquireSRWLockExclusive(&m_srw_lock); + } - bool try_lock() override { - return TryAcquireSRWLockExclusive(&m_srw_lock) != 0; - } + bool try_lock() override { + return TryAcquireSRWLockExclusive(&m_srw_lock) != 0; + } - bool try_lock_for(unsigned int) override { - // STL will call try_lock_for once again if this call will not succeed - return stl_critical_section_win7::try_lock(); - } + bool try_lock_for(unsigned int) override { + // STL will call try_lock_for once again if this call will not succeed + return stl_critical_section_win7::try_lock(); + } - void unlock() override { - ReleaseSRWLockExclusive(&m_srw_lock); - } + void unlock() override { + ReleaseSRWLockExclusive(&m_srw_lock); + } - PSRWLOCK native_handle() { - return &m_srw_lock; - } + PSRWLOCK native_handle() { + return &m_srw_lock; + } - private: - SRWLOCK m_srw_lock; - }; + private: + SRWLOCK m_srw_lock; + }; - class stl_condition_variable_win7 final : public stl_condition_variable_interface { - public: - stl_condition_variable_win7() { - InitializeConditionVariable(&m_condition_variable); - } + class stl_condition_variable_win7 final : public stl_condition_variable_interface { + public: + stl_condition_variable_win7() { + InitializeConditionVariable(&m_condition_variable); + } - ~stl_condition_variable_win7() = delete; - stl_condition_variable_win7(const stl_condition_variable_win7&) = delete; - stl_condition_variable_win7& operator=(const stl_condition_variable_win7&) = delete; + ~stl_condition_variable_win7() = delete; + stl_condition_variable_win7(const stl_condition_variable_win7&) = delete; + stl_condition_variable_win7& operator=(const stl_condition_variable_win7&) = delete; - void destroy() override {} + void destroy() override {} - void wait(stl_critical_section_interface* lock) override { - if (!stl_condition_variable_win7::wait_for(lock, INFINITE)) { - std::terminate(); - } + void wait(stl_critical_section_interface* lock) override { + if (!stl_condition_variable_win7::wait_for(lock, INFINITE)) { + std::terminate(); } + } - bool wait_for(stl_critical_section_interface* lock, unsigned int timeout) override { - return SleepConditionVariableSRW(&m_condition_variable, - static_cast(lock)->native_handle(), timeout, 0) - != 0; - } + bool wait_for(stl_critical_section_interface* lock, unsigned int timeout) override { + return SleepConditionVariableSRW(&m_condition_variable, + static_cast(lock)->native_handle(), timeout, 0) + != 0; + } - void notify_one() override { - WakeConditionVariable(&m_condition_variable); - } + void notify_one() override { + WakeConditionVariable(&m_condition_variable); + } - void notify_all() override { - WakeAllConditionVariable(&m_condition_variable); - } + void notify_all() override { + WakeAllConditionVariable(&m_condition_variable); + } - private: - CONDITION_VARIABLE m_condition_variable; - }; + private: + CONDITION_VARIABLE m_condition_variable; + }; - inline void create_stl_critical_section(stl_critical_section_interface* p) { - new (p) stl_critical_section_win7; - } + inline void create_stl_critical_section(stl_critical_section_interface* p) { + new (p) stl_critical_section_win7; + } - inline void create_stl_condition_variable(stl_condition_variable_interface* p) { - new (p) stl_condition_variable_win7; - } + inline void create_stl_condition_variable(stl_condition_variable_interface* p) { + new (p) stl_condition_variable_win7; + } #ifdef _WIN64 - const size_t sizeof_stl_critical_section_concrt = 64; - const size_t sizeof_stl_condition_variable_concrt = 72; - const size_t sizeof_stl_critical_section_vista = 48; - const size_t sizeof_stl_condition_variable_vista = 16; + constexpr size_t sizeof_stl_critical_section_concrt = 64; + constexpr size_t sizeof_stl_condition_variable_concrt = 72; + constexpr size_t sizeof_stl_critical_section_vista = 48; + constexpr size_t sizeof_stl_condition_variable_vista = 16; #else // ^^^ 64-bit / 32-bit vvv - const size_t sizeof_stl_critical_section_concrt = 36; - const size_t sizeof_stl_condition_variable_concrt = 40; - const size_t sizeof_stl_critical_section_vista = 28; - const size_t sizeof_stl_condition_variable_vista = 8; + constexpr size_t sizeof_stl_critical_section_concrt = 36; + constexpr size_t sizeof_stl_condition_variable_concrt = 40; + constexpr size_t sizeof_stl_critical_section_vista = 28; + constexpr size_t sizeof_stl_condition_variable_vista = 8; #endif // ^^^ 32-bit ^^^ #if defined(_CRT_WINDOWS) - const size_t stl_critical_section_max_size = sizeof(stl_critical_section_win7); - const size_t stl_condition_variable_max_size = sizeof(stl_condition_variable_win7); + constexpr size_t stl_critical_section_max_size = sizeof(stl_critical_section_win7); + constexpr size_t stl_condition_variable_max_size = sizeof(stl_condition_variable_win7); #elif defined(_STL_CONCRT_SUPPORT) - const size_t stl_critical_section_max_size = sizeof_stl_critical_section_concrt; - const size_t stl_condition_variable_max_size = sizeof_stl_condition_variable_concrt; + constexpr size_t stl_critical_section_max_size = sizeof_stl_critical_section_concrt; + constexpr size_t stl_condition_variable_max_size = sizeof_stl_condition_variable_concrt; #else // vvv !defined(_CRT_WINDOWS) && !defined(_STL_CONCRT_SUPPORT) vvv - const size_t stl_critical_section_max_size = sizeof_stl_critical_section_vista; - const size_t stl_condition_variable_max_size = sizeof_stl_condition_variable_vista; + constexpr size_t stl_critical_section_max_size = sizeof_stl_critical_section_vista; + constexpr size_t stl_condition_variable_max_size = sizeof_stl_condition_variable_vista; #endif // ^^^ !defined(_CRT_WINDOWS) && !defined(_STL_CONCRT_SUPPORT) ^^^ - // concrt, vista, and win7 alignments are all identical to alignof(void*) - const size_t stl_critical_section_max_alignment = alignof(stl_critical_section_win7); - const size_t stl_condition_variable_max_alignment = alignof(stl_condition_variable_win7); - } // namespace details -} // namespace Concurrency + // concrt, vista, and win7 alignments are all identical to alignof(void*) + constexpr size_t stl_critical_section_max_alignment = alignof(stl_critical_section_win7); + constexpr size_t stl_condition_variable_max_alignment = alignof(stl_condition_variable_win7); +} // namespace Concurrency::details diff --git a/stl/src/taskscheduler.cpp b/stl/src/taskscheduler.cpp index 4236bdd4214..62196909c48 100644 --- a/stl/src/taskscheduler.cpp +++ b/stl/src/taskscheduler.cpp @@ -13,159 +13,155 @@ #pragma warning(disable : 4074) #pragma init_seg(compiler) -static std::_Init_locks initlocks; - extern "C" IMAGE_DOS_HEADER __ImageBase; -namespace Concurrency { - namespace details { - namespace { - // When the CRT and STL are statically linked into an EXE, their uninitialization will take place - // inside of the call to exit(), before ExitProcess() is called. This means that their - // uninitialization occurs before other threads in the process are terminated. We block the exit - // from proceeding until all outstanding tasks have completed, to ensure that they do not run after - // we destroy the STL's internal locks. - - // When the CRT and STL are hosted in DLLs (either in their own DLLs or statically linked into a - // user DLL), they are uninitialized when the DLL is notified for DLL_PROCESS_DETACH before it is - // unloaded. Termination unload occurs after all other threads in the process have been terminated, - // so there is no risk of other threads touching internal STL state. We prevent non-termination - // unload from occurring while there are outstanding tasks, by having each task own a reference to - // the DLL in which the callback is located. - - HMODULE _Call_get_module_handle_ex(DWORD _Flags, LPCWSTR _Addr) { +namespace Concurrency::details { + namespace { + // When the CRT and STL are statically linked into an EXE, their uninitialization will take place + // inside of the call to exit(), before ExitProcess() is called. This means that their + // uninitialization occurs before other threads in the process are terminated. We block the exit + // from proceeding until all outstanding tasks have completed, to ensure that they do not run after + // we destroy the STL's internal locks. + + // When the CRT and STL are hosted in DLLs (either in their own DLLs or statically linked into a + // user DLL), they are uninitialized when the DLL is notified for DLL_PROCESS_DETACH before it is + // unloaded. Termination unload occurs after all other threads in the process have been terminated, + // so there is no risk of other threads touching internal STL state. We prevent non-termination + // unload from occurring while there are outstanding tasks, by having each task own a reference to + // the DLL in which the callback is located. + + HMODULE _Call_get_module_handle_ex(DWORD _Flags, LPCWSTR _Addr) { #if defined(_CRT_APP) - // We can't call GetModuleHandleExW from an app context, so treat - // that as a failure to call. - (void) _Flags; - (void) _Addr; - return nullptr; + // We can't call GetModuleHandleExW from an app context, so treat + // that as a failure to call. + (void) _Flags; + (void) _Addr; + return nullptr; #else // ^^^ defined(_CRT_APP) ^^^ // vvv !defined(_CRT_APP) vvv - HMODULE _Result; - if (!GetModuleHandleExW(_Flags, _Addr, &_Result)) { - return nullptr; - } + HMODULE _Result; + if (!GetModuleHandleExW(_Flags, _Addr, &_Result)) { + return nullptr; + } - return _Result; + return _Result; #endif // defined(_CRT_APP) - } + } - enum class _STL_host_status { _Exe, _Dll, _Unknown }; + enum class _STL_host_status { _Exe, _Dll, _Unknown }; - _STL_host_status _Get_STL_host_status() { + _STL_host_status _Get_STL_host_status() { #ifdef CRTDLL2 - return _STL_host_status::_Dll; + return _STL_host_status::_Dll; #else // ^^^ CRTDLL2 ^^^ // vvv !CRTDLL2 vvv - HANDLE _HExe = _Call_get_module_handle_ex(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, nullptr); - if (_HExe == nullptr) { - return _STL_host_status::_Unknown; - } else if (_HExe == reinterpret_cast(&__ImageBase)) { - return _STL_host_status::_Exe; - } else { - return _STL_host_status::_Dll; - } -#endif // CRTDLL2 + HANDLE _HExe = _Call_get_module_handle_ex(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, nullptr); + if (_HExe == nullptr) { + return _STL_host_status::_Unknown; + } else if (_HExe == reinterpret_cast(&__ImageBase)) { + return _STL_host_status::_Exe; + } else { + return _STL_host_status::_Dll; } +#endif // CRTDLL2 + } #ifdef CRTDLL2 - // If the STL is a DLL, no reference counting is necessary, because the CRT shutdown is - // always through DLL_PROCESS_DETACH, so keeping the owning reference to the callback - // code is sufficient. - void _Increment_outstanding() {} - void _Decrement_outstanding() {} + // If the STL is a DLL, no reference counting is necessary, because the CRT shutdown is + // always through DLL_PROCESS_DETACH, so keeping the owning reference to the callback + // code is sufficient. + void _Increment_outstanding() {} + void _Decrement_outstanding() {} #else // ^^^ CRTDLL2 ^^^ // vvv !CRTDLL2 vvv - size_t _Outstanding_tasks = 0; - _STD mutex _Task_cv_mutex; - _STD condition_variable _Task_cv; + size_t _Outstanding_tasks = 0; + _STD mutex _Task_cv_mutex; + _STD condition_variable _Task_cv; - void _Increment_outstanding() { // block shutdown - if (_Get_STL_host_status() == _STL_host_status::_Dll) { - return; - } - - _STD lock_guard<_STD mutex> _Lg(_Task_cv_mutex); - ++_Outstanding_tasks; + void _Increment_outstanding() { // block shutdown + if (_Get_STL_host_status() == _STL_host_status::_Dll) { + return; } - void _Decrement_outstanding() { // release shutdown - if (_Get_STL_host_status() == _STL_host_status::_Dll) { - return; - } + _STD lock_guard<_STD mutex> _Lg(_Task_cv_mutex); + ++_Outstanding_tasks; + } - size_t _Dec_outstanding; - { - _STD lock_guard<_STD mutex> _Lg(_Task_cv_mutex); - _Dec_outstanding = --_Outstanding_tasks; - } + void _Decrement_outstanding() { // release shutdown + if (_Get_STL_host_status() == _STL_host_status::_Dll) { + return; + } - if (_Dec_outstanding == 0) { - _Task_cv.notify_all(); - } + size_t _Dec_outstanding; + { + _STD lock_guard<_STD mutex> _Lg(_Task_cv_mutex); + _Dec_outstanding = --_Outstanding_tasks; } - struct _Task_scheduler_main_block { - _Task_scheduler_main_block() = default; - _Task_scheduler_main_block(const _Task_scheduler_main_block&) = delete; - _Task_scheduler_main_block& operator=(const _Task_scheduler_main_block&) = delete; - ~_Task_scheduler_main_block() noexcept { // block shutdown of the CRT until std::async shutdown has - // completed - _STD unique_lock<_STD mutex> _Lck(_Task_cv_mutex); - _Task_cv.wait(_Lck, [] { return _Outstanding_tasks == 0; }); - } - } _Task_scheduler_main_block_instance; + if (_Dec_outstanding == 0) { + _Task_cv.notify_all(); + } + } + + struct _Task_scheduler_main_block { + _Task_scheduler_main_block() = default; + _Task_scheduler_main_block(const _Task_scheduler_main_block&) = delete; + _Task_scheduler_main_block& operator=(const _Task_scheduler_main_block&) = delete; + ~_Task_scheduler_main_block() noexcept { // block shutdown of the CRT until std::async shutdown has + // completed + _STD unique_lock<_STD mutex> _Lck(_Task_cv_mutex); + _Task_cv.wait(_Lck, [] { return _Outstanding_tasks == 0; }); + } + } _Task_scheduler_main_block_instance; #endif // CRTDLL2 - void CALLBACK _Task_scheduler_callback(PTP_CALLBACK_INSTANCE _Pci, PVOID _Args, PTP_WORK) noexcept { - _Increment_outstanding(); - const auto _Chore = static_cast<_Threadpool_chore*>(_Args); - if (_Get_STL_host_status() != _STL_host_status::_Exe) { // ensure user code held alive by - // _Reschedule_chore is freed when we're done - const HMODULE _Callback_dll = _Call_get_module_handle_ex( - GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - reinterpret_cast(_Chore->_M_callback)); - - if (_Callback_dll != nullptr) { - FreeLibraryWhenCallbackReturns(_Pci, _Callback_dll); - } + void CALLBACK _Task_scheduler_callback(PTP_CALLBACK_INSTANCE _Pci, PVOID _Args, PTP_WORK) noexcept { + _Increment_outstanding(); + const auto _Chore = static_cast<_Threadpool_chore*>(_Args); + if (_Get_STL_host_status() != _STL_host_status::_Exe) { // ensure user code held alive by + // _Reschedule_chore is freed when we're done + const HMODULE _Callback_dll = _Call_get_module_handle_ex( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(_Chore->_M_callback)); + + if (_Callback_dll != nullptr) { + FreeLibraryWhenCallbackReturns(_Pci, _Callback_dll); } - - _Chore->_M_callback(_Chore->_M_data); - _Decrement_outstanding(); } - } // namespace - _CRTIMP2 void __cdecl _Release_chore(_Threadpool_chore* _Chore) { - if (_Chore->_M_work != nullptr) { - CloseThreadpoolWork(static_cast(_Chore->_M_work)); - _Chore->_M_work = nullptr; - } + _Chore->_M_callback(_Chore->_M_data); + _Decrement_outstanding(); } + } // namespace - _CRTIMP2 int __cdecl _Reschedule_chore(const _Threadpool_chore* _Chore) { - _ASSERT(_Chore->_M_work); + _CRTIMP2 void __cdecl _Release_chore(_Threadpool_chore* _Chore) { + if (_Chore->_M_work != nullptr) { + CloseThreadpoolWork(static_cast(_Chore->_M_work)); + _Chore->_M_work = nullptr; + } + } - // Adds a reference to the DLL with the code to execute on async; the callback will - // FreeLibraryWhenCallbackReturns this DLL once it starts running. - if (_Get_STL_host_status() != _STL_host_status::_Exe) { - (void) _Call_get_module_handle_ex( - GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast(_Chore->_M_callback)); - } + _CRTIMP2 int __cdecl _Reschedule_chore(const _Threadpool_chore* _Chore) { + _ASSERT(_Chore->_M_work); - SubmitThreadpoolWork(static_cast(_Chore->_M_work)); - return 0; + // Adds a reference to the DLL with the code to execute on async; the callback will + // FreeLibraryWhenCallbackReturns this DLL once it starts running. + if (_Get_STL_host_status() != _STL_host_status::_Exe) { + (void) _Call_get_module_handle_ex( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast(_Chore->_M_callback)); } - _CRTIMP2 int __cdecl _Schedule_chore(_Threadpool_chore* _Chore) { - _ASSERT(_Chore->_M_work == nullptr); - _ASSERT(_Chore->_M_callback != nullptr); + SubmitThreadpoolWork(static_cast(_Chore->_M_work)); + return 0; + } - _Chore->_M_work = CreateThreadpoolWork(_Task_scheduler_callback, _Chore, nullptr); + _CRTIMP2 int __cdecl _Schedule_chore(_Threadpool_chore* _Chore) { + _ASSERT(_Chore->_M_work == nullptr); + _ASSERT(_Chore->_M_callback != nullptr); - if (_Chore->_M_work) { - return _Reschedule_chore(_Chore); - } else { - return static_cast(GetLastError()); // LastError won't be 0 when it's in error state - } + _Chore->_M_work = CreateThreadpoolWork(_Task_scheduler_callback, _Chore, nullptr); + + if (_Chore->_M_work) { + return _Reschedule_chore(_Chore); + } else { + return static_cast(GetLastError()); // LastError won't be 0 when it's in error state } - } // namespace details -} // namespace Concurrency + } +} // namespace Concurrency::details diff --git a/stl/src/vector_algorithms.cpp b/stl/src/vector_algorithms.cpp index 507a0fa8547..fb10f8de433 100644 --- a/stl/src/vector_algorithms.cpp +++ b/stl/src/vector_algorithms.cpp @@ -313,7 +313,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_1( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 5 << 5); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~31); do { _Advance_bytes(_Last, -32); const __m256i _Block = _mm256_loadu_si256(static_cast(_Last)); @@ -327,7 +327,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_1( if (_Byte_length(_First, _Last) >= 16 && _Use_sse42()) { const __m128i _Reverse_char_sse = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 4 << 4); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~15); do { _Advance_bytes(_Last, -16); const __m128i _Block = _mm_loadu_si128(static_cast(_Last)); @@ -348,7 +348,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_2( 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, // 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14); const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 5 << 5); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~31); do { _Advance_bytes(_Last, -32); const __m256i _Block = _mm256_loadu_si256(static_cast(_Last)); @@ -362,7 +362,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_2( if (_Byte_length(_First, _Last) >= 16 && _Use_sse42()) { const __m128i _Reverse_short_sse = _mm_set_epi8(1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14); const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 4 << 4); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~15); do { _Advance_bytes(_Last, -16); const __m128i _Block = _mm_loadu_si128(static_cast(_Last)); @@ -380,7 +380,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_4( const void* _First, const void* _Last, void* _Dest) noexcept { if (_Byte_length(_First, _Last) >= 32 && _Use_avx2()) { const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 5 << 5); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~31); const __m256i _Shuf = _mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7); do { _Advance_bytes(_Last, -32); @@ -393,7 +393,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_4( if (_Byte_length(_First, _Last) >= 16 && _Use_sse2()) { const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 4 << 4); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~15); do { _Advance_bytes(_Last, -16); const __m128i _Block = _mm_loadu_si128(static_cast(_Last)); @@ -411,7 +411,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_8( const void* _First, const void* _Last, void* _Dest) noexcept { if (_Byte_length(_First, _Last) >= 32 && _Use_avx2()) { const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 5 << 5); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~31); do { _Advance_bytes(_Last, -32); const __m256i _Block = _mm256_loadu_si256(static_cast(_Last)); @@ -423,7 +423,7 @@ __declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_8( if (_Byte_length(_First, _Last) >= 16 && _Use_sse2()) { const void* _Stop_at = _Dest; - _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) >> 4 << 4); + _Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~15); do { _Advance_bytes(_Last, -16); const __m128i _Block = _mm_loadu_si128(static_cast(_Last)); diff --git a/stl/src/xdateord.cpp b/stl/src/xdateord.cpp index d21d53858d3..95b77ac67b1 100644 --- a/stl/src/xdateord.cpp +++ b/stl/src/xdateord.cpp @@ -11,7 +11,7 @@ _EXTERN_C_UNLESS_PURE _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL _Getdateorder() { // return date order for current locale wchar_t buf[2] = {0}; - GetLocaleInfoEx(___lc_locale_name_func()[LC_TIME], LOCALE_ILDATE, buf, sizeof(buf) / sizeof(buf[0])); + GetLocaleInfoEx(___lc_locale_name_func()[LC_TIME], LOCALE_ILDATE, buf, static_cast(std::size(buf))); switch (buf[0]) { case L'0': diff --git a/stl/src/xdint.cpp b/stl/src/xdint.cpp index 677f8d597bc..0a83efdb94b 100644 --- a/stl/src/xdint.cpp +++ b/stl/src/xdint.cpp @@ -33,7 +33,7 @@ short _Dint(double* px, short xexp) { // test and drop (scaled) fraction bits 0x000f, 0x001f, 0x003f, 0x007f, // 0x00ff, 0x01ff, 0x03ff, 0x07ff, // 0x0fff, 0x1fff, 0x3fff, 0x7fff}; - static const size_t sub[] = {_D3, _D2, _D1, _D0}; + static constexpr size_t sub[] = {_D3, _D2, _D1, _D0}; unsigned short frac = mask[xchar & 0xf]; xchar >>= 4; @@ -49,8 +49,9 @@ short _Dint(double* px, short xexp) { // test and drop (scaled) fraction bits case 1: frac |= ps->_Sh[_D3]; ps->_Sh[_D3] = 0; + default: + return frac == 0 ? 0 : _FINITE; } - return frac != 0 ? _FINITE : 0; } } diff --git a/stl/src/xdscale.cpp b/stl/src/xdscale.cpp index f6a6702dee2..4445316992d 100644 --- a/stl/src/xdscale.cpp +++ b/stl/src/xdscale.cpp @@ -37,7 +37,7 @@ short _Dscale(double* px, long lexp) { // scale *px by 2^xexp with checking ps->_Sh[_D3] = 0; return 0; } else { // nonzero, align fraction - short xexp = static_cast(lexp); + int xexp = static_cast(lexp); unsigned short psx = 0; for (; xexp <= -16; xexp += 16) { // scale by words @@ -47,7 +47,8 @@ short _Dscale(double* px, long lexp) { // scale *px by 2^xexp with checking ps->_Sh[_D1] = ps->_Sh[_D0]; ps->_Sh[_D0] = 0; } - if ((xexp = static_cast(-xexp)) != 0) { // scale by bits + if (xexp != 0) { // scale by bits + xexp = -xexp; psx = (ps->_Sh[_D3] << (16 - xexp)) | (psx != 0 ? 1 : 0); ps->_Sh[_D3] = static_cast(ps->_Sh[_D3] >> xexp | ps->_Sh[_D2] << (16 - xexp)); ps->_Sh[_D2] = static_cast(ps->_Sh[_D2] >> xexp | ps->_Sh[_D1] << (16 - xexp)); diff --git a/stl/src/xdtest.cpp b/stl/src/xdtest.cpp index 4886b0fadee..b5ec78c2ba5 100644 --- a/stl/src/xdtest.cpp +++ b/stl/src/xdtest.cpp @@ -11,9 +11,8 @@ _CRTIMP2_PURE short __CLRCALL_PURE_OR_CDECL _Dtest(double* px) { // categorize * const auto ps = reinterpret_cast<_Dval*>(px); if ((ps->_Sh[_D0] & _DMASK) == _DMAX << _DOFF) { - return static_cast( - (ps->_Sh[_D0] & _DFRAC) != 0 || ps->_Sh[_D1] != 0 || ps->_Sh[_D2] != 0 || ps->_Sh[_D3] != 0 ? _NANCODE - : _INFCODE); + return (ps->_Sh[_D0] & _DFRAC) != 0 || ps->_Sh[_D1] != 0 || ps->_Sh[_D2] != 0 || ps->_Sh[_D3] != 0 ? _NANCODE + : _INFCODE; } else if ((ps->_Sh[_D0] & ~_DSIGN) != 0 || ps->_Sh[_D1] != 0 || ps->_Sh[_D2] != 0 || ps->_Sh[_D3] != 0) { return (ps->_Sh[_D0] & _DMASK) == 0 ? _DENORM : _FINITE; } else { diff --git a/stl/src/xexp.cpp b/stl/src/xexp.cpp index d9c53d96bef..7c9918de77f 100644 --- a/stl/src/xexp.cpp +++ b/stl/src/xexp.cpp @@ -8,12 +8,12 @@ _EXTERN_C_UNLESS_PURE // coefficients -static const double p[] = {1.0, 420.30235984910635, 15132.70094680474802}; -static const double q[] = {30.01511290683317, 3362.72154416553028, 30265.40189360949691}; -static const double c1 = 22713.0 / 32768.0; -static const double c2 = 1.4286068203094172321214581765680755e-6; -static const double hugexp = HUGE_EXP; -static const double invln2 = 1.4426950408889634073599246810018921; +static constexpr double p[] = {1.0, 420.30235984910635, 15132.70094680474802}; +static constexpr double q[] = {30.01511290683317, 3362.72154416553028, 30265.40189360949691}; +static constexpr double c1 = 22713.0 / 32768.0; +static constexpr double c2 = 1.4286068203094172321214581765680755e-6; +static constexpr double hugexp = HUGE_EXP; +static constexpr double invln2 = 1.4426950408889634073599246810018921; _CRTIMP2_PURE short __CLRCALL_PURE_OR_CDECL _Exp( double* px, double y, short eoff) { // compute y * e^(*px), (*px) finite, |y| not huge diff --git a/stl/src/xfdint.cpp b/stl/src/xfdint.cpp index d9966ff287b..9c91579790d 100644 --- a/stl/src/xfdint.cpp +++ b/stl/src/xfdint.cpp @@ -30,7 +30,7 @@ short _FDint(float* px, short xexp) { // test and drop (scaled) fraction bits 0x000f, 0x001f, 0x003f, 0x007f, // 0x00ff, 0x01ff, 0x03ff, 0x07ff, // 0x0fff, 0x1fff, 0x3fff, 0x7fff}; - static const size_t sub[] = {_F1, _F0}; + static constexpr size_t sub[] = {_F1, _F0}; unsigned short frac = mask[xchar & 0xf]; xchar >>= 4; diff --git a/stl/src/xfdnorm.cpp b/stl/src/xfdnorm.cpp index c9f8322cc83..46f7e287ebd 100644 --- a/stl/src/xfdnorm.cpp +++ b/stl/src/xfdnorm.cpp @@ -8,10 +8,9 @@ _EXTERN_C_UNLESS_PURE short _FDnorm(_Fval* ps) { // normalize float fraction - short xchar; + short xchar = 1; unsigned short sign = static_cast(ps->_Sh[_F0] & _FSIGN); - xchar = 1; if ((ps->_Sh[_F0] &= _FFRAC) != 0 || ps->_Sh[_F1]) { // nonzero, scale if (ps->_Sh[_F0] == 0) { ps->_Sh[_F0] = ps->_Sh[_F1]; diff --git a/stl/src/xfexp.cpp b/stl/src/xfexp.cpp index 221138a9789..86cbd706573 100644 --- a/stl/src/xfexp.cpp +++ b/stl/src/xfexp.cpp @@ -7,12 +7,12 @@ _EXTERN_C_UNLESS_PURE -static const float p[] = {1.0F, 60.09114349F}; -static const float q[] = {12.01517514F, 120.18228722F}; -static const float c1 = (22713.0F / 32768.0F); -static const float c2 = 1.4286068203094172321214581765680755e-6F; -static const float hugexp = FHUGE_EXP; -static const float invln2 = 1.4426950408889634073599246810018921F; +static constexpr float p[] = {1.0F, 60.09114349F}; +static constexpr float q[] = {12.01517514F, 120.18228722F}; +static constexpr float c1 = (22713.0F / 32768.0F); +static constexpr float c2 = 1.4286068203094172321214581765680755e-6F; +static constexpr float hugexp = FHUGE_EXP; +static constexpr float invln2 = 1.4426950408889634073599246810018921F; _CRTIMP2_PURE short __CLRCALL_PURE_OR_CDECL _FExp( float* px, float y, short eoff) { // compute y * e^(*px), (*px) finite, |y| not huge diff --git a/stl/src/xfsinh.cpp b/stl/src/xfsinh.cpp index 763ff1feb2a..3a38de2ed02 100644 --- a/stl/src/xfsinh.cpp +++ b/stl/src/xfsinh.cpp @@ -8,7 +8,7 @@ _EXTERN_C_UNLESS_PURE // coefficients -static const float p[] = {0.00020400F, 0.00832983F, 0.16666737F, 0.99999998F}; +static constexpr float p[] = {0.00020400F, 0.00832983F, 0.16666737F, 0.99999998F}; _CRTIMP2_PURE float __CLRCALL_PURE_OR_CDECL _FSinh(float x, float y) { // compute y * sinh(x), |y| <= 1 short neg; diff --git a/stl/src/xfvalues.cpp b/stl/src/xfvalues.cpp index d0329ee4860..a45fde7f6b6 100644 --- a/stl/src/xfvalues.cpp +++ b/stl/src/xfvalues.cpp @@ -22,11 +22,11 @@ _EXTERN_C_UNLESS_PURE // static data extern /* const */ _Dconst _FDenorm = {INIT2(0, 1)}; -extern const _Dconst _FEps = {INIT((_FBIAS - NBITS - 1) << _FOFF)}; +extern constexpr _Dconst _FEps = {INIT((_FBIAS - NBITS - 1) << _FOFF)}; extern /* const */ _Dconst _FInf = {INIT(_FMAX << _FOFF)}; extern /* const */ _Dconst _FNan = {INIT((_FMAX << _FOFF) | (1 << (_FOFF - 1)))}; extern /* const */ _Dconst _FSnan = {INIT2(_FMAX << _FOFF, 1)}; -extern const _Dconst _FRteps = {INIT((_FBIAS - NBITS / 2) << _FOFF)}; +extern constexpr _Dconst _FRteps = {INIT((_FBIAS - NBITS / 2) << _FOFF)}; extern const float _FXbig = (NBITS + 2) * 0.347f; diff --git a/stl/src/xlexp.cpp b/stl/src/xlexp.cpp index ed58e416539..5d0d0a2b37d 100644 --- a/stl/src/xlexp.cpp +++ b/stl/src/xlexp.cpp @@ -8,13 +8,13 @@ _EXTERN_C_UNLESS_PURE // coefficients -static const long double p[] = {42.038913947607355L, 10096.353102778762831L, 333228.767219512631062L}; -static const long double q[] = {1.0L, 841.167880526530790L, 75730.834075476293976L, 666457.534439025262146L}; +static constexpr long double p[] = {42.038913947607355L, 10096.353102778762831L, 333228.767219512631062L}; +static constexpr long double q[] = {1.0L, 841.167880526530790L, 75730.834075476293976L, 666457.534439025262146L}; -static const long double c1 = (22713.0L / 32768.0L); -static const long double c2 = 1.4286068203094172321214581765680755e-6L; -static const long double hugexp = LHUGE_EXP; -static const long double invln2 = 1.4426950408889634073599246810018921L; +static constexpr long double c1 = (22713.0L / 32768.0L); +static constexpr long double c2 = 1.4286068203094172321214581765680755e-6L; +static constexpr long double hugexp = LHUGE_EXP; +static constexpr long double invln2 = 1.4426950408889634073599246810018921L; _CRTIMP2_PURE short __CLRCALL_PURE_OR_CDECL _LExp( long double* px, long double y, short eoff) { // compute y * e^(*px), (*px) finite, |y| not huge diff --git a/stl/src/xlgamma.cpp b/stl/src/xlgamma.cpp index 9805efc9e0d..72296a76560 100644 --- a/stl/src/xlgamma.cpp +++ b/stl/src/xlgamma.cpp @@ -14,7 +14,7 @@ _CRTIMP2_PURE double __CLRCALL_PURE_OR_CDECL _XLgamma(double); _CRTIMP2_PURE long double __CLRCALL_PURE_OR_CDECL _XLgamma(long double); float __CLRCALL_PURE_OR_CDECL _XLgamma(float x) { // moderately accurate log gamma - static const float coeff[6] = {76.18009172947146F, -86.50532032941677F, 24.01409824083091F, -1.23173972450155F, + static constexpr float coeff[6] = {76.18009172947146F, -86.50532032941677F, 24.01409824083091F, -1.23173972450155F, 0.1208650973866179E-2F, -0.5395239384953E-5F}; float val0 = x + 5.5F; @@ -22,7 +22,7 @@ float __CLRCALL_PURE_OR_CDECL _XLgamma(float x) { // moderately accurate log gam float val1 = 1.000000000190015F; float y = x + 1.0F; - for (int i = 0; i < 6; ++i, y += 1.0) { + for (unsigned int i = 0; i < 6; ++i, y += 1.0) { val1 += coeff[i] / y; } @@ -30,7 +30,7 @@ float __CLRCALL_PURE_OR_CDECL _XLgamma(float x) { // moderately accurate log gam } double __CLRCALL_PURE_OR_CDECL _XLgamma(double x) { // moderately accurate log gamma - static const double coeff[6] = {76.18009172947146, -86.50532032941677, 24.01409824083091, -1.23173972450155, + static constexpr double coeff[6] = {76.18009172947146, -86.50532032941677, 24.01409824083091, -1.23173972450155, 0.1208650973866179E-2, -0.5395239384953E-5}; double val0 = x + 5.5; @@ -38,7 +38,7 @@ double __CLRCALL_PURE_OR_CDECL _XLgamma(double x) { // moderately accurate log g double val1 = 1.000000000190015; double y = x + 1; - for (int i = 0; i < 6; ++i, y += 1.0) { + for (unsigned int i = 0; i < 6; ++i, y += 1.0) { val1 += coeff[i] / y; } @@ -46,15 +46,15 @@ double __CLRCALL_PURE_OR_CDECL _XLgamma(double x) { // moderately accurate log g } long double __CLRCALL_PURE_OR_CDECL _XLgamma(long double x) { // moderately accurate log gamma - static const long double coeff[6] = {76.18009172947146, -86.50532032941677, 24.01409824083091, -1.23173972450155, - 0.1208650973866179E-2, -0.5395239384953E-5}; + static constexpr long double coeff[6] = {76.18009172947146, -86.50532032941677, 24.01409824083091, + -1.23173972450155, 0.1208650973866179E-2, -0.5395239384953E-5}; long double val0 = x + 5.5; val0 -= (x + 0.5) * _STD log(val0); long double val1 = 1.000000000190015; long double y = x + 1; - for (int i = 0; i < 6; ++i, y += 1.0) { + for (unsigned int i = 0; i < 6; ++i, y += 1.0) { val1 += coeff[i] / y; } diff --git a/stl/src/xlsinh.cpp b/stl/src/xlsinh.cpp index 2b53f056ee8..fa4d8d1ea80 100644 --- a/stl/src/xlsinh.cpp +++ b/stl/src/xlsinh.cpp @@ -8,11 +8,11 @@ _EXTERN_C_UNLESS_PURE // coefficients -static const long double p[] = {0.0000000000000028486835L, 0.0000000000007646464279L, 0.0000000001605905091647L, +static constexpr long double p[] = {0.0000000000000028486835L, 0.0000000000007646464279L, 0.0000000001605905091647L, 0.0000000250521083436962L, 0.0000027557319224130455L, 0.0001984126984126956009L, 0.0083333333333333336073L, 0.1666666666666666666564L, 1.0000000000000000000001L}; -static constexpr size_t NP = sizeof(p) / sizeof(p[0]) - 1; +static constexpr size_t NP = std::size(p) - 1; _CRTIMP2_PURE long double __CLRCALL_PURE_OR_CDECL _LSinh(long double x, long double y) { // compute y * sinh(x), |y| <= 1 @@ -40,7 +40,7 @@ _CRTIMP2_PURE long double __CLRCALL_PURE_OR_CDECL _LSinh(long double x, long dou if (x < _LRteps._Long_double) { x *= y; // x tiny } else if (x < 1.0L) { - long double w = x * x; + const long double w = x * x; x += x * w * _LPoly(w, p, NP - 1); x *= y; diff --git a/stl/src/xlvalues.cpp b/stl/src/xlvalues.cpp index 7ec133738c2..df3134e2141 100644 --- a/stl/src/xlvalues.cpp +++ b/stl/src/xlvalues.cpp @@ -20,10 +20,10 @@ // static data extern /* const */ _Dconst _LDenorm = {INIT2(0, 1)}; -extern const _Dconst _LEps = {INIT((_DBIAS - NBITS - 1) << _DOFF)}; +extern constexpr _Dconst _LEps = {INIT((_DBIAS - NBITS - 1) << _DOFF)}; extern /* const */ _Dconst _LInf = {INIT(_DMAX << _DOFF)}; extern /* const */ _Dconst _LNan = {INIT((_DMAX << _DOFF) | (1 << (_DOFF - 1)))}; extern /* const */ _Dconst _LSnan = {INIT2(_DMAX << _DOFF, 1)}; -extern const _Dconst _LRteps = {INIT((_DBIAS - NBITS / 2) << _DOFF)}; +extern constexpr _Dconst _LRteps = {INIT((_DBIAS - NBITS / 2) << _DOFF)}; extern const long double _LXbig = (NBITS + 2) * 0.347L; diff --git a/stl/src/xmbtowc.cpp b/stl/src/xmbtowc.cpp index 6b8068f37d3..b071a35705a 100644 --- a/stl/src/xmbtowc.cpp +++ b/stl/src/xmbtowc.cpp @@ -66,7 +66,7 @@ static int _Decode_utf8_trailing_byte(unsigned long* partialCh, unsigned char ch // -2 (if partial conversion) // number of bytes comprising converted mbc _MRTIMP2 _Success_(return >= 0) int __cdecl _Mbrtowc( - _When_(n != 0, _Out_) wchar_t* pwc, const char* s, size_t n, mbstate_t* pst, const _Cvtvec* ploc) { + _When_(n != 0, _Out_) wchar_t* pwc, const char* s, size_t n, const mbstate_t* pst, const _Cvtvec* ploc) { (void) pst; if (n == 0) { // indicate do not have state-dependent encodings, handle zero length string return 0; diff --git a/stl/src/xnotify.cpp b/stl/src/xnotify.cpp index 64a513855d9..88c3c04c045 100644 --- a/stl/src/xnotify.cpp +++ b/stl/src/xnotify.cpp @@ -46,12 +46,12 @@ void _Cnd_register_at_thread_exit( block = block->next; } else { // found block with available space - for (int i = 0; i < _Nitems; ++i) { // find empty slot - if (block->data[i].mtx == nullptr) { // store into empty slot - block->data[i].id._Id = GetCurrentThreadId(); - block->data[i].mtx = mtx; - block->data[i].cnd = cnd; - block->data[i].res = p; + for (auto& i : block->data) { // find empty slot + if (i.mtx == nullptr) { // store into empty slot + i.id._Id = GetCurrentThreadId(); + i.mtx = mtx; + i.cnd = cnd; + i.res = p; ++block->num_used; break; } @@ -68,7 +68,7 @@ void _Cnd_unregister_at_thread_exit(_Mtx_t mtx) { // unregister condition variab _Lock_at_thread_exit_mutex(); while (block != nullptr) { // loop through list of blocks - for (int i = 0; block->num_used != 0 && i < _Nitems; ++i) { + for (unsigned int i = 0; block->num_used != 0 && i < _Nitems; ++i) { if (block->data[i].mtx == mtx) { // release slot block->data[i].mtx = nullptr; --block->num_used; @@ -87,7 +87,7 @@ void _Cnd_do_broadcast_at_thread_exit() { // notify condition variables waiting _Lock_at_thread_exit_mutex(); while (block != nullptr) { // loop through list of blocks - for (int i = 0; block->num_used != 0 && i < _Nitems; ++i) { + for (unsigned int i = 0; block->num_used != 0 && i < _Nitems; ++i) { if (block->data[i].mtx != nullptr && block->data[i].id._Id == currentThreadId) { // notify and release slot if (block->data[i].res) { *block->data[i].res = 1; diff --git a/stl/src/xsinh.cpp b/stl/src/xsinh.cpp index 5f0e127b7d7..333b6d279de 100644 --- a/stl/src/xsinh.cpp +++ b/stl/src/xsinh.cpp @@ -11,7 +11,7 @@ _EXTERN_C_UNLESS_PURE static constexpr double p[] = {0.0000000001632881, 0.0000000250483893, 0.0000027557344615, 0.0001984126975233, 0.0083333333334816, 0.1666666666666574, 1.0000000000000001}; -static constexpr size_t NP = sizeof(p) / sizeof(p[0]) - 1; +static constexpr size_t NP = std::size(p) - 1; _CRTIMP2_PURE double __CLRCALL_PURE_OR_CDECL _Sinh(double x, double y) { // compute y * sinh(x), |y| <= 1 short neg; diff --git a/stl/src/xstoul.cpp b/stl/src/xstoul.cpp index 9dd8385b8d5..3cf2152d6fc 100644 --- a/stl/src/xstoul.cpp +++ b/stl/src/xstoul.cpp @@ -17,7 +17,7 @@ _EXTERN_C_UNLESS_PURE constexpr int _Base_max = 36; // largest valid base // static data -static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; // valid digits +static constexpr char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; // valid digits // 32-bits! static const char ndigs[_Base_max + 1] = {0, 0, 33, 21, 17, 14, 13, 12, 11, 11, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, diff --git a/stl/src/xstoull.cpp b/stl/src/xstoull.cpp index 3deaf903e6e..0fd3c83de24 100644 --- a/stl/src/xstoull.cpp +++ b/stl/src/xstoull.cpp @@ -17,7 +17,7 @@ _EXTERN_C_UNLESS_PURE constexpr int _Base_max = 36; // largest valid base // static data -static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; // valid digits +static constexpr char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; // valid digits // 64-bits! static const char ndigs[_Base_max + 1] = {0, 0, 65, 41, 33, 28, 25, 23, 22, 21, 20, 19, 18, 18, 17, 17, 17, 16, 16, 16, diff --git a/stl/src/xstoxflt.cpp b/stl/src/xstoxflt.cpp index d8b2c590574..9a3d4fd0801 100644 --- a/stl/src/xstoxflt.cpp +++ b/stl/src/xstoxflt.cpp @@ -25,8 +25,8 @@ _In_range_(0, maxsig) int _Stoxflt( int word = 0; // current long word to fill const char* pd; - static const char digits[] = "0123456789abcdefABCDEF"; - static const char vals[] = {// values of hex digits + static constexpr char digits[] = "0123456789abcdefABCDEF"; + static const char vals[] = {// values of hex digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15}; maxsig *= _Ndig; // convert word count to digit count diff --git a/stl/src/xstrcoll.cpp b/stl/src/xstrcoll.cpp index 389813b2209..ad19c899e29 100644 --- a/stl/src/xstrcoll.cpp +++ b/stl/src/xstrcoll.cpp @@ -40,7 +40,7 @@ _EXTERN_C_UNLESS_PURE // errno = EINVAL _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL _Strcoll( const char* string1, const char* end1, const char* string2, const char* end2, const _Collvec* ploc) { - int ret = 0; + int ret; UINT codepage; int n1 = static_cast(end1 - string1); int n2 = static_cast(end2 - string2); diff --git a/stl/src/xstrxfrm.cpp b/stl/src/xstrxfrm.cpp index c83198306d6..7470a5777d3 100644 --- a/stl/src/xstrxfrm.cpp +++ b/stl/src/xstrxfrm.cpp @@ -53,7 +53,7 @@ _EXTERN_C_UNLESS_PURE // Non-standard: if OM/API error, return INT_MAX. _CRTIMP2_PURE size_t __CLRCALL_PURE_OR_CDECL _Strxfrm(_Out_writes_(end1 - string1) _Post_readable_size_(return ) char* string1, - _In_z_ char* end1, const char* string2, const char* end2, const _Collvec* ploc) { + _In_z_ const char* end1, const char* string2, const char* end2, const _Collvec* ploc) { size_t n1 = end1 - string1; size_t n2 = end2 - string2; size_t retval = static_cast(-1); // NON-ANSI: default if OM or API error @@ -75,8 +75,8 @@ _CRTIMP2_PURE size_t __CLRCALL_PURE_OR_CDECL _Strxfrm(_Out_writes_(end1 - string retval = n2; } else { // Inquire size of dst string in BYTES - const int dstlen = - __crtLCMapStringA(locale_name, LCMAP_SORTKEY, string2, static_cast(n2), nullptr, 0, codepage, TRUE); + const int dstlen = __crtLCMapStringA( + locale_name, LCMAP_SORTKEY, string2, static_cast(n2), nullptr, 0, static_cast(codepage), TRUE); if (dstlen != 0) { retval = dstlen; @@ -85,7 +85,7 @@ _CRTIMP2_PURE size_t __CLRCALL_PURE_OR_CDECL _Strxfrm(_Out_writes_(end1 - string if (dstlen <= static_cast(n1)) { // Map src string to dst string __crtLCMapStringA(locale_name, LCMAP_SORTKEY, string2, static_cast(n2), string1, - static_cast(n1), codepage, TRUE); + static_cast(n1), static_cast(codepage), TRUE); } } } diff --git a/stl/src/xvalues.cpp b/stl/src/xvalues.cpp index 1d9a1fb1e30..39733c3b219 100644 --- a/stl/src/xvalues.cpp +++ b/stl/src/xvalues.cpp @@ -21,11 +21,11 @@ // static data extern /* const */ _Dconst _Denorm = {INIT2(0, 1)}; -extern const _Dconst _Eps = {INIT((_DBIAS - NBITS - 1) << _DOFF)}; +extern constexpr _Dconst _Eps = {INIT((_DBIAS - NBITS - 1) << _DOFF)}; extern /* const */ _Dconst _Hugeval = {INIT(_DMAX << _DOFF)}; extern /* const */ _Dconst _Inf = {INIT(_DMAX << _DOFF)}; extern /* const */ _Dconst _Nan = {INIT((_DMAX << _DOFF) | (1 << (_DOFF - 1)))}; extern /* const */ _Dconst _Snan = {INIT2(_DMAX << _DOFF, 1)}; -extern const _Dconst _Rteps = {INIT((_DBIAS - NBITS / 2) << _DOFF)}; +extern constexpr _Dconst _Rteps = {INIT((_DBIAS - NBITS / 2) << _DOFF)}; -extern const double _Xbig = (NBITS + 2) * 0.347; +extern constexpr double _Xbig = (NBITS + 2) * 0.347; diff --git a/stl/src/xwcscoll.cpp b/stl/src/xwcscoll.cpp index 160f1110323..1de15e3303e 100644 --- a/stl/src/xwcscoll.cpp +++ b/stl/src/xwcscoll.cpp @@ -37,9 +37,9 @@ _EXTERN_C_UNLESS_PURE // errno = EINVAL _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL _Wcscoll( const wchar_t* string1, const wchar_t* end1, const wchar_t* string2, const wchar_t* end2, const _Collvec* ploc) { - int n1 = static_cast(end1 - string1); - int n2 = static_cast(end2 - string2); - int ret = 0; + int n1 = static_cast(end1 - string1); + int n2 = static_cast(end2 - string2); + int ret; const wchar_t* locale_name; if (ploc == nullptr) { diff --git a/stl/src/xwcsxfrm.cpp b/stl/src/xwcsxfrm.cpp index 185b45c502f..aa13731da78 100644 --- a/stl/src/xwcsxfrm.cpp +++ b/stl/src/xwcsxfrm.cpp @@ -46,7 +46,7 @@ _EXTERN_C_UNLESS_PURE // Non-standard: if OM/API error, return INT_MAX. _CRTIMP2_PURE size_t __CLRCALL_PURE_OR_CDECL _Wcsxfrm(_Out_writes_(end1 - string1) _Post_readable_size_(return ) wchar_t* string1, - _In_z_ wchar_t* end1, const wchar_t* string2, const wchar_t* end2, const _Collvec* ploc) { + _In_z_ const wchar_t* end1, const wchar_t* string2, const wchar_t* end2, const _Collvec* ploc) { size_t n1 = end1 - string1; size_t n2 = end2 - string2; size_t size = static_cast(-1); diff --git a/stl/src/xwctomb.cpp b/stl/src/xwctomb.cpp index e53bc0ff551..fbf69e486be 100644 --- a/stl/src/xwctomb.cpp +++ b/stl/src/xwctomb.cpp @@ -56,7 +56,7 @@ _CRTIMP2_PURE _Cvtvec __CLRCALL_PURE_OR_CDECL _Getcvt() { // get conversion info // None. _CRTIMP2_PURE _Success_(return != -1) int __CLRCALL_PURE_OR_CDECL - _Wcrtomb(_Out_ char* s, wchar_t wchar, mbstate_t* pst, const _Cvtvec* ploc) { + _Wcrtomb(_Out_ char* s, wchar_t wchar, const mbstate_t* pst, const _Cvtvec* ploc) { _CRT_UNUSED(pst); if (ploc->_Isclocale) { if (wchar > 255) { // validate high byte diff --git a/stl/src/xwstoxfl.cpp b/stl/src/xwstoxfl.cpp index 174ab1cf351..9f67078d2d1 100644 --- a/stl/src/xwstoxfl.cpp +++ b/stl/src/xwstoxfl.cpp @@ -22,13 +22,13 @@ _In_range_(0, maxsig) int _WStoxflt(const wchar_t* s0, const wchar_t* s, wchar_t char buf[_Maxsig + 1]; // worst case, with room for rounding digit int nsig = 0; // number of significant digits seen int seen = 0; // any valid field characters seen - int word = 0; // current long word to fill + int word; // current long word to fill const wchar_t* pd; - static const wchar_t digits[] = {// hex digits in both cases + static constexpr wchar_t digits[] = {// hex digits in both cases L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', L'a', L'b', L'c', L'd', L'e', L'f', L'A', L'B', L'C', L'D', L'E', L'F', L'\0'}; - static const char vals[] = {// values of hex digits + static const char vals[] = {// values of hex digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15}; maxsig *= _Ndig; // convert word count to digit count @@ -55,7 +55,7 @@ _In_range_(0, maxsig) int _WStoxflt(const wchar_t* s0, const wchar_t* s, wchar_t seen = 1; } - if (*s == localeconv()->decimal_point[0]) { + if (*s == localeconv()->_W_decimal_point[0]) { ++s; } @@ -129,7 +129,7 @@ _In_range_(0, maxsig) int _WStoxflt(const wchar_t* s0, const wchar_t* s, wchar_t } } - if (!seen) { + else { word = 0; // return zero if bad parse } diff --git a/stl/src/xxxdtent.hpp b/stl/src/xxxdtent.hpp index d4272282673..c99892d1694 100644 --- a/stl/src/xxxdtent.hpp +++ b/stl/src/xxxdtent.hpp @@ -18,7 +18,7 @@ _EXTERN_C #if FBITS == 53 #define FRAC_BITS 67108864.0L // 2^26 -static const FTYPE tenth[] = { +static constexpr FTYPE tenth[] = { // 53-bit: 0.100000 static_cast(FLIT(6710886.0) / FRAC_BITS), static_cast(FLIT(26843545.0) / FRAC_BITS_2), @@ -29,7 +29,7 @@ static const FTYPE tenth[] = { #elif FBITS == 24 #define FRAC_BITS 4096.0L // 2^12 -static const FTYPE tenth[] = { +static constexpr FTYPE tenth[] = { // 24-bit: 0.100000 static_cast(FLIT(409.0) / FRAC_BITS), static_cast(FLIT(2457.0) / FRAC_BITS_2), diff --git a/stl/src/xxxprec.hpp b/stl/src/xxxprec.hpp index dc3a4491b43..df38cd9b536 100644 --- a/stl/src/xxxprec.hpp +++ b/stl/src/xxxprec.hpp @@ -197,7 +197,7 @@ FTYPE* FNAME(Xp_addh)(FTYPE* p, int n, FTYPE x0) { // add a half-precision value FNAME(Dunscale)(&yexp, &xscaled); prevexp = yexp; } - } else if (k + 1 == n) { + } else if (k == n - 1) { break; // don't truncate bits in last word } else { // propagate any excess bits down x0 = p[k]; diff --git a/tools/validate/validate.cpp b/tools/validate/validate.cpp index 6b045808566..068b230d5c3 100644 --- a/tools/validate/validate.cpp +++ b/tools/validate/validate.cpp @@ -19,15 +19,13 @@ constexpr size_t max_line_length = 120; class BinaryFile { public: - explicit BinaryFile(const filesystem::path& filepath) { - m_file = _wfopen(filepath.c_str(), L"rb"); - + explicit BinaryFile(const filesystem::path& filepath) : m_file(_wfopen(filepath.c_str(), L"rb")) { if (!m_file) { fwprintf(stderr, L"Validation failed: %ls couldn't be opened.\n", filepath.c_str()); } } - [[nodiscard]] bool read_next_block(vector& buffer) { + [[nodiscard]] bool read_next_block(vector& buffer) const { constexpr size_t BlockSize = 65536; buffer.resize(BlockSize); @@ -191,9 +189,9 @@ void scan_file( L".py"sv, L".yml"sv, }; - static_assert(is_sorted(checked_extensions.begin(), checked_extensions.end())); + static_assert(ranges::is_sorted(checked_extensions)); - if (binary_search(checked_extensions.begin(), checked_extensions.end(), filepath.extension().wstring())) { + if (ranges::binary_search(checked_extensions, filepath.extension().wstring())) { validation_failure(any_errors, filepath, L"file contains %zu lines with more than %zu columns.\n", overlength_lines, max_line_length); } @@ -229,10 +227,10 @@ int main() { L".gitmodules"sv, }; - static_assert(is_sorted(skipped_directories.begin(), skipped_directories.end())); - static_assert(is_sorted(skipped_extensions.begin(), skipped_extensions.end())); - static_assert(is_sorted(bad_extensions.begin(), bad_extensions.end())); - static_assert(is_sorted(tabby_filenames.begin(), tabby_filenames.end())); + static_assert(ranges::is_sorted(skipped_directories)); + static_assert(ranges::is_sorted(skipped_extensions)); + static_assert(ranges::is_sorted(bad_extensions)); + static_assert(ranges::is_sorted(tabby_filenames)); vector buffer; // reused for performance bool any_errors = false; @@ -244,7 +242,7 @@ int main() { if (!rdi->is_regular_file()) { if (rdi->is_directory()) { - if (binary_search(skipped_directories.begin(), skipped_directories.end(), filename)) { + if (ranges::binary_search(skipped_directories, filename)) { rdi.disable_recursion_pending(); } } @@ -266,18 +264,17 @@ int main() { const wstring extension = filepath.extension().wstring(); - if (binary_search(skipped_extensions.begin(), skipped_extensions.end(), extension)) { + if (ranges::binary_search(skipped_extensions, extension)) { continue; } - if (binary_search(bad_extensions.begin(), bad_extensions.end(), extension)) { + if (ranges::binary_search(bad_extensions, extension)) { validation_failure(any_errors, filepath, L"file should not be checked in."); continue; } - const TabPolicy tab_policy = binary_search(tabby_filenames.begin(), tabby_filenames.end(), filename) - ? TabPolicy::Allowed - : TabPolicy::Forbidden; + const TabPolicy tab_policy = + ranges::binary_search(tabby_filenames, filename) ? TabPolicy::Allowed : TabPolicy::Forbidden; scan_file(any_errors, filepath, tab_policy, buffer); }