본문으로 건너뛰기
AI NEWS JOURNAL
Hacker News Show HNAI NEWS / JOURNAL

Show HN: I abandoned my AVX2 128-bit fixed-point library

Article URL: https://github.com/arjavsharma91/v4q128 Comments URL: https://news.ycombinator.com/item?id=50434431 Points: 1 # Comments: 1

번역 서비스가 일시 중지되어 원문을 표시합니다.

An experimental, archived 4-lane AVX2 SIMD Q64.64 fixed-point arithmetic library. Developed to explore high-throughput 128-bit fixed-point math on x86_64 architecture, the project was archived after extensive benchmarking proved that manual vectorization overhead and fundamental hardware limitations surrender performance to modern compiler-optimized scalar code. Performance Benchmarks Architecture & Design 1. Memory Layout & Dual-Limb SoA Structure-of-Arrays (SoA): Separates lower and upper 64-bit limbs across distinct AVX2 registers (__m256i lo, __m256i hi) to process four 128-bit values concurrently within 256-bit registers. Cross-Lane Avoidance: Segregates limbs within local 128-bit lanes to completely avoid expensive cross-lane permutations (vpermq) and instruction latency penalties. 2. Addition and Subtraction Due to the absence of native unsigned 64-bit comparison instructions in AVX2, arithmetic is implemented via branchless sign-bit masking: Addition: Computes the unsigned sum of lo limbs, maps them to signed ranges via an MSB flip (_mm256_xor_si256), detects wrap-around carries via _mm256_cmpgt_epi64, and adjusts hi via masked subtraction. Subtraction: Computes lo differences, detects borrows, and adjusts hi using a -1 bitmask. 3. Unary Operations neg(v): Two's complement two-step using 0 - lo and not_hi - is_zero (using an all-ones mask when lo == 0) to handle carry propagation branchlessly. abs(v) & sign(v): Isolates the sign via _mm256_cmpgt_epi64 against hi and blends conditionally using _mm256_blendv_epi8. 4. Multiplication & ILP Strategy 32-Bit Sub-Limb Splitting: Deconstructs 64-bit limbs into 32-bit chunks to cross-multiply without widening bottlenecks. Deliberate Register Spilling: Intentionally permits register spilling during complex multiplication, allowing spilled data to sit in the L1 store buffer. This trades a minor 5–7 cycle store-to-load forwarding cost to avoid serial carry chain execution stalls and maximize Instruction-Level Parallelism (ILP). 5. Division Strategy (Dual-Path) div_approx: Converts vectors into double-precision floating-point (__m256d), performs hardware SIMD division, and converts back (trading bit-exact accuracy for significant speed gains). div_exact: Extracts lanes and falls back to a branchless scalar 128-bit __int128 implementation to guarantee bit-exact accuracy without rounding drift. 6. Comparison Operations Equality (==): Combines independent lo and hi equality checks via bitwise AND (_mm256_and_si256). Greater Than (>): Hierarchical check evaluating hi > hi_b, or hi == hi_b combined with MSB-adjusted lo > lo_b. All other comparisons (<, <=, >=, !=) derive logically from these base operations. 7. Shift Operations (shl, shr, sra) Compile-Time Constant Shifts (_imm): Utilizes constexpr branching. Isolates bits crossing the 64-bit boundary via opposite shifts combined with bitwise OR for $N &lt; 64$, direct limb remapping for $64 \le N &lt; 128$, and zeroing/sign-extension for $N \ge 128$. Runtime Variable Shifts (_var): Combines AVX2 variable shift primitives (_mm256_sllv_epi64, _mm256_srlv_epi64) with comparison masks (gt63, gt127) and blending logic (_mm256_blendv_epi8) to dynamically route shift counts. Root Cause Analysis: Why Manual Vectorization Failed 1. The Missing Hardware Instruction Tax Modern x86_64 AVX2 was designed primarily for 32-bit floating-point throughput and 8/16/32-bit integer multimedia processing. For wide 128-bit fixed-point arithmetic, AVX2 exhibits critical hardware gaps: No Native 128-bit Registers / Datapaths: The architecture lacks native 128-bit integer registers and true cross-lane carry/borrow propagation. No Unsigned 64-bit Comparisons: AVX2 lacks unsigned 64-bit comparison instructions (_mm256_cmpgt_epi64 treats operands as signed). This forces an intricate sign-bit masking workaround (_mm256_xor_si256 with MSB masks) just to emulate unsigned carry detection. The Result: A scalar 64-bit instruction set handles 128-bit additions natively via a simple add followed by an adc (add-with-carry) hardware pipeline execution with zero instruction bloat. In contrast, emulating that exact carry chain across split limbs in AVX2 requires a multi-instruction sequence per lane (adds, XORs, signed compares, and masked adjustments), completely destroying the performance advantage of parallel execution. 2. Register Pressure and Compiler Allocator Choke To implement a 4-lane Structure-of-Arrays (SoA) layout, the codebase must simultaneously juggle multiple __m256i vectors for lower (lo) and upper (hi) limbs across all operands. AVX2 provides only 16 YMM registers (ymm0-ymm15). During complex routines like multiplication and variable shifts, tracking split limbs, constants, and temporary masks forces the compiler's register allocator into heavy register spilling to the stack. While deliberate register spilling was utilized to maintain Instruction-Level Parallelism (ILP) via L1 store forwarding, the cumulative overhead of memory round-trips outweighed the throughput gains of vectorization. 3. The Auto-Vectorization Superiority Paradox Manual vectorization binds the developer to rigid intrinsic schedules. Modern LLVM and GCC auto-vectorizers frequently outperform manual implementations on scalar code structures because: They evaluate scalar loops with native __int128 types or optimized 64-bit limb sequences without the artificial register-allocation bottlenecks imposed by split-limb intrinsics. They dynamically handle loop unrolling, peeling, and instruction scheduling based on the target microarchitecture (e.g., Skylake vs. Zen execution port pressures), whereas static intrinsics force rigid execution patterns that can cause port contention on specific ALU execution units.

자료 출처

원문 읽기 ↗

관련 주제

← 전체 소식 이어 읽기