
{% macro scalar(label, op, from, to, flipped=false) %}
{{L}}{{label}}:
    vbroadcastss    zmm12, dword ptr [rdi + 8]
    {% if flipped %}
        {% for reg in range(from, to + 1) %}
            {{op}}          zmm{{reg}}, zmm{{reg}}, zmm12
        {% endfor %}
    {% else %}
        {% for reg in range(from, to + 1) %}
            {{op}}          zmm{{reg}}, zmm12, zmm{{reg}}
        {% endfor %}
    {% endif %}

    jmp    {{L}}non_linear_loop
{% endmacro %}

{% macro per_row(label, op, mr, from, to, flipped=false) %}
{{L}}{{label}}:
    mov             rax, [ rdi + 8 ]

{% set mr_over_16 = mr // 16 %}
{% set mr_over_16_min_1 = mr // 16 - 1 %}

{% for ix in range(0, mr_over_16_min_1 + 1) %}
    vmovups         zmm{{ to + 1 + ix }},  [rax + {{ ix * 64 }}]
{% endfor %}

{% if flipped %}
    {% for acc in range(from, to + 1) %}
        {{op}} zmm{{acc}}, zmm{{acc}}, zmm{{ acc % mr_over_16 + to + 1 }}
    {% endfor %}
{% else %}
    {% for acc in range(from, to + 1) %}
        {{op}} zmm{{acc}}, zmm{{ acc % mr_over_16 + to + 1 }}, zmm{{acc}}
    {% endfor %}
{% endif %}

    jmp {{L}}non_linear_loop
{% endmacro %}

{% macro per_col(label, op, mr, from, to, flipped=false) %}
{{L}}{{label}}:
    mov             rax, [ rdi + 8 ]

{% set mr_over_16 = mr // 16 %}
{% set mr_over_16_min_1 = mr // 16 - 1 %}

{% set tmp = to + 1 %}

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

{% for right in range(0, cols_min_1 + 1) %}
    vbroadcastss    zmm{{tmp}}, dword ptr [ rax ]
    add             rax, 4

    {% for down in range(0, mr_over_16_min_1 + 1) %}
        {% set acc = mr_over_16 * right + from + down %}
        {% if flipped %}
            {{op}} zmm{{acc}}, zmm{{acc}}, zmm{{tmp}}
        {% else %}
            {{op}} zmm{{acc}}, zmm{{tmp}}, zmm{{acc}}
        {% endif %}
    {% endfor %}
{% endfor %}

    jmp {{L}}non_linear_loop
{% endmacro %}
