/**
 * @file hv_comm_DataType.h
 * @brief Header file of mute module.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 *
 * @endverbatim
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-23
 */

#ifndef __SDK_COMMON_DATA_TYPE_H__
#define __SDK_COMMON_DATA_TYPE_H__

typedef int                 INT32;
typedef unsigned int        UINT32;
typedef short               SHORT16;
typedef unsigned short      USHORT16;
typedef char                CHAR8;
typedef unsigned char       UCHAR8;
typedef unsigned char       BYTE;
typedef long                SLONG32;
typedef unsigned long       ULONG32;
typedef unsigned long long  UINT64;
typedef long long           INT64;
typedef float               FLOAT32;
typedef double              DOUBLE64;
typedef UINT32              Status;
typedef UCHAR8              BOOL;
typedef void                VOID;



#define INVALID_PARAM_UHCAR8          (UCHAR8)0xFF
#define INVALID_PARAM_USHORT16        (USHORT16)0xFFFF
#define INVALID_PARAM_UINT32          (UINT32)0xFFFFFFFF
#define INVALID_PARAM_UINT64          (UINT64)0xFFFFFFFFFFFFFFFF

// Definitions of Bits
#define BIT_0       0x00000001  ///< define BIT_0
#define BIT_1       0x00000002  ///< define BIT_1
#define BIT_2       0x00000004  ///< define BIT_2
#define BIT_3       0x00000008  ///< define BIT_3
#define BIT_4       0x00000010  ///< define BIT_4
#define BIT_5       0x00000020  ///< define BIT_5
#define BIT_6       0x00000040  ///< define BIT_6
#define BIT_7       0x00000080  ///< define BIT_7
#define BIT_8       0x00000100  ///< define BIT_8
#define BIT_9       0x00000200  ///< define BIT_9
#define BIT_10      0x00000400  ///< define BIT_10
#define BIT_11      0x00000800  ///< define BIT_11
#define BIT_12      0x00001000  ///< define BIT_12
#define BIT_13      0x00002000  ///< define BIT_13
#define BIT_14      0x00004000  ///< define BIT_14
#define BIT_15      0x00008000  ///< define BIT_15
#define BIT_16      0x00010000  ///< define BIT_16
#define BIT_17      0x00020000  ///< define BIT_17
#define BIT_18      0x00040000  ///< define BIT_18
#define BIT_19      0x00080000  ///< define BIT_19
#define BIT_20      0x00100000  ///< define BIT_20
#define BIT_21      0x00200000  ///< define BIT_21
#define BIT_22      0x00400000  ///< define BIT_22
#define BIT_23      0x00800000  ///< define BIT_23
#define BIT_24      0x01000000  ///< define BIT_24
#define BIT_25      0x02000000  ///< define BIT_25
#define BIT_26      0x04000000  ///< define BIT_26
#define BIT_27      0x08000000  ///< define BIT_27
#define BIT_28      0x10000000  ///< define BIT_28
#define BIT_29      0x20000000  ///< define BIT_29
#define BIT_30      0x40000000  ///< define BIT_30
#define BIT_31      0x80000000  ///< define BIT_31

#define BIT(nr)             (1UL << (nr))

#define ABS(x, y)           (((x) < (y)) ? ((y) - (x)) : ((x) - (y)))
#ifndef MAX
    #define MAX(a, b)       (((a) > (b)) ? (a) : (b))
#endif

#ifndef MIN
    #define MIN(a, b)       (((a) < (b)) ? (a) : (b))
#endif
#define ROUND_DIV(a, b)     (((a) + ((b) - 1)) / (b))

#define UNUSED(var)         (void)((var) = (var))


/* Porting from kernel.h start */
#define USHRT_MAX   ((u16)(~0U))
#define SHRT_MAX    ((s16)(USHRT_MAX>>1))
#define SHRT_MIN    ((s16)(-SHRT_MAX - 1))
#define INT_MAX     ((int)(~0U>>1))
#define INT_MIN     (-INT_MAX - 1)
#define UINT_MAX    (~0U)
#define LONG_MAX    ((long)(~0UL>>1))
#define LONG_MIN    (-LONG_MAX - 1)
#define ULONG_MAX   (~0UL)
//#define LLONG_MAX   ((long long)(~0ULL>>1))
//#define LLONG_MAX   (0x7FFFFFFFFFFFFFFF)
#define LLONG_MIN   (-LLONG_MAX - 1)
#define ULLONG_MAX  (~0ULL)
#define SIZE_T_MAX  (~(size_t)0)

