Skip to content

In SIMD context, how to use correctly conditional expressions with loops in C++? #891

Closed Answered by merlinND
g1r4ff3 asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @g1r4ff3,

If you have a small number of roots to iterate over, a standard C++ for loop is fine (it will lead be fully unrolled in the generated kernel, but that's okay for a small iteration count and loop body size).

I would then write something like:

FloatP t = dr::Largest<FloatP>;
for (size_t root_i = 0; root_i < n_roots; ++root_i) {  // <-- only scalar quantities here
    // Update entries of `t` for this this is the closest root
    Mask is_closest_root = ...;  // your logic
    t = dr::select(is_closest_root, roots[i], t);
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@g1r4ff3
Comment options

Answer selected by g1r4ff3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants