libtommath/mp_clamp.c

28 lines
665 B
C
Raw Normal View History

#include "tommath_private.h"
#ifdef MP_CLAMP_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2003-02-28 16:08:34 +00:00
2017-08-30 03:51:11 +00:00
/* trim unused digits
2003-02-28 16:09:08 +00:00
*
* This is used to ensure that leading zero digits are
* trimed and the leading "used" digit will be non-zero
* Typically very fast. Also fixes the sign if there
* are no more leading digits
*/
2017-08-30 17:07:12 +00:00
void mp_clamp(mp_int *a)
2003-02-28 16:08:34 +00:00
{
2017-08-29 20:23:48 +00:00
/* decrease used while the most significant digit is
* zero.
*/
2017-10-15 17:57:12 +00:00
while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) {
2017-08-29 20:23:48 +00:00
--(a->used);
}
2003-08-05 01:24:44 +00:00
/* reset the sign flag if zero */
if (mp_iszero(a)) {
2017-08-29 20:23:48 +00:00
a->sign = MP_ZPOS;
}
2003-02-28 16:08:34 +00:00
}
2004-10-29 22:07:18 +00:00
#endif