summaryrefslogtreecommitdiff
path: root/riscv/riscv-probe/libfemto/std/ctz.c
diff options
context:
space:
mode:
Diffstat (limited to 'riscv/riscv-probe/libfemto/std/ctz.c')
-rw-r--r--riscv/riscv-probe/libfemto/std/ctz.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/riscv/riscv-probe/libfemto/std/ctz.c b/riscv/riscv-probe/libfemto/std/ctz.c
new file mode 100644
index 0000000..098687c
--- /dev/null
+++ b/riscv/riscv-probe/libfemto/std/ctz.c
@@ -0,0 +1,22 @@
+// See LICENSE for license details.
+
+#include <stdint.h>
+
+#define GLUE_HELPER(x, y) x##y
+#define GLUE(x, y) GLUE_HELPER(x, y)
+
+#define DEFINE_CTZ(T,bits) \
+int GLUE(ctz,bits)(T val) \
+{ \
+ int n = 0; \
+ for (n = 0; n < bits; n++) { \
+ if (val & 1) break; \
+ val >>= 1; \
+ } \
+ return n; \
+}
+
+DEFINE_CTZ(int8_t,8)
+DEFINE_CTZ(int16_t,16)
+DEFINE_CTZ(int32_t,32)
+DEFINE_CTZ(int64_t,64)