libzahl

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

zstr_length.c (645B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "internals.h"
      3 
      4 #define num  libzahl_tmp_str_num
      5 #define mag  libzahl_tmp_str_mag
      6 #define div  libzahl_tmp_str_div
      7 
      8 
      9 size_t
     10 zstr_length(z_t a, unsigned long long int radix)
     11 {
     12 	size_t size_total = 1, size_temp;
     13 	if (check(radix < 2))
     14 		libzahl_failure(-ZERROR_INVALID_RADIX);
     15 	zset(num, a);
     16 	while (!zzero(num)) {
     17 		zsetu(mag, radix);
     18 		zset(div, mag);
     19 		size_temp = 1;
     20 		while (zcmpmag(mag, num) <= 0) {
     21 			zset(div, mag);
     22 			zsqr(mag, mag);
     23 			size_temp <<= 1;
     24 		}
     25 		size_temp >>= 1;
     26 		size_total += size_temp;
     27 		zdiv(num, num, div);
     28 	}
     29 	return size_total + (zsignum(a) < 0);
     30 }