libzahl

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

zpowu.c (550B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "internals.h"
      3 
      4 #define tb  libzahl_tmp_pow_b
      5 
      6 
      7 void
      8 zpowu(z_t a, z_t b, unsigned long long int c)
      9 {
     10 	int neg;
     11 
     12 	if (unlikely(!c)) {
     13 		if (check(zzero(b)))
     14 			libzahl_failure(-ZERROR_0_POW_0);
     15 		zsetu(a, 1);
     16 		return;
     17 	} else if (unlikely(zzero(b))) {
     18 		SET_SIGNUM(a, 0);
     19 		return;
     20 	}
     21 
     22 	neg = znegative(b) && (c & 1);
     23 	zabs(tb, b);
     24 
     25 	if (c & 1)
     26 		zset(a, tb);
     27 	else
     28 		zsetu(a, 1);
     29 	while (c >>= 1) {
     30 		zsqr_ll(tb, tb);
     31 		if (c & 1)
     32 			zmul_ll(a, a, tb);
     33 	}
     34 
     35 	if (neg)
     36 		zneg(a, a);
     37 }