' asm_mmx.pb — MMX SIMD operations simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL dataA(100) AS LONG
    LOCAL dataB(100) AS LONG
    LOCAL result(100) AS LONG
    LOCAL dataSize AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL sumA AS LONG
    LOCAL sumB AS LONG
    LOCAL sumResult AS LONG
    LOCAL packedSum AS LONG
    LOCAL packedDiff AS LONG
    LOCAL avgVal AS LONG
    LOCAL iter AS LONG
    LOCAL numOps AS LONG
    LOCAL aVal AS LONG
    LOCAL bVal AS LONG
    LOCAL resVal AS LONG
    LOCAL totalTime AS LONG

    dataSize = 16

    PRINT "=== MMX SIMD Simulation ==="
    PRINT "Vector size: " + STR$(dataSize) + " elements"

    FOR i = 0 TO dataSize - 1
        dataA(i) = i * 3
        dataB(i) = i * 2
    NEXT i

    PRINT "Data A:"
    FOR i = 0 TO dataSize - 1
        sumA = sumA + dataA(i)
    NEXT i
    PRINT "  Sum: " + STR$(sumA)

    PRINT "Data B:"
    FOR i = 0 TO dataSize - 1
        sumB = sumB + dataB(i)
    NEXT i
    PRINT "  Sum: " + STR$(sumB)

    PRINT "SIMD Packed Add (paddw):"
    FOR i = 0 TO dataSize - 1
        aVal = dataA(i)
        bVal = dataB(i)
        resVal = aVal + bVal
        result(i) = resVal
        packedSum = packedSum + resVal
        IF i < 8 THEN
            PRINT "  [" + STR$(i) + "] " + STR$(aVal) + " + " + STR$(bVal) + " = " + STR$(resVal)
        END IF
    NEXT i
    PRINT "  Packed sum: " + STR$(packedSum)

    PRINT "SIMD Packed Sub (psubw):"
    FOR i = 0 TO dataSize - 1
        aVal = dataA(i)
        bVal = dataB(i)
        resVal = aVal - bVal
        result(i) = resVal
        packedDiff = packedDiff + resVal
        IF i < 8 THEN
            PRINT "  [" + STR$(i) + "] " + STR$(aVal) + " - " + STR$(bVal) + " = " + STR$(resVal)
        END IF
    NEXT i
    PRINT "  Packed diff sum: " + STR$(packedDiff)

    PRINT "=== End MMX ==="
    FUNCTION = 0
END FUNCTION
