Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen-max-2025-01-25· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

Write SIMD and some architecture specific optimized memcpy code for Zen2. in ASM and C.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219-thinking-32k’s answer.
  1. User

    Write SIMD and some architecture specific optimized memcpy code for Zen2. in ASM and C.

  2. Qwen Assistant

    Understanding the Problem

    The task is to write an optimized memcpy function for AMD's Zen 2 architecture, leveraging SIMD (Single Instruction, Multiple Data) instructions and architecture-specific features. Zen 2 processors support AVX2 (Advanced Vector Extensions 2), which allows operations on 256-bit wide registers. This can significantly speed up memory copy operations by processing larger chunks of data in parallel.

    Key Considerations

    1. Alignment: Modern CPUs perform better when memory accesses are aligned to cache line boundaries (typically 64 bytes). Misaligned accesses can degrade performance.
    2. SIMD Instructions: Use AVX2 instructions (vmovdqa, vmovdqu, etc.) to move 32 bytes at a time.
    3. Loop Unrolling: Reduce loop overhead by processing multiple SIMD operations per iteration.
    4. Residual Handling: Handle cases where the total size is not a multiple of the SIMD vector width (32 bytes for AVX2).
    5. Branch Prediction: Minimize unpredictable branches to avoid pipeline stalls.

    Implementation in Assembly (x86-64, Zen 2 Optimized)

    Below is an assembly implementation of memcpy using AVX2 instructions:

    section .text
    global memcpy_avx2
    
    ; void* memcpy_avx2(void* dst, const void* src, size_t n)
    ; rdi = dst, rsi = src, rdx = n
    memcpy_avx2:
        ; Prologue
        mov     rcx, rdx                ; Save length in RCX
        test    rcx, rcx                ; Check if length is zero
        jz      .done                   ; If zero, return immediately
    
        ; Align destination pointer to 32-byte boundary
        mov     r8, rdi                 ; Copy dst to R8
        and     r8, 0x1F                ; Get misalignment offset
        jz      .aligned                ; If already aligned, skip alignment phase
    
        ; Align dst by copying byte-by-byte
        neg     r8                      ; Calculate bytes to align
        add     r8, 32                  ; Ensure we don't overshoot
        cmp     r8, rcx                 ; Ensure we don't copy more than length
        cmovg   r8, rcx                 ; Clamp to remaining length
        sub     rcx, r8                 ; Update remaining length
    .copy_misaligned:
        mov     al, [rsi]               ; Load byte from src
        mov     [rdi], al               ; Store byte to dst
        inc     rsi                     ; Increment src pointer
        inc     rdi                     ; Increment dst pointer
        dec     r8                      ; Decrement alignment counter
        jnz     .copy_misaligned        ; Repeat until aligned
    
    .aligned:
        ; Main SIMD loop (process 32 bytes per iteration)
        mov     r9, rcx                 ; Save remaining length
        shr     rcx, 5                  ; Divide by 32 to get number of full vectors
        jz      .residual               ; If no full vectors, skip to residual handling
    
    .simd_loop:
        vmovdqu ymm0, [rsi]             ; Load 32 bytes from src
        vmovdqa [rdi], ymm0             ; Store 32 bytes to dst
        add     rsi, 32                 ; Increment src pointer
        add     rdi, 32                 ; Increment dst pointer
        dec     rcx                     ; Decrement vector counter
        jnz     .simd_loop              ; Repeat until all full vectors are copied
    
    .residual:
        ; Handle residual bytes (less than 32)
        and     r9, 0x1F                ; Get residual length
        jz      .done                   ; If no residual, we're done
    
    .residual_loop:
        mov     al, [rsi]               ; Load byte from src
        mov     [rdi], al               ; Store byte to dst
        inc     rsi                     ; Increment src pointer
        inc     rdi                     ; Increment dst pointer
        dec     r9                      ; Decrement residual counter
        jnz     .residual_loop          ; Repeat until all residuals are copied
    
    .done:
        vzeroupper                      ; Clear upper YMM bits to avoid penalties
        ret                             ; Return dst pointer
    

    Implementation in C (Using Intrinsics)

    Below is a C implementation using AVX2 intrinsics. This approach is portable and easier to integrate into existing codebases while still leveraging SIMD optimizations.

    #include <immintrin.h>
    #include <string.h>
    
    void* memcpy_avx2(void* dst, const void* src, size_t n) {
        char* d = (char*)dst;
        const char* s = (const char*)src;
    
        // Handle small sizes or misaligned cases
        if (n < 32) {
            return memcpy(dst, src, n);
        }
    
        // Align destination to 32-byte boundary
        size_t align_offset = (size_t)d & 0x1F;
        if (align_offset != 0) {
            align_offset = 32 - align_offset;
            if (align_offset > n) align_offset = n;
            memcpy(d, s, align_offset);
            d += align_offset;
            s += align_offset;
            n -= align_offset;
        }
    
        // Main SIMD loop
        __m256i* dst_ptr = (__m256i*)d;
        const __m256i* src_ptr = (const __m256i*)s;
        size_t simd_count = n / 32;
    
        for (size_t i = 0; i < simd_count; ++i) {
            __m256i data = _mm256_loadu_si256(src_ptr + i);
            _mm256_store_si256(dst_ptr + i, data);
        }
    
        // Handle residual bytes
        size_t residual = n % 32;
        if (residual > 0) {
            memcpy((char*)(dst_ptr + simd_count), (const char*)(src_ptr + simd_count), residual);
        }
    
        return dst;
    }
    

    Explanation of the Code

    1. Assembly Version:

      • The assembly version explicitly handles alignment and uses AVX2 instructions (vmovdqu and vmovdqa) for efficient memory transfers.
      • It processes 32 bytes per iteration in the main loop and falls back to byte-by-byte copying for residuals.
    2. C Version:

      • The C version uses AVX2 intrinsics (_mm256_loadu_si256 and _mm256_store_si256) to achieve similar performance.
      • It relies on memcpy for small or misaligned cases, ensuring correctness without sacrificing readability.

    Performance Notes

    • Zen 2 Architecture: Zen 2 has a high throughput for AVX2 instructions, making this implementation highly efficient.
    • Cache Efficiency: Both implementations ensure that memory accesses are cache-friendly by aligning pointers and minimizing misaligned loads/stores.
    • Fallback: For very small sizes, the fallback to memcpy ensures compatibility and avoids unnecessary overhead.

    This optimized memcpy implementation should provide significant performance improvements over generic implementations on Zen 2 processors.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.