-
Notifications
You must be signed in to change notification settings - Fork 52
/
Utility.h
54 lines (38 loc) · 1.36 KB
/
Utility.h
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
47
48
49
50
51
52
53
54
/// @author Johannes de Fine Licht (definelicht@inf.ethz.ch)
/// @copyright This software is copyrighted under the BSD 3-Clause License.
#pragma once
#include <cmath>
#include <string>
#include <stdexcept>
namespace hlslib {
// Constexpr methods for compile-time computations
constexpr unsigned char ConstLog2(unsigned long val) {
return val == 0 ? 0 : 1 + ConstLog2(val >> 1);
}
// Integer division with ceil instead of floor
template <typename T>
constexpr T CeilDivide(T a, T b) {
return (a + b - 1) / b;
}
template <typename T>
constexpr T &min(T &a, T &b) { return (a < b) ? a : b; }
template <typename T>
constexpr T const &min(T const &a, const T &b) { return (a < b) ? a : b; }
template <typename T>
constexpr T &max(T &a, T &b) { return (a > b) ? a : b; }
template <typename T>
constexpr T const &max(T const &a, const T &b) { return (a > b) ? a : b; }
constexpr signed long abs(const signed long a) { return (a < 0) ? -a : a; }
#ifndef HLSLIB_SYNTHESIS
inline void SetEnvironmentVariable(std::string const &key,
std::string const &val) {
const auto ret = setenv(key.c_str(), val.c_str(), 1);
if (ret != 0) {
throw std::runtime_error("Failed to set environment variable " + key);
}
}
inline void UnsetEnvironmentVariable(std::string const &key) {
unsetenv(key.c_str());
}
#endif
} // End namespace hlslib