LIBML Version 4.1.9
LIBML NN Software Library
Loading...
Searching...
No Matches
tpt_math_nn.h File Reference
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "libnn/tpt_libnn_version.h"
#include "libnn/nn_pi_values.h"
#include "libnn/tpt_activation.h"
#include "libnn/tpt_basicmath.h"
#include "libnn/tpt_concatenation.h"
#include "libnn/tpt_convolution.h"
#include "libnn/tpt_fully_connected.h"
#include "libnn/tpt_pooling.h"
#include "libnn/tpt_reshape.h"
#include "libnn/tpt_softmax.h"
#include "libnn/tpt_support.h"
#include "libnn/tpt_svd.h"
#include "libnn/tpt_util.h"
#include "libnn/nds_math.h"
Include dependency graph for tpt_math_nn.h:

Data Structures

struct  tpt_nn_double
 Union for data type long long. More...
union  tpt_nn_long_long
union  _union32_t

Macros

#define OPT_BRANCH_PREDICTION   1
#define TPT_MATH_LOOPUNROLL   8
#define LEFT_SHIFT(_shift)
#define RIGHT_SHIFT(_shift)
#define MASK_IF_ZERO(x)
#define MASK_IF_NON_ZERO(x)
#define SELECT_USING_MASK(mask, a, b)
#define NN_ROUND(out_shift)
#define TPT_MUL_SAT(a, b)
#define TPT_MUL_POW2(a, b)
#define TPT_DIV_POW2_V2(a, b)
#define TPT_DIV_POW2(a, b)
#define TPT_EXP_ON_NEG(x)
#define TPT_ONE_OVER1(x)
#define MAX(A, B)
#define MIN(A, B)
#define CLAMP(x, h, l)
#define REDUCE_MULTIPLIER(_mult)
#define likely(x)
#define unlikely(x)
#define Q63_MAX   (q63_t)0x7FFFFFFFFFFFFFFFLL
#define Q63_MIN   (q63_t)0x8000000000000000LL
#define Q31_MAX   (q31_t)0x7FFFFFFF
#define Q31_MIN   (q31_t)0x80000000
#define Q15_MAX   (q15_t)0x7FFF
#define Q15_MIN   (q15_t)0x8000
#define Q7_MAX   (q7_t)0x7F
#define Q7_MIN   (q7_t)0x80
#define ABS(val, bit)
#define CLIP(val, bit)
#define CLIPS(val, bit)
#define SELECT_IF_NON_ZERO(x)

Typedefs

typedef int64_t q63_t
 64-bit fractional data type in 1.63 format.
typedef int32_t q31_t
 32-bit fractional data type in 1.31 format.
typedef int16_t q15_t
 16-bit fractional data type in 1.15 format.
typedef int8_t q7_t
 8-bit fractional data type in 1.7 format.
typedef float f32_t
 32-bit floating-point type definition.

Functions

static int32_t tpt_nn_doubling_high_mult_no_sat (const int32_t m1, const int32_t m2)
 Doubling high multiply without saturation. This is intended for requantization where the scale is a positive integer.
static uint32_t __ABS (q31_t data)
static uint8_t __NN_CLZ (uint32_t aData)
 This function counts leading zeros of the given value.
static int8_t * tpt_memcpy_q7 (int8_t *buf, const int8_t *start, uint16_t input_c)
static uint8_t * tpt_memcpy_u8 (uint8_t *buf, const uint8_t *start, uint16_t input_c)
static uint8_t * tpt_memcpy_mul_of_four_u8 (uint8_t *buf, const uint8_t *start, uint16_t input_c)
static int8_t * tpt_memcpy_mul_of_four_q7 (int8_t *buf, const int8_t *start, uint16_t input_c)
static int8_t * tpt_memset_q7 (int8_t *buf, uint16_t input_c)
static uint8_t * tpt_memset_u8 (uint8_t *buf, uint16_t input_c)
static int8_t * tpt_memset_mul_of_four_q7 (int8_t *buf, uint16_t input_c)
static uint8_t * tpt_memset_mul_of_four_u8 (uint8_t *buf, uint16_t input_c)
static int32_t tpt_nn_requantize (const int32_t val, const int32_t multiplier, const int32_t shift)
 Requantize a given value.
