+ +
6603e77 test(simd): detect qemu-aarch64 CPU implementer 0x00 in is_emulated_cpu
saifmukhtar · 2026-11-25
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * test/openavx_simd_audit_test.c - SIMD safety, alignment, and parity tests |
| 7 | * |
| 8 | * Tests edge-case safety (Part A) and SIMD/scalar parity and throughput (Part B) |
| 9 | * inside monitored subprocesses. |
| 10 | */ |
| 11 | |
| 12 | #define _GNU_SOURCE |
| 13 | #define _POSIX_C_SOURCE 200809L |
| 14 | #define _DEFAULT_SOURCE |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <string.h> |
| 21 | #include <signal.h> |
| 22 | #include <unistd.h> |
| 23 | #include <errno.h> |
| 24 | #include <assert.h> |
| 25 | #include <sys/utsname.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #include <openavx.h> |
| 30 | #include <avx_internal.h> |
| 31 | #include <avx_simd.h> |
| 32 | |
| 33 | #if defined(_WIN32) |
| 34 | #define exit(code) do { if ((code) != 0) { abort(); } else { return; } } while(0) |
| 35 | #endif |
| 36 | #include "avx_audio.h" |
| 37 | |
| 38 | #if defined(__x86_64__) || defined(_M_X64) |
| 39 | #include <immintrin.h> |
| 40 | #endif |
| 41 | |
| 42 | static double get_time_ms(void) { |
| 43 | struct timespec ts; |
| 44 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 45 | return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1000000.0; |
| 46 | } |
| 47 | |
| 48 | typedef struct { |
| 49 | bool passed; |
| 50 | bool crashed_as_expected; |
| 51 | int termination_signal; |
| 52 | double duration_ms; |
| 53 | char details[512]; |
| 54 | } audit_result_t; |
| 55 | |
| 56 | /* Run a test function in an isolated child process to safely intercept crashes (SIGSEGV, SIGILL) */ |
| 57 | static audit_result_t run_monitored_crash_test(void (*test_fn)(void), int timeout_ms, const char* name, int expected_signal) { |
| 58 | audit_result_t res; |
| 59 | memset(&res, 0, sizeof(res)); |
| 60 | |
| 61 | printf("\n----------------------------------------------------------------------\n"); |
| 62 | printf(" PROOF TEST: %s\n", name); |
| 63 | printf(" (Monitored subprocess; expecting %s)\n", |
| 64 | expected_signal == SIGSEGV ? "SIGSEGV (Segmentation Fault)" : |
| 65 | (expected_signal == SIGILL ? "SIGILL (Illegal Instruction)" : "Exit Code 0")); |
| 66 | printf("----------------------------------------------------------------------\n"); |
| 67 | |
| 68 | #if defined(_WIN32) |
| 69 | double t0 = get_time_ms(); |
| 70 | test_fn(); |
| 71 | double t1 = get_time_ms(); |
| 72 | res.duration_ms = t1 - t0; |
| 73 | res.passed = true; |
| 74 | snprintf(res.details, sizeof(res.details), "PASSED: Clean completion with code 0 in %.2f ms", res.duration_ms); |
| 75 | return res; |
| 76 | #else |
| 77 | double t0 = get_time_ms(); |
| 78 | pid_t pid = fork(); |
| 79 | |
| 80 | if (pid == 0) { |
| 81 | alarm((unsigned int)(timeout_ms / 1000 + 2)); |
| 82 | test_fn(); |
| 83 | exit(0); |
| 84 | } |
| 85 | |
| 86 | if (pid < 0) { |
| 87 | res.passed = false; |
| 88 | snprintf(res.details, sizeof(res.details), "fork() failed: %s", strerror(errno)); |
| 89 | return res; |
| 90 | } |
| 91 | |
| 92 | int status = 0; |
| 93 | int elapsed = 0; |
| 94 | int step_ms = 20; |
| 95 | |
| 96 | while (elapsed < timeout_ms) { |
| 97 | pid_t p = waitpid(pid, &status, WNOHANG); |
| 98 | if (p == pid) { |
| 99 | double t1 = get_time_ms(); |
| 100 | res.duration_ms = t1 - t0; |
| 101 | |
| 102 | if (WIFSIGNALED(status)) { |
| 103 | int sig = WTERMSIG(status); |
| 104 | res.termination_signal = sig; |
| 105 | if (sig == expected_signal) { |
| 106 | res.passed = true; |
| 107 | res.crashed_as_expected = true; |
| 108 | snprintf(res.details, sizeof(res.details), |
| 109 | "PROVEN: Process crashed with signal %d (%s) as predicted in %.2f ms", |
| 110 | sig, strsignal(sig), res.duration_ms); |
| 111 | } else { |
| 112 | res.passed = false; |
| 113 | snprintf(res.details, sizeof(res.details), |
| 114 | "UNEXPECTED: Process killed by signal %d (%s) instead of %d in %.2f ms", |
| 115 | sig, strsignal(sig), expected_signal, res.duration_ms); |
| 116 | } |
| 117 | } else if (WIFEXITED(status)) { |
| 118 | int code = WEXITSTATUS(status); |
| 119 | if (expected_signal == 0 && code == 0) { |
| 120 | res.passed = true; |
| 121 | snprintf(res.details, sizeof(res.details), |
| 122 | "PASSED: Clean completion with code 0 in %.2f ms", res.duration_ms); |
| 123 | } else if (expected_signal != 0) { |
| 124 | res.passed = false; |
| 125 | snprintf(res.details, sizeof(res.details), |
| 126 | "FAILED: Process did not crash; exited normally with code %d (expected signal %d)", |
| 127 | code, expected_signal); |
| 128 | } else { |
| 129 | res.passed = false; |
| 130 | snprintf(res.details, sizeof(res.details), |
| 131 | "FAILED: Process exited with error code %d", code); |
| 132 | } |
| 133 | } |
| 134 | return res; |
| 135 | } |
| 136 | usleep((useconds_t)(step_ms * 1000)); |
| 137 | elapsed += step_ms; |
| 138 | } |
| 139 | |
| 140 | /* Timed out */ |
| 141 | kill(pid, SIGKILL); |
| 142 | waitpid(pid, NULL, 0); |
| 143 | res.passed = false; |
| 144 | snprintf(res.details, sizeof(res.details), "Watchdog timeout after %d ms", timeout_ms); |
| 145 | return res; |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | /* ========================================================================= */ |
| 150 | /* PROOF A-1: avx2_downmix_5_1 Graceful NULL Channel Pointer Handling */ |
| 151 | /* ========================================================================= */ |
| 152 | |
| 153 | static void proof_a1_null_channel_pointer(void) { |
| 154 | float fl[64], fr[64], bl[64], br[64]; |
| 155 | float out_l[64], out_r[64]; |
| 156 | for (int i = 0; i < 64; ++i) { |
| 157 | fl[i] = 0.5f; fr[i] = 0.5f; bl[i] = 0.2f; br[i] = 0.2f; |
| 158 | } |
| 159 | |
| 160 | /* Channel 2 (Center) is passed as NULL pointer */ |
| 161 | const float* in[6] = { fl, fr, NULL, NULL, bl, br }; |
| 162 | |
| 163 | printf("[Child] Invoking avx_audio_downmix_5_1_to_stereo() with in_channels[2] = NULL...\n"); |
| 164 | fflush(stdout); |
| 165 | |
| 166 | /* Should handle NULL safely and return cleanly without SIGSEGV */ |
| 167 | avx_audio_downmix_5_1_to_stereo(in, out_l, out_r, 64, AVX_AUDIO_NORMALIZE_NONE); |
| 168 | |
| 169 | printf("[Child] PASSED: function safely validated null channel pointer without crash!\n"); |
| 170 | exit(0); |
| 171 | } |
| 172 | |
| 173 | /* ========================================================================= */ |
| 174 | /* PROOF A-2: FMA Capability Check Verification */ |
| 175 | /* ========================================================================= */ |
| 176 | |
| 177 | static void proof_a2_missing_fma_detection(void) { |
| 178 | printf("[Child] Inspecting CPU dispatch logic in avx_simd_get_caps()...\n"); |
| 179 | fflush(stdout); |
| 180 | |
| 181 | avx_simd_type_e caps = avx_simd_get_caps(); |
| 182 | const char* name = avx_simd_get_name(); |
| 183 | printf("[Child] Detected SIMD caps: %d (%s)\n", caps, name); |
| 184 | |
| 185 | #if defined(__x86_64__) |
| 186 | bool supports_avx2 = __builtin_cpu_supports("avx2"); |
| 187 | bool supports_fma = __builtin_cpu_supports("fma"); |
| 188 | |
| 189 | printf("[Child] CPU features: __builtin_cpu_supports('avx2') = %s\n", supports_avx2 ? "TRUE" : "FALSE"); |
| 190 | printf("[Child] CPU features: __builtin_cpu_supports('fma') = %s\n", supports_fma ? "TRUE" : "FALSE"); |
| 191 | |
| 192 | if (supports_avx2 && supports_fma) { |
| 193 | if (caps == AVX_SIMD_AVX2) { |
| 194 | printf("[Child] PASSED: AVX2+FMA verified and enabled.\n"); |
| 195 | exit(0); |
| 196 | } else { |
| 197 | printf("[Child] FAILED: AVX2 and FMA supported but caps not set to AVX2!\n"); |
| 198 | exit(1); |
| 199 | } |
| 200 | } else { |
| 201 | if (caps != AVX_SIMD_AVX2) { |
| 202 | printf("[Child] PASSED: Non-FMA CPU correctly falls back to scalar.\n"); |
| 203 | exit(0); |
| 204 | } else { |
| 205 | printf("[Child] FAILED: AVX2 enabled despite missing FMA support!\n"); |
| 206 | exit(1); |
| 207 | } |
| 208 | } |
| 209 | #else |
| 210 | exit(0); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | /* ========================================================================= */ |
| 215 | /* PROOF A-3: Multi-Channel planes[8] Bounds Safety */ |
| 216 | /* ========================================================================= */ |
| 217 | |
| 218 | static void proof_a3_planes_buffer_over_read(void) { |
| 219 | printf("[Child] Constructing 40-channel audio frame with 32-element planes array...\n"); |
| 220 | fflush(stdout); |
| 221 | |
| 222 | float channel_data[64 * 40]; |
| 223 | for (int i = 0; i < 64 * 40; ++i) channel_data[i] = 0.1f; |
| 224 | |
| 225 | oavx_audio_frame_t src_frame; |
| 226 | memset(&src_frame, 0, sizeof(src_frame)); |
| 227 | src_frame.sample_rate = 48000; |
| 228 | src_frame.channels = 40; /* Greater than OAVX_MAX_AUDIO_CHANNELS (32) */ |
| 229 | src_frame.frames = 64; |
| 230 | src_frame.stride_bytes = 64 * sizeof(float); |
| 231 | src_frame.format = OAVX_AUDIO_FMT_FLT_PLANAR; |
| 232 | |
| 233 | /* planes[0] has contiguous channel data; planes[1..31] are NULL */ |
| 234 | src_frame.planes[0] = channel_data; |
| 235 | for (int c = 1; c < OAVX_MAX_AUDIO_CHANNELS; ++c) src_frame.planes[c] = NULL; |
| 236 | |
| 237 | /* Poison pts_ns to verify it is NEVER accessed as planes[32] */ |
| 238 | src_frame.pts_ns = (int64_t)0xDEADBEEF00000000ULL; |
| 239 | |
| 240 | oavx_audio_frame_t dst_frame; |
| 241 | memset(&dst_frame, 0, sizeof(dst_frame)); |
| 242 | |
| 243 | printf("[Child] Calling avx_audio_frame_downmix_to_stereo() with 40 channels...\n"); |
| 244 | fflush(stdout); |
| 245 | |
| 246 | bool ok = avx_audio_frame_downmix_to_stereo(&src_frame, &dst_frame, AVX_AUDIO_NORMALIZE_NONE); |
| 247 | |
| 248 | if (ok && dst_frame.planes[0] && dst_frame.planes[1]) { |
| 249 | printf("[Child] PASSED: 40-channel downmix completed safely without out-of-bounds access!\n"); |
| 250 | free(dst_frame.planes[0]); |
| 251 | exit(0); |
| 252 | } else { |
| 253 | printf("[Child] FAILED: downmix failed or returned null output\n"); |
| 254 | exit(1); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* ========================================================================= */ |
| 259 | /* PROOF A-4: Remainder Odd Pixel Handling in avx_simd_uyvy_to_rgba */ |
| 260 | /* ========================================================================= */ |
| 261 | |
| 262 | static void proof_a4_remainder_odd_pixel_drop(void) { |
| 263 | /* Test an odd pixel count: 15 pixels (8 pixels vector block + 7 pixels remainder) */ |
| 264 | const int width = 15; |
| 265 | const int height = 1; |
| 266 | const size_t uyvy_bytes = width * height * 2; /* 30 bytes */ |
| 267 | const size_t rgba_bytes = width * height * 4; /* 60 bytes */ |
| 268 | |
| 269 | uint8_t *src_uyvy = (uint8_t*)malloc(uyvy_bytes); |
| 270 | uint8_t *dst_rgba = (uint8_t*)malloc(rgba_bytes); |
| 271 | assert(src_uyvy && dst_rgba); |
| 272 | |
| 273 | /* Fill src with dummy UYVY: U=128, Y0=128, V=128, Y1=128 (neutral grey) */ |
| 274 | for (size_t i = 0; i < uyvy_bytes; ++i) { |
| 275 | src_uyvy[i] = 128; |
| 276 | } |
| 277 | |
| 278 | /* Pre-fill destination buffer with poison sentinel 0xAA */ |
| 279 | memset(dst_rgba, 0xAA, rgba_bytes); |
| 280 | |
| 281 | printf("[Child] Converting 15 pixels (UYVY -> RGBA)...\n"); |
| 282 | avx_simd_uyvy_to_rgba(src_uyvy, dst_rgba, width, height); |
| 283 | |
| 284 | size_t last_px_offset = (width - 1) * 4; /* Pixel 14 (15th pixel) */ |
| 285 | uint8_t r = dst_rgba[last_px_offset + 0]; |
| 286 | uint8_t g = dst_rgba[last_px_offset + 1]; |
| 287 | uint8_t b = dst_rgba[last_px_offset + 2]; |
| 288 | uint8_t a = dst_rgba[last_px_offset + 3]; |
| 289 | |
| 290 | printf("[Child] Pixel 0 (Vector) Alpha: %u (expected 255)\n", dst_rgba[3]); |
| 291 | printf("[Child] Pixel 13 (Remainder) Alpha: %u (expected 255)\n", dst_rgba[13 * 4 + 3]); |
| 292 | printf("[Child] Pixel 14 (Last Pixel) Raw Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", r, g, b, a); |
| 293 | |
| 294 | if (a == 255 && r == 0x82 && g == 0x82 && b == 0x82) { |
| 295 | printf("[Child] PASSED: The 15th odd pixel is correctly converted and written!\n"); |
| 296 | free(src_uyvy); |
| 297 | free(dst_rgba); |
| 298 | exit(0); |
| 299 | } else { |
| 300 | printf("[Child] FAILED: Unexpected pixel 14 value (got 0x%02X 0x%02X 0x%02X 0x%02X).\n", r, g, b, a); |
| 301 | free(src_uyvy); |
| 302 | free(dst_rgba); |
| 303 | exit(1); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* ========================================================================= */ |
| 308 | /* PROOF A-5: Remainder Odd Pixel Handling in avx_simd_uyvy_to_bgra */ |
| 309 | /* ========================================================================= */ |
| 310 | |
| 311 | static void proof_a5_remainder_odd_pixel_drop_bgra(void) { |
| 312 | const int width = 15; |
| 313 | const int height = 1; |
| 314 | const size_t uyvy_bytes = width * height * 2; |
| 315 | const size_t bgra_bytes = width * height * 4; |
| 316 | |
| 317 | uint8_t *src_uyvy = (uint8_t*)malloc(uyvy_bytes); |
| 318 | uint8_t *dst_bgra = (uint8_t*)malloc(bgra_bytes); |
| 319 | assert(src_uyvy && dst_bgra); |
| 320 | |
| 321 | memset(src_uyvy, 128, uyvy_bytes); |
| 322 | memset(dst_bgra, 0x55, bgra_bytes); /* Poison sentinel 0x55 */ |
| 323 | |
| 324 | printf("[Child] Converting 15 pixels (UYVY -> BGRA)...\n"); |
| 325 | avx_simd_uyvy_to_bgra(src_uyvy, dst_bgra, width, height); |
| 326 | |
| 327 | size_t last_px_offset = (width - 1) * 4; |
| 328 | uint8_t b = dst_bgra[last_px_offset + 0]; |
| 329 | uint8_t g = dst_bgra[last_px_offset + 1]; |
| 330 | uint8_t r = dst_bgra[last_px_offset + 2]; |
| 331 | uint8_t a = dst_bgra[last_px_offset + 3]; |
| 332 | |
| 333 | printf("[Child] Pixel 14 (Last Pixel) Raw Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", b, g, r, a); |
| 334 | |
| 335 | if (a == 255 && b == 0x82 && g == 0x82 && r == 0x82) { |
| 336 | printf("[Child] PASSED: The 15th odd pixel in UYVY->BGRA is correctly converted!\n"); |
| 337 | free(src_uyvy); |
| 338 | free(dst_bgra); |
| 339 | exit(0); |
| 340 | } else { |
| 341 | printf("[Child] FAILED: Unexpected pixel 14 value (got 0x%02X 0x%02X 0x%02X 0x%02X).\n", b, g, r, a); |
| 342 | free(src_uyvy); |
| 343 | free(dst_bgra); |
| 344 | exit(1); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /* ========================================================================= */ |
| 349 | /* A-6: Unaligned Access & Byte-Order Safety in avx_scalar_rgba_to_bgra */ |
| 350 | /* ========================================================================= */ |
| 351 | |
| 352 | static void proof_a6_aliasing_and_endianness_hazard(void) { |
| 353 | printf("[Child] Checking avx_scalar_rgba_to_bgra() on unaligned buffers...\n"); |
| 354 | fflush(stdout); |
| 355 | |
| 356 | /* Allocate buffer with unaligned offset (+1 byte) */ |
| 357 | uint8_t raw_buf[64]; |
| 358 | uint8_t *unaligned_src = raw_buf + 1; |
| 359 | uint8_t *unaligned_dst = raw_buf + 33; |
| 360 | |
| 361 | /* Pixel 0: R=10, G=20, B=30, A=255 */ |
| 362 | unaligned_src[0] = 10; |
| 363 | unaligned_src[1] = 20; |
| 364 | unaligned_src[2] = 30; |
| 365 | unaligned_src[3] = 255; |
| 366 | |
| 367 | avx_scalar_rgba_to_bgra(unaligned_src, unaligned_dst, 1, 1); |
| 368 | |
| 369 | printf("[Child] In: R=%u, G=%u, B=%u, A=%u\n", |
| 370 | unaligned_src[0], unaligned_src[1], unaligned_src[2], unaligned_src[3]); |
| 371 | printf("[Child] Out: B=%u, G=%u, R=%u, A=%u\n", |
| 372 | unaligned_dst[0], unaligned_dst[1], unaligned_dst[2], unaligned_dst[3]); |
| 373 | |
| 374 | if (unaligned_dst[0] == 30 && unaligned_dst[1] == 20 && |
| 375 | unaligned_dst[2] == 10 && unaligned_dst[3] == 255) { |
| 376 | printf("[Child] PASS: avx_scalar_rgba_to_bgra produced exact BGRA byte order\n"); |
| 377 | exit(0); |
| 378 | } else { |
| 379 | printf("[Child] FAIL: unexpected BGRA output\n"); |
| 380 | exit(1); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* ========================================================================= */ |
| 385 | /* B-1: AVX2 RGBA to UYVY Throughput & Parity */ |
| 386 | /* ========================================================================= */ |
| 387 | |
| 388 | static void proof_b1_compiler_vectorized_rgba_to_uyvy(void) { |
| 389 | printf("[Child] Testing throughput and parity of avx_simd_rgba_to_uyvy...\n"); |
| 390 | fflush(stdout); |
| 391 | |
| 392 | const int width = 1920; |
| 393 | const int height = 1080; |
| 394 | size_t rgba_bytes = (size_t)width * height * 4; |
| 395 | size_t uyvy_bytes = (size_t)width * height * 2; |
| 396 | |
| 397 | uint8_t *src = (uint8_t*)malloc(rgba_bytes); |
| 398 | uint8_t *dst_scalar = (uint8_t*)malloc(uyvy_bytes); |
| 399 | uint8_t *dst_simd = (uint8_t*)malloc(uyvy_bytes); |
| 400 | assert(src && dst_scalar && dst_simd); |
| 401 | |
| 402 | for (size_t i = 0; i < rgba_bytes; ++i) { |
| 403 | src[i] = (uint8_t)((i * 37 + 19) & 0xFF); |
| 404 | } |
| 405 | |
| 406 | /* Bitwise parity check */ |
| 407 | avx_scalar_rgba_to_uyvy(src, dst_scalar, width, height); |
| 408 | avx_simd_rgba_to_uyvy(src, dst_simd, width, height); |
| 409 | |
| 410 | if (memcmp(dst_scalar, dst_simd, uyvy_bytes) != 0) { |
| 411 | printf("[Child] FAIL: mismatch between scalar and SIMD RGBA->UYVY\n"); |
| 412 | free(src); free(dst_scalar); free(dst_simd); |
| 413 | exit(1); |
| 414 | } |
| 415 | |
| 416 | /* Run scalar */ |
| 417 | double t0 = get_time_ms(); |
| 418 | for (int i = 0; i < 20; ++i) { |
| 419 | avx_scalar_rgba_to_uyvy(src, dst_scalar, width, height); |
| 420 | } |
| 421 | double t_scalar = get_time_ms() - t0; |
| 422 | |
| 423 | /* Run SIMD path */ |
| 424 | t0 = get_time_ms(); |
| 425 | for (int i = 0; i < 20; ++i) { |
| 426 | avx_simd_rgba_to_uyvy(src, dst_simd, width, height); |
| 427 | } |
| 428 | double t_simd = get_time_ms() - t0; |
| 429 | |
| 430 | double ratio = t_scalar / t_simd; |
| 431 | printf("[Child] 20 iterations 1080p RGBA->UYVY: Scalar = %.2f ms, SIMD = %.2f ms (Ratio: %.2fx)\n", |
| 432 | t_scalar, t_simd, ratio); |
| 433 | |
| 434 | printf("[Child] PASS: RGBA->UYVY parity matches scalar\n"); |
| 435 | |
| 436 | free(src); |
| 437 | free(dst_scalar); |
| 438 | free(dst_simd); |
| 439 | exit(0); |
| 440 | } |
| 441 | |
| 442 | /* ========================================================================= */ |
| 443 | /* B-2: SIMD Alpha Blend Throughput & Parity */ |
| 444 | /* ========================================================================= */ |
| 445 | |
| 446 | static bool is_emulated_cpu(void) { |
| 447 | if (getenv("QEMU_LD_PREFIX") != NULL || getenv("QEMU_UNAME") != NULL) { |
| 448 | return true; |
| 449 | } |
| 450 | #if defined(__aarch64__) || defined(__arm__) |
| 451 | struct utsname uts; |
| 452 | if (uname(&uts) == 0 && |
| 453 | strncmp(uts.machine, "aarch64", 7) != 0 && |
| 454 | strncmp(uts.machine, "arm", 3) != 0) { |
| 455 | return true; |
| 456 | } |
| 457 | FILE *f = fopen("/proc/cpuinfo", "r"); |
| 458 | if (f) { |
| 459 | char line[256]; |
| 460 | bool foreign_vendor = false; |
| 461 | while (fgets(line, sizeof(line), f)) { |
| 462 | if (strstr(line, "GenuineIntel") || strstr(line, "AuthenticAMD") || |
| 463 | strstr(line, "QEMU") || |
| 464 | (strstr(line, "CPU implementer") && strstr(line, "0x00"))) { |
| 465 | foreign_vendor = true; |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | fclose(f); |
| 470 | if (foreign_vendor) return true; |
| 471 | } |
| 472 | #endif |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | static void proof_b2_avx2_alpha_blend(void) { |
| 477 | avx_simd_type_e caps = avx_simd_get_caps(); |
| 478 | const char *backend = avx_simd_get_name(); |
| 479 | bool emulated = is_emulated_cpu(); |
| 480 | |
| 481 | printf("[Child] Testing %s alpha blend performance and parity...\n", backend); |
| 482 | fflush(stdout); |
| 483 | |
| 484 | const int width = 1920; |
| 485 | const int height = 1080; |
| 486 | size_t bytes = (size_t)width * height * 4; |
| 487 | |
| 488 | uint8_t *fg = (uint8_t*)malloc(bytes); |
| 489 | uint8_t *bg = (uint8_t*)malloc(bytes); |
| 490 | uint8_t *out_s = (uint8_t*)malloc(bytes); |
| 491 | uint8_t *out_v = (uint8_t*)malloc(bytes); |
| 492 | assert(fg && bg && out_s && out_v); |
| 493 | |
| 494 | for (size_t i = 0; i < bytes; ++i) { |
| 495 | fg[i] = (uint8_t)(i & 0xFF); |
| 496 | bg[i] = (uint8_t)((i * 3 + 17) & 0xFF); |
| 497 | } |
| 498 | |
| 499 | avx_scalar_alpha_blend_rgba(fg, bg, out_s, width, height); |
| 500 | avx_simd_alpha_blend_rgba(fg, bg, out_v, width, height); |
| 501 | |
| 502 | if (memcmp(out_s, out_v, bytes) != 0) { |
| 503 | printf("[Child] FAIL: mismatch between scalar and SIMD alpha blend\n"); |
| 504 | free(fg); free(bg); free(out_s); free(out_v); |
| 505 | exit(1); |
| 506 | } |
| 507 | |
| 508 | double t0 = get_time_ms(); |
| 509 | for (int i = 0; i < 20; ++i) { |
| 510 | avx_scalar_alpha_blend_rgba(fg, bg, out_s, width, height); |
| 511 | } |
| 512 | double t_scalar = get_time_ms() - t0; |
| 513 | |
| 514 | t0 = get_time_ms(); |
| 515 | for (int i = 0; i < 20; ++i) { |
| 516 | avx_simd_alpha_blend_rgba(fg, bg, out_v, width, height); |
| 517 | } |
| 518 | double t_simd = get_time_ms() - t0; |
| 519 | |
| 520 | double speedup = (t_simd > 0.0) ? (t_scalar / t_simd) : 0.0; |
| 521 | printf("[Child] 20 iters 1080p Alpha Blend (%s): Scalar = %.2f ms, SIMD = %.2f ms (Speedup: %.2fx)\n", |
| 522 | backend, t_scalar, t_simd, speedup); |
| 523 | |
| 524 | int rc = 0; |
| 525 | if (speedup >= 2.0) { |
| 526 | printf("[Child] PASS: %s alpha blend %.2fx faster with scalar parity\n", backend, speedup); |
| 527 | } else if (emulated || caps == AVX_SIMD_NONE) { |
| 528 | printf("[Child] WARN: speedup %.2fx < 2.0x (%s%s; bitwise parity passed)\n", |
| 529 | speedup, backend, emulated ? ", QEMU emulation" : ""); |
| 530 | } else { |
| 531 | printf("[Child] FAIL: speedup %.2fx < 2.0x on native %s\n", speedup, backend); |
| 532 | rc = 1; |
| 533 | } |
| 534 | |
| 535 | free(fg); free(bg); free(out_s); free(out_v); |
| 536 | exit(rc); |
| 537 | } |
| 538 | |
| 539 | /* ========================================================================= */ |
| 540 | /* PROOF B-3: Complete BGRA to UYVY Parity Across Multiple Resolutions */ |
| 541 | /* ========================================================================= */ |
| 542 | |
| 543 | static void proof_b3_bgra_to_uyvy_parity(void) { |
| 544 | printf("[Child] Testing BGRA -> UYVY bitwise parity and odd remainder handling...\n"); |
| 545 | fflush(stdout); |
| 546 | |
| 547 | int test_widths[] = {1, 2, 3, 4, 7, 8, 15, 16, 17, 31, 32, 64, 1920}; |
| 548 | int test_heights[] = {1, 2, 4, 1080}; |
| 549 | |
| 550 | for (size_t w_i = 0; w_i < sizeof(test_widths)/sizeof(test_widths[0]); ++w_i) { |
| 551 | for (size_t h_i = 0; h_i < sizeof(test_heights)/sizeof(test_heights[0]); ++h_i) { |
| 552 | int w = test_widths[w_i]; |
| 553 | int h = test_heights[h_i]; |
| 554 | size_t total_px = (size_t)w * h; |
| 555 | size_t bgra_bytes = total_px * 4; |
| 556 | size_t uyvy_bytes = total_px * 2; |
| 557 | |
| 558 | uint8_t *src = (uint8_t*)malloc(bgra_bytes); |
| 559 | uint8_t *dst_s = (uint8_t*)malloc(uyvy_bytes); |
| 560 | uint8_t *dst_v = (uint8_t*)malloc(uyvy_bytes); |
| 561 | assert(src && dst_s && dst_v); |
| 562 | |
| 563 | for (size_t i = 0; i < bgra_bytes; ++i) { |
| 564 | src[i] = (uint8_t)((i * 37 + 19) & 0xFF); |
| 565 | } |
| 566 | |
| 567 | avx_scalar_bgra_to_uyvy(src, dst_s, w, h); |
| 568 | avx_simd_bgra_to_uyvy(src, dst_v, w, h); |
| 569 | |
| 570 | if (memcmp(dst_s, dst_v, uyvy_bytes) != 0) { |
| 571 | printf("[Child] FAILED: Bitwise mismatch in BGRA->UYVY for %dx%d!\n", w, h); |
| 572 | free(src); free(dst_s); free(dst_v); |
| 573 | exit(1); |
| 574 | } |
| 575 | |
| 576 | avx_scalar_rgba_to_uyvy(src, dst_s, w, h); |
| 577 | avx_simd_rgba_to_uyvy(src, dst_v, w, h); |
| 578 | |
| 579 | if (memcmp(dst_s, dst_v, uyvy_bytes) != 0) { |
| 580 | printf("[Child] FAILED: Bitwise mismatch in RGBA->UYVY for %dx%d!\n", w, h); |
| 581 | free(src); free(dst_s); free(dst_v); |
| 582 | exit(1); |
| 583 | } |
| 584 | |
| 585 | free(src); free(dst_s); free(dst_v); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | printf("[Child] PASS: BGRA -> UYVY parity verified across odd and even dimensions\n"); |
| 590 | exit(0); |
| 591 | } |
| 592 | |
| 593 | /* ========================================================================= */ |
| 594 | /* MAIN TEST RUNNER */ |
| 595 | /* ========================================================================= */ |
| 596 | |
| 597 | int main(void) { |
| 598 | printf("=== OpenAVX SIMD Safety & Parity Tests ===\n"); |
| 599 | |
| 600 | int passed_count = 0; |
| 601 | int total_count = 9; |
| 602 | |
| 603 | /* A-1 */ |
| 604 | audit_result_t r1 = run_monitored_crash_test(proof_a1_null_channel_pointer, 3000, |
| 605 | "A-1: NULL channel pointer handling in avx_audio_downmix_5_1", |
| 606 | 0); |
| 607 | if (r1.passed) passed_count++; |
| 608 | printf(" [%s] %s\n", r1.passed ? "PASS" : "FAIL", r1.details); |
| 609 | |
| 610 | /* A-2 */ |
| 611 | audit_result_t r2 = run_monitored_crash_test(proof_a2_missing_fma_detection, 3000, |
| 612 | "A-2: detect_cpu_caps FMA check and thread safety", |
| 613 | 0); |
| 614 | if (r2.passed) passed_count++; |
| 615 | printf(" [%s] %s\n", r2.passed ? "PASS" : "FAIL", r2.details); |
| 616 | |
| 617 | /* A-3 */ |
| 618 | audit_result_t r3 = run_monitored_crash_test(proof_a3_planes_buffer_over_read, 3000, |
| 619 | "A-3: Multi-channel planes bounds (>32 channels)", |
| 620 | 0); |
| 621 | if (r3.passed) passed_count++; |
| 622 | printf(" [%s] %s\n", r3.passed ? "PASS" : "FAIL", r3.details); |
| 623 | |
| 624 | /* A-4 */ |
| 625 | audit_result_t r4 = run_monitored_crash_test(proof_a4_remainder_odd_pixel_drop, 3000, |
| 626 | "A-4: Remainder pixel handling in avx_simd_uyvy_to_rgba", |
| 627 | 0); |
| 628 | if (r4.passed) passed_count++; |
| 629 | printf(" [%s] %s\n", r4.passed ? "PASS" : "FAIL", r4.details); |
| 630 | |
| 631 | /* A-5 */ |
| 632 | audit_result_t r5 = run_monitored_crash_test(proof_a5_remainder_odd_pixel_drop_bgra, 3000, |
| 633 | "A-5: Remainder pixel handling in avx_simd_uyvy_to_bgra", |
| 634 | 0); |
| 635 | if (r5.passed) passed_count++; |
| 636 | printf(" [%s] %s\n", r5.passed ? "PASS" : "FAIL", r5.details); |
| 637 | |
| 638 | /* A-6 */ |
| 639 | audit_result_t r6 = run_monitored_crash_test(proof_a6_aliasing_and_endianness_hazard, 3000, |
| 640 | "A-6: Unaligned access and byte order in avx_scalar_rgba_to_bgra", |
| 641 | 0); |
| 642 | if (r6.passed) passed_count++; |
| 643 | printf(" [%s] %s\n", r6.passed ? "PASS" : "FAIL", r6.details); |
| 644 | |
| 645 | /* B-1 */ |
| 646 | audit_result_t r7 = run_monitored_crash_test(proof_b1_compiler_vectorized_rgba_to_uyvy, 5000, |
| 647 | "B-1: AVX2 RGBA to UYVY throughput and parity", |
| 648 | 0); |
| 649 | if (r7.passed) passed_count++; |
| 650 | printf(" [%s] %s\n", r7.passed ? "PASS" : "FAIL", r7.details); |
| 651 | |
| 652 | /* B-2 */ |
| 653 | audit_result_t r8 = run_monitored_crash_test(proof_b2_avx2_alpha_blend, 5000, |
| 654 | "B-2: SIMD alpha blend throughput and parity", |
| 655 | 0); |
| 656 | if (r8.passed) passed_count++; |
| 657 | printf(" [%s] %s\n", r8.passed ? "PASS" : "FAIL", r8.details); |
| 658 | |
| 659 | /* B-3 */ |
| 660 | audit_result_t r9 = run_monitored_crash_test(proof_b3_bgra_to_uyvy_parity, 3000, |
| 661 | "B-3: BGRA to UYVY parity across odd and even widths", |
| 662 | 0); |
| 663 | if (r9.passed) passed_count++; |
| 664 | printf(" [%s] %s\n", r9.passed ? "PASS" : "FAIL", r9.details); |
| 665 | |
| 666 | printf("\n[%s] openavx_simd_audit_test: %d / %d passed\n\n", |
| 667 | (passed_count == total_count) ? "PASS" : "FAIL", passed_count, total_count); |
| 668 | |
| 669 | return (passed_count == total_count) ? 0 : 1; |
| 670 | } |