Skip to content

Lesson 3 shuffle pseudocode incorrect #45

@Finxx1

Description

@Finxx1

At the end of lesson 3, the following code is given as being analogous to pshufb:

for(int i = 0; i < 16; i++) {
    if(src[i] & 0x80)
        dst[i] = 0;
    else
        dst[i] = dst[src[i]]
}

This code is not analogous because it overwrites dst while looping. If you try swapping two bytes, the second byte will just use its own value that was swapped to the first byte earlier. Example:

/* not 16 bytes but just pretend */
dst = { 4, 8, 12 };
src = { -1, 2, 1 };
shuffle();
/* dst is now { 0, 12, 12 } when it should be { 0, 12, 8 } instead */

"although in SIMD all 16 loop iterations happen in parallel" may have been an attempt to address this, but I feel like it should be made clearer that this code does not work properly when ran normally.

The easiest way to address this would be to copy the contents of dst to a temporary variable and use that instead of dst when assigning in the loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions