Skip to content

Commit

Permalink
feat: add num_bits() function (#570)
Browse files Browse the repository at this point in the history
## 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 #
  • Loading branch information
Ethan-000 authored Aug 7, 2024
1 parent badb8c5 commit 621676b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions icicle/include/fields/field.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 621676b

Please sign in to comment.