summaryrefslogtreecommitdiff
path: root/py/misc.h
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2024-04-16 17:07:05 +1000
committerDamien George <damien@micropython.org>2024-06-24 14:06:54 +1000
commitd933210d960a6f9337e85753e9568619bbfd54ec (patch)
tree170494d79ff403281ccf997ff174f9aab23d7ed4 /py/misc.h
parentcebc9b0ae2b12c61eac39a3c599edb3b1b65dd54 (diff)
py/misc: Move mp_clz and mp_ctz intrinsics into misc.h.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Diffstat (limited to 'py/misc.h')
-rw-r--r--py/misc.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/py/misc.h b/py/misc.h
index eea3e8b0f..9f8a8c1e1 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -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