aboutsummaryrefslogtreecommitdiff
path: root/aberth/util.h
blob: 49195b5eee6606b6171da8716ed1667a4bcdfdbd (plain)
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
#pragma once

#include <iostream>
#include "defs.h"

using namespace std;


template <typename T>
constexpr static T clearLowestBit(T value) {
	return value & (value - 1);
}

template <typename T>
constexpr static bool ispow2(T value) {
	return clearLowestBit(value) == 0;
}

template <typename T>
constexpr static T ceil2(T value) {
	T value2 = clearLowestBit(value);
	if (value2 == 0) return value;

	while (true) {
		value = value2;
		value2 = clearLowestBit(value);
		if (value2 == 0) return value << 1;
	}
}

template <typename T>
constexpr static T ipow(T base, T ex) {
	T x = 1, y = base;
	while (ex > 0) {
		if ((ex & 1) == 1) x *= y;
		y *= y;
		ex >>= 1;
	}
	return x;
}