Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
2.1 kB
4
Indexable
Never
#include <iostream>
#include <chrono>

void ASM_PUSH();

int main() {
    ASM_PUSH();
    return 0;
}

void ASM_PUSH() {
    const size_t numIterations = 1000000;
    const size_t arraySize = 1024 * 1024 * 10;

    char* data = new char[arraySize];
    if (!data) {
        std::cerr << "Memory allocation failed" << std::endl;
        return;
    }

    memset(data, 'A', arraySize);

    auto start = std::chrono::high_resolution_clock::now();

    asm volatile (
        "mov x1, %[data]\n\t"          // Set x1 to point to data
        "mov x2, %[arraySize]\n\t"     // Set x2 to arraySize
        "mov x3, %[numIterations]\n\t" // Set x3 to numIterations
        "1:\n\t"                       // Outer loop label
        "mov x4, x2\n\t"               // Reset inner loop counter
        "2:\n\t"                       // Inner loop label
        "ldr b0, [x1], #1\n\t"         // Load byte from memory and increment pointer
        "ldr b0, [x1], #1\n\t"
        "ldr b0, [x1], #1\n\t"
        "ldr b0, [x1], #1\n\t"
        "ldr b0, [x1], #1\n\t"
        "ldr b0, [x1], #1\n\t"
        "ldr b0, [x1], #1\n\t"
        "ldr b0, [x1], #1\n\t"
        "subs x4, x4, #8\n\t"          // Decrement inner loop counter by 8
        "b.ne 2b\n\t"                  // Branch to inner loop label if not zero
        "subs x3, x3, #1\n\t"          // Decrement outer loop counter
        "b.ne 1b\n\t"                  // Branch to outer loop label if not zero
        :
        : [data] "r" (data), [arraySize] "r" (arraySize), [numIterations] "r" (numIterations)
        : "x0", "x1", "x2", "x3", "x4", "memory"
    );

    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;

    double elapsedTime = elapsed.count();
    double bandwidthMBps = (arraySize * numIterations / (1024.0 * 1024.0)) / elapsedTime;

    std::cout << "Elapsed Time: " << elapsedTime << " seconds" << std::endl;
    std::cout << "Memory Read Bandwidth: " << bandwidthMBps << " MB/s" << std::endl;

    delete[] data;
}
Leave a Comment