zig_kernels.zig 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // zig_kernels.zig — Pure x86-64 SSE2 SIMD Kernels (v0.8.1.19)
  2. // Compiled with: zig build-obj -OReleaseFast -target x86_64-linux-gnu -mcpu=x86_64 -fPIC
  3. // Safe on all Intel 2nd..8th Gen Core / Celeron / Pentium CPUs (no AVX/POPCNT required).
  4. const std = @import("std");
  5. // 1. SIMD-accelerated terminal cleaner (SSE2 @Vector(16, u8) fallback)
  6. pub export fn zig_clean_terminal_fast(
  7. src_ptr: [*]const u8,
  8. src_len: usize,
  9. dst_ptr: [*]u8,
  10. first_ctrl_hint: usize,
  11. ) callconv(.c) usize {
  12. const src = src_ptr[0..src_len];
  13. var out_len: usize = 0;
  14. var i: usize = 0;
  15. var last_cursor_row: usize = 0;
  16. if (first_ctrl_hint > 0 and first_ctrl_hint <= src_len) {
  17. @memcpy(dst_ptr[0..first_ctrl_hint], src[0..first_ctrl_hint]);
  18. out_len = first_ctrl_hint;
  19. i = first_ctrl_hint;
  20. }
  21. const Vec16 = @Vector(16, u8);
  22. const v_nul: Vec16 = @splat(0x00);
  23. const v_bs: Vec16 = @splat(0x08);
  24. const v_esc: Vec16 = @splat(0x1B);
  25. while (i < src_len) {
  26. while (i + 16 <= src_len) {
  27. const chunk: Vec16 = src_ptr[i..][0..16].*;
  28. const m_esc = chunk == v_esc;
  29. const m_bs = chunk == v_bs;
  30. const m_nul = chunk == v_nul;
  31. const m_any = @as(u16, @bitCast(m_esc)) | @as(u16, @bitCast(m_bs)) | @as(u16, @bitCast(m_nul));
  32. if (m_any != 0) {
  33. const adv: usize = @ctz(m_any);
  34. if (adv > 0) {
  35. @memcpy(dst_ptr[out_len .. out_len + adv], src[i .. i + adv]);
  36. out_len += adv;
  37. i += adv;
  38. }
  39. break;
  40. }
  41. dst_ptr[out_len..][0..16].* = chunk;
  42. out_len += 16;
  43. i += 16;
  44. }
  45. if (i >= src_len) break;
  46. const b = src[i];
  47. if (b == 0x00) {
  48. i += 1;
  49. continue;
  50. }
  51. if (b == 0x1B) {
  52. if (i + 1 < src_len and src[i + 1] == '[') {
  53. var j = i + 2;
  54. var row: usize = 0;
  55. var col: usize = 0;
  56. var seen_semi = false;
  57. while (j < src_len) : (j += 1) {
  58. const c = src[j];
  59. if (c >= '0' and c <= '9') {
  60. if (!seen_semi) {
  61. row = row * 10 + (c - '0');
  62. } else {
  63. col = col * 10 + (c - '0');
  64. }
  65. } else if (c == ';') {
  66. seen_semi = true;
  67. } else if (c >= 0x40 and c <= 0x7E) {
  68. if (c == 'H' or c == 'f') {
  69. const r = if (row == 0) 1 else row;
  70. if (last_cursor_row != 0 and r != last_cursor_row) {
  71. if (out_len > 0 and dst_ptr[out_len - 1] != '\n') {
  72. dst_ptr[out_len] = '\n';
  73. out_len += 1;
  74. }
  75. } else if (col > 1 and out_len > 0 and dst_ptr[out_len - 1] != ' ' and dst_ptr[out_len - 1] != '\n') {
  76. dst_ptr[out_len] = ' ';
  77. out_len += 1;
  78. }
  79. last_cursor_row = r;
  80. } else if (c == 'C') {
  81. dst_ptr[out_len] = ' ';
  82. out_len += 1;
  83. }
  84. j += 1;
  85. break;
  86. }
  87. }
  88. i = j;
  89. continue;
  90. } else if (i + 1 < src_len and src[i + 1] == ']') {
  91. var j = i + 2;
  92. while (j < src_len) : (j += 1) {
  93. if (src[j] == 0x07) {
  94. j += 1;
  95. break;
  96. }
  97. if (src[j] == 0x1B and j + 1 < src_len and src[j + 1] == '\\') {
  98. j += 2;
  99. break;
  100. }
  101. }
  102. i = j;
  103. continue;
  104. }
  105. i += 1;
  106. continue;
  107. }
  108. if (b == 0x08) {
  109. if (out_len > 0) {
  110. out_len -= 1;
  111. while (out_len > 0 and (dst_ptr[out_len] & 0xC0) == 0x80) {
  112. out_len -= 1;
  113. }
  114. }
  115. i += 1;
  116. continue;
  117. }
  118. dst_ptr[out_len] = b;
  119. out_len += 1;
  120. i += 1;
  121. }
  122. return out_len;
  123. }
  124. // 2. RFC 6455 Sec-WebSocket-Accept computation (SHA-1 + Base64)
  125. pub export fn zig_ws_accept_key(
  126. key_ptr: [*]const u8,
  127. key_len: usize,
  128. out_ptr: [*]u8,
  129. ) callconv(.c) usize {
  130. const magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  131. var sha1 = std.crypto.hash.Sha1.init(.{});
  132. sha1.update(key_ptr[0..key_len]);
  133. sha1.update(magic);
  134. var digest: [20]u8 = undefined;
  135. sha1.final(&digest);
  136. const encoded = std.base64.standard.Encoder.encode(out_ptr[0..28], &digest);
  137. return encoded.len;
  138. }
  139. pub export fn zig_base64_encode(
  140. src_ptr: [*]const u8,
  141. src_len: usize,
  142. dst_ptr: [*]u8,
  143. dst_cap: usize,
  144. ) callconv(.c) usize {
  145. const enc_len = std.base64.standard.Encoder.calcSize(src_len);
  146. if (enc_len > dst_cap) return 0;
  147. const res = std.base64.standard.Encoder.encode(dst_ptr[0..enc_len], src_ptr[0..src_len]);
  148. return res.len;
  149. }
  150. pub export fn zig_base64_decode(
  151. src_ptr: [*]const u8,
  152. src_len: usize,
  153. dst_ptr: [*]u8,
  154. dst_cap: usize,
  155. ) callconv(.c) usize {
  156. const max_len = std.base64.standard.Decoder.calcSizeForSlice(src_ptr[0..src_len]) catch return 0;
  157. if (max_len > dst_cap) return 0;
  158. std.base64.standard.Decoder.decode(dst_ptr[0..max_len], src_ptr[0..src_len]) catch return 0;
  159. return max_len;
  160. }
  161. // 3. CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF) for CPLD 6400/MRDIMM local thermal hood UART
  162. pub export fn zig_crc16_ccitt_false(
  163. data_ptr: [*]const u8,
  164. len: usize,
  165. ) callconv(.c) u16 {
  166. var crc: u16 = 0xFFFF;
  167. for (data_ptr[0..len]) |b| {
  168. crc ^= @as(u16, b) << 8;
  169. var bit: u4 = 0;
  170. while (bit < 8) : (bit += 1) {
  171. if ((crc & 0x8000) != 0) {
  172. crc = (crc << 1) ^ 0x1021;
  173. } else {
  174. crc <<= 1;
  175. }
  176. }
  177. }
  178. return crc;
  179. }
  180. // 4. SSE2 SIMD Historical Log Rescan CPU Fallback
  181. pub export fn zig_batch_rescan_logs_cpu(
  182. buf_ptr: [*]const u8,
  183. len: usize,
  184. out_offsets: [*]u32,
  185. out_tags: [*]u8,
  186. max_lines: usize,
  187. ) callconv(.c) usize {
  188. if (max_lines == 0 or len == 0) return 0;
  189. const Vec16 = @Vector(16, u8);
  190. const v_lf: Vec16 = @splat('\n');
  191. const v_cr: Vec16 = @splat('\r');
  192. const v_br: Vec16 = @splat('[');
  193. const v_gt: Vec16 = @splat('>');
  194. const v_hash: Vec16 = @splat('#');
  195. const v_E: Vec16 = @splat('E');
  196. const v_F: Vec16 = @splat('F');
  197. const v_W: Vec16 = @splat('W');
  198. const v_U: Vec16 = @splat('U');
  199. const v_0: Vec16 = @splat('0');
  200. const v_9: Vec16 = @splat('9');
  201. var line_count: usize = 0;
  202. var line_start: usize = 0;
  203. var cur_tag: u8 = 0;
  204. var prev_cr: bool = false;
  205. var i: usize = 0;
  206. while (i < len and line_count < max_lines) {
  207. while (!prev_cr and i + 16 <= len) {
  208. const chunk: Vec16 = buf_ptr[i..][0..16].*;
  209. const m_lf = @as(u16, @bitCast(chunk == v_lf));
  210. const m_cr = @as(u16, @bitCast(chunk == v_cr));
  211. if ((m_lf | m_cr) != 0) break;
  212. if (@as(u16, @bitCast(chunk == v_br)) != 0) cur_tag |= 1;
  213. if ((@as(u16, @bitCast(chunk == v_gt)) | @as(u16, @bitCast(chunk == v_hash))) != 0) cur_tag |= 2;
  214. if ((@as(u16, @bitCast(chunk == v_E)) | @as(u16, @bitCast(chunk == v_F)) |
  215. @as(u16, @bitCast(chunk == v_W)) | @as(u16, @bitCast(chunk == v_U))) != 0) cur_tag |= 4;
  216. if (@as(u16, @bitCast((chunk >= v_0) & (chunk <= v_9))) != 0) cur_tag |= 8;
  217. i += 16;
  218. }
  219. if (i >= len) break;
  220. const c = buf_ptr[i];
  221. if (c == '\n') {
  222. if (prev_cr) {
  223. prev_cr = false;
  224. line_start = i + 1;
  225. i += 1;
  226. continue;
  227. }
  228. if (i > line_start) {
  229. out_offsets[line_count] = @intCast(line_start);
  230. out_tags[line_count] = cur_tag;
  231. line_count += 1;
  232. }
  233. cur_tag = 0;
  234. line_start = i + 1;
  235. } else if (c == '\r') {
  236. if (i > line_start) {
  237. out_offsets[line_count] = @intCast(line_start);
  238. out_tags[line_count] = cur_tag;
  239. line_count += 1;
  240. }
  241. cur_tag = 0;
  242. prev_cr = true;
  243. line_start = i + 1;
  244. } else {
  245. prev_cr = false;
  246. if (c == '[') cur_tag |= 1;
  247. if (c == '>' or c == '#') cur_tag |= 2;
  248. if (c == 'E' or c == 'F' or c == 'W' or c == 'U') cur_tag |= 4;
  249. if (c >= '0' and c <= '9') cur_tag |= 8;
  250. }
  251. i += 1;
  252. }
  253. if (line_start < len and line_count < max_lines) {
  254. out_offsets[line_count] = @intCast(line_start);
  255. out_tags[line_count] = cur_tag;
  256. line_count += 1;
  257. }
  258. return line_count;
  259. }
  260. // 5. SSE2 4-wide Float Multi-Station RMT Margin Matrix Reduction & 16x16 Eye Interpolation
  261. pub export fn zig_rmt_eye_reduce_cpu(
  262. margins_in: [*]const f32,
  263. station_count: usize,
  264. eye_grid_out: [*]f32,
  265. summary_out: [*]f32,
  266. ) callconv(.c) void {
  267. const Vec4f = @Vector(4, f32);
  268. var st: usize = 0;
  269. while (st < station_count) : (st += 1) {
  270. var min_timing: f32 = 999.0;
  271. var min_vref: f32 = 999.0;
  272. var sum_timing: f32 = 0.0;
  273. var sum_vref: f32 = 0.0;
  274. var active_ch: f32 = 0.0;
  275. var ch: usize = 0;
  276. while (ch < 24) : (ch += 1) {
  277. const base_in = (st * 24 + ch) * 4;
  278. const rx_lo = @abs(margins_in[base_in + 0]);
  279. const rx_hi = @abs(margins_in[base_in + 1]);
  280. const tx_lo = @abs(margins_in[base_in + 2]);
  281. const tx_hi = @abs(margins_in[base_in + 3]);
  282. const timing_span = rx_lo + rx_hi;
  283. const vref_span = tx_lo + tx_hi;
  284. if (timing_span > 0.0 or vref_span > 0.0) {
  285. if (timing_span < min_timing) min_timing = timing_span;
  286. if (vref_span < min_vref) min_vref = vref_span;
  287. sum_timing += timing_span;
  288. sum_vref += vref_span;
  289. active_ch += 1.0;
  290. }
  291. // 16x16 normalized diamond/elliptical eye opening interpolation using 4-wide SSE2
  292. const half_t = @max((rx_lo + rx_hi) * 0.5, 0.001);
  293. const half_v = @max((tx_lo + tx_hi) * 0.5, 0.001);
  294. const grid_base = (st * 24 + ch) * 256;
  295. var row: usize = 0;
  296. while (row < 16) : (row += 1) {
  297. const ny = (@as(f32, @floatFromInt(row)) - 7.5) / 7.5 * 32.0;
  298. const vy_norm = @abs(ny) / half_v;
  299. const v_vy: Vec4f = @splat(vy_norm * vy_norm);
  300. const v_inv_ht: Vec4f = @splat(1.0 / half_t);
  301. const v_one: Vec4f = @splat(1.0);
  302. const v_zero: Vec4f = @splat(0.0);
  303. var col: usize = 0;
  304. while (col < 16) : (col += 4) {
  305. const c0 = (@as(f32, @floatFromInt(col + 0)) - 7.5) / 7.5 * 32.0;
  306. const c1 = (@as(f32, @floatFromInt(col + 1)) - 7.5) / 7.5 * 32.0;
  307. const c2 = (@as(f32, @floatFromInt(col + 2)) - 7.5) / 7.5 * 32.0;
  308. const c3 = (@as(f32, @floatFromInt(col + 3)) - 7.5) / 7.5 * 32.0;
  309. const vx = Vec4f{ @abs(c0), @abs(c1), @abs(c2), @abs(c3) } * v_inv_ht;
  310. const dist2 = vx * vx + v_vy;
  311. const margin = @max(v_zero, v_one - dist2);
  312. eye_grid_out[grid_base + row * 16 + col ..][0..4].* = margin;
  313. }
  314. }
  315. }
  316. const out_s = st * 4;
  317. if (active_ch > 0.0) {
  318. summary_out[out_s + 0] = min_timing;
  319. summary_out[out_s + 1] = min_vref;
  320. summary_out[out_s + 2] = sum_timing / active_ch;
  321. summary_out[out_s + 3] = sum_vref / active_ch;
  322. } else {
  323. summary_out[out_s + 0] = 0.0;
  324. summary_out[out_s + 1] = 0.0;
  325. summary_out[out_s + 2] = 0.0;
  326. summary_out[out_s + 3] = 0.0;
  327. }
  328. }
  329. }