xref: /aosp_15_r20/external/XNNPACK/src/u32-vlog/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/vlog.h>
13
14extern XNN_INTERNAL const uint16_t xnn_table_vlog[129];
15
16#define LOG_SEGMENTS_LOG2 7
17#define LOG_SCALE 65536
18#define LOG_SCALE_LOG2 16
19#define LOG_COEFF 45426
20
21static uint32_t xnn_u32_log32(uint32_t x, uint32_t out_scale) {
22  const uint32_t log2x = math_clz_nonzero_u32(x) ^ 31;
23  int32_t frac = x - (UINT32_C(1) << log2x);
24  frac <<= math_doz_u32(LOG_SCALE_LOG2, log2x);
25  frac >>= math_doz_u32(log2x, LOG_SCALE_LOG2);
26
27  const uint32_t base_seg = frac >> (LOG_SCALE_LOG2 - LOG_SEGMENTS_LOG2);
28  const uint32_t seg_unit = (UINT32_C(1) << LOG_SCALE_LOG2) >> LOG_SEGMENTS_LOG2;
29
30  const int32_t c0 = xnn_table_vlog[base_seg];
31  const int32_t c1 = xnn_table_vlog[base_seg + 1];
32  const int32_t seg_base = seg_unit * base_seg;
33  const int32_t rel_pos = math_asr_s32((c1 - c0) * (frac - seg_base), LOG_SCALE_LOG2);
34  const uint32_t fraction = frac + c0 + rel_pos;
35  const uint32_t log2 = (log2x << LOG_SCALE_LOG2) + fraction;
36  const uint32_t round = LOG_SCALE >> 1;
37  const uint32_t loge = (math_mulext_u32(log2, LOG_COEFF) + round) >> LOG_SCALE_LOG2;
38
39  const uint32_t loge_scaled = (out_scale * loge + round) >> LOG_SCALE_LOG2;
40  return loge_scaled;
41}
42
43void xnn_u32_vlog_ukernel__scalar_x${BATCH_TILE}(
44    size_t batch,
45    const uint32_t* input,
46    uint32_t input_lshift,
47    uint32_t output_scale,
48    uint16_t* output) {
49
50  assert(batch != 0);
51  assert(input != NULL);
52  assert(input_lshift < 32);
53  assert(output != NULL);
54
55  $if BATCH_TILE > 1:
56    for (; batch >= ${BATCH_TILE}; batch -= ${BATCH_TILE}) {
57      $for N in range(BATCH_TILE):
58        const uint32_t vi${N} = input[${N}];
59      input += ${BATCH_TILE};
60
61      $for N in range(BATCH_TILE):
62        const uint32_t scaled${N} = vi${N} << input_lshift;
63
64      $for N in range(BATCH_TILE):
65        const uint32_t log_value${N} = XNN_LIKELY(scaled${N} != 0) ? xnn_u32_log32(scaled${N}, output_scale) : 0;
66
67        const uint32_t vout${N} = math_min_u32(log_value${N}, (uint32_t) INT16_MAX);  // signed max value
68        output[${N}] = (uint16_t) vout${N};
69
70      output += ${BATCH_TILE};
71    }
72
73  if XNN_UNLIKELY(batch != 0) {
74    do {
75      const uint32_t vi = *input++;
76      const uint32_t scaled = vi << input_lshift;
77
78      const uint32_t log_value = XNN_LIKELY(scaled != 0) ? xnn_u32_log32(scaled, output_scale) : 0;
79
80      const uint32_t vout = math_min_u32(log_value, (uint32_t) INT16_MAX);
81      *output++ = (uint16_t) vout;
82    } while (--batch != 0);
83  }
84}
85