From 362d965b080fbe11103f68611822e15d76379e91 Mon Sep 17 00:00:00 2001 From: victhor Date: Mon, 2 Mar 2026 18:52:00 +0100 Subject: [PATCH 1/6] Fixed issues with MSVC on windows --- Inc/MockedDrivers/compiler_specific.hpp | 68 ++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/Inc/MockedDrivers/compiler_specific.hpp b/Inc/MockedDrivers/compiler_specific.hpp index 1524c886a..f37def8a6 100644 --- a/Inc/MockedDrivers/compiler_specific.hpp +++ b/Inc/MockedDrivers/compiler_specific.hpp @@ -1,6 +1,12 @@ #pragma once #include +#if defined(_MSC_VER) +#define WIN32_LEAN_AND_MEAN +#include /* _BitScanForward, _BitScanReverse */ +#include /* _byteswap_ulong */ +#endif + /* * This file contains implementations or alternate names for * ARM GCC intrinsics that don't work on x86_64 / host platforms. @@ -38,7 +44,67 @@ static inline uint32_t __RBIT(uint32_t val) { return val; } -#define __CLZ __builtin_clz +inline uint32_t __CLZ(uint32_t val) { +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_clz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanReverse(&idx, val); + return 31 - idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << (31 - i))) != 0) return i; + } + return 32; +#endif +} + +inline uint32_t __CLZ(uint64_t val) { +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_clz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanReverse64(&idx, val); + return 63 - idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << (31 - i))) != 0) return i; + } + return 32; +#endif +} + +inline uint32_t __CTZ(uint32_t val) +{ +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_ctz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanForward(&idx, val); + return idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << i)) != 0) return i; + } + return 32; +#endif +} + +inline uint32_t __CTZ(uint64_t val) +{ +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_ctz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanForward64(&idx, val); + return idx; +#else + for(uint32_t i = 0; i < 64; i++) { + if((val & (1 << i)) != 0) return i; + } + return 64; +#endif +} #if defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__) void _ReadWriteBarrier(void); From c5783f3bdb6bf48ecac3bdb7228c2b9a71d882f5 Mon Sep 17 00:00:00 2001 From: victhor Date: Mon, 2 Mar 2026 18:57:22 +0100 Subject: [PATCH 2/6] Move implementation into cpp --- CMakeLists.txt | 1 + Inc/MockedDrivers/compiler_specific.hpp | 64 ++----------------------- Src/MockedDrivers/compiler_specific.cpp | 61 +++++++++++++++++++++++ 3 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 Src/MockedDrivers/compiler_specific.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dfdc9a58..fbb38af80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -423,6 +423,7 @@ add_library(${STLIB_LIBRARY} OBJECT $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Time/Scheduler.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Models/TimerDomain/TimerDomain.cpp> + $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/compiler_specific.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/mocked_hal_adc.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/mocked_hal_dma.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/mocked_hal_spi.cpp> diff --git a/Inc/MockedDrivers/compiler_specific.hpp b/Inc/MockedDrivers/compiler_specific.hpp index f37def8a6..d0b4a9a7c 100644 --- a/Inc/MockedDrivers/compiler_specific.hpp +++ b/Inc/MockedDrivers/compiler_specific.hpp @@ -44,67 +44,11 @@ static inline uint32_t __RBIT(uint32_t val) { return val; } -inline uint32_t __CLZ(uint32_t val) { -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_clz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanReverse(&idx, val); - return 31 - idx; -#else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << (31 - i))) != 0) return i; - } - return 32; -#endif -} - -inline uint32_t __CLZ(uint64_t val) { -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_clz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanReverse64(&idx, val); - return 63 - idx; -#else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << (31 - i))) != 0) return i; - } - return 32; -#endif -} +uint32_t __CLZ(uint32_t val); +uint32_t __CLZ(uint64_t val); -inline uint32_t __CTZ(uint32_t val) -{ -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_ctz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanForward(&idx, val); - return idx; -#else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << i)) != 0) return i; - } - return 32; -#endif -} - -inline uint32_t __CTZ(uint64_t val) -{ -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_ctz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanForward64(&idx, val); - return idx; -#else - for(uint32_t i = 0; i < 64; i++) { - if((val & (1 << i)) != 0) return i; - } - return 64; -#endif -} +uint32_t __CTZ(uint32_t val); +uint32_t __CTZ(uint64_t val); #if defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__) void _ReadWriteBarrier(void); diff --git a/Src/MockedDrivers/compiler_specific.cpp b/Src/MockedDrivers/compiler_specific.cpp new file mode 100644 index 000000000..c408f03c5 --- /dev/null +++ b/Src/MockedDrivers/compiler_specific.cpp @@ -0,0 +1,61 @@ +#include "MockedDrivers/compiler_specific.hpp" + +uint32_t __CLZ(uint32_t val) { +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_clz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanReverse(&idx, val); + return 31 - idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << (31 - i))) != 0) return i; + } + return 32; +#endif +} +uint32_t __CLZ(uint64_t val) { +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_clz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanReverse64(&idx, val); + return 63 - idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << (31 - i))) != 0) return i; + } + return 32; +#endif +} + +uint32_t __CTZ(uint32_t val) +{ +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_ctz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanForward(&idx, val); + return idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << i)) != 0) return i; + } + return 32; +#endif +} +uint32_t __CTZ(uint64_t val) +{ +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_ctz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanForward64(&idx, val); + return idx; +#else + for(uint32_t i = 0; i < 64; i++) { + if((val & (1 << i)) != 0) return i; + } + return 64; +#endif +} \ No newline at end of file From cccc41f59bdf08ce5a6a8e3770687e58ddf1cca8 Mon Sep 17 00:00:00 2001 From: victhor Date: Mon, 2 Mar 2026 19:09:12 +0100 Subject: [PATCH 3/6] Return to inlining clz and ctz --- CMakeLists.txt | 1 - Inc/MockedDrivers/compiler_specific.hpp | 33 +++++++++++-- Src/MockedDrivers/compiler_specific.cpp | 61 ------------------------- 3 files changed, 29 insertions(+), 66 deletions(-) delete mode 100644 Src/MockedDrivers/compiler_specific.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fbb38af80..9dfdc9a58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -423,7 +423,6 @@ add_library(${STLIB_LIBRARY} OBJECT $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Services/Time/Scheduler.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/HALAL/Models/TimerDomain/TimerDomain.cpp> - $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/compiler_specific.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/mocked_hal_adc.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/mocked_hal_dma.cpp> $<$>:${CMAKE_CURRENT_LIST_DIR}/Src/MockedDrivers/mocked_hal_spi.cpp> diff --git a/Inc/MockedDrivers/compiler_specific.hpp b/Inc/MockedDrivers/compiler_specific.hpp index d0b4a9a7c..8e063d44d 100644 --- a/Inc/MockedDrivers/compiler_specific.hpp +++ b/Inc/MockedDrivers/compiler_specific.hpp @@ -44,11 +44,36 @@ static inline uint32_t __RBIT(uint32_t val) { return val; } -uint32_t __CLZ(uint32_t val); -uint32_t __CLZ(uint64_t val); +inline uint32_t __CLZ(uint32_t val) { +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_clz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanReverse(&idx, val); + return 31 - idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << (31 - i))) != 0) return i; + } + return 32; +#endif +} -uint32_t __CTZ(uint32_t val); -uint32_t __CTZ(uint64_t val); +inline uint32_t __CTZ(uint32_t val) +{ +#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) + return __builtin_ctz(val); +#elif defined(_MSC_VER) + DWORD idx = 0; + _BitScanForward(&idx, val); + return idx; +#else + for(uint32_t i = 0; i < 32; i++) { + if((val & (1 << i)) != 0) return i; + } + return 32; +#endif +} #if defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__) void _ReadWriteBarrier(void); diff --git a/Src/MockedDrivers/compiler_specific.cpp b/Src/MockedDrivers/compiler_specific.cpp deleted file mode 100644 index c408f03c5..000000000 --- a/Src/MockedDrivers/compiler_specific.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "MockedDrivers/compiler_specific.hpp" - -uint32_t __CLZ(uint32_t val) { -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_clz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanReverse(&idx, val); - return 31 - idx; -#else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << (31 - i))) != 0) return i; - } - return 32; -#endif -} -uint32_t __CLZ(uint64_t val) { -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_clz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanReverse64(&idx, val); - return 63 - idx; -#else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << (31 - i))) != 0) return i; - } - return 32; -#endif -} - -uint32_t __CTZ(uint32_t val) -{ -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_ctz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanForward(&idx, val); - return idx; -#else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << i)) != 0) return i; - } - return 32; -#endif -} -uint32_t __CTZ(uint64_t val) -{ -#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) - return __builtin_ctz(val); -#elif defined(_MSC_VER) - DWORD idx = 0; - _BitScanForward64(&idx, val); - return idx; -#else - for(uint32_t i = 0; i < 64; i++) { - if((val & (1 << i)) != 0) return i; - } - return 64; -#endif -} \ No newline at end of file From a3bdcebb1b326d35dad6c8883b135de9e2cf0066 Mon Sep 17 00:00:00 2001 From: victhor Date: Mon, 2 Mar 2026 19:11:26 +0100 Subject: [PATCH 4/6] fix formatting (hopefully) --- Inc/MockedDrivers/compiler_specific.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Inc/MockedDrivers/compiler_specific.hpp b/Inc/MockedDrivers/compiler_specific.hpp index 8e063d44d..a8d58e075 100644 --- a/Inc/MockedDrivers/compiler_specific.hpp +++ b/Inc/MockedDrivers/compiler_specific.hpp @@ -4,7 +4,7 @@ #if defined(_MSC_VER) #define WIN32_LEAN_AND_MEAN #include /* _BitScanForward, _BitScanReverse */ -#include /* _byteswap_ulong */ +#include /* _byteswap_ulong */ #endif /* @@ -53,7 +53,8 @@ inline uint32_t __CLZ(uint32_t val) { return 31 - idx; #else for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << (31 - i))) != 0) return i; + if((val & (1 << (31 - i))) != 0) + return i; } return 32; #endif @@ -69,7 +70,8 @@ inline uint32_t __CTZ(uint32_t val) return idx; #else for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << i)) != 0) return i; + if((val & (1 << i)) != 0) + return i; } return 32; #endif From 4abdc1fc7f18016eda1999d8eb854b9c00d701e3 Mon Sep 17 00:00:00 2001 From: victhor Date: Mon, 2 Mar 2026 19:13:02 +0100 Subject: [PATCH 5/6] more formatting fixes --- Inc/MockedDrivers/compiler_specific.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Inc/MockedDrivers/compiler_specific.hpp b/Inc/MockedDrivers/compiler_specific.hpp index a8d58e075..2787b18c0 100644 --- a/Inc/MockedDrivers/compiler_specific.hpp +++ b/Inc/MockedDrivers/compiler_specific.hpp @@ -52,8 +52,8 @@ inline uint32_t __CLZ(uint32_t val) { _BitScanReverse(&idx, val); return 31 - idx; #else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << (31 - i))) != 0) + for (uint32_t i = 0; i < 32; i++) { + if ((val & (1 << (31 - i))) != 0) return i; } return 32; @@ -69,8 +69,8 @@ inline uint32_t __CTZ(uint32_t val) _BitScanForward(&idx, val); return idx; #else - for(uint32_t i = 0; i < 32; i++) { - if((val & (1 << i)) != 0) + for (uint32_t i = 0; i < 32; i++) { + if ((val & (1 << i)) != 0) return i; } return 32; From a6b766c4d94964f0a6252b3b007ea1e5380e75b9 Mon Sep 17 00:00:00 2001 From: victhor Date: Mon, 2 Mar 2026 19:14:43 +0100 Subject: [PATCH 6/6] fix formatting final please --- Inc/MockedDrivers/compiler_specific.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Inc/MockedDrivers/compiler_specific.hpp b/Inc/MockedDrivers/compiler_specific.hpp index 2787b18c0..3e62d63c7 100644 --- a/Inc/MockedDrivers/compiler_specific.hpp +++ b/Inc/MockedDrivers/compiler_specific.hpp @@ -60,8 +60,7 @@ inline uint32_t __CLZ(uint32_t val) { #endif } -inline uint32_t __CTZ(uint32_t val) -{ +inline uint32_t __CTZ(uint32_t val) { #if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) return __builtin_ctz(val); #elif defined(_MSC_VER)