// zig_kernels.zig — Pure x86-64 SSE2 SIMD Kernels // Compiled with: zig build-obj -O ReleaseFast -fPIC -mcpu=x86_64 // Safe on all x86-64 CPUs (no AVX/POPCNT required). const std = @import("std"); // 1. RFC 6455 Sec-WebSocket-Accept fallback & RFC 4648 Base64 decoder pub export fn zig_ws_accept_key( key_ptr: [*]const u8, key_len: usize, out_ptr: [*]u8, ) callconv(.c) usize { const magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; var sha1 = std.crypto.hash.Sha1.init(.{}); sha1.update(key_ptr[0..key_len]); sha1.update(magic); var digest: [20]u8 = undefined; sha1.final(&digest); const encoded = std.base64.standard.Encoder.encode(out_ptr[0..28], &digest); return encoded.len; } pub export fn zig_base64_decode( src_ptr: [*]const u8, src_len: usize, dst_ptr: [*]u8, dst_cap: usize, ) callconv(.c) usize { const max_len = std.base64.standard.Decoder.calcSizeForSlice(src_ptr[0..src_len]) catch return 0; if (max_len > dst_cap) return 0; std.base64.standard.Decoder.decode(dst_ptr[0..max_len], src_ptr[0..src_len]) catch return 0; return max_len; } // 2. SSE2 SIMD Historical Log Rescan CPU Fallback pub export fn zig_batch_rescan_logs_cpu( buf_ptr: [*]const u8, len: usize, out_offsets: [*]u32, out_tags: [*]u8, max_lines: usize, ) callconv(.c) usize { if (max_lines == 0 or len == 0) return 0; const Vec16 = @Vector(16, u8); const v_lf: Vec16 = @splat('\n'); const v_cr: Vec16 = @splat('\r'); const v_br: Vec16 = @splat('['); const v_gt: Vec16 = @splat('>'); const v_hash: Vec16 = @splat('#'); const v_E: Vec16 = @splat('E'); const v_F: Vec16 = @splat('F'); const v_W: Vec16 = @splat('W'); const v_U: Vec16 = @splat('U'); const v_0: Vec16 = @splat('0'); const v_9: Vec16 = @splat('9'); var line_count: usize = 0; var line_start: usize = 0; var cur_tag: u8 = 0; var prev_cr: bool = false; var i: usize = 0; while (i < len and line_count < max_lines) { while (!prev_cr and i + 16 <= len) { const chunk: Vec16 = buf_ptr[i..][0..16].*; const m_lf = @as(u16, @bitCast(chunk == v_lf)); const m_cr = @as(u16, @bitCast(chunk == v_cr)); if ((m_lf | m_cr) != 0) break; if (@as(u16, @bitCast(chunk == v_br)) != 0) cur_tag |= 1; if ((@as(u16, @bitCast(chunk == v_gt)) | @as(u16, @bitCast(chunk == v_hash))) != 0) cur_tag |= 2; if ((@as(u16, @bitCast(chunk == v_E)) | @as(u16, @bitCast(chunk == v_F)) | @as(u16, @bitCast(chunk == v_W)) | @as(u16, @bitCast(chunk == v_U))) != 0) cur_tag |= 4; if (@as(u16, @bitCast((chunk >= v_0) & (chunk <= v_9))) != 0) cur_tag |= 8; i += 16; } if (i >= len) break; const c = buf_ptr[i]; if (c == '\n') { if (prev_cr) { prev_cr = false; line_start = i + 1; i += 1; continue; } if (i > line_start) { out_offsets[line_count] = @intCast(line_start); out_tags[line_count] = cur_tag; line_count += 1; } cur_tag = 0; line_start = i + 1; } else if (c == '\r') { if (i > line_start) { out_offsets[line_count] = @intCast(line_start); out_tags[line_count] = cur_tag; line_count += 1; } cur_tag = 0; prev_cr = true; line_start = i + 1; } else { prev_cr = false; if (c == '[') cur_tag |= 1; if (c == '>' or c == '#') cur_tag |= 2; if (c == 'E' or c == 'F' or c == 'W' or c == 'U') cur_tag |= 4; if (c >= '0' and c <= '9') cur_tag |= 8; } i += 1; } if (line_start < len and line_count < max_lines) { out_offsets[line_count] = @intCast(line_start); out_tags[line_count] = cur_tag; line_count += 1; } return line_count; } // 5. SSE2 4-wide Float Multi-Station RMT Margin Matrix Reduction & 16x16 Eye Interpolation pub export fn zig_rmt_eye_reduce_cpu( margins_in: [*]const f32, station_count: usize, eye_grid_out: [*]f32, summary_out: [*]f32, ) callconv(.c) void { const Vec4f = @Vector(4, f32); var st: usize = 0; while (st < station_count) : (st += 1) { var min_timing: f32 = 999.0; var min_vref: f32 = 999.0; var sum_timing: f32 = 0.0; var sum_vref: f32 = 0.0; var active_ch: f32 = 0.0; var ch: usize = 0; while (ch < 24) : (ch += 1) { const base_in = (st * 24 + ch) * 4; const rx_lo = @abs(margins_in[base_in + 0]); const rx_hi = @abs(margins_in[base_in + 1]); const tx_lo = @abs(margins_in[base_in + 2]); const tx_hi = @abs(margins_in[base_in + 3]); const timing_span = rx_lo + rx_hi; const vref_span = tx_lo + tx_hi; if (timing_span > 0.0 or vref_span > 0.0) { if (timing_span < min_timing) min_timing = timing_span; if (vref_span < min_vref) min_vref = vref_span; sum_timing += timing_span; sum_vref += vref_span; active_ch += 1.0; } // 16x16 normalized diamond/elliptical eye opening interpolation using 4-wide SSE2 const half_t = @max((rx_lo + rx_hi) * 0.5, 0.001); const half_v = @max((tx_lo + tx_hi) * 0.5, 0.001); const grid_base = (st * 24 + ch) * 256; var row: usize = 0; while (row < 16) : (row += 1) { const ny = (@as(f32, @floatFromInt(row)) - 7.5) / 7.5 * 32.0; const vy_norm = @abs(ny) / half_v; const v_vy: Vec4f = @splat(vy_norm * vy_norm); const v_inv_ht: Vec4f = @splat(1.0 / half_t); const v_one: Vec4f = @splat(1.0); const v_zero: Vec4f = @splat(0.0); var col: usize = 0; while (col < 16) : (col += 4) { const c0 = (@as(f32, @floatFromInt(col + 0)) - 7.5) / 7.5 * 32.0; const c1 = (@as(f32, @floatFromInt(col + 1)) - 7.5) / 7.5 * 32.0; const c2 = (@as(f32, @floatFromInt(col + 2)) - 7.5) / 7.5 * 32.0; const c3 = (@as(f32, @floatFromInt(col + 3)) - 7.5) / 7.5 * 32.0; const vx = Vec4f{ @abs(c0), @abs(c1), @abs(c2), @abs(c3) } * v_inv_ht; const dist2 = vx * vx + v_vy; const margin = @max(v_zero, v_one - dist2); eye_grid_out[grid_base + row * 16 + col ..][0..4].* = margin; } } } const out_s = st * 4; if (active_ch > 0.0) { summary_out[out_s + 0] = min_timing; summary_out[out_s + 1] = min_vref; summary_out[out_s + 2] = sum_timing / active_ch; summary_out[out_s + 3] = sum_vref / active_ch; } else { summary_out[out_s + 0] = 0.0; summary_out[out_s + 1] = 0.0; summary_out[out_s + 2] = 0.0; summary_out[out_s + 3] = 0.0; } } }