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_x1(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_x1(
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
59 if XNN_UNLIKELY(batch != 0) {
60 do {
61 const uint32_t vi = *input++;
62 const uint32_t scaled = vi << input_lshift;
63
64 const uint32_t log_value = XNN_LIKELY(scaled != 0) ? xnn_u32_log32(scaled, output_scale) : 0;
65
66 const uint32_t vout = math_min_u32(log_value, (uint32_t) INT16_MAX);
67 *output++ = (uint16_t) vout;
68 } while (--batch != 0);
69 }
70 }
71