ReviewOS

also looking at this

zig-utils/zig-js

fix(lexer): size the SIMD identifier scan by lane count, not byte size

#625
Merged glennmichael123 wants to merge fix/vector-lane-count-not-sizeof into main
1 file +10 -3

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

src/lexer.zigmodified+10-3
Changes to src/lexer.zig
@@ -320,6 +320,13 @@ pub const Lexer = struct {
320320 /// original boundary. The scalar tail also covers short identifiers.
321321 inline fn consumeAsciiIdentPart(self: *Lexer) void {
322322 const Block = @Vector(8, u8);
323 // Lane count, taken from the type so the two cannot drift. Deliberately
324 // not the vector's byte size: a vector is padded up to its alignment,
325 // so that is 16 on x86_64 and 8 on aarch64. Sizing the loop by it
326 // happened to match the lane count on one target and not the other,
327 // where it does not even compile — slicing 16 bytes to coerce into an
328 // 8-lane vector is `expected type '@Vector(8, u8)', found '[16]u8'`.
329 const lanes = @typeInfo(Block).vector.len;
323330 const lower_a: Block = @splat('a');
324331 const lower_z: Block = @splat('z');
325332 const upper_a: Block = @splat('A');
@@ -328,14 +335,14 @@ pub const Lexer = struct {
328335 const digit_9: Block = @splat('9');
329336 const underscore: Block = @splat('_');
330337 const dollar: Block = @splat('$');
331 while (self.src.len - self.i >= @sizeOf(Block)) {
332 const bytes: Block = self.src[self.i..][0..@sizeOf(Block)].*;
338 while (self.src.len - self.i >= lanes) {
339 const bytes: Block = self.src[self.i..][0..lanes].*;
333340 const valid = ((bytes >= lower_a) & (bytes <= lower_z)) |
334341 ((bytes >= upper_a) & (bytes <= upper_z)) |
335342 ((bytes >= digit_0) & (bytes <= digit_9)) |
336343 (bytes == underscore) | (bytes == dollar);
337344 if (!@reduce(.And, valid)) break;
338 self.i += @sizeOf(Block);
345 self.i += lanes;
339346 }
340347 while (self.i < self.src.len and isIdentPart(self.src[self.i])) self.i += 1;
341348 }