static int32_t tpt_nn_requantize_op (const int32_t val, const int32_t multiplier, const int32_t shift)
 Requantize a given value with overflow protection.
static int32_t tpt_nn_requantize_ns (const int32_t val, const int32_t multiplier, const int32_t shift)
static int32_t tpt_nn_requantize_q63 (const int64_t val, const int32_t multiplier, const int32_t shift)
static q31_t tpt_nn_divide_by_power_of_two (const q31_t dividend, const q31_t exponent)
static q31_t tpt_nn_divide_by_power_of_two_v2 (const q31_t dividend, const q31_t exponent)
static q31_t tpt_nn_mult_by_power_of_two (const int32_t val, const int32_t exp)
static int32_t tpt_nn_sat_doubling_high_mult (const int32_t m1, const int32_t m2)
static int32_t tpt_nn_exp_on_negative_values (int32_t val)
static int32_t tpt_nn_one_over_one_plus_x_for_x_in_0_1 (int32_t val)

Macro Definition Documentation

◆ ABS

#define ABS ( val,
bit )
Value:
(val > 0) ? val : ((val == Q##bit##_MIN) ? Q##bit##_MAX : -val)

◆ CLAMP

#define CLAMP ( x,
h,
l )
Value:
MAX(MIN((x), (h)), (l))
#define MAX(A, B)
Definition tpt_math_nn.h:88
#define MIN(A, B)
Definition tpt_math_nn.h:89

◆ CLIP

#define CLIP ( val,
bit )
Value:
((val) > UINT##bit##_MAX) ? UINT##bit##_MAX : ((val) < 0 ? 0U : (val))

◆ CLIPS

#define CLIPS ( val,
bit )
Value:
((val) < Q##bit##_MIN) ? Q##bit##_MIN \
: ((val) > Q##bit##_MAX) ? Q##bit##_MAX \
: (val)

◆ LEFT_SHIFT

#define LEFT_SHIFT ( _shift)
Value:
(_shift > 0 ? _shift : 0)

◆ likely

#define likely ( x)
Value:
(x)

◆ MASK_IF_NON_ZERO

#define MASK_IF_NON_ZERO ( x)
Value:
(x) != 0 ? ~0 : 0

◆ MASK_IF_ZERO

#define MASK_IF_ZERO ( x)
Value:
(x) == 0 ? ~0 : 0

◆ MAX

#define MAX ( A,
B )
Value:
((A) > (B) ? (A) : (B))

◆ MIN

#define MIN ( A,
B )
Value:
((A) < (B) ? (A) : (B))

◆ NN_ROUND

#define NN_ROUND ( out_shift)
Value:
(0x1 << (out_shift - 1))

◆ OPT_BRANCH_PREDICTION

#define OPT_BRANCH_PREDICTION   1

◆ Q15_MAX

#define Q15_MAX   (q15_t)0x7FFF

◆ Q15_MIN

#define Q15_MIN   (q15_t)0x8000

◆ Q31_MAX

#define Q31_MAX   (q31_t)0x7FFFFFFF

◆ Q31_MIN

#define Q31_MIN   (q31_t)0x80000000

◆ Q63_MAX

#define Q63_MAX   (q63_t)0x7FFFFFFFFFFFFFFFLL

◆ Q63_MIN

#define Q63_MIN   (q63_t)0x8000000000000000LL

◆ Q7_MAX

#define Q7_MAX   (q7_t)0x7F

◆ Q7_MIN

#define Q7_MIN   (q7_t)0x80

◆ REDUCE_MULTIPLIER

#define REDUCE_MULTIPLIER ( _mult)
Value:
((_mult < 0x7FFF0000) ? ((_mult + (1 << 15)) >> 16) : 0x7FFF)

