
{% macro scalar(label, op, from, to, flipped=false) %}
.{{label}}:
    add         x2, x0, #8
    ld1         {v0.s}[0], [ x2 ]
    dup         v0.4s, v0.s[0]
    {% if flipped %}
        {% for reg in range(from, to + 1) %}
            {{op}}       v{{reg}}.4s, v{{reg}}.4s, v0.4s
        {% endfor %}
    {% else %}
        {% for reg in range(from, to + 1) %}
            {{op}}       v{{reg}}.4s, v0.4s, v{{reg}}.4s
        {% endfor %}
    {% endif %}

    b           .non_linear_loop
{% endmacro %}

{% macro per_row(label, op, mr, from, to, flipped=false) %}
.{{label}}:
    ldr         x2, [x0, #8]

{% set mr_over_4 = mr // 4 %}
{% set mr_over_4_min_1 = mr // 4 - 1 %}

{% for reg in range(0, mr_over_4_min_1 + 1) %}
    ldr         q{{reg}}, [ x2 ], #16
{% endfor %}

{% if flipped %}
    {% for acc in range(from, to + 1) %}
        {% set other = (acc - from) % mr_over_4 %}
        {{op}} v{{acc}}.4s, v{{acc}}.4s, v{{other}}.4s
    {% endfor %}
{% else %}
    {% for acc in range(from, to + 1) %}
        {% set other = (acc - from) % mr_over_4 %}
        {{op}} v{{acc}}.4s, v{{other}}.4s, v{{acc}}.4s
    {% endfor %}
{% endif %}

b           .non_linear_loop
{% endmacro %}

{% macro per_col(label, op, mr, from, to, flipped=false) %}
.{{label}}:
    ldr         x2, [x0, #8]

{% set mr_over_4 = mr // 4 %}
{% set cols = (to + 1 - from) // mr_over_4 %}

{% set loads = cols // 4 %}

{%if cols == 1 %}
        ld1         {v0.s}[0], [ x2 ]
{% elif cols == 3 %}
        ldr         d0, [ x2 ], #8
        ld1         {v0.s}[2], [ x2 ]
{% else %}
    {% for reg in range(1, loads + 1) %}
        ldr         q{{ reg - 1 }}, [ x2 ], #16
    {% endfor %}
{% endif %}

// {{mr}} {{cols}}

{% for col in range(1, cols + 1) %}
    dup v3.4s, v{{ (col - 1) // 4 }}.s[{{ (col - 1) % 4 }}]
    {% for row in range(1, mr_over_4 + 1) %}
        {% set acc = (col - 1) * mr_over_4 + row - 1 + from %}
        {% if flipped %}
            {{op}} v{{acc}}.4s, v{{acc}}.4s, v3.4s
        {% else %}
            {{op}} v{{acc}}.4s, v3.4s, v{{acc}}.4s
        {% endif %}
    {% endfor %}
{% endfor %}

b           .non_linear_loop
{% endmacro %}
