// Copyright 2021 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

$assert BATCH_TILE >= 1
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include "xnnpack/lut.h"
#include "xnnpack/common.h"


void xnn_x8_lut_ukernel__scalar_u${BATCH_TILE}(
    size_t batch,
    const uint8_t* input,
    uint8_t* output,
    const uint8_t table[restrict XNN_MIN_ELEMENTS(256)])
{
  assert(batch != 0);
  assert(batch % sizeof(uint8_t) == 0);
  assert(input != NULL);
  assert(output != NULL);

  $if BATCH_TILE > 1:
    for (; batch >= ${BATCH_TILE} * sizeof(uint8_t); batch -= ${BATCH_TILE} * sizeof(uint8_t)) {
      $for N in range(BATCH_TILE):
        const size_t vx${N} = (size_t) input[${N}];
      input += ${BATCH_TILE};

      $for N in range(BATCH_TILE):
        const uint32_t vt${N} = (uint32_t) table[vx${N}];

      $for N in range(BATCH_TILE):
        output[${N}] = (uint8_t) vt${N};
      output += ${BATCH_TILE};
    }
    if XNN_UNLIKELY(batch != 0) {
      $if BATCH_TILE > 2:
        do {
          const size_t vx = (size_t) *input++;
          const uint32_t vt = (uint32_t) table[vx];
          *output++ = (uint8_t) vt;
          batch -= sizeof(uint8_t);
        } while (batch != 0);
      $else:
        const size_t vx = (size_t) *input;
        const uint32_t vt = (uint32_t) table[vx];
        *output = (uint8_t) vt;
    }
  $else:
    do {
      const size_t vx = (size_t) *input++;
      const uint32_t vt = (uint32_t) table[vx];
      *output++ = (uint8_t) vt;
      batch -= sizeof(uint8_t);
    } while (batch != 0);
}
