[crypto] Add bigint_msb_is_set() to clarify code

Add a dedicated bigint_msb_is_set() to reduce the amount of open
coding required in the common case of testing the sign of a two's
complement big integer.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2024-11-07 14:43:56 +00:00
parent e9a23a5b39
commit da6da6eb3b
3 changed files with 30 additions and 5 deletions

View File

@@ -142,6 +142,16 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
unsigned int size = bigint_size (value); \
bigint_bit_is_set_raw ( (value)->element, size, bit ); } )
/**
* Test if most significant bit is set in big integer
*
* @v value Big integer
* @ret is_set Most significant bit is set
*/
#define bigint_msb_is_set( value ) ( { \
unsigned int size = bigint_size (value); \
bigint_msb_is_set_raw ( (value)->element, size ); } )
/**
* Find highest bit set in big integer
*
@@ -358,6 +368,23 @@ bigint_bit_is_set_raw ( const bigint_element_t *value0, unsigned int size,
return ( !! ( value->element[index] & ( 1UL << subindex ) ) );
}
/**
* Test if most significant bit is set in big integer
*
* @v value0 Element 0 of big integer
* @v size Number of elements
* @ret is_set Most significant bit is set
*/
static inline __attribute__ (( always_inline )) int
bigint_msb_is_set_raw ( const bigint_element_t *value0, unsigned int size ) {
const bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( const void * ) value0 );
unsigned int index = ( size - 1 );
unsigned int subindex = ( ( 8 * sizeof ( value->element[0] ) ) - 1 );
return ( !! ( value->element[index] & ( 1UL << subindex ) ) );
}
void bigint_init_raw ( bigint_element_t *value0, unsigned int size,
const void *data, size_t len );
void bigint_done_raw ( const bigint_element_t *value0, unsigned int size,