diff --git a/source/lib/bits.h b/source/lib/bits.h index 094fa34496..5ef37b3519 100644 --- a/source/lib/bits.h +++ b/source/lib/bits.h @@ -122,14 +122,12 @@ inline T SetBitsTo(T num, size_t lo_idx, size_t hi_idx, size_t value) /** - * @return number of 1-bits in mask + * @return number of 1-bits in mask. + * execution time is proportional to number of 1-bits in mask. **/ template -inline size_t PopulationCount(T mask) +inline size_t SparsePopulationCount(T mask) { - // note: a more complex but probably faster method is given at - // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel - size_t num1Bits = 0; while(mask) { @@ -140,6 +138,24 @@ inline size_t PopulationCount(T mask) return num1Bits; } +/** + * @return number of 1-bits in mask. + * execution time is logarithmic in the total number of bits. + * supports up to 128-bit integers (if their arithmetic operators are defined). + * [http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel] + **/ +template +static inline size_t PopulationCount(T x) +{ + const T mask = T(~T(0)); + x -= (x >> 1) & (mask/3); // count 2 bits + x = (x & (mask/15*3)) + ((x >> 2) & (mask/15*3)); // count 4 bits + x = (x + (x >> 4)) & (mask/255*15); // count 8 bits + return (x * (mask/255)) >> ((sizeof(T)-1)*CHAR_BIT); +} + + + /** * @return whether the given number is a power of two. **/ diff --git a/source/lib/debug.cpp b/source/lib/debug.cpp index 46d1986bdb..8391c053e4 100644 --- a/source/lib/debug.cpp +++ b/source/lib/debug.cpp @@ -225,7 +225,7 @@ public: { va_list ap; va_start(ap, fmt); - const int len = vswprintf_s(m_pos, m_charsLeft, fmt, ap); + const int len = vswprintf(m_pos, m_charsLeft, fmt, ap); va_end(ap); if(len < 0) return false; diff --git a/source/lib/sysdep/os/win/wposix/waio.cpp b/source/lib/sysdep/os/win/wposix/waio.cpp index ae93ecde45..d7dc27e97f 100644 --- a/source/lib/sysdep/os/win/wposix/waio.cpp +++ b/source/lib/sysdep/os/win/wposix/waio.cpp @@ -455,7 +455,11 @@ Status waio_Preallocate(int fd, off_t size) // allocate all space up front to reduce fragmentation LARGE_INTEGER size64; size64.QuadPart = alignedSize; WARN_IF_FALSE(SetFilePointerEx(hFile, size64, 0, FILE_BEGIN)); - WARN_IF_FALSE(SetEndOfFile(hFile)); + if(!SetEndOfFile(hFile)) + { + debug_printf(L"Preallocate(%lld) failed: %d\n", size, GetLastError()); + return ERR::FAIL; // NOWARN (probably not enough disk space) + } // avoid synchronous zero-fill (see discussion in header) if(pSetFileValidData) diff --git a/source/lib/sysdep/os/win/wvm.cpp b/source/lib/sysdep/os/win/wvm.cpp index ab97bc902c..c3d0471293 100644 --- a/source/lib/sysdep/os/win/wvm.cpp +++ b/source/lib/sysdep/os/win/wvm.cpp @@ -54,7 +54,7 @@ static WUTIL_FUNC(pVirtualAllocExNuma, LPVOID, (HANDLE, LPVOID, SIZE_T, DWORD, D static DWORD WINAPI EmulateGetCurrentProcessorNumber(VOID) { const u8 apicId = x86_x64_ApicId(); - const DWORD processor = ProcessorFromApicId(apicId); + const DWORD processor = (DWORD)ProcessorFromApicId(apicId); ASSERT(processor < os_cpu_MaxProcessors); return processor; }