mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-07-28 18:25:16 +00:00
Refresh 005_arrays2 exercise
Updated the exercise to use SIMD. This allows us to cross-reference the concept from 112_vectors back to 005_arrays2, introducing a handy feature of vectors early on. See #430
This commit is contained in:
@@ -26,8 +26,13 @@ pub fn main() void {
|
||||
// (Problem 2)
|
||||
// Please set this array using repetition.
|
||||
// It should result in: 1 0 0 1 1 0 0 1 1 0 0 1
|
||||
const bit_pattern_unit = [_]u8{ ??? };
|
||||
const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit)));
|
||||
const bit_pattern_unit = ???;
|
||||
|
||||
// How long should the bit pattern be?
|
||||
const len = ???;
|
||||
|
||||
// For now, don't worry about the use of SIMD.
|
||||
const bit_pattern: [len]u8 = std.simd.repeat(len, bit_pattern_unit);
|
||||
|
||||
// Okay, that's all of the problems. Let's see the results.
|
||||
//
|
||||
@@ -53,3 +58,12 @@ pub fn main() void {
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
// For the curious:
|
||||
//
|
||||
// The `std.simd.repeat` function takes a target length and a pattern,
|
||||
// and returns a vector filled with that pattern repeated to the
|
||||
// desired length.
|
||||
//
|
||||
// For example, `repeat(5, [_]u8{1, 2})` will return a vector
|
||||
// equivalent to `.{1, 2, 1, 2, 1}`.
|
||||
|
||||
@@ -145,3 +145,7 @@ pub fn main() void {
|
||||
print("Max difference (old fn): {d: >5.3}\n", .{mpd_old});
|
||||
print("Max difference (new fn): {d: >5.3}\n", .{mpd_new});
|
||||
}
|
||||
|
||||
// Another cool feature of Vectors is repeating patterns.
|
||||
// Remember the arrays exercise from earlier where we created an array
|
||||
// by repeating a pattern? See `005_arrays2.zig`.
|
||||
|
||||
Reference in New Issue
Block a user