
{% macro scalar(label, op, from, to, flipped=false) %}
.{{label}}:
    add         x2, x0, #8
    ld1         {v0.h}[0], [ x2 ]
    dup         v0.8h, v0.h[0]
    {% if flipped %}
        {% for reg in range(from, to + 1) %}
            {{op}}       v{{reg}}.8h, v{{reg}}.8h, v0.8h
        {% endfor %}
    {% else %}
        {% for reg in range(from, to + 1) %}
            {{op}}       v{{reg}}.8h, v0.8h, v{{reg}}.8h
        {% 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_8 = mr // 8 %}
{% set mr_over_8_min_1 = mr // 8 - 1 %}

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

{% if flipped %}
    {% for acc in range(from, to + 1) %}
        {% set other = (acc - from) % mr_over_8 %}
        {{op}} v{{acc}}.8h, v{{acc}}.8h, v{{other}}.8h
    {% endfor %}
{% else %}
    {% for acc in range(from, to + 1) %}
        {% set other = (acc - from) % mr_over_8 %}
        {{op}} v{{acc}}.8h, v{{other}}.8h, v{{acc}}.8h
    {% 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_8 = mr // 8 %}
{% set cols = (to + 1 - from) // mr_over_8 %}

{% set loads = cols // 8 %}

{%if cols == 1 %}
        ld1         {v0.h}[0], [ x2 ]
{% elif cols == 3 %}
        ld1         {v0.s}[0], [ x2 ], #4
        ld1         {v0.h}[2], [ x2 ]
{% elif cols == 4 %}
        ldr         d0, [ x2 ]
{% elif cols == 6 %}
        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:{{mr}} {{ loads }} {{cols}}

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

b           .non_linear_loop
{% endmacro %}
