18#ifndef LUCI_INTERPRETER_PAL_TANH_H
19#define LUCI_INTERPRETER_PAL_TANH_H
26inline void Tanh(
const int flat_size,
const float *input_data,
float *output_data)
28 for (
int i = 0; i < flat_size; i++)
30 float val = input_data[i];
31 float result = std::tanh(val);
32 output_data[i] = result;
36inline void Tanh(int32_t input_multiplier, int32_t input_left_shift,
const int flat_size,
37 const int16_t *ptr_input_data, int16_t *ptr_output_data)
47 if (input_multiplier == 0)
49 input_multiplier = 3 << input_left_shift;
53 int32_t round = (input_left_shift > 0) ? 1 << (input_left_shift - 1) : 0;
55 for (
int i = 0; i < flat_size; ++i, ptr_input_data++, ptr_output_data++)
57 int32_t input_data = ((*ptr_input_data) * input_multiplier + round) >> input_left_shift;
59 uint32_t abs_input_data = abs(input_data);
60 uint32_t uh = abs_input_data >> 8;
70 uint32_t ua = sigmoid_table_uint16[uh];
71 uint32_t ub = sigmoid_table_uint16[uh + 1];
73 uint8_t ut = abs_input_data & 0xFF;
75 result = (ua << 8) + ut * (ub - ua);
78 result = (input_data >= 0) ? (result - (1 << (14 + 9)) + (1 << (9 - 2)))
79 : (-result + (1 << (14 + 9)) + (1 << (9 - 2)) - 1);
84 *ptr_output_data = result;
89inline void Tanh(int32_t input_zero_point, int32_t input_range_radius,
90 int32_t input_multiplier, int32_t input_shift,
91 const int flat_size,
const int8_t* input_data, int8_t* output_data) {
93 static constexpr int32_t kInputIntegerBits = 4;
94 static constexpr int32_t kOutputScale = 7;
95 static constexpr int32_t kMinInt8 = std::numeric_limits<int8_t>::min();
96 static constexpr int32_t kMaxInt8 = std::numeric_limits<int8_t>::max();
98 for (
int i = 0; i < flat_size; ++i) {
100 static_cast<int32_t
>(input_data[i]) - input_zero_point;
101 if (input <= -input_range_radius) {
102 output_data[i] = kMinInt8;
103 }
else if (input >= input_range_radius) {
106 const int32_t input_in_q4 =
108 const int32_t output_in_q0 = std::tanh(input_in_q4);
110 int32_t output_in_q24 =
112 output_in_q24 = std::min(std::max(output_in_q24, kMinInt8), kMaxInt8);
113 output_data[i] =
static_cast<int8_t
>(output_in_q24);
void Tanh(const int flat_size, const float *input_data, float *output_data)
int32_t roundingDivideByPOT(int32_t x, int32_t exponent)
int32_t multiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)