// Jinja2 macros for armv7neon scalar / per-row / per-col operations.
// Shared by both f32 and i32 kernel variants.

{% macro scalar(label, op, from, to, flipped=false) %}
.{{label}}:
    vmov            s0, r3
    vdup.32         q0, d0[0]
    {% if flipped %}
        {% for reg in range(from, to + 1) %}
            {{op}}    q{{reg}}, q{{reg}}, q0
        {% endfor %}
    {% else %}
        {% for reg in range(from, to + 1) %}
            {{op}}    q{{reg}}, q0, q{{reg}}
        {% endfor %}
    {% endif %}
    b .non_linear_loop
{% endmacro %}

{% macro per_row(label, op, mr, from, to, flipped=false) %}
.{{label}}:

{% 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) %}
    vldmia         r3!, { q{{reg}} }
{% endfor %}

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

b           .non_linear_loop
{% endmacro %}

{% macro per_col(label, op, mr, from, to, flipped=false) %}
.{{label}}:

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

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

{% if cols == 1 %}
    vld1.f32        d0[0], [ r3 ]
{% else %}
    {% set cols_over_2_minus_1 = cols // 2 - 1 %}
    {% for c in range(0, cols_over_2_minus_1 + 1) %}
        vldmia      r3!, { d{{c}} }
    {% endfor %}
{% endif %}


{% for right in range(0, cols_min_1 + 1) %}
    vdup.f32 q3, d{{ right // 2 }}[{{ right % 2 }}]
    {% for down in range(0, mr_over_4_min_1 + 1) %}
        {% set acc = mr_over_4 * right + from + down %}
        {% if flipped %}
            {{op}} q{{acc}}, q{{acc}}, q3
        {% else %}
            {{op}} q{{acc}}, q3, q{{acc}}
        {% endif %}
    {% endfor %}
{% endfor %}

    b .non_linear_loop
{% endmacro %}