#define U8_MAX      ((u8)~0U)
#define S8_MAX      ((s8)(U8_MAX>>1))
#define S8_MIN      ((s8)(-S8_MAX - 1))
#define U16_MAX     ((u16)~0U)
#define S16_MAX     ((s16)(U16_MAX>>1))
#define S16_MIN     ((s16)(-S16_MAX - 1))
#define U32_MAX     ((u32)~0U)
#define S32_MAX     ((s32)(U32_MAX>>1))
#define S32_MIN     ((s32)(-S32_MAX - 1))
#define U64_MAX     ((u64)~0ULL)
#define S64_MAX     ((s64)(U64_MAX>>1))
#define S64_MIN     ((s64)(-S64_MAX - 1))

#define STACK_MAGIC 0xdeadbeef

#define REPEAT_BYTE(x)  ((~0ul / 0xff) * (x))

#define ALIGN(x, a)     __ALIGN_KERNEL((x), (a))
#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define ALIGN_DOWN(x, a)    __ALIGN_KERNEL((x) - ((a) - 1), (a))
#define __ALIGN_MASK(x, mask)   __ALIGN_KERNEL_MASK((x), (mask))
#define PTR_ALIGN(p, a)     ((typeof(p))ALIGN((unsigned long)(p), (a)))
#define IS_ALIGNED(x, a)        (((x) & ((typeof(x))(a) - 1)) == 0)

#if defined(__GNUC__)
    #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
    #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
    #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
    #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
#else
    #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])
#endif

/*
 * This looks more complex than it should be. But we need to
 * get the type for the ~ right in round_down (it needs to be
 * as wide as the result!), and we want to evaluate the macro
 * arguments just once each.
 */
#define __round_mask(x, y) ((__typeof__(x))((y)-1))
#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
#define round_down(x, y) ((x) & ~__round_mask(x, y))

/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
#define roundup(x, y) (                 \
{                           \
    const typeof(y) __y = y;            \
    (((x) + (__y - 1)) / __y) * __y;        \
}                           \
)

#define rounddown(x, y) (               \
{                           \
    typeof(x) __x = (x);                \
    __x - (__x % (y));              \
}                           \
)

/* x除以y，结果向上取整 */
#define roundup_div(x, y) (                 \
{                           \
    const typeof(y) __y = y;            \
    (((x) + (__y - 1)) / __y);        \
}                           \
)

/* 将 `x` 除以 `y` 并将结果四舍五入到最接近的整数 */
#define round_div(x, y)  (((x * 2 / y) + 1) / 2)

/*
 * Divide positive or negative dividend by positive divisor and round
 * to closest integer. Result is undefined for negative divisors and
 * for negative dividends if the divisor variable type is unsigned.
 */
#define DIV_ROUND_CLOSEST(x, divisor)(          \
{                           \
    typeof(x) __x = x;              \
    typeof(divisor) __d = divisor;          \
    (((typeof(x))-1) > 0 ||             \
     ((typeof(divisor))-1) > 0 || (__x) > 0) ?  \
        (((__x) + ((__d) / 2)) / (__d)) :   \
        (((__x) - ((__d) / 2)) / (__d));    \
}                           \
)
/*
 * Same as above but for u64 dividends. divisor must be a 32-bit
 * number.
 */
#define DIV_ROUND_CLOSEST_ULL(x, divisor)(      \
{                           \
    typeof(divisor) __d = divisor;          \
    unsigned long long _tmp = (x) + (__d) / 2;  \
    do_div(_tmp, __d);              \
    _tmp;                       \
}                           \
)

/*
 * Multiplies an integer by a fraction, while avoiding unnecessary
 * overflow or loss of precision.
 */
#define mult_frac(x, numer, denom)(         \
{                           \
    typeof(x) quot = (x) / (denom);         \
    typeof(x) rem  = (x) % (denom);         \
    (quot * (numer)) + ((rem * (numer)) / (denom)); \
}                           \
)

/**
 * upper_32_bits - return bits 32-63 of a number
 * @n: the number we're accessing
 *
 * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
 * the "right shift count >= width of type" warning when that quantity is
 * 32-bits.
 */
#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))

/**
 * lower_32_bits - return bits 0-31 of a number
 * @n: the number we're accessing
 */
#define lower_32_bits(n) ((u32)(n))

#if 0
/**
 * abs - return absolute value of an argument
 * @x: the value.  If it is unsigned type, it is converted to signed type first.
 *     char is treated as if it was signed (regardless of whether it really is)
 *     but the macro's return type is preserved as char.
 *
 * Return: an absolute value of x.
 */
