Skip to content

Commit 07bf317

Browse files
stinosdpgeorge
authored andcommitted
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 908ab1c) Signed-off-by: stijn <[email protected]>
1 parent 093d0c0 commit 07bf317

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

py/misc.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,38 +338,44 @@ typedef const char *mp_rom_error_text_t;
338338
#ifdef _MSC_VER
339339
#include <intrin.h>
340340

341-
static uint32_t mp_clz(uint32_t x) {
341+
static inline uint32_t mp_clz(uint32_t x) {
342342
unsigned long lz = 0;
343343
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
344344
}
345345

346-
static uint32_t mp_clzl(unsigned long x) {
346+
static inline uint32_t mp_clzl(unsigned long x) {
347347
unsigned long lz = 0;
348348
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
349349
}
350350

351351
#ifdef _WIN64
352-
static uint32_t mp_clzll(unsigned long long x) {
352+
static inline uint32_t mp_clzll(unsigned long long x) {
353353
unsigned long lz = 0;
354354
return _BitScanReverse64(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
355355
}
356356
#else
357357
// Microsoft don't ship _BitScanReverse64 on Win32, so emulate it
358-
static uint32_t mp_clzll(unsigned long long x) {
358+
static inline uint32_t mp_clzll(unsigned long long x) {
359359
unsigned long h = x >> 32;
360360
return h ? mp_clzl(h) : (mp_clzl(x) + 32);
361361
}
362362
#endif
363363

364-
static uint32_t mp_ctz(uint32_t x) {
364+
static inline uint32_t mp_ctz(uint32_t x) {
365365
unsigned long tz = 0;
366366
return _BitScanForward(&tz, x) ? tz : 0;
367367
}
368+
369+
// Workaround for 'warning C4127: conditional expression is constant'.
370+
static inline bool mp_check(bool value) {
371+
return value;
372+
}
368373
#else
369374
#define mp_clz(x) __builtin_clz(x)
370375
#define mp_clzl(x) __builtin_clzl(x)
371376
#define mp_clzll(x) __builtin_clzll(x)
372377
#define mp_ctz(x) __builtin_ctz(x)
378+
#define mp_check(x) (x)
373379
#endif
374380

375381
// 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) {
378384
|| sizeof(mp_int_t) == sizeof(long));
379385

380386
// ugly, but should compile to single intrinsic unless O0 is set
381-
if (sizeof(mp_int_t) == sizeof(long)) {
382-
return mp_clzl(x);
387+
if (mp_check(sizeof(mp_int_t) == sizeof(long))) {
388+
return mp_clzl((unsigned long)x);
383389
} else {
384-
return mp_clzll(x);
390+
return mp_clzll((unsigned long long)x);
385391
}
386392
}
387393

0 commit comments

Comments
 (0)