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_x4(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_x4(
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 >= 4; batch -= 4) {
59 const uint32_t vi0 = input[0];
60 const uint32_t vi1 = input[1];
61 const uint32_t vi2 = input[2];
62 const uint32_t vi3 = input[3];
63 input += 4;
64
65 const uint32_t scaled0 = vi0 << input_lshift;
66 const uint32_t scaled1 = vi1 << input_lshift;
67 const uint32_t scaled2 = vi2 << input_lshift;
68 const uint32_t scaled3 = vi3 << input_lshift;
69
70 const uint32_t log_value0 = XNN_LIKELY(scaled0 != 0) ? xnn_u32_log32(scaled0, output_scale) : 0;
71
72 const uint32_t vout0 = math_min_u32(log_value0, (uint32_t) INT16_MAX); // signed max value
73 output[0] = (uint16_t) vout0;
74 const uint32_t log_value1 = XNN_LIKELY(scaled1 != 0) ? xnn_u32_log32(scaled1, output_scale) : 0;
75
76 const uint32_t vout1 = math_min_u32(log_value1, (uint32_t) INT16_MAX); // signed max value
77 output[1] = (uint16_t) vout1;
78 const uint32_t log_value2 = XNN_LIKELY(scaled2 != 0) ? xnn_u32_log32(scaled2, output_scale) : 0;
79
80 const uint32_t vout2 = math_min_u32(log_value2, (uint32_t) INT16_MAX); // signed max value
81 output[2] = (uint16_t) vout2;
82 const uint32_t log_value3 = XNN_LIKELY(scaled3 != 0) ? xnn_u32_log32(scaled3, output_scale) : 0;
83
84 const uint32_t vout3 = math_min_u32(log_value3, (uint32_t) INT16_MAX); // signed max value
85 output[3] = (uint16_t) vout3;
86
87 output += 4;
88 }
89
90 if XNN_UNLIKELY(batch != 0) {
91 do {
92 const uint32_t vi = *input++;
93 const uint32_t scaled = vi << input_lshift;
94
95 const uint32_t log_value = XNN_LIKELY(scaled != 0) ? xnn_u32_log32(scaled, output_scale) : 0;
96
97 const uint32_t vout = math_min_u32(log_value, (uint32_t) INT16_MAX);
98 *output++ = (uint16_t) vout;
99 } while (--batch != 0);
100 }
101 }
102