Created
February 19, 2016 22:06
-
-
Save nandor/80cb1781155de22dedc5 to your computer and use it in GitHub Desktop.
Power
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Computes the nth power of x in a rather funny way. | |
*/ | |
template<size_t N> inline uint64_t power(int x) { | |
const auto half = power<N >> 1>(x); | |
const auto half2 = half * half; | |
return (N & 1) ? (half2 * x) : half2; | |
} | |
template<> inline uint64_t power<0>(int x) { return 1; } | |
template<> inline uint64_t power<1>(int x) { return x; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment