xref: /aosp_15_r20/external/XNNPACK/src/cs16-vsquareabs/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/vsquareabs.h>
13
14
15void xnn_cs16_vsquareabs_ukernel__scalar_x${BATCH_TILE}(
16    size_t batch,
17    const int16_t* input,
18    uint32_t* output) {
19
20  assert(batch != 0);
21  assert(input != NULL);
22  assert(output != NULL);
23
24  $if BATCH_TILE > 1:
25    for (; batch >= ${BATCH_TILE}; batch -= ${BATCH_TILE}) {
26      $for C in range(BATCH_TILE):
27        const int32_t vr${C} = (int32_t) input[${C * 2}];
28        const int32_t vi${C} = (int32_t) input[${C * 2 + 1}];
29      input += ${BATCH_TILE} * 2;
30
31      $for C in range(BATCH_TILE):
32        const uint32_t vrsquare${C} = (uint32_t) (vr${C} * vr${C});
33        const uint32_t visquare${C} = (uint32_t) (vi${C} * vi${C});
34
35      $for C in range(BATCH_TILE):
36        const uint32_t vout${C} = vrsquare${C} + visquare${C};
37
38      $for C in range(BATCH_TILE):
39        output[${C}] = vout${C};
40      output += ${BATCH_TILE};
41    }
42
43  if XNN_UNLIKELY(batch != 0) {
44    do {
45      const int32_t vr = (int32_t) input[0];
46      const int32_t vi = (int32_t) input[1];
47      input += 2;
48
49      const uint32_t vrsquare = (uint32_t) (vr * vr);
50      const uint32_t visquare = (uint32_t) (vi * vi);
51
52      const uint32_t vout = vrsquare + visquare;
53
54      *output++ = vout;
55    } while (--batch != 0);
56  }
57}
58