xref: /aosp_15_r20/external/XNNPACK/src/s16-rmaxabs/scalar.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1// Copyright 2022 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6$assert BATCH_TILE >= 1
7#include <assert.h>
8#include <stddef.h>
9#include <stdint.h>
10
11#include <xnnpack/math.h>
12#include <xnnpack/rmaxabs.h>
13
14
15void xnn_s16_rmaxabs_ukernel__scalar_x${BATCH_TILE}(
16    size_t batch,
17    const int16_t* input,
18    uint16_t* output) {
19
20  assert(batch > 0);
21  assert(input != NULL);
22  assert(output != NULL);
23
24  $for N in range(BATCH_TILE):
25    uint32_t vmax${N} = 0;
26
27  $if BATCH_TILE > 1:
28    for (; batch >= ${BATCH_TILE}; batch -= ${BATCH_TILE}) {
29      $for N in range(BATCH_TILE):
30        const int32_t vi${N} = (int32_t) input[${N}];
31      input += ${BATCH_TILE};
32
33      $for N in range(BATCH_TILE):
34        const uint32_t vabs${N} = math_abs_s32(vi${N});
35
36      $for N in range(BATCH_TILE):
37        vmax${N} = math_max_u32(vmax${N}, vabs${N});
38    }
39
40    $BATCH_SLICE = 1
41    $while BATCH_SLICE < BATCH_TILE:
42      $for S in range(0, BATCH_TILE, BATCH_SLICE * 2):
43        $if S + BATCH_SLICE < BATCH_TILE:
44          vmax${S} = math_max_u32(vmax${S}, vmax${S + BATCH_SLICE});
45      $BATCH_SLICE *= 2
46
47  if (batch != 0) {
48    do {
49      const int32_t vi = (int32_t) *input++;
50      const uint32_t vabs = math_abs_s32(vi);
51      vmax0 = math_max_u32(vmax0, vabs);
52    } while (--batch != 0);
53  }
54  *output = (uint16_t) vmax0;
55}
56