-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[stdlib] SIMD.from_bytes()
and SIMD.as_bytes()
#3966
Conversation
Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
docs/changelog.md
Outdated
@@ -100,6 +100,10 @@ what we publish. | |||
|
|||
### Standard library changes | |||
|
|||
- New `SIMD.from_bytes()` and `SIMD.as_bytes()` functions to convert a list of bytes | |||
to an scalars and vice versa, accepting the endianess as an argument. Similar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to an scalars
-> to scalars
or to a list of scalars
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Fixed: msaelices@399b460
stdlib/src/builtin/simd.mojo
Outdated
@@ -1956,6 +1959,60 @@ struct SIMD[type: DType, size: Int]( | |||
|
|||
return bitcast[_integral_type_of[type](), size](self).cast[int_dtype]() | |||
|
|||
@staticmethod | |||
fn from_bytes[ | |||
big_endian: Bool = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably default to is_big_endian()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, this is should be sys.info.is_big_endian()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion! Done: msaelices@5852cb9
stdlib/src/builtin/simd.mojo
Outdated
return value | ||
|
||
fn as_bytes[ | ||
big_endian: Bool = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably default to is_big_endian().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: msaelices@5852cb9
stdlib/src/builtin/simd.mojo
Outdated
@staticmethod | ||
fn from_bytes[ | ||
big_endian: Bool = False | ||
](bytes: InlineArray[Byte, Self.type_len]) -> Scalar[type]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want this to be an InlineArray
? Those are annoying to construct from a List or other type, thoughts on a Span
variant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That can be a follow up, for now the stricter one is fine with me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to Span
for the API type here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we approach this? The Python int.from_bytes()
allows an arbitrary large input because its output is an infinite sized int, like so:
>>> int.from_bytes(b'12321323123212133211213',"big")
4712049637989858926791184397276765665808674789488996659
If we allow an arbitrary large input, should the output stop being an scalar but a n-sized SIMD, being n power or two?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would only consider the first Self.type_len
bytes, ignoring everything else, and erroring if not enough bytes are present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me try this and open a new PR if I feel useful. I don't like the current ergonomics using InlineArray
stdlib/src/builtin/simd.mojo
Outdated
value = byte_swap(value) | ||
|
||
var ptr = UnsafePointer.address_of(value) | ||
var array = InlineArray[Byte, Self.type_len](fill=0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to leave this uninitalized so that LLVM can clearly see it can remove the zeroing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems like more of an optimization, I'm not going to block on it right now
@lsh do you mind TAL at this since you reviewed the original one before it got reverted internally? Thanks! |
stdlib/src/builtin/simd.mojo
Outdated
@@ -268,6 +269,8 @@ struct SIMD[type: DType, size: Int]( | |||
alias _Mask = SIMD[DType.bool, size] | |||
|
|||
alias element_type = type | |||
alias type_len = type.sizeof() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quibble: I think this alias makes the methods more confusing rather than more clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: msaelices@513af8a
6a6ad76
to
67ba5cd
Compare
Signed-off-by: Manuel Saelices <[email protected]>
67ba5cd
to
399b460
Compare
Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
!sync |
✅🟣 This contribution has been merged 🟣✅ Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the main branch during the next Mojo nightly release, typically within the next 24-48 hours. We use Copybara to merge external contributions, click here to learn more. |
Landed in eb56605! Thank you for your contribution 🎉 |
Similar to the Python's int.from_bytes() and int.to_bytes() one.