
{% macro scalar(label, op, from, to, type="f32", flipped=false) %}
{{L}}{{label}}:
    {% if type == "f16" %}
        pinsrw          xmm12, word ptr [rdi + 8], 0
        vcvtph2ps       ymm12, xmm12
        vbroadcastss    ymm12, xmm12
    {% else %}
        vbroadcastss    ymm12, dword ptr [rdi + 8]
    {% endif %}

    {% if flipped %}
        {% for reg in range(from, to + 1) %}
            {{op}}          ymm{{reg}}, ymm{{reg}}, ymm12
        {% endfor %}
    {% else %}
        {% for reg in range(from, to + 1) %}
            {{op}}          ymm{{reg}}, ymm12, ymm{{reg}}
        {% endfor %}
    {% endif %}

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

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

{% set mr_over_8 = mr // 8 %}
{% set mr_over_8_min_1 = mr // 8 - 1 %}

{% if type == "f16" %}
    {% for ix in range(0, mr_over_8_min_1 + 1) %}
        vmovups         xmm{{ to + 1 + ix }},  [rax + {{ ix * 16 }}]
    {% endfor %}
    {% for ix in range(0, mr_over_8_min_1 + 1) %}
        vcvtph2ps       ymm{{ to + 1 + ix }}, xmm{{ to + 1 + ix }}
    {% endfor %}
{% else %}
    {% for ix in range(0, mr_over_8_min_1 + 1) %}
        vmovups         ymm{{ to + 1 + ix }},  [rax + {{ ix * 32 }}]
    {% endfor %}
{% endif %}

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

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

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

{% set mr_over_8 = mr // 8 %}
{% set mr_over_8_min_1 = mr // 8 - 1 %}

{% set tmp = to + 1 %}

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


{% for right in range(0, cols_min_1 + 1) %}
    {% if type == "f16" %}
        pinsrw          xmm{{tmp}}, word ptr [ rax ], 0
        add             rax, 2
        vcvtph2ps      ymm{{tmp}}, xmm{{tmp}}
        vbroadcastss    ymm{{tmp}}, xmm{{tmp}}
    {% else %}
        vbroadcastss    ymm{{tmp}}, dword ptr [ rax ]
        add             rax, 4
    {% endif %}
    {% for down in range(0, mr_over_8_min_1 + 1) %}
        {% set acc = mr_over_8 * right + from + down %}
        {% if flipped %}
            {{op}} ymm{{acc}}, ymm{{acc}}, ymm{{tmp}}
        {% else %}
            {{op}} ymm{{acc}}, ymm{{tmp}}, ymm{{acc}}
        {% endif %}
    {% endfor %}
{% endfor %}

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