/*                                                                          */
/* Function    : define useful numbers formats & values                     */
/*               might be used in compile time also                         */
/* Macros      1)                                                           */
/*                                                                          */
/*                                                                          */

#include  "windef.h"
#ifndef _MBDR_NUMBERS_H__
#define _MBDR_NUMBERS_H__

//#define   MBDR_DSP_INT_16_BITS_MIN_VALUE  0x8000
#define   MBDR_DSP_INT_16_BITS_MIN_VALUE  ((-1)*(1<<15))
#define   MBDR_DSP_INT_16_BITS_MAX_VALUE  0x7fff
#define   MBDR_DSP_UINT_16_BITS_MAX_VALUE 0xffff


/* converting float to required fixed, optional rounding */
#define  MBDR_DSP_FLOAT_TO_FIXED_M(PI_FLOAT, PI_Q, PI_ROUND_F)\
         ((LONG)( ((PI_FLOAT) + PI_ROUND_F*0.5/(1<<(PI_Q)))*(1<<(PI_Q))))


#define MBDR_DSP_FLOAT_TO_S32Q16_M(PI_NUMBER, PI_ROUND_F)\
        MBDR_DSP_FLOAT_TO_FIXED_M(PI_NUMBER, 16, PI_ROUND_F)

#define MBDR_DSP_PI_FLOAT  3.14159265358979
#define MBDR_DSP_PI_S32Q16 (MBDR_DSP_FLOAT_TO_S32Q16_M(MBDR_DSP_PI_FLOAT, 0))

#define MBDR_DSP_SQRT_2_FLOAT  1.41421356237310
#define MBDR_DSP_SQRT_2_S32Q16 MBDR_DSP_FLOAT_TO_S32Q16_M(MBDR_DSP_SQRT_2_FLOAT, 0)

/* converts [degrees] -> [radian] units */
#define MBDR_DSP_DEGREES_2_RADIAN_M(PI_DEGREES)\
                     ((PI_DEGREES)*MBDR_DSP_PI_FLOAT/180.0)

/* power 2 input number */
#define MBDR_DSP_SQR_M(PI_NUMBER) ((PI_NUMBER)*(PI_NUMBER))


/* define useful constants*/
#define MBDR_DSP_RECIPROCAL_PI_32BIT_Q16 0x0517d
#define MBDR_DSP_PI_32BIT_Q16            0x3243f

/* converts input fixed number to S32Q16           */
/*    PI_FIXED - fixed number                      */
/*    PI_Q     - Q of PI_FIXED                     */
/* prerequisite  Q of PI_FIXED  PI_Q <= 16         */
#define    MBDR_DSP_UTILS_SELECTED_Q_TO_S32Q16_M(PI_FIXED, PI_Q)\
             (((LONG)(PI_FIXED)) << (16-PI_Q))


#define    MBDR_DSP_UTILS_FLOAT_TO_S32Q16_M(PI_FLOAT)\
             ( (LONG)( (PI_FLOAT)*(1<<16) ))
#define    MBDR_DSP_UTILS_INT_TO_S32Q16_M(PI_INT)\
             ( ((LONG)(PI_INT)) << 16)

/* truncates u32q16 to integer useful for find range index */
#define MBDR_DSP_UTILS_TRUNCATE_U32Q16_TO_INT_M(PI_S32Q16) ((PI_S32Q16) >> 16)

/* converts and round s32q16 to integer */
#define MBDR_DSP_UTILS_CONVERT_AND_ROUND_S32Q16_TO_INT_M(PI_S32Q16)\
             ((PI_S32Q16 + 0X08000) >> 16)

/* converts and ceil s32q16 to integer */
#define MBDR_DSP_UTILS_CONVERT_AND_CEIL_S32Q16_TO_INT_M(PI_S32Q16)\
             ((PI_S32Q16 + MBDR_DSP_UINT_16_BITS_MAX_VALUE) >> 16)

