xref: /aosp_15_r20/external/XNNPACK/src/u32-vlog/gen/scalar-x3.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/u32-vlog/scalar.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2022 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
10 #include <assert.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 
14 #include <xnnpack/math.h>
15 #include <xnnpack/vlog.h>
16 
17 extern XNN_INTERNAL const uint16_t xnn_table_vlog[129];
18 
19 #define LOG_SEGMENTS_LOG2 7
20 #define LOG_SCALE 65536
21 #define LOG_SCALE_LOG2 16
22 #define LOG_COEFF 45426
23 
xnn_u32_log32(uint32_t x,uint32_t out_scale)24 static uint32_t xnn_u32_log32(uint32_t x, uint32_t out_scale) {
25   const uint32_t log2x = math_clz_nonzero_u32(x) ^ 31;
26   int32_t frac = x - (UINT32_C(1) << log2x);
27   frac <<= math_doz_u32(LOG_SCALE_LOG2, log2x);
28   frac >>= math_doz_u32(log2x, LOG_SCALE_LOG2);
29 
30   const uint32_t base_seg = frac >> (LOG_SCALE_LOG2 - LOG_SEGMENTS_LOG2);
31   const uint32_t seg_unit = (UINT32_C(1) << LOG_SCALE_LOG2) >> LOG_SEGMENTS_LOG2;
32 
33   const int32_t c0 = xnn_table_vlog[base_seg];
34   const int32_t c1 = xnn_table_vlog[base_seg + 1];
35   const int32_t seg_base = seg_unit * base_seg;
36   const int32_t rel_pos = math_asr_s32((c1 - c0) * (frac - seg_base), LOG_SCALE_LOG2);
37   const uint32_t fraction = frac + c0 + rel_pos;
38   const uint32_t log2 = (log2x << LOG_SCALE_LOG2) + fraction;
39   const uint32_t round = LOG_SCALE >> 1;
40   const uint32_t loge = (math_mulext_u32(log2, LOG_COEFF) + round) >> LOG_SCALE_LOG2;
41 
42   const uint32_t loge_scaled = (out_scale * loge + round) >> LOG_SCALE_LOG2;
43   return loge_scaled;
44 }
45 
xnn_u32_vlog_ukernel__scalar_x3(size_t batch,const uint32_t * input,uint32_t input_lshift,uint32_t output_scale,uint16_t * output)46 void xnn_u32_vlog_ukernel__scalar_x3(
47     size_t batch,
48     const uint32_t* input,
49     uint32_t input_lshift,
50     uint32_t output_scale,
51     uint16_t* output) {
52 
53   assert(batch != 0);
54   assert(input != NULL);
55   assert(input_lshift < 32);
56   assert(output != NULL);
57 
58   for (; batch >= 3; batch -= 3) {
59     const uint32_t vi0 = input[0];
60     const uint32_t vi1 = input[1];
61     const uint32_t vi2 = input[2];
62     input += 3;
63 
64     const uint32_t scaled0 = vi0 << input_lshift;
65     const uint32_t scaled1 = vi1 << input_lshift;
66     const uint32_t scaled2 = vi2 << input_lshift;
67 
68     const uint32_t log_value0 = XNN_LIKELY(scaled0 != 0) ? xnn_u32_log32(scaled0, output_scale) : 0;
69 
70     const uint32_t vout0 = math_min_u32(log_value0, (uint32_t) INT16_MAX);  // signed max value
71     output[0] = (uint16_t) vout0;
72     const uint32_t log_value1 = XNN_LIKELY(scaled1 != 0) ? xnn_u32_log32(scaled1, output_scale) : 0;
73 
74     const uint32_t vout1 = math_min_u32(log_value1, (uint32_t) INT16_MAX);  // signed max value
75     output[1] = (uint16_t) vout1;
76     const uint32_t log_value2 = XNN_LIKELY(scaled2 != 0) ? xnn_u32_log32(scaled2, output_scale) : 0;
77 
78     const uint32_t vout2 = math_min_u32(log_value2, (uint32_t) INT16_MAX);  // signed max value
79     output[2] = (uint16_t) vout2;
80 
81     output += 3;
82   }
83 
84   if XNN_UNLIKELY(batch != 0) {
85     do {
86       const uint32_t vi = *input++;
87       const uint32_t scaled = vi << input_lshift;
88 
89       const uint32_t log_value = XNN_LIKELY(scaled != 0) ? xnn_u32_log32(scaled, output_scale) : 0;
90 
91       const uint32_t vout = math_min_u32(log_value, (uint32_t) INT16_MAX);
92       *output++ = (uint16_t) vout;
93     } while (--batch != 0);
94   }
95 }
96