Skip to content

Commit

Permalink
Make bitops proper inline functions.
Browse files Browse the repository at this point in the history
Avoid macro conflicts.
  • Loading branch information
Themaister committed Jul 12, 2024
1 parent efd0eb5 commit f788e2a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion audio/audio_mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ StreamID Mixer::add_mixer_stream(MixerStream *stream, bool start_playing,
if (!vacant_mask)
continue;

uint32_t subindex = trailing_zeroes(vacant_mask);
uint32_t subindex = Util::trailing_zeroes(vacant_mask);
uint32_t index = i * 32 + subindex;

MixerStream *old_stream = mixer_streams[index];
Expand Down
4 changes: 2 additions & 2 deletions renderer/fft/fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ std::vector<unsigned> FFT::Impl::split_radices(unsigned N) const
return {};

std::vector<unsigned> splits;
unsigned N_log2 = trailing_zeroes(N);
unsigned N_log2 = Util::trailing_zeroes(N);

// This should be deduced based on limits.
constexpr unsigned max_split = 9;
Expand Down Expand Up @@ -733,4 +733,4 @@ unsigned FFT::get_num_iterations() const
return 0;
return unsigned(impl->iterations.size());
}
}
}
2 changes: 1 addition & 1 deletion renderer/lights/clusterer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ void LightClusterer::build_cluster(Vulkan::CommandBuffer &cmd, Vulkan::ImageView

Push push = {
inverse_cluster_transform,
uvec4(res_x, res_y, res_z, trailing_zeroes(res_z)),
uvec4(res_x, res_y, res_z, Util::trailing_zeroes(res_z)),
vec4(1.0f / res_x, 1.0f / res_y, 1.0f / ((ClusterHierarchies + 1) * res_z), 1.0f),
vec4(inv_res, radius),
legacy.spots.count, legacy.points.count,
Expand Down
2 changes: 1 addition & 1 deletion renderer/render_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2434,7 +2434,7 @@ void RenderGraph::enqueue_swapchain_scale_pass(Vulkan::Device &device_)
physical_events[index].to_flush_access = 0;
for (auto &e : physical_events[index].invalidated_in_stage)
e = 0;
physical_events[index].invalidated_in_stage[trailing_zeroes(VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT)] = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT;
physical_events[index].invalidated_in_stage[Util::trailing_zeroes(VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT)] = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT;

cmd->end_region();
if (physical_dimensions[index].uses_semaphore())
Expand Down
4 changes: 2 additions & 2 deletions renderer/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ void Scene::update_transform_tree(TaskComposer *composer)
uint32_t mask = pending_hierarchy_level_mask.load(std::memory_order_relaxed);
if (!mask)
return;
uint32_t num_pending_levels = 32 - leading_zeroes(mask);
uint32_t num_pending_levels = 32 - Util::leading_zeroes(mask);

TaskComposer stage_composer(thread_group);
for (unsigned level = 0, count = num_pending_levels; level < count; level++)
Expand All @@ -624,7 +624,7 @@ void Scene::update_transform_tree(TaskComposer *composer)
uint32_t mask = pending_hierarchy_level_mask.load(std::memory_order_relaxed);
if (mask)
{
uint32_t num_pending_levels = 32 - leading_zeroes(mask);
uint32_t num_pending_levels = 32 - Util::leading_zeroes(mask);
for (unsigned level = 0, count = num_pending_levels; level < count; level++)
perform_per_level_updates(level, nullptr);
}
Expand Down
4 changes: 2 additions & 2 deletions scene-export/meshlet_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void adjust_quant(std::vector<T> &values, int &exp)
if (active_bits == 0)
return;

int extra_shift = trailing_zeroes(active_bits);
int extra_shift = Util::trailing_zeroes(active_bits);
for (auto &value : values)
for (auto &c : value.data)
c >>= extra_shift;
Expand Down Expand Up @@ -297,7 +297,7 @@ static std::vector<i16vec2> mesh_extract_uv_snorm_scale(const SceneFormats::Mesh
// Analyze bits required to encode a delta.
static uint32_t compute_required_bits_unsigned(uint32_t delta)
{
return delta == 0 ? 0 : (32 - leading_zeroes(delta));
return delta == 0 ? 0 : (32 - Util::leading_zeroes(delta));
}

static vec3 decode_snorm_exp(i16vec3 p, int exp)
Expand Down
2 changes: 1 addition & 1 deletion tests/meshlet_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ struct MeshletViewerApplication : Granite::Application, Granite::EventHandler //
unsigned count = 0;
auto *m = static_cast<const uint32_t *>(device.map_host_buffer(buffer, MEMORY_ACCESS_READ_BIT));
for (uint32_t i = 0; i < buffer.get_create_info().size / 4; i++)
count += popcount32(m[i]);
count += Util::popcount32(m[i]);
return count;
};

Expand Down
37 changes: 23 additions & 14 deletions util/bitops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@
namespace Util
{
#ifdef __GNUC__
#define leading_zeroes(x) ((x) == 0 ? 32 : __builtin_clz(x))
#define trailing_zeroes(x) ((x) == 0 ? 32 : __builtin_ctz(x))
#define trailing_ones(x) __builtin_ctz(~uint32_t(x))
#define leading_zeroes64(x) ((x) == 0 ? 64 : __builtin_clzll(x))
#define trailing_zeroes64(x) ((x) == 0 ? 64 : __builtin_ctzll(x))
#define trailing_ones64(x) __builtin_ctzll(~uint64_t(x))
#define popcount32(x) __builtin_popcount(x)
#define leading_zeroes_(x) ((x) == 0 ? 32 : __builtin_clz(x))
#define trailing_zeroes_(x) ((x) == 0 ? 32 : __builtin_ctz(x))
#define trailing_ones_(x) __builtin_ctz(~uint32_t(x))
#define leading_zeroes64_(x) ((x) == 0 ? 64 : __builtin_clzll(x))
#define trailing_zeroes64_(x) ((x) == 0 ? 64 : __builtin_ctzll(x))
#define trailing_ones64_(x) __builtin_ctzll(~uint64_t(x))
#define popcount32_(x) __builtin_popcount(x)

static inline uint32_t leading_zeroes(uint32_t x) { return leading_zeroes_(x); }
static inline uint32_t trailing_zeroes(uint32_t x) { return trailing_zeroes_(x); }
static inline uint32_t trailing_ones(uint32_t x) { return trailing_ones_(x); }
static inline uint32_t leading_zeroes64(uint64_t x) { return leading_zeroes64_(x); }
static inline uint32_t trailing_zeroes64(uint64_t x) { return trailing_zeroes64_(x); }
static inline uint32_t trailing_ones64(uint64_t x) { return trailing_ones64_(x); }
static inline uint32_t popcount32(uint32_t x) { return popcount32_(x); }

#elif defined(_MSC_VER)
namespace Internal
{
Expand Down Expand Up @@ -81,13 +90,13 @@ static inline uint32_t ctz64(uint64_t x)
}
}

#define popcount32(x) ::Util::Internal::popcount32(x)
#define leading_zeroes(x) ::Util::Internal::clz(x)
#define trailing_zeroes(x) ::Util::Internal::ctz(x)
#define trailing_ones(x) ::Util::Internal::ctz(~uint32_t(x))
#define leading_zeroes64(x) ::Util::Internal::clz64(x)
#define trailing_zeroes64(x) ::Util::Internal::ctz64(x)
#define trailing_ones64(x) ::Util::Internal::ctz64(~uint64_t(x))
static inline uint32_t leading_zeroes(uint32_t x) { return Internal::clz(x); }
static inline uint32_t trailing_zeroes(uint32_t x) { return Internal::ctz(x); }
static inline uint32_t trailing_ones(uint32_t x) { return Internal::ctz(~x); }
static inline uint32_t leading_zeroes64(uint64_t x) { return Internal::clz64(x); }
static inline uint32_t trailing_zeroes64(uint64_t x) { return Internal::ctz64(x); }
static inline uint32_t trailing_ones64(uint64_t x) { return Internal::ctz64(~x); }
static inline uint32_t popcount32(uint32_t x) { return Internal::popcount32(x); }
#else
#error "Implement me."
#endif
Expand Down

0 comments on commit f788e2a

Please sign in to comment.