Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] [stdlib] [proposal] Add comp time SIMD range constructor #3103

Closed
1 task done
martinvuyk opened this issue Jun 22, 2024 · 0 comments
Closed
1 task done
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@martinvuyk
Copy link
Contributor

martinvuyk commented Jun 22, 2024

Review Mojo's priorities

What is your request?

@staticmethod
fn from_range[start: Int, end: Int, step: Int = 1]() -> Self:
    """Construct from a compile time range using the index as value.

    Parameters:
        start: The start of the range.
        end: The end of the range.
        step: The step of the range.

    Constraints:
        The range must be equal to or less than SIMD size.

    Examples:
    ```mojo
    print(SIMD[DType.uint8, 4].from_range[0, 4]()) # [0, 1, 2, 3]
    print(SIMD[DType.uint8, 4].from_range[3, -1, -1]()) # [3, 2, 1, 0]
    ```
    .
    """

    constrained[
        abs((start - end) // step) <= size,
        "The range must be equal to or less than SIMD size.",
    ]()
    var vec = Self()
    var idx = 0

    @parameter
    for i in range(start, end, step):
        vec[idx] = i
        idx  = 1
    return vec

test:

def test_range_constructor():
    alias INum = SIMD[DType.uint8, 4]
    assert_equal(INum.from_range[0, 4](), INum(0, 1, 2, 3))
    assert_equal(INum.from_range[3, -1, -1](), INum(3, 2, 1, 0))
    alias FNum = SIMD[DType.float16, 4]
    assert_equal(FNum.from_range[0, 4](), FNum(0, 1, 2, 3))
    assert_equal(FNum.from_range[3, -1, -1](), FNum(3, 2, 1, 0))

What is your motivation for this change?

To be able to implement a SIMD.index() function, it would be needed to be able to contruct a range for the SIMD vec
for example if a generic version for SIMD.index() is wanted:

fn index(self, value: Scalar[T]) -> Optional[Int]:
    alias indices = SIMD[DType.uint16].from_range[size -1, -1, -1]()
    var idx = ((self == value).cast[DType.uint16]() * indices).reduce_max()
    if idx == 0:
        return None
    return size - int(idx)

To be able to construct a reversed function for example shuffle could take in a compile time range

fn reverse(inout self):
    self = self.shuffle[range(size -1, -1, -1)]()

or using the range constructor

fn reverse(inout self):
    self = self.shuffle[Self.from_range[size -1, -1, -1]()]()

Any other details?

No response

@martinvuyk martinvuyk added enhancement New feature or request mojo-repo Tag all issues with this label labels Jun 22, 2024
@martinvuyk martinvuyk changed the title [Feature Request] [stdlib] Add comp time SIMD range constructor [Feature Request] [stdlib] [proposal] Add comp time SIMD range constructor Jun 24, 2024
@martinvuyk martinvuyk closed this as not planned Won't fix, can't repro, duplicate, stale Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

1 participant