OpenGM  2.3.x
Discrete Graphical Model Library
unsigned_integer_pow.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_UNSIGNED_INTEGER_POW_HXX_
2 #define OPENGM_UNSIGNED_INTEGER_POW_HXX_
3 
4 namespace opengm {
5 
6 size_t unsignedIntegerPow(size_t base, size_t exponent) {
7  if(base == 1) {
8  return 1;
9  } else if((base == 0) && (exponent > 0)) {
10  return 0;
11  } else {
12  // exponentiation by squaring
13  size_t result = 1;
14  while(exponent) {
15  // check if exponent is odd
16  if(exponent % 2 == 1) {
17  result *= base;
18  }
19 
20  // halve exponent
21  exponent /= 2;
22 
23  // square base
24  base *= base;
25  }
26  return result;
27  }
28 }
29 
30 } // namespace opengm
31 
50 #endif /* OPENGM_UNSIGNED_INTEGER_POW_HXX_ */
The OpenGM namespace.
Definition: config.hxx:43
size_t unsignedIntegerPow(size_t base, size_t exponent)
Unsigned integer power function.