Skip to content

Commit d933210

Browse files
projectgusdpgeorge
authored andcommitted
py/misc: Move mp_clz and mp_ctz intrinsics into misc.h.
Signed-off-by: Angus Gratton <[email protected]>
1 parent cebc9b0 commit d933210

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

py/asmthumb.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,7 @@
3535

3636
#include "py/mpstate.h"
3737
#include "py/asmthumb.h"
38-
39-
#ifdef _MSC_VER
40-
#include <intrin.h>
41-
42-
static uint32_t mp_clz(uint32_t x) {
43-
unsigned long lz = 0;
44-
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
45-
}
46-
47-
static uint32_t mp_ctz(uint32_t x) {
48-
unsigned long tz = 0;
49-
return _BitScanForward(&tz, x) ? tz : 0;
50-
}
51-
#else
52-
#define mp_clz(x) __builtin_clz(x)
53-
#define mp_ctz(x) __builtin_ctz(x)
54-
#endif
38+
#include "py/misc.h"
5539

5640
#define UNSIGNED_FIT5(x) ((uint32_t)(x) < 32)
5741
#define UNSIGNED_FIT7(x) ((uint32_t)(x) < 128)

py/misc.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,22 @@ typedef const char *mp_rom_error_text_t;
334334
// For now, forward directly to MP_COMPRESSED_ROM_TEXT.
335335
#define MP_ERROR_TEXT(x) (mp_rom_error_text_t)MP_COMPRESSED_ROM_TEXT(x)
336336

337+
// Portable implementations of CLZ and CTZ intrinsics
338+
#ifdef _MSC_VER
339+
#include <intrin.h>
340+
341+
static uint32_t mp_clz(uint32_t x) {
342+
unsigned long lz = 0;
343+
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
344+
}
345+
346+
static uint32_t mp_ctz(uint32_t x) {
347+
unsigned long tz = 0;
348+
return _BitScanForward(&tz, x) ? tz : 0;
349+
}
350+
#else
351+
#define mp_clz(x) __builtin_clz(x)
352+
#define mp_ctz(x) __builtin_ctz(x)
353+
#endif
354+
337355
#endif // MICROPY_INCLUDED_PY_MISC_H

0 commit comments

Comments
 (0)