Skip to content

Commit e3be76b

Browse files
authored
Merge pull request #6435 from SparkiDev/sp_int_count_bits_cleanup
SP int: cleanup sp_count_bits
2 parents 1218cfb + 622375b commit e3be76b

1 file changed

Lines changed: 35 additions & 48 deletions

File tree

wolfcrypt/src/sp_int.c

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5473,76 +5473,63 @@ int sp_is_bit_set(const sp_int* a, unsigned int b)
54735473
*/
54745474
int sp_count_bits(const sp_int* a)
54755475
{
5476-
int n = 0;
5476+
int n = -1;
54775477

54785478
/* Check parameter. */
5479-
if (a != NULL) {
5479+
if ((a != NULL) && (a->used > 0)) {
54805480
/* Get index of last word. */
54815481
n = (int)(a->used - 1);
54825482
/* Don't count leading zeros. */
54835483
while ((n >= 0) && (a->dp[n] == 0)) {
54845484
n--;
54855485
}
5486-
/* -1 indicates SP integer value was zero. */
5487-
if (n < 0) {
5488-
n = 0;
5489-
}
5490-
else {
5491-
#ifdef SP_ASM_HI_BIT_SET_IDX
5492-
sp_int_digit hi;
5493-
sp_int_digit d;
5486+
}
54945487

5495-
/* Get the most significant word. */
5496-
d = a->dp[n];
5497-
/* Count of bits up to last word. */
5498-
n *= SP_WORD_SIZE;
5488+
/* -1 indicates SP integer value was zero. */
5489+
if (n < 0) {
5490+
n = 0;
5491+
}
5492+
else {
5493+
/* Get the most significant word. */
5494+
sp_int_digit d = a->dp[n];
5495+
/* Count of bits up to last word. */
5496+
n *= SP_WORD_SIZE;
54995497

5498+
#ifdef SP_ASM_HI_BIT_SET_IDX
5499+
{
5500+
sp_int_digit hi;
55005501
/* Get index of highest set bit. */
55015502
SP_ASM_HI_BIT_SET_IDX(d, hi);
5502-
55035503
/* Add bits up to and including index. */
55045504
n += (int)hi + 1;
5505-
#elif defined(SP_ASM_LZCNT)
5505+
}
5506+
#elif defined(SP_ASM_LZCNT)
5507+
{
55065508
sp_int_digit lz;
5507-
sp_int_digit d;
5508-
5509-
/* Get the most significant word. */
5510-
d = a->dp[n];
5511-
/* Count of bits up to last word. */
5512-
n *= SP_WORD_SIZE;
5513-
55145509
/* Count number of leading zeros in highest non-zero digit. */
55155510
SP_ASM_LZCNT(d, lz);
5516-
55175511
/* Add non-leading zero bits count. */
55185512
n += SP_WORD_SIZE - (int)lz;
5519-
#else
5520-
sp_int_digit d;
5521-
5522-
/* Get the most significant word. */
5523-
d = a->dp[n];
5524-
/* Count of bits up to last word. */
5525-
n *= SP_WORD_SIZE;
5526-
5527-
/* Check if top word has more than half the bits set. */
5528-
if (d > SP_HALF_MAX) {
5529-
/* Set count to a full last word. */
5530-
n += SP_WORD_SIZE;
5531-
/* Don't count leading zero bits. */
5532-
while ((d & ((sp_int_digit)1 << (SP_WORD_SIZE - 1))) == 0) {
5533-
n--;
5534-
d <<= 1;
5535-
}
5513+
}
5514+
#else
5515+
/* Check if top word has more than half the bits set. */
5516+
if (d > SP_HALF_MAX) {
5517+
/* Set count to a full last word. */
5518+
n += SP_WORD_SIZE;
5519+
/* Don't count leading zero bits. */
5520+
while ((d & ((sp_int_digit)1 << (SP_WORD_SIZE - 1))) == 0) {
5521+
n--;
5522+
d <<= 1;
55365523
}
5537-
else {
5538-
/* Add to count until highest set bit is shifted out. */
5539-
while (d != 0) {
5540-
n++;
5541-
d >>= 1;
5542-
}
5524+
}
5525+
else {
5526+
/* Add to count until highest set bit is shifted out. */
5527+
while (d != 0) {
5528+
n++;
5529+
d >>= 1;
55435530
}
5544-
#endif
55455531
}
5532+
#endif
55465533
}
55475534

55485535
return n;

0 commit comments

Comments
 (0)