Skip to content

Commit b7a80d5

Browse files
authored
bpo-32436: Don't use native popcount() (also fixes bpo-32641) (#5292)
1 parent 6b273f7 commit b7a80d5

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

Python/context.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ get_token_missing(void)
11711171
int
11721172
PyContext_ClearFreeList(void)
11731173
{
1174-
int size = ctx_freelist_len;
1174+
Py_ssize_t size = ctx_freelist_len;
11751175
while (ctx_freelist_len) {
11761176
PyContext *ctx = ctx_freelist;
11771177
ctx_freelist = (PyContext *)ctx->ctx_weakreflist;

Python/hamt.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
#include "internal/pystate.h"
55
#include "internal/hamt.h"
66

7-
/* popcnt support in Visual Studio */
8-
#ifdef _MSC_VER
9-
#include <intrin.h>
10-
#endif
11-
127
/*
138
This file provides an implemention of an immutable mapping using the
149
Hash Array Mapped Trie (or HAMT) datastructure.
@@ -440,18 +435,21 @@ hamt_bitpos(int32_t hash, uint32_t shift)
440435
static inline uint32_t
441436
hamt_bitcount(uint32_t i)
442437
{
443-
#if defined(__GNUC__) && (__GNUC__ > 4)
444-
return (uint32_t)__builtin_popcountl(i);
445-
#elif defined(__clang__) && (__clang_major__ > 3)
446-
return (uint32_t)__builtin_popcountl(i);
447-
#elif defined(_MSC_VER)
448-
return (uint32_t)__popcnt(i);
449-
#else
450-
/* https://graphics.stanford.edu/~seander/bithacks.html */
438+
/* We could use native popcount instruction but that would
439+
require to either add configure flags to enable SSE4.2
440+
support or to detect it dynamically. Otherwise, we have
441+
a risk of CPython not working properly on older hardware.
442+
443+
In practice, there's no observable difference in
444+
performance between using a popcount instruction or the
445+
following fallback code.
446+
447+
The algorithm is copied from:
448+
https://graphics.stanford.edu/~seander/bithacks.html
449+
*/
451450
i = i - ((i >> 1) & 0x55555555);
452451
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
453452
return ((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
454-
#endif
455453
}
456454

457455
static inline uint32_t

0 commit comments

Comments
 (0)