# SIMD Vector to Mask for Substring Search

**URL:** <https://ziggit.dev/t/simd-vector-to-mask-for-substring-search/2538>\
**Category:** Help\
**Tags:** vectors\
**Created:** [December 22, 2023, 2:09am UTC](https://ziggit.dev/t/simd-vector-to-mask-for-substring-search/2538 "2023-12-22T02:09:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrapitup](https://ziggit.dev/user_avatar/ziggit.dev/wrapitup/32/1246_2.png) [@wrapitup](https://ziggit.dev/u/wrapitup)\
**Post date:** [December 22, 2023, 2:09am UTC](https://ziggit.dev/t/simd-vector-to-mask-for-substring-search/2538/1 "2023-12-22T02:09:33Z")

</div>

Hi, I’m trying to implement a SIMD substring search based on the following post: [simd-strfind](http://0x80.pl/articles/simd-strfind.html#implementation). There’s a particular part in the code where the following instrinsic is called: `_mm256_movemask_epi8`.

My understanding is that you can take a vector and create a mask using the most significant bit in the byte (or whichever type you use)

```zig
 // for example:
const x: @Vector(8, u8) = [_] {1, 1, 1, 1, 0, 0, 0, 0};
// expected output: u4 = 0b11110000

```

Now, I’m honestly just a beginner in using SIMD and the I found [openmymind](https://www.openmymind.net/SIMD-With-Zig/) and `std/simd.zig` to be very helpful in learning the basics. I think I came up with a solution with the following routine:

```zig
inline fn movemask(v: @Vector(32, bool)) u32 {
    const mask: @Vector(32, u32) = comptime blk: {
        var out: [32]u32 = undefined;
        for (0..32) |i| {
            out[i] = 1 << (31 - i);
        }
        break :blk out;
    };
    const i: @Vector(32, u32) = std.simd.iota(u32, 32);
    const r = @select(u32, v, i, mask);
    return ~@reduce(.Or, r);
}

```

The thing is that, this really is a single instruction on x86: `vpmovmskb` but I can’t seem to emit that instruction when putting this into [compiler explorer](https://gcc.godbolt.org/z/fsvxGabc8).

Is there anyway I can adjust my routine to get the instruction I want? Or will it require using the inline assembly?

(title edit to reflect question is mostly about the mask).

---

<div class="post-metadata">

**Author:** ![Validark](https://ziggit.dev/user_avatar/ziggit.dev/validark/32/646_2.png) [@Validark](https://ziggit.dev/u/Validark)\
**Post date:** [December 23, 2023, 2:45pm UTC](https://ziggit.dev/t/simd-vector-to-mask-for-substring-search/2538/2 "2023-12-23T14:45:02Z")

</div>

Movmask doesn’t typically need to be written so explicitly. Just produce a vector of bools and `@bitCast` it to an integer.

---

<div class="post-metadata">

**Author:** ![wrapitup](https://ziggit.dev/user_avatar/ziggit.dev/wrapitup/32/1246_2.png) [@wrapitup](https://ziggit.dev/u/wrapitup)\
**Post date:** [December 23, 2023, 7:32pm UTC](https://ziggit.dev/t/simd-vector-to-mask-for-substring-search/2538/3 "2023-12-23T19:32:09Z")

</div>

Thanks! I validated that it indeed compiles to the instruction I was looking for.

```zig
fn movemask(mask: @Vector(8, u8)) u8 {
    const zeros: @Vector(8, u8) = [_]u4{0} ** 8;
    return @bitCast(mask != zeros);
}

test movemask {
    const a: @Vector(8, u8) = [_]u8{ 1, 1, 0, 0, 1, 1, 0, 0 };
    try std.testing.expect(movemask(a) == 0b00110011);
}

```

```asm
movemask:
        vpxor xmm1, xmm1, xmm1
        vpcmpeqb xmm0, xmm0, xmm1
        vpmovmskb eax, xmm0
        not eax
        ret

```
