libzahl

big integer library
git clone git://git.suckless.org/libzahl
Log | Files | Refs | README | LICENSE

ztrunc.c (560B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "internals.h"
      3 
      4 
      5 void
      6 ztrunc(z_t a, z_t b, size_t bits)
      7 {
      8 	size_t chars;
      9 
     10 	if (unlikely(zzero(b))) {
     11 		SET_SIGNUM(a, 0);
     12 		return;
     13 	}
     14 
     15 	chars = CEILING_BITS_TO_CHARS(bits);
     16 	a->used = MIN(chars, b->used);
     17 	if (unlikely(a->used < chars))
     18 		bits = 0;
     19 	if (likely(a != b)) {
     20 		a->sign = b->sign;
     21 		ENSURE_SIZE(a, a->used);
     22 		zmemcpy(a->chars, b->chars, a->used);
     23 	}
     24 	bits = BITS_IN_LAST_CHAR(bits);
     25 	if (likely(bits))
     26 		a->chars[a->used - 1] &= ((zahl_char_t)1 << bits) - 1;
     27 
     28 	TRIM_AND_ZERO(a);
     29 }