Skip to content

Commit 71c30a0

Browse files
committed
msc
1 parent d7b0a0c commit 71c30a0

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

mypyc/lib-rt/int_ops.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <Python.h>
66
#include "CPy.h"
77

8+
#ifdef _MSC_VER
9+
#include <intrin.h>
10+
#endif
11+
812
#ifndef _WIN32
913
// On 64-bit Linux and macOS, ssize_t and long are both 64 bits, and
1014
// PyLong_FromLong is faster than PyLong_FromSsize_t, so use the faster one
@@ -606,7 +610,19 @@ CPyTagged CPyTagged_BitLength(CPyTagged self) {
606610
Py_ssize_t absval = val < 0 ? -val : val;
607611
int bits = 0;
608612
if (absval) {
609-
#if defined(__GNUC__) || defined(__clang__)
613+
#if defined(_MSC_VER)
614+
#if defined(_WIN64)
615+
unsigned long idx;
616+
if (_BitScanReverse64(&idx, (unsigned __int64)absval)) {
617+
bits = (int)(idx + 1);
618+
}
619+
#else
620+
unsigned long idx;
621+
if (_BitScanReverse(&idx, (unsigned long)absval)) {
622+
bits = (int)(idx + 1);
623+
}
624+
#endif
625+
#elif defined(__GNUC__) || defined(__clang__)
610626
bits = (int)(CPY_BITS - CPY_CLZ(absval));
611627
#else
612628
// Fallback to loop if no builtin

0 commit comments

Comments
 (0)