mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-31 10:20:45 +00:00
Unify mask generation functions
Generate all-bits 0 or all bits 1 mask from a value instead of from a bit. Signed-off-by: gabor-mezei-arm <gabor.mezei@arm.com>
This commit is contained in:
parent
87ac5bef97
commit
396438c57b
@ -72,9 +72,9 @@ unsigned mbedtls_cf_uint_mask( unsigned value )
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Turn a bit into a mask:
|
* Turn a value into a mask:
|
||||||
* - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
|
* - if value != 0, return the all-bits 1 mask, aka (size_t) -1
|
||||||
* - if bit == 0, return the all-bits 0 mask, aka 0
|
* - if value == 0, return the all-bits 0 mask, aka 0
|
||||||
*
|
*
|
||||||
* This function can be used to write constant-time code by replacing branches
|
* This function can be used to write constant-time code by replacing branches
|
||||||
* with bit operations using masks.
|
* with bit operations using masks.
|
||||||
@ -82,7 +82,7 @@ unsigned mbedtls_cf_uint_mask( unsigned value )
|
|||||||
* This function is implemented without using comparison operators, as those
|
* This function is implemented without using comparison operators, as those
|
||||||
* might be translated to branches by some compilers on some platforms.
|
* might be translated to branches by some compilers on some platforms.
|
||||||
*/
|
*/
|
||||||
size_t mbedtls_cf_size_mask( size_t bit )
|
size_t mbedtls_cf_size_mask( size_t value )
|
||||||
{
|
{
|
||||||
/* MSVC has a warning about unary minus on unsigned integer types,
|
/* MSVC has a warning about unary minus on unsigned integer types,
|
||||||
* but this is well-defined and precisely what we want to do here. */
|
* but this is well-defined and precisely what we want to do here. */
|
||||||
@ -90,7 +90,7 @@ size_t mbedtls_cf_size_mask( size_t bit )
|
|||||||
#pragma warning( push )
|
#pragma warning( push )
|
||||||
#pragma warning( disable : 4146 )
|
#pragma warning( disable : 4146 )
|
||||||
#endif
|
#endif
|
||||||
return -bit;
|
return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
#pragma warning( pop )
|
#pragma warning( pop )
|
||||||
#endif
|
#endif
|
||||||
|
@ -36,7 +36,7 @@ int mbedtls_cf_memcmp( const void *a,
|
|||||||
|
|
||||||
unsigned mbedtls_cf_uint_mask( unsigned value );
|
unsigned mbedtls_cf_uint_mask( unsigned value );
|
||||||
|
|
||||||
size_t mbedtls_cf_size_mask( size_t bit );
|
size_t mbedtls_cf_size_mask( size_t value );
|
||||||
|
|
||||||
size_t mbedtls_cf_size_mask_lt( size_t x,
|
size_t mbedtls_cf_size_mask_lt( size_t x,
|
||||||
size_t y );
|
size_t y );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user