1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-08-08 17:42:09 +03:00

Size/perf optimisation for mbedtls_mpi_core_clz

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman
2023-04-19 17:40:28 +01:00
parent 3c3b94a31b
commit fe8a8cd100
3 changed files with 102 additions and 0 deletions

View File

@@ -309,6 +309,43 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE */
void mpi_core_clz(int lz, int tz)
{
if ((size_t) (lz + tz) >= (sizeof(mbedtls_mpi_uint) * 8)) {
// can't fit required number of leading and trailing zeros - skip test
goto exit;
}
mbedtls_mpi_uint x;
size_t expected;
// generate x with lz leading zeros and tz trailing zeros, all other bits set.
if (lz == -1) {
// special case: all zero
x = 0;
expected = sizeof(mbedtls_mpi_uint) * 8;
} else {
expected = lz;
if ((lz + tz) > 0) {
// some zero bits
uint32_t s = (sizeof(mbedtls_mpi_uint) * 8 - lz - tz);
x = ((1ULL << s) - 1) << tz;
} else {
// all bits set
x = ~0ULL;
}
}
size_t n = mbedtls_mpi_core_clz(x);
TEST_EQUAL(n, expected);
exit:
;
}
/* END_CASE */
/* BEGIN_CASE */
void mpi_core_lt_ct(char *input_X, char *input_Y, int exp_ret)
{