-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipow.cc
46 lines (39 loc) · 992 Bytes
/
ipow.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdint>
// Print 128 bit long integers. Not very fast. Ignore formatting
std::ostream& operator<<(std::ostream& os, __uint128_t x) {
static constexpr int buflen = 40;
char buf[buflen];
char* ptr = &buf[buflen - 1];
*ptr = '\0';
do {
--ptr;
*ptr = ((char)(x % 10 + '0'));
x /= 10;
} while(x > 0);
return os << ptr;
}
template<typename T>
// constexpr typename std::enable_if<std::is_integral<T>::value, T>::type
T
ipow(T base, unsigned int exp) {
T result = 1;
for(;;) {
std::cout << exp << ' ' << base << ' ' << result << '\n';
if(exp & 1) result *= base;
exp >>= 1;
if(!exp) break;
base *= base;
}
return result;
}
int main(int argc, char* argv[]) {
uint64_t a64 = 4;
__uint128_t a128 = 4;
int exp = 63;
std::cout << a64 << "**" << exp << '\n'
<< ipow(a64, exp) << '\n'
<< a128 << "**" << exp << '\n'
<< ipow(a128, exp) << std::endl;
return 0;
}