#define abs(x)  __abs_choose_expr(x, long long,             \
        __abs_choose_expr(x, long,              \
        __abs_choose_expr(x, int,               \
        __abs_choose_expr(x, short,             \
        __abs_choose_expr(x, char,              \
        __builtin_choose_expr(                  \
            __builtin_types_compatible_p(typeof(x), char),  \
            (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
            ((void)0)))))))

#define __abs_choose_expr(x, type, other) __builtin_choose_expr(    \
    __builtin_types_compatible_p(typeof(x),   signed type) ||   \
    __builtin_types_compatible_p(typeof(x), unsigned type),     \
    ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
#endif
/*
 * min()/max()/clamp() macros that also do
 * strict type-checking.. See the
 * "unnecessary" pointer comparison.
 */

/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
#define ___PASTE(a,b) a##b
#define __PASTE(a,b) ___PASTE(a,b)

#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)

#define __min(t1, t2, min1, min2, x, y) ({      \
    t1 min1 = (x);                  \
    t2 min2 = (y);                  \
    (void) (&min1 == &min2);            \
    min1 < min2 ? min1 : min2; })
#define min(x, y)                   \
    __min(typeof(x), typeof(y),         \
          __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),   \
          x, y)

#define __max(t1, t2, max1, max2, x, y) ({      \
    t1 max1 = (x);                  \
    t2 max2 = (y);                  \
    (void) (&max1 == &max2);            \
    max1 > max2 ? max1 : max2; })
#define max(x, y)                   \
    __max(typeof(x), typeof(y),         \
          __UNIQUE_ID(max1_), __UNIQUE_ID(max2_),   \
          x, y)

#define min3(x, y, z) min((typeof(x))min(x, y), z)
#define max3(x, y, z) max((typeof(x))max(x, y), z)

/**
 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
 * @x: value1
 * @y: value2
 */
#define min_not_zero(x, y) ({           \
    typeof(x) __x = (x);            \
    typeof(y) __y = (y);            \
    __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })

/**
 * clamp - return a value clamped to a given range with strict typechecking
 * @val: current value
 * @lo: lowest allowable value
 * @hi: highest allowable value
 *
 * This macro does strict typechecking of lo/hi to make sure they are of the
 * same type as val.  See the unnecessary pointer comparisons.
 */
#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)

/*
 * ..and if you can't take the strict
 * types, you can specify one yourself.
 *
 * Or not use min/max/clamp at all, of course.
 */
#define min_t(type, x, y)               \
    __min(type, type,               \
          __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),   \
          x, y)

#define max_t(type, x, y)               \
    __max(type, type,               \
          __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),   \
          x, y)

/**
 * clamp_t - return a value clamped to a given range using a given type
 * @type: the type of variable to use
 * @val: current value
 * @lo: minimum allowable value
 * @hi: maximum allowable value
 *
 * This macro does no typechecking and uses temporary variables of type
 * 'type' to make all the comparisons.
 */
#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)

/**
 * clamp_val - return a value clamped to a given range using val's type
 * @val: current value
 * @lo: minimum allowable value
 * @hi: maximum allowable value
 *
 * This macro does no typechecking and uses temporary variables of whatever
 * type the input argument 'val' is.  This is useful when val is an unsigned
 * type and min and max are literals that will otherwise be assigned a signed
 * integer type.
 */
#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)

/*
 * swap - swap value of @a and @b
 */
#define swap(a, b) \
    do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:   the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

/* Porting from kernel.h end */

/* 根据当前值在原始范围的位置获取对应的实际值 */
#define LINEAR_MAPPING_VALUE(Cur, orgMin, orgMax, newMin, newMax) \
({\
    UINT32 uiVal;\
    UINT32 uiClampCurVal = clamp_val(Cur, orgMin, orgMax);\
    if(orgMax - orgMin == 0)\
    {\
        uiVal = newMin;\
    }\
    else\
    {\
        uiVal = ((((uiClampCurVal)-(orgMin))*((newMax)-(newMin))+(((orgMax)-(orgMin))>>1))/((orgMax)-(orgMin))+(newMin));\
    }\
    uiVal;\
})

#define LINEAR_REVERT_MAPPING_VALUE(Cur, orgMin, orgMax, newMin, newMax) LINEAR_MAPPING_VALUE((orgMax - Cur + orgMin), orgMin, orgMax, newMin, newMax);

#endif