◆ RIGHT_SHIFT

#define RIGHT_SHIFT ( _shift)
Value:
(_shift > 0 ? 0 : -_shift)

◆ SELECT_IF_NON_ZERO

#define SELECT_IF_NON_ZERO ( x)
Value:
{ \
mask = MASK_IF_NON_ZERO(remainder & (1 << shift++)); \
result = SELECT_USING_MASK(mask, TPT_MUL_SAT(result, x), result); \
}
#define MASK_IF_NON_ZERO(x)
Definition nn_ref_math.h:26
#define SELECT_USING_MASK(mask, a, b)
Definition nn_ref_math.h:27
#define TPT_MUL_SAT(a, b)
Definition tpt_math_nn.h:77

◆ SELECT_USING_MASK

#define SELECT_USING_MASK ( mask,
a,
b )
Value:
((mask) & (a)) ^ (~(mask) & (b))

◆ TPT_DIV_POW2

#define TPT_DIV_POW2 ( a,
b )
Value:
static q31_t tpt_nn_divide_by_power_of_two(const q31_t dividend, const q31_t exponent)
Definition tpt_math_nn.h:501

◆ TPT_DIV_POW2_V2

#define TPT_DIV_POW2_V2 ( a,
b )
Value:
static q31_t tpt_nn_divide_by_power_of_two_v2(const q31_t dividend, const q31_t exponent)
Definition tpt_math_nn.h:528

◆ TPT_EXP_ON_NEG

#define TPT_EXP_ON_NEG ( x)
Value:
static int32_t tpt_nn_exp_on_negative_values(int32_t val)
Definition tpt_math_nn.h:589

◆ TPT_MATH_LOOPUNROLL

#define TPT_MATH_LOOPUNROLL   8

◆ TPT_MUL_POW2

#define TPT_MUL_POW2 ( a,
b )
Value:
static q31_t tpt_nn_mult_by_power_of_two(const int32_t val, const int32_t exp)
Definition tpt_math_nn.h:550

◆ TPT_MUL_SAT

#define TPT_MUL_SAT ( a,
b )
Value:
static int32_t tpt_nn_sat_doubling_high_mult(const int32_t m1, const int32_t m2)
Definition tpt_math_nn.h:560

◆ TPT_ONE_OVER1

#define TPT_ONE_OVER1 ( x)
Value:
static int32_t tpt_nn_one_over_one_plus_x_for_x_in_0_1(int32_t val)
Definition tpt_math_nn.h:631

◆ unlikely

#define unlikely ( x)
Value:
(x)

Typedef Documentation

◆ f32_t

typedef float f32_t

32-bit floating-point type definition.

◆ q15_t

typedef int16_t q15_t

16-bit fractional data type in 1.15 format.

◆ q31_t

typedef int32_t q31_t

32-bit fractional data type in 1.31 format.

◆ q63_t

typedef int64_t q63_t

64-bit fractional data type in 1.63 format.

◆ q7_t

typedef int8_t q7_t

8-bit fractional data type in 1.7 format.

Function Documentation

◆ __ABS()

uint32_t __ABS ( q31_t data)
inlinestatic

◆ __NN_CLZ()

uint8_t __NN_CLZ ( uint32_t aData)
inlinestatic

This function counts leading zeros of the given value.

Parameters
aData
Returns
** uint8_t

◆ tpt_memcpy_mul_of_four_q7()

int8_t * tpt_memcpy_mul_of_four_q7 ( int8_t * buf,
const int8_t * start,
uint16_t input_c )
inlinestatic

◆ tpt_memcpy_mul_of_four_u8()

uint8_t * tpt_memcpy_mul_of_four_u8 ( uint8_t * buf,
const uint8_t * start,
uint16_t input_c )
inlinestatic

◆ tpt_memcpy_q7()

int8_t * tpt_memcpy_q7 ( int8_t * buf,
const int8_t * start,
uint16_t input_c )
inlinestatic

