chat3.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc3100/spring2026/notes/swar/code/chat3.cpp
 
// GNU C++ SIMD Within A Register (SWAR) example for x86
// Uses GCC vector extensions (works with g++)
//
// Compile with:
// g++ -O2 -msse2 simd_gnu.cpp -o simd_gnu

#include <iostream>

// Define a 128-bit vector containing four 32-bit integers
typedef float v4sd __attribute__((vector_size(32)));

int main() {
    v4sd a = {1, 2, 3, 4};
    v4sd b = {10, 20, 30, 40};

    // SIMD addition happens in one instruction
    v4sd c = a + b;

    std::cout << "Results:\n";
    for (int i = 0; i < 4; i++) {
        std::cout << a[i] << " + " << b[i]
                  << " = " << c[i] << "\n";
    }

    return 0;
}