flâneur — a map of the web's best reading

SIMD with Zig

openmymind.net · 2,270 words · saved by 1 readers

To find the index of the first instance of a character within a body of [ASCII] text, you might write something like: Or use the std.mem.indexOfScalar function from the standard library, which is essentially the same implementation. This implementation loops through the input and checks each character, one by one, to see if it's equal to our target. With SIMD, we can leverage CPU instructions to check multiple characters of our input in parallel. Let's do that in Zig. To keep this simple for now, let's pretend that our input is always exactly 8 characters long (we'll look at dynamic input lengths after, but 8 characters means we can illustrate the full content of our vectors). As an example, say we have "Hello Jo" and we want the first index of "o" (which is 4). Our first step is to create a vector (think of it as an array) of 8 elements, each containing the value "o": Our @as cast is a very Zig-specicific thing. 'o' on its own is a comptime_int. If we try to use that, we'll get an err

SIMD with Zig May 02, 2023 To find the index of the first instance of a character within a body of [ASCII] text, you might write something like: fn indexOf ( haystack : [ ] const u8 , needle : u8 ) ? usize { for ( haystack , 0 .. ) | c , i | { if ( c == needle ) return i ; } return null ; } Or use the std.mem.indexOfScalar function from the standard library, which is essentially the same implementation. This implementation loops through the input and checks each character, one by one, to see if it's equal to our target. With SIMD, we can leverage CPU instructions to check multiple characters o

Explore this link on the map →

related reading