From 621676bd41659bc101f29dc7ac54f29c7cdaa92b Mon Sep 17 00:00:00 2001 From: Ethan-000 <70556667+Ethan-000@users.noreply.github.com> Date: Wed, 7 Aug 2024 07:37:16 +0100 Subject: [PATCH] feat: add `num_bits()` function (#570) ## Describe the changes adding a `num_bits()` function similar to https://github.com/arkworks-rs/algebra/blob/dcf73a5f9610ba9d16a3c8e0de0b3835e5e5d5e4/ff/src/biginteger/mod.rs#L482 this could be useful for small field optimizations ## Linked Issues Resolves # --- icicle/include/fields/field.cuh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/icicle/include/fields/field.cuh b/icicle/include/fields/field.cuh index 6fe3e069..5fddbc44 100644 --- a/icicle/include/fields/field.cuh +++ b/icicle/include/fields/field.cuh @@ -124,6 +124,19 @@ public: */ static constexpr HOST_DEVICE_INLINE unsigned num_of_reductions() { return CONFIG::num_of_reductions; } + // count number of bits of the field element without leading zeros. + static constexpr HOST_DEVICE_INLINE unsigned num_bits(const Field& x) + { + size_t size = sizeof(x.limbs_storage.limbs[0]) * 8; + unsigned ret = size * TLC; + for (unsigned i = TLC; i-- > 0;) { + int leading = __clz(x.limbs_storage.limbs[i]); + ret -= leading; + if (leading != size) { break; } + } + return ret; + } + static constexpr unsigned slack_bits = 32 * TLC - NBITS; struct Wide {