zig_kernels.zig 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // zig_kernels.zig — Pure x86-64 SSE2 SIMD Kernels
  2. // Compiled with: zig build-obj -O ReleaseFast -fPIC -mcpu=x86_64
  3. // Safe on all x86-64 CPUs (no AVX/POPCNT required).
  4. const std = @import("std");
  5. // 1. RFC 6455 Sec-WebSocket-Accept fallback & RFC 4648 Base64 decoder
  6. pub export fn zig_ws_accept_key(
  7. key_ptr: [*]const u8,
  8. key_len: usize,
  9. out_ptr: [*]u8,
  10. ) callconv(.c) usize {
  11. const magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  12. var sha1 = std.crypto.hash.Sha1.init(.{});
  13. sha1.update(key_ptr[0..key_len]);
  14. sha1.update(magic);
  15. var digest: [20]u8 = undefined;
  16. sha1.final(&digest);
  17. const encoded = std.base64.standard.Encoder.encode(out_ptr[0..28], &digest);
  18. return encoded.len;
  19. }
  20. pub export fn zig_base64_decode(
  21. src_ptr: [*]const u8,
  22. src_len: usize,
  23. dst_ptr: [*]u8,
  24. dst_cap: usize,
  25. ) callconv(.c) usize {
  26. const max_len = std.base64.standard.Decoder.calcSizeForSlice(src_ptr[0..src_len]) catch return 0;
  27. if (max_len > dst_cap) return 0;
  28. std.base64.standard.Decoder.decode(dst_ptr[0..max_len], src_ptr[0..src_len]) catch return 0;
  29. return max_len;
  30. }
  31. // 2. SSE2 SIMD Historical Log Rescan CPU Fallback
  32. pub export fn zig_batch_rescan_logs_cpu(
  33. buf_ptr: [*]const u8,
  34. len: usize,
  35. out_offsets: [*]u32,
  36. out_tags: [*]u8,
  37. max_lines: usize,
  38. ) callconv(.c) usize {
  39. if (max_lines == 0 or len == 0) return 0;
  40. const Vec16 = @Vector(16, u8);
  41. const v_lf: Vec16 = @splat('\n');
  42. const v_cr: Vec16 = @splat('\r');
  43. const v_br: Vec16 = @splat('[');
  44. const v_gt: Vec16 = @splat('>');
  45. const v_hash: Vec16 = @splat('#');
  46. const v_E: Vec16 = @splat('E');
  47. const v_F: Vec16 = @splat('F');
  48. const v_W: Vec16 = @splat('W');
  49. const v_U: Vec16 = @splat('U');
  50. const v_0: Vec16 = @splat('0');
  51. const v_9: Vec16 = @splat('9');
  52. var line_count: usize = 0;
  53. var line_start: usize = 0;
  54. var cur_tag: u8 = 0;
  55. var prev_cr: bool = false;
  56. var i: usize = 0;
  57. while (i < len and line_count < max_lines) {
  58. while (!prev_cr and i + 16 <= len) {
  59. const chunk: Vec16 = buf_ptr[i..][0..16].*;
  60. const m_lf = @as(u16, @bitCast(chunk == v_lf));
  61. const m_cr = @as(u16, @bitCast(chunk == v_cr));
  62. if ((m_lf | m_cr) != 0) break;
  63. if (@as(u16, @bitCast(chunk == v_br)) != 0) cur_tag |= 1;
  64. if ((@as(u16, @bitCast(chunk == v_gt)) | @as(u16, @bitCast(chunk == v_hash))) != 0) cur_tag |= 2;
  65. if ((@as(u16, @bitCast(chunk == v_E)) | @as(u16, @bitCast(chunk == v_F)) |
  66. @as(u16, @bitCast(chunk == v_W)) | @as(u16, @bitCast(chunk == v_U))) != 0) cur_tag |= 4;
  67. if (@as(u16, @bitCast((chunk >= v_0) & (chunk <= v_9))) != 0) cur_tag |= 8;
  68. i += 16;
  69. }
  70. if (i >= len) break;
  71. const c = buf_ptr[i];
  72. if (c == '\n') {
  73. if (prev_cr) {
  74. prev_cr = false;
  75. line_start = i + 1;
  76. i += 1;
  77. continue;
  78. }
  79. if (i > line_start) {
  80. out_offsets[line_count] = @intCast(line_start);
  81. out_tags[line_count] = cur_tag;
  82. line_count += 1;
  83. }
  84. cur_tag = 0;
  85. line_start = i + 1;
  86. } else if (c == '\r') {
  87. if (i > line_start) {
  88. out_offsets[line_count] = @intCast(line_start);
  89. out_tags[line_count] = cur_tag;
  90. line_count += 1;
  91. }
  92. cur_tag = 0;
  93. prev_cr = true;
  94. line_start = i + 1;
  95. } else {
  96. prev_cr = false;
  97. if (c == '[') cur_tag |= 1;
  98. if (c == '>' or c == '#') cur_tag |= 2;
  99. if (c == 'E' or c == 'F' or c == 'W' or c == 'U') cur_tag |= 4;
  100. if (c >= '0' and c <= '9') cur_tag |= 8;
  101. }
  102. i += 1;
  103. }
  104. if (line_start < len and line_count < max_lines) {
  105. out_offsets[line_count] = @intCast(line_start);
  106. out_tags[line_count] = cur_tag;
  107. line_count += 1;
  108. }
  109. return line_count;
  110. }
  111. // 5. SSE2 4-wide Float Multi-Station RMT Margin Matrix Reduction & 16x16 Eye Interpolation
  112. pub export fn zig_rmt_eye_reduce_cpu(
  113. margins_in: [*]const f32,
  114. station_count: usize,
  115. eye_grid_out: [*]f32,
  116. summary_out: [*]f32,
  117. ) callconv(.c) void {
  118. const Vec4f = @Vector(4, f32);
  119. var st: usize = 0;
  120. while (st < station_count) : (st += 1) {
  121. var min_timing: f32 = 999.0;
  122. var min_vref: f32 = 999.0;
  123. var sum_timing: f32 = 0.0;
  124. var sum_vref: f32 = 0.0;
  125. var active_ch: f32 = 0.0;
  126. var ch: usize = 0;
  127. while (ch < 24) : (ch += 1) {
  128. const base_in = (st * 24 + ch) * 4;
  129. const rx_lo = @abs(margins_in[base_in + 0]);
  130. const rx_hi = @abs(margins_in[base_in + 1]);
  131. const tx_lo = @abs(margins_in[base_in + 2]);
  132. const tx_hi = @abs(margins_in[base_in + 3]);
  133. const timing_span = rx_lo + rx_hi;
  134. const vref_span = tx_lo + tx_hi;
  135. if (timing_span > 0.0 or vref_span > 0.0) {
  136. if (timing_span < min_timing) min_timing = timing_span;
  137. if (vref_span < min_vref) min_vref = vref_span;
  138. sum_timing += timing_span;
  139. sum_vref += vref_span;
  140. active_ch += 1.0;
  141. }
  142. // 16x16 normalized diamond/elliptical eye opening interpolation using 4-wide SSE2
  143. const half_t = @max((rx_lo + rx_hi) * 0.5, 0.001);
  144. const half_v = @max((tx_lo + tx_hi) * 0.5, 0.001);
  145. const grid_base = (st * 24 + ch) * 256;
  146. var row: usize = 0;
  147. while (row < 16) : (row += 1) {
  148. const ny = (@as(f32, @floatFromInt(row)) - 7.5) / 7.5 * 32.0;
  149. const vy_norm = @abs(ny) / half_v;
  150. const v_vy: Vec4f = @splat(vy_norm * vy_norm);
  151. const v_inv_ht: Vec4f = @splat(1.0 / half_t);
  152. const v_one: Vec4f = @splat(1.0);
  153. const v_zero: Vec4f = @splat(0.0);
  154. var col: usize = 0;
  155. while (col < 16) : (col += 4) {
  156. const c0 = (@as(f32, @floatFromInt(col + 0)) - 7.5) / 7.5 * 32.0;
  157. const c1 = (@as(f32, @floatFromInt(col + 1)) - 7.5) / 7.5 * 32.0;
  158. const c2 = (@as(f32, @floatFromInt(col + 2)) - 7.5) / 7.5 * 32.0;
  159. const c3 = (@as(f32, @floatFromInt(col + 3)) - 7.5) / 7.5 * 32.0;
  160. const vx = Vec4f{ @abs(c0), @abs(c1), @abs(c2), @abs(c3) } * v_inv_ht;
  161. const dist2 = vx * vx + v_vy;
  162. const margin = @max(v_zero, v_one - dist2);
  163. eye_grid_out[grid_base + row * 16 + col ..][0..4].* = margin;
  164. }
  165. }
  166. }
  167. const out_s = st * 4;
  168. if (active_ch > 0.0) {
  169. summary_out[out_s + 0] = min_timing;
  170. summary_out[out_s + 1] = min_vref;
  171. summary_out[out_s + 2] = sum_timing / active_ch;
  172. summary_out[out_s + 3] = sum_vref / active_ch;
  173. } else {
  174. summary_out[out_s + 0] = 0.0;
  175. summary_out[out_s + 1] = 0.0;
  176. summary_out[out_s + 2] = 0.0;
  177. summary_out[out_s + 3] = 0.0;
  178. }
  179. }
  180. }