/* converts and round input float to required Q fixed */
#define MBDR_DSP_FLOAT_TO_Q_AND_ROUND_M(PI_FLOAT, PI_Q)\
          ( ((LONG)(1.0+(PI_FLOAT)*(1<<(PI_Q+1)) ))>>1 )

/* upper rounding up float to integer  */
#define    MBDR_DSP_UTILS_CEIL_FLOAT_TO_INT_M(PI_FLOAT)\
               ( (LONG) (0.9999 + PI_FLOAT) )

/* computes reciprocal of input unsigend integer to U16Q16  */
#define MBDR_DSP_UTILS_RECIPROCAL_UINT_2_Q16U16_M(PI_X)\
        ((LONG)((0X0FFFF)*((PI_X)==1))+((PI_X)!=1)*((1/(1.0*(PI_X))*(1<<16))))

#define MBDR_DSP_1_LOG_2     0
#define MBDR_DSP_2_LOG_2     1
#define MBDR_DSP_4_LOG_2     2
#define MBDR_DSP_8_LOG_2     3
#define MBDR_DSP_16_LOG_2    4
#define MBDR_DSP_32_LOG_2    5
#define MBDR_DSP_64_LOG_2    6
#define MBDR_DSP_128_LOG_2   7
#define MBDR_DSP_256_LOG_2   8
#define MBDR_DSP_512_LOG_2   9
#define MBDR_DSP_1024_LOG_2 10

#define MBDR_DSP_2_POWER_X_M(PI_POWER) (1<<(PI_POWER))

/* PI_POWER [1..31]*/
#define MBDR_DSP_2_POWER_X_MASK_M(PI_POWER) (MBDR_DSP_2_POWER_X_M(PI_POWER) - 1)


#define MBDR_DSP_1   MBDR_DSP_2_POWER_X_M(MBDR_DSP_1_LOG_2)
#define MBDR_DSP_2   MBDR_DSP_2_POWER_X_M(MBDR_DSP_2_LOG_2)
#define MBDR_DSP_4   MBDR_DSP_2_POWER_X_M(MBDR_DSP_4_LOG_2)
#define MBDR_DSP_8   MBDR_DSP_2_POWER_X_M(MBDR_DSP_8_LOG_2)
#define MBDR_DSP_16   MBDR_DSP_2_POWER_X_M(MBDR_DSP_16_LOG_2)
#define MBDR_DSP_32   MBDR_DSP_2_POWER_X_M(MBDR_DSP_32_LOG_2)
#define MBDR_DSP_64   MBDR_DSP_2_POWER_X_M(MBDR_DSP_64_LOG_2)
#define MBDR_DSP_128  MBDR_DSP_2_POWER_X_M(MBDR_DSP_128_LOG_2)
#define MBDR_DSP_256  MBDR_DSP_2_POWER_X_M(MBDR_DSP_256_LOG_2)
#define MBDR_DSP_512  MBDR_DSP_2_POWER_X_M(MBDR_DSP_512_LOG_2)
#define MBDR_DSP_1024 MBDR_DSP_2_POWER_X_M(MBDR_DSP_1024_LOG_2)

#define MBDR_DSP_BITS_NUMBER_TO_MAX_UNSIGNED_VALUE_M(PI_BITS_NUMBER)\
        ((~0)^((~0)<<(PI_BITS_NUMBER)))


#define   MBDR_DSP_CLIP_4_BIT_COUNTER_REPORT(PI_NUMBER)\
           min(PI_NUMBER, MBDR_DSP_BITS_NUMBER_TO_MAX_UNSIGNED_VALUE_M(4))

#define   MBDR_DSP_CLIP_8_BIT_COUNTER_REPORT(PI_NUMBER)\
           min(PI_NUMBER, MBDR_DSP_BITS_NUMBER_TO_MAX_UNSIGNED_VALUE_M(8))


#define DSP_BYTE_BITS_NUMBER   8
#define DSP_SHORT_BITS_NUMBER (2*DSP_BYTE_BITS_NUMBER)
#define DSP_LONG_BITS_NUMBER  (2*DSP_SHORT_BITS_NUMBER)

#endif
