Skip to content

Commit

Permalink
py/misc: Move mp_clz and mp_ctz intrinsics into misc.h.
Browse files Browse the repository at this point in the history
Signed-off-by: Angus Gratton <[email protected]>
  • Loading branch information
projectgus authored and dpgeorge committed Jun 24, 2024
1 parent cebc9b0 commit d933210
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
18 changes: 1 addition & 17 deletions py/asmthumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,7 @@

#include "py/mpstate.h"
#include "py/asmthumb.h"

#ifdef _MSC_VER
#include <intrin.h>

static uint32_t mp_clz(uint32_t x) {
unsigned long lz = 0;
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
}

static uint32_t mp_ctz(uint32_t x) {
unsigned long tz = 0;
return _BitScanForward(&tz, x) ? tz : 0;
}
#else
#define mp_clz(x) __builtin_clz(x)
#define mp_ctz(x) __builtin_ctz(x)
#endif
#include "py/misc.h"

#define UNSIGNED_FIT5(x) ((uint32_t)(x) < 32)
#define UNSIGNED_FIT7(x) ((uint32_t)(x) < 128)
Expand Down
18 changes: 18 additions & 0 deletions py/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,22 @@ typedef const char *mp_rom_error_text_t;
// For now, forward directly to MP_COMPRESSED_ROM_TEXT.
#define MP_ERROR_TEXT(x) (mp_rom_error_text_t)MP_COMPRESSED_ROM_TEXT(x)

// Portable implementations of CLZ and CTZ intrinsics
#ifdef _MSC_VER
#include <intrin.h>

static uint32_t mp_clz(uint32_t x) {
unsigned long lz = 0;
return _BitScanReverse(&lz, x) ? (sizeof(x) * 8 - 1) - lz : 0;
}

static uint32_t mp_ctz(uint32_t x) {
unsigned long tz = 0;
return _BitScanForward(&tz, x) ? tz : 0;
}
#else
#define mp_clz(x) __builtin_clz(x)
#define mp_ctz(x) __builtin_ctz(x)
#endif

#endif // MICROPY_INCLUDED_PY_MISC_H

0 comments on commit d933210

Please sign in to comment.