I was trying to recreate the following for two coordinates:
vTemporalCounter = _mm256_add_epi32(vTemporalCounter, _mm256_and_si256(vTemporalMask, vOne));
vTemporalCounter = _mm256_andnot_si256(vTemporalMask, vTemporalCounter);
I have turned into:
vTemporalCounter = _mm256_add_epi32(_mm256_add_epi32(vTemporalCounter, _mm256_and_si256(vTemporalMaskx, vOne)), _mm256_add_epi32(vTemporalCounter, _mm256_and_si256(vTemporalMasky, vOne)));
vTemporalCounter = _mm256_andnot_si256(vTemporalMaskx, vTemporalCounter);
vTemporalCounter = _mm256_andnot_si256(vTemporalMasky, vTemporalCounter);
Is it correct?
Assistant
The transformation you've made does not correctly replicate the original operation for each coordinate separately. The original operation is applied to a single vector vTemporalCounter using a single mask , effectively updating based on the mask and then applying a "mask not" operation to filter the result based on the same mask. When you're working with two coordinates, you're correct in wanting to apply the operation to each coordinate independently, but the way you've structured the new operations does not achieve this correctly.
User
What if there is overlap between the masks sometimes?
Assistant
If there is overlap between the masks, the operations will still be applied correctly, but the results for the overlapping positions will depend on the order of operations. The key point to understand is that the operations on and are independent of each other, so the order in which you process and does not affect their individual results based on their respective masks.
User
Do the same adaption from one coordinate to two (vSpatialMaskx and vSpatialMasky) to:
__m256i vSplitMask = _mm256_or_si256(_mm256_cmpgt_epi32(vTemporalCounter, vTemporalLimit),
_mm256_castps_si256(vSpatialMaskx));
int mask = _mm256_movemask_ps(_mm256_castsi256_ps(vSplitMask));
Assistant
To adapt the operation to work independently for two coordinates (x and y), you would need to perform the comparison and spatial mask operation for each coordinate separately and then combine the results. The original operation is comparing vTemporalCounter with vTemporalLimit and incorporating vSpatialMaskx into the result. To extend this for two coordinates, you would do the same operation for and , each with their respective spatial masks and .
vTemporalMask
vTemporalCounter
The original operations do the following:
Add vOne to vTemporalCounter where vTemporalMask is true.
Keep the original vTemporalCounter where vTemporalMask is false.
Then, keep vTemporalCounter as is where vTemporalMask is false.
For two coordinates (let's call them x and y), you want to do the same operation independently for each coordinate. Given vTemporalMaskx and vTemporalMasky are masks applied independently to the x and y coordinates, the correct way to parallelize this operation for both coordinates independently would be:
For x coordinate:
Add vOne to vTemporalCounterx where vTemporalMaskx is true.
Keep the original vTemporalCounterx where vTemporalMaskx is false.
Keep vTemporalCounterx as is where vTemporalMaskx is false.
Similarly, for y coordinate:
Add vOne to vTemporalCountery where vTemporalMasky is true.
Keep the original vTemporalCountery where vTemporalMasky is false.
Keep vTemporalCountery as is where vTemporalMasky is false.
The correct sequence of operations, maintaining the independence for each coordinate, would be:
This ensures that the operations are applied independently to each coordinate vector (vTemporalCounterx and vTemporalCountery) using their respective masks (vTemporalMaskx and vTemporalMasky). The original operation is not dupl for each coordinate within a single vTemporalCounter vector but rather applied correctly to separate vectors for each coordinate.
vTemporalCounterx
vTemporalCountery
x
y
However, if you're concerned about the final combined result (i.e., combining vTemporalCounterx and vTemporalCountery into a single vector after these operations), you need to be cautious about the overlap between the masks when combining the results. If you simply concatenate or combine vTemporalCounterx and vTemporalCountery after these operations, the values in the overlapping positions will come from one coordinate or the other, depending on which operation was performed last.
To handle overlaps correctly and achieve a specific behavior (e.g., preferring the x coordinate's value in case of overlap), you might need to adjust your strategy for combining the results. One approach to prioritize the x coordinate's values over the y coordinate's values in case of overlap could be:
cpp
// Assuming vTemporalCounterx and vTemporalCountery have been updated independently with their masks
// Create a mask that is true for the positions where vTemporalMaskx is true and vTemporalMasky is false
// This can be done by ANDing the inverse of vTemporalMasky with vTemporalMaskx
vCombineMask = _mm256_and_si256(_mm256_andnot_si256(vTemporalMasky, _mm256_set1_epi32(-1)), vTemporalMaskx);
// Combine vTemporalCounterx and vTemporalCountery such that:
// - vTemporalCounterx values are kept where vTemporalMaskx is true and vTemporalMasky is false
// - vTemporalCountery values are kept where vTemporalMasky is true (and vTemporalMaskx is false due to the overlap)
vFinalCounter = _mm256_blendv_epi8(vTemporalCountery, vTemporalCounterx, vCombineMask);
This approach uses _mm256_blendv_epi8 to blend the two counter vectors based on the vCombineMask. The vCombineMask is designed to be true (non-zero) where vTemporalMaskx is true and vTemporalMasky is false, ensuring that in the overlapping positions, the vTemporalCounterx values are selected.
Remember, the exact behavior you want in case of overlap should dictate how you combine vTemporalCounterx and vTemporalCountery. The example above is just one way to prioritize one coordinate over the other in the presence of overlap.
vTemporalCounterx
vTemporalCountery
vSpatialMaskx
vSpatialMasky
Here's how you could adapt the operation:
For x coordinate:
Compare vTemporalCounterx with vTemporalLimit.
Incorporate vSpatialMaskx into the result.
For y coordinate:
Compare vTemporalCountery with vTemporalLimit.
Incorporate vSpatialMasky into the result.
Combine the masks from x and y coordinates to get the final mask.
The adapted code would look something like this:
cpp
// Comparison for x coordinate
__m256i vSplitMaskx = _mm256_or_si256(_mm256_cmpgt_epi32(vTemporalCounterx, vTemporalLimit),
_mm256_castps_si256(vSpatialMaskx));
int maskx = _mm256_movemask_ps(_mm256_castsi256_ps(vSplitMaskx));
// Comparison for y coordinate
__m256i vSplitMasky = _mm256_or_si256(_mm256_cmpgt_epi32(vTemporalCountery, vTemporalLimit),
_mm256_castps_si256(vSpatialMasky));
int masky = _mm256_movemask_ps(_mm256_castsi256_ps(vSplitMasky));
// Combine masks from x and y coordinates
int mask = (maskx | masky);
This code performs the comparison and spatial mask incorporation for both x and y coordinates separately and then combines the resulting masks using a bitwise OR operation. The mask variable at the end will contain a bit mask where each bit set to 1 indicates a corresponding element that passed the comparison for either x or y (or both), according to their respective spatial masks.
Note that the _mm256_movemask_ps instruction is used to obtain a bit mask from the comparison results, and this mask is combined for both coordinates using a bitwise OR (|) operation outside the AVX2 intrinsics context.