Arty
common.h
1 #ifndef COMMON_H
2 #define COMMON_H
3 
4 #include <iostream>
5 #include <cstdlib>
6 #include <cstdint>
7 #include <random>
8 
9 #define UNUSED(x) (void)(x)
10 
11 #if defined(__GNUC__) || defined(__clang__)
12 #define likely(x) (__builtin_expect((x), true))
13 #define unlikely(x) (__builtin_expect((x), false))
14 #else
15 #define likely(x) (x)
16 #define unlikely(x) (x)
17 #endif
18 
19 static constexpr float pi = 3.14159265359f;
20 
22 inline float radians(float x) {
23  return x * pi / 180.0f;
24 }
25 
27 inline float degrees(float x) {
28  return x * 180.0f / pi;
29 }
30 
32 template <typename T>
33 inline T clamp(T a, T b, T c) {
34  return (a < b) ? b : ((a > c) ? c : a);
35 }
36 
38 template <typename T>
39 inline T closest_log2(T i) {
40  T p = 1, q = 0;
41  while (i > p) p <<= 1, q++;
42  return q;
43 }
44 
46 inline int32_t float_as_int(float f) {
47  union { float vf; int32_t vi; } v;
48  v.vf = f;
49  return v.vi;
50 }
51 
53 inline float int_as_float(int32_t i) {
54  union { float vf; int32_t vi; } v;
55  v.vi = i;
56  return v.vf;
57 }
58 
60 inline float prodsign(float x, float y) {
61  return int_as_float(float_as_int(x) ^ (float_as_int(y) & 0x80000000));
62 }
63 
65 template <typename T, typename U>
66 T lerp(T a, T b, U u) {
67  return a * (1 - u) + b * u;
68 }
69 
71 template <typename T, typename U>
72 T lerp(T a, T b, T c, U u, U v) {
73  return a * (1 - u - v) + b * u + c * v;
74 }
75 
77 template <typename T>
78 T reflect(T v, T n) {
79  return (2 * dot(n, v)) * n - v;
80 }
81 
82 inline void error() {
83  std::cerr << std::endl;
84 }
85 
87 template <typename T, typename... Args>
88 inline void error(T t, Args... args) {
89 #if COLORIZE_LOG
90  std::cerr << "\033[1;31m";
91 #endif
92  std::cerr << t;
93 #if COLORIZE_LOG
94  std::cerr << "\033[0m";
95 #endif
96  error(args...);
97 }
98 
99 inline void info() {
100  std::cout << std::endl;
101 }
102 
104 template <typename T, typename... Args>
105 inline void info(T t, Args... args) {
106  std::cout << t;
107  info(args...);
108 }
109 
110 inline void warn() {
111  std::clog << std::endl;
112 }
113 
115 template <typename T, typename... Args>
116 inline void warn(T t, Args... args) {
117 #if COLORIZE_LOG
118  std::clog << "\033[1;33m";
119 #endif
120  std::clog << t;
121 #if COLORIZE_LOG
122  std::clog << "\033[0m";
123 #endif
124  warn(args...);
125 }
126 
127 #define assert_normalized(x) check_normalized(x, __FILE__, __LINE__)
128 
129 template <typename T>
130 inline void check_normalized(const T& n, const char* file, size_t line) {
131 #ifdef CHECK_NORMALS
132  const float len = length(n);
133  const float tolerance = 0.001f;
134  if (len < 1.0f - tolerance || len > 1.0f + tolerance) {
135  error("Vector not normalized in \'", file, "\', line ", line);
136  abort();
137  }
138 #endif
139 }
140 
142 template <typename T>
143 struct Atom {
144  T value;
145  Atom(const T& t) : value(t) {}
146 };
147 
149 template <typename T>
150 Atom<T> atomically(const T& t) {
151  return Atom<T>(t);
152 }
153 
154 #endif // COMMON_H
Class that represents a value that has to be treated atomically.
Definition: common.h:143