+inline float cycle_shift(float value, float by = 0, float max = 1)
+{
+ if (value + by < 0) return value + by + max;
+ if (value + by > max) return value + by - max;
+ return value + by;
+}
+
+inline bool is_power_of_two(u32 n)
+{
+ return n != 0 && (n & (n - 1)) == 0;
+}
+
+// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
+// Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
+inline u32 npot2(u32 orig) {
+ orig--;
+ orig |= orig >> 1;
+ orig |= orig >> 2;
+ orig |= orig >> 4;
+ orig |= orig >> 8;
+ orig |= orig >> 16;
+ return orig + 1;
+}
+
+// Gradual steps towards the target value in a wrapped (circular) system
+// using the shorter of both ways
+template<typename T>
+inline void wrappedApproachShortest(T ¤t, const T target, const T stepsize,
+ const T maximum)
+{
+ T delta = target - current;
+ if (delta < 0)
+ delta += maximum;
+
+ if (delta > stepsize && maximum - delta > stepsize) {
+ current += (delta < maximum / 2) ? stepsize : -stepsize;
+ if (current >= maximum)
+ current -= maximum;
+ } else {
+ current = target;
+ }
+}
+
+void setPitchYawRollRad(core::matrix4 &m, const v3f &rot);
+
+inline void setPitchYawRoll(core::matrix4 &m, const v3f &rot)
+{
+ setPitchYawRollRad(m, rot * core::DEGTORAD64);
+}
+
+v3f getPitchYawRollRad(const core::matrix4 &m);