| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- // zig_kernels.zig — Pure x86-64 SSE2 SIMD Kernels (v0.8.1.19)
- // Compiled with: zig build-obj -OReleaseFast -target x86_64-linux-gnu -mcpu=x86_64 -fPIC
- // Safe on all Intel 2nd..8th Gen Core / Celeron / Pentium CPUs (no AVX/POPCNT required).
- const std = @import("std");
- // 1. SIMD-accelerated terminal cleaner (SSE2 @Vector(16, u8) fallback)
- pub export fn zig_clean_terminal_fast(
- src_ptr: [*]const u8,
- src_len: usize,
- dst_ptr: [*]u8,
- first_ctrl_hint: usize,
- ) callconv(.c) usize {
- const src = src_ptr[0..src_len];
- var out_len: usize = 0;
- var i: usize = 0;
- var last_cursor_row: usize = 0;
- if (first_ctrl_hint > 0 and first_ctrl_hint <= src_len) {
- @memcpy(dst_ptr[0..first_ctrl_hint], src[0..first_ctrl_hint]);
- out_len = first_ctrl_hint;
- i = first_ctrl_hint;
- }
- const Vec16 = @Vector(16, u8);
- const v_nul: Vec16 = @splat(0x00);
- const v_bs: Vec16 = @splat(0x08);
- const v_esc: Vec16 = @splat(0x1B);
- while (i < src_len) {
- while (i + 16 <= src_len) {
- const chunk: Vec16 = src_ptr[i..][0..16].*;
- const m_esc = chunk == v_esc;
- const m_bs = chunk == v_bs;
- const m_nul = chunk == v_nul;
- const m_any = @as(u16, @bitCast(m_esc)) | @as(u16, @bitCast(m_bs)) | @as(u16, @bitCast(m_nul));
- if (m_any != 0) {
- const adv: usize = @ctz(m_any);
- if (adv > 0) {
- @memcpy(dst_ptr[out_len .. out_len + adv], src[i .. i + adv]);
- out_len += adv;
- i += adv;
- }
- break;
- }
- dst_ptr[out_len..][0..16].* = chunk;
- out_len += 16;
- i += 16;
- }
- if (i >= src_len) break;
- const b = src[i];
- if (b == 0x00) {
- i += 1;
- continue;
- }
- if (b == 0x1B) {
- if (i + 1 < src_len and src[i + 1] == '[') {
- var j = i + 2;
- var row: usize = 0;
- var col: usize = 0;
- var seen_semi = false;
- while (j < src_len) : (j += 1) {
- const c = src[j];
- if (c >= '0' and c <= '9') {
- if (!seen_semi) {
- row = row * 10 + (c - '0');
- } else {
- col = col * 10 + (c - '0');
- }
- } else if (c == ';') {
- seen_semi = true;
- } else if (c >= 0x40 and c <= 0x7E) {
- if (c == 'H' or c == 'f') {
- const r = if (row == 0) 1 else row;
- if (last_cursor_row != 0 and r != last_cursor_row) {
- if (out_len > 0 and dst_ptr[out_len - 1] != '\n') {
- dst_ptr[out_len] = '\n';
- out_len += 1;
- }
- } else if (col > 1 and out_len > 0 and dst_ptr[out_len - 1] != ' ' and dst_ptr[out_len - 1] != '\n') {
- dst_ptr[out_len] = ' ';
- out_len += 1;
- }
- last_cursor_row = r;
- } else if (c == 'C') {
- dst_ptr[out_len] = ' ';
- out_len += 1;
- }
- j += 1;
- break;
- }
- }
- i = j;
- continue;
- } else if (i + 1 < src_len and src[i + 1] == ']') {
- var j = i + 2;
- while (j < src_len) : (j += 1) {
- if (src[j] == 0x07) {
- j += 1;
- break;
- }
- if (src[j] == 0x1B and j + 1 < src_len and src[j + 1] == '\\') {
- j += 2;
- break;
- }
- }
- i = j;
- continue;
- }
- i += 1;
- continue;
- }
- if (b == 0x08) {
- if (out_len > 0) {
- out_len -= 1;
- while (out_len > 0 and (dst_ptr[out_len] & 0xC0) == 0x80) {
- out_len -= 1;
- }
- }
- i += 1;
- continue;
- }
- dst_ptr[out_len] = b;
- out_len += 1;
- i += 1;
- }
- return out_len;
- }
- // 2. RFC 6455 Sec-WebSocket-Accept computation (SHA-1 + Base64)
- 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_encode(
- src_ptr: [*]const u8,
- src_len: usize,
- dst_ptr: [*]u8,
- dst_cap: usize,
- ) callconv(.c) usize {
- const enc_len = std.base64.standard.Encoder.calcSize(src_len);
- if (enc_len > dst_cap) return 0;
- const res = std.base64.standard.Encoder.encode(dst_ptr[0..enc_len], src_ptr[0..src_len]);
- return res.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;
- }
- // 3. CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF) for CPLD 6400/MRDIMM local thermal hood UART
- pub export fn zig_crc16_ccitt_false(
- data_ptr: [*]const u8,
- len: usize,
- ) callconv(.c) u16 {
- var crc: u16 = 0xFFFF;
- for (data_ptr[0..len]) |b| {
- crc ^= @as(u16, b) << 8;
- var bit: u4 = 0;
- while (bit < 8) : (bit += 1) {
- if ((crc & 0x8000) != 0) {
- crc = (crc << 1) ^ 0x1021;
- } else {
- crc <<= 1;
- }
- }
- }
- return crc;
- }
- // 4. 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');
- 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;
- 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;
- }
- 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;
- }
- }
- }
|