From 331a0dc8d866f95424d7f2519702305b783c29fb Mon Sep 17 00:00:00 2001 From: Nicola Cabiddu Date: Tue, 5 Mar 2024 15:23:51 +0000 Subject: [PATCH] please windows builder warnings + x86 --- src/realm/array_direct.hpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/realm/array_direct.hpp b/src/realm/array_direct.hpp index ab011903627..03c70d324a7 100644 --- a/src/realm/array_direct.hpp +++ b/src/realm/array_direct.hpp @@ -23,6 +23,9 @@ #include #include #include +#ifdef __WIN32 +#include +#endif // clang-format off /* wid == 16/32 likely when accessing offsets in B tree */ @@ -795,13 +798,28 @@ constexpr uint32_t inverse_width[65] = { 65536 * 64 / 61, 65536 * 64 / 62, 65536 * 64 / 63, 65536 * 64 / 64, }; -inline int first_field_marked(int width, uint64_t vector) +inline int countr_zero(uint64_t vector) { -#if REALM_WINDOWS - int lz = (int)_tzcnt_u64(vector); // TODO: not clear if this is ok on all platforms + unsigned long where; +#if defined(_WIN64) + if (_BitScanForward64(&where, vector)) + return static_cast(where); + return 64; +#elif defined(_WIN32) + if (_BitScanForward(&where, static_cast(vector))) + return static_cast(where); + if (_BitScanForward(&where, static_cast(vector >> 32))) + return static_cast(where + 32); + return 32; #else - int lz = __builtin_ctzll(vector); + where = __builtin_ctzll(vector); + return static_cast(where); #endif +} + +inline int first_field_marked(int width, uint64_t vector) +{ + const auto lz = countr_zero(vector); int field = (lz * inverse_width[width]) >> 22; REALM_ASSERT_DEBUG(field == (lz / width)); return field;