Skip to content

Commit 079f628

Browse files
committed
std.simd.iota: make it always called at comptime
There's no reason for this to ever run at runtime; it should always be used to generate a constant.
1 parent 5d94295 commit 079f628

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

lib/std/simd.zig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ pub fn VectorCount(comptime VectorType: type) type {
8686

8787
/// Returns a vector containing the first `len` integers in order from 0 to `len`-1.
8888
/// For example, `iota(i32, 8)` will return a vector containing `.{0, 1, 2, 3, 4, 5, 6, 7}`.
89-
pub fn iota(comptime T: type, comptime len: usize) @Vector(len, T) {
90-
var out: [len]T = undefined;
91-
for (out) |*element, i| {
92-
element.* = switch (@typeInfo(T)) {
93-
.Int => @intCast(T, i),
94-
.Float => @intToFloat(T, i),
95-
else => @compileError("Can't use type " ++ @typeName(T) ++ " in iota."),
96-
};
89+
pub inline fn iota(comptime T: type, comptime len: usize) @Vector(len, T) {
90+
comptime {
91+
var out: [len]T = undefined;
92+
for (out) |*element, i| {
93+
element.* = switch (@typeInfo(T)) {
94+
.Int => @intCast(T, i),
95+
.Float => @intToFloat(T, i),
96+
else => @compileError("Can't use type " ++ @typeName(T) ++ " in iota."),
97+
};
98+
}
99+
return @as(@Vector(len, T), out);
97100
}
98-
return @as(@Vector(len, T), out);
99101
}
100102

101103
/// Returns a vector containing the same elements as the input, but repeated until the desired length is reached.

0 commit comments

Comments
 (0)