{"slug": "why-does-the-simd-path-use-instead-of-in-std-mem-findscalarpos", "title": "Why does the SIMD path use < instead of <= in std.mem.findScalarPos?", "summary": "A Zig developer questioned why the SIMD path in std.mem.findScalarPos uses `<` instead of `<=` when checking if a block fits within the slice, noting that a block ending exactly at slice.len is valid. The discussion references TigerBeetle's blog post on Index, Count, Offset, Size to explain the distinction, but the original poster maintains that the end position should allow `<=`.", "body_md": "[noobie](https://ziggit.dev/u/noobie)\n1\nI’m trying to understand the reasoning behind the `<`\n\nchecks in this SIMD code.\n\nThe relevant part is:\n\n``` js\nif (i + 2 * block_len < slice.len) {\n    const mask: Block = @splat(value);\n    while (true) {\n        inline for (0..2) |_| {\n            const block: Block = slice[i..][0..block_len].*;\n            const matches = block == mask;\n            if (@reduce(.Or, matches)) {\n                return i + std.simd.firstTrue(matches).?;\n            }\n            i += block_len;\n        }\n        if (i + 2 * block_len >= slice.len) break;\n    }\n}\n```\n\nThen the tail handling has:\n\n``` js\nconst block_x_len = block_len / (1 << j);\n\nif (i + block_x_len < slice.len) {\n    const block: BlockX = slice[i..][0..block_x_len].*;\n    ...\n}\n```\n\nI’m wondering why these use `<`\n\ninstead of `<=`\n\n.\n\nIf a block ends exactly at `slice.len`\n\n, wouldn’t it still be valid? For example, if `slice.len == 16`\n\nand `block_len == 8`\n\n, the block `[8..16)`\n\nfits completely.\n\nWith `<`\n\n, that final block is left for the scalar loop instead.\n\nI’m mainly trying to understand if there is a performance, bounds-check, or other implementation reason for using `<`\n\nhere that I’m missing.\n\nLooks to me like the usual difference between Offset (`i`\n\n) and Size (`slice.len`\n\n). There is a nice visualization at TigerBeetles Blog post [Index, Count, Offset, Size](https://tigerbeetle.com/blog/2026-02-16-index-count-offset-size/)\n\nI think the use of `<`\n\nis appropriate here.\n\n[noobie](https://ziggit.dev/u/noobie)\n3\nI see the index/count distinction, but in this case I think `i + block_len`\n\nis an end position rather than an index. If `i = 8`\n\n, `block_len = 8`\n\n, and `slice.len = 16`\n\n, the accessed range is `[8..16)`\n\n, so the block fits exactly. That’s why I’m wondering why the condition uses `<`\n\nrather than `<=`\n\n.", "url": "https://wpnews.pro/news/why-does-the-simd-path-use-instead-of-in-std-mem-findscalarpos", "canonical_source": "https://ziggit.dev/t/why-does-the-simd-path-use-instead-of-in-std-mem-findscalarpos/17267#post_3", "published_at": "2026-08-16 19:08:22+00:00", "updated_at": "2026-08-16 19:11:58.417573+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Zig", "TigerBeetle"], "alternates": {"html": "https://wpnews.pro/news/why-does-the-simd-path-use-instead-of-in-std-mem-findscalarpos", "markdown": "https://wpnews.pro/news/why-does-the-simd-path-use-instead-of-in-std-mem-findscalarpos.md", "text": "https://wpnews.pro/news/why-does-the-simd-path-use-instead-of-in-std-mem-findscalarpos.txt", "jsonld": "https://wpnews.pro/news/why-does-the-simd-path-use-instead-of-in-std-mem-findscalarpos.jsonld"}}