[stdlib] Add `DType.get_dtype()`
Add DType.get_dtype().
This will enable a lot of vector based optimizations for more generic function signatures.
This is a split-off from PR #3577
Examples:
fn memset[type: Copyable](ptr: UnsafePointer[type], value: type, count: Int):
alias dt = DType.get_dtype[type]()
@parameter
if dt is not DType.invalid:
var p = ptr.bitcast[Scalar[dt]]()
_memset_impl[dt](p, rebind[Scalar[dt]](value), count)
else:
for i in range(count):
(ptr + i).init_pointee_copy(value)
The _from_str fixes seem "obviously good" to me, the extension I have less insight into though
@JoeLoser now that I'm going through the stdlib searching for places where we should use UInt instead of Int I came across this:
# TODO: Simplify this with a UInt type.
debug_assert(
-self._len <= Int(idx) < self._len, "index must be within bounds"
)
# TODO(MSTDL-1086): optimize away SIMD/UInt normalization check
var offset = Int(idx)
if offset < 0:
offset += len(self)
return self._data[offset]
I already changed it to:
var offset = UInt(idx.__index__())
# TODO(MSTDL-1086): optimize away SIMD normalization check
if not _type_is_eq[I, UInt]():
if Int(idx) < 0:
offset = len(self) + Int(idx)
debug_assert(offset < self._len, "index must be within bounds")
return self._data[offset]
MSTDL-1086 would be solved by doing:
var offset = UInt(idx.__index__())
alias dt = DType.get_dtype[I]()
@parameter
if not _type_is_eq[I, UInt]() and not dt.is_unsigned():
if Int(idx) < 0:
offset = len(self) + Int(idx)
debug_assert(offset < self._len, "index must be within bounds")
return self._data[offset]
!sync
!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 cc194a9e44f703e1ac60ef1d09501640825c16ea! Thank you for your contribution 🎉