Skip to content

Commit

Permalink
Use constexpr so VoxelBuffer default values are compile-time
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Nov 2, 2023
1 parent 91d9c31 commit b39e61f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions storage/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,31 @@ void fill_3d_region_zxy(Span<T> dst, Vector3i dst_size, Vector3i dst_min, Vector
// https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#fundamentals-fixedconv
// Converts an int8 value into a float in the range [-1..1], which includes an exact value for 0.
// -128 is one value of the int8 which will not have a corresponding result, it will be clamped to -1.
inline float s8_to_snorm(int8_t v) {
inline constexpr float s8_to_snorm(int8_t v) {
return math::max(v / 127.f, -1.f);
}
inline float s8_to_snorm_noclamp(int8_t v) {
inline constexpr float s8_to_snorm_noclamp(int8_t v) {
return v / 127.f;
}

// Converts a float value in the range [-1..1] to an int8.
// The float will be clamped if it lies outside of the expected range.
inline int8_t snorm_to_s8(float v) {
inline constexpr int8_t snorm_to_s8(float v) {
return math::clamp(v, -1.f, 1.f) * 127;
}

// Converts an int8 value into a float in the range [-1..1], which includes an exact value for 0.
// -32767 is one value of the int16 which will not have a corresponding result, it will be clamped to -1.
inline float s16_to_snorm(int16_t v) {
inline constexpr float s16_to_snorm(int16_t v) {
return math::max(v / 32767.f, -1.f);
}
inline float s16_to_snorm_noclamp(int16_t v) {
inline constexpr float s16_to_snorm_noclamp(int16_t v) {
return v / 32767.f;
}

// Converts a float value in the range [-1..1] to an int16.
// The float will be clamped if it lies outside of the expected range.
inline int16_t snorm_to_s16(float v) {
inline constexpr int16_t snorm_to_s16(float v) {
return math::clamp(v, -1.f, 1.f) * 32767;
}

Expand Down Expand Up @@ -177,12 +177,12 @@ inline FixedArray<uint8_t, 4> decode_indices_from_packed_u16(uint16_t packed_ind
return indices;
}

inline uint16_t encode_indices_to_packed_u16(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
inline constexpr uint16_t encode_indices_to_packed_u16(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
return (a & 0xf) | ((b & 0xf) << 4) | ((c & 0xf) << 8) | ((d & 0xf) << 12);
}

// Encodes from 0..255 to 0..15 values packed in 16 bits. Lower 4 bits of input values will not be preserved.
inline uint16_t encode_weights_to_packed_u16_lossy(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
inline constexpr uint16_t encode_weights_to_packed_u16_lossy(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
return (a >> 4) | ((b >> 4) << 4) | ((c >> 4) << 8) | ((d >> 4) << 12);
}

Expand Down
6 changes: 3 additions & 3 deletions util/math/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace zylann::math {
// Generic math functions, only using scalar types.

template <typename T>
inline T min(const T a, const T b) {
inline constexpr T min(const T a, const T b) {
return a < b ? a : b;
}

template <typename T>
inline T max(const T a, const T b) {
inline constexpr T max(const T a, const T b) {
return a > b ? a : b;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ inline double maxf(double a, double b) {
}

template <typename T>
inline T clamp(const T x, const T min_value, const T max_value) {
inline constexpr T clamp(const T x, const T min_value, const T max_value) {
// TODO Optimization: clang can optimize a min/max implementation. Worth changing to that?
// TODO Enforce T as being numeric
if (x < min_value) {
Expand Down

0 comments on commit b39e61f

Please sign in to comment.