py/misc: Fix msvc and C++ compatibility.

Use explicit casts to suppress warnings about implicit conversions, add a
workaround for constant expression conditional, and make functions static
inline (as is done in the rest of the codebase) to suppress 'warning C4505:
unreferenced function with internal linkage has been removed'.

(Follow up to fix commit 908ab1ceca)

Signed-off-by: stijn <stijn@ignitron.net>
This commit is contained in:
stijn 2024-07-22 13:41:02 +02:00 committed by Damien George
parent 093d0c0a17
commit 07bf3179f6

View file

@ -338,38 +338,44 @@ typedef const char *mp_rom_error_text_t;
#ifdef _MSC_VER #ifdef _MSC_VER
#include <intrin.h> #include <intrin.h>
static uint32_t mp_clz(uint32_t x) { static inline uint32_t mp_clz(uint32_t x) {
unsigned long lz = 0; unsigned long lz = 0;
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0; return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
} }
static uint32_t mp_clzl(unsigned long x) { static inline uint32_t mp_clzl(unsigned long x) {
unsigned long lz = 0; unsigned long lz = 0;
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0; return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
} }
#ifdef _WIN64 #ifdef _WIN64
static uint32_t mp_clzll(unsigned long long x) { static inline uint32_t mp_clzll(unsigned long long x) {
unsigned long lz = 0; unsigned long lz = 0;
return _BitScanReverse64(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0; return _BitScanReverse64(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
} }
#else #else
// Microsoft don't ship _BitScanReverse64 on Win32, so emulate it // Microsoft don't ship _BitScanReverse64 on Win32, so emulate it
static uint32_t mp_clzll(unsigned long long x) { static inline uint32_t mp_clzll(unsigned long long x) {
unsigned long h = x >> 32; unsigned long h = x >> 32;
return h ? mp_clzl(h) : (mp_clzl(x) + 32); return h ? mp_clzl(h) : (mp_clzl(x) + 32);
} }
#endif #endif
static uint32_t mp_ctz(uint32_t x) { static inline uint32_t mp_ctz(uint32_t x) {
unsigned long tz = 0; unsigned long tz = 0;
return _BitScanForward(&tz, x) ? tz : 0; return _BitScanForward(&tz, x) ? tz : 0;
} }
// Workaround for 'warning C4127: conditional expression is constant'.
static inline bool mp_check(bool value) {
return value;
}
#else #else
#define mp_clz(x) __builtin_clz(x) #define mp_clz(x) __builtin_clz(x)
#define mp_clzl(x) __builtin_clzl(x) #define mp_clzl(x) __builtin_clzl(x)
#define mp_clzll(x) __builtin_clzll(x) #define mp_clzll(x) __builtin_clzll(x)
#define mp_ctz(x) __builtin_ctz(x) #define mp_ctz(x) __builtin_ctz(x)
#define mp_check(x) (x)
#endif #endif
// mp_int_t can be larger than long, i.e. Windows 64-bit, nan-box variants // mp_int_t can be larger than long, i.e. Windows 64-bit, nan-box variants
@ -378,10 +384,10 @@ static inline uint32_t mp_clz_mpi(mp_int_t x) {
|| sizeof(mp_int_t) == sizeof(long)); || sizeof(mp_int_t) == sizeof(long));
// ugly, but should compile to single intrinsic unless O0 is set // ugly, but should compile to single intrinsic unless O0 is set
if (sizeof(mp_int_t) == sizeof(long)) { if (mp_check(sizeof(mp_int_t) == sizeof(long))) {
return mp_clzl(x); return mp_clzl((unsigned long)x);
} else { } else {
return mp_clzll(x); return mp_clzll((unsigned long long)x);
} }
} }