◆ tpt_memcpy_u8()

uint8_t * tpt_memcpy_u8 ( uint8_t * buf,
const uint8_t * start,
uint16_t input_c )
inlinestatic

◆ tpt_memset_mul_of_four_q7()

int8_t * tpt_memset_mul_of_four_q7 ( int8_t * buf,
uint16_t input_c )
inlinestatic

◆ tpt_memset_mul_of_four_u8()

uint8_t * tpt_memset_mul_of_four_u8 ( uint8_t * buf,
uint16_t input_c )
inlinestatic

◆ tpt_memset_q7()

int8_t * tpt_memset_q7 ( int8_t * buf,
uint16_t input_c )
inlinestatic

◆ tpt_memset_u8()

uint8_t * tpt_memset_u8 ( uint8_t * buf,
uint16_t input_c )
inlinestatic

◆ tpt_nn_divide_by_power_of_two()

q31_t tpt_nn_divide_by_power_of_two ( const q31_t dividend,
const q31_t exponent )
inlinestatic

◆ tpt_nn_divide_by_power_of_two_v2()

q31_t tpt_nn_divide_by_power_of_two_v2 ( const q31_t dividend,
const q31_t exponent )
inlinestatic

◆ tpt_nn_doubling_high_mult_no_sat()

int32_t tpt_nn_doubling_high_mult_no_sat ( const int32_t m1,
const int32_t m2 )
inlinestatic

Doubling high multiply without saturation. This is intended for requantization where the scale is a positive integer.

Parameters
[in]m1Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
[in]m2Multiplier Range: {NN_Q31_MIN, NN_Q31_MAX}
Returns
Result of multiplication.
Note
The result of this matches that of neon instruction VQRDMULH for m1 in range {NN_Q31_MIN, NN_Q31_MAX} and m2 in range {NN_Q31_MIN + 1, NN_Q31_MAX}. Saturation occurs when m1 equals m2 equals NN_Q31_MIN and that is not handled by this function.

◆ tpt_nn_exp_on_negative_values()

int32_t tpt_nn_exp_on_negative_values ( int32_t val)
inlinestatic

◆ tpt_nn_mult_by_power_of_two()

q31_t tpt_nn_mult_by_power_of_two ( const int32_t val,
const int32_t exp )
inlinestatic

◆ tpt_nn_one_over_one_plus_x_for_x_in_0_1()

int32_t tpt_nn_one_over_one_plus_x_for_x_in_0_1 ( int32_t val)
inlinestatic

◆ tpt_nn_requantize()

int32_t tpt_nn_requantize ( const int32_t val,
const int32_t multiplier,
const int32_t shift )
inlinestatic

Requantize a given value.

Parameters
[in]valValue to be requantized
[in]multipliermultiplier. Range {NN_Q31_MIN + 1, Q31_MAX}
[in]shiftleft or right shift for 'val * multiplier'
Returns
Returns (val * multiplier)/(2 ^ (shift))

◆ tpt_nn_requantize_ns()

int32_t tpt_nn_requantize_ns ( const int32_t val,
const int32_t multiplier,
const int32_t shift )
inlinestatic

◆ tpt_nn_requantize_op()

int32_t tpt_nn_requantize_op ( const int32_t val,
const int32_t multiplier,
const int32_t shift )
inlinestatic

Requantize a given value with overflow protection.

Parameters
[in]valValue to be requantized
[in]multipliermultiplier. Range {NN_Q31_MIN + 1, Q32_MAX}
[in]shiftleft or right shift for 'val * multiplier'
Returns
Returns (val * multiplier)/(2 ^ (shift))

◆ tpt_nn_requantize_q63()

int32_t tpt_nn_requantize_q63 ( const int64_t val,
const int32_t multiplier,
const int32_t shift )
inlinestatic

◆ tpt_nn_sat_doubling_high_mult()

int32_t tpt_nn_sat_doubling_high_mult ( const int32_t m1,
const int32_t m2 )
inlinestatic