ModularM
Modular2y ago
9 replies
Henk-Jan Lebbink

How to rewrite this code into something not ugly

I need to call shuffle on a parameter mask of different lengths, the following code is the shortest that I could make. Please fill in the dots to appreciate what would happen with width 1024. Any thoughts are appreciated.

fn my_shuffle[T: DType, width: Int, p: StaticIntTuple[width]](v: SIMD[T, width]) -> SIMD[T, width]:
    @parameter
    if width == 8:
        return v.shuffle[
            p[0],
            p[1],
            p[2],
            p[3],
            p[4],
            p[5],
            p[6],
            p[7],
        ]()
    elif width == 16:
        return v.shuffle[
            p[0],
...
            p[15],
        ]()
    elif width == 32:
        return v.shuffle[
            p[0],
...
            p[31],
        ]()
    elif width == 64:
        return v.shuffle[
            p[0],
...   
            p[63],
        ]()
    elif width == 128:
        return v.shuffle[
            p[0],
...
            p[127],
        ]()
    elif width == 256:
        return v.shuffle[
            p[0],
...
            p[255],
        ]()
    else:
        constrained[False]()
        return v
Was this page helpful?