//#define __IQMATHLIB_H_INCLUDED__


/**
* Èììèòàöèÿ áèáëèîòåêè IQmath äëÿ òåñòèðîâàíèÿ â MATLAB
*
*/
#ifndef IQ_MATH_LIB
#define IQ_MATH_LIB


#ifndef   GLOBAL_Q
#define   GLOBAL_Q       24
#endif

typedef   long    _iq;
typedef   long    _iq30;
typedef   long    _iq29;
typedef   long    _iq28;
typedef   long    _iq27;
typedef   long    _iq26;
typedef   long    _iq25;
typedef   long    _iq24;
typedef   long    _iq23;
typedef   long    _iq22;
typedef   long    _iq21;
typedef   long    _iq20;
typedef   long    _iq19;
typedef   long    _iq18;
typedef   long    _iq17;
typedef   long    _iq16;
typedef   long    _iq15;
typedef   long    _iq14;
typedef   long    _iq13;
typedef   long    _iq12;
typedef   long    _iq11;
typedef   long    _iq10;
typedef   long    _iq9;
typedef   long    _iq8;
typedef   long    _iq7;
typedef   long    _iq6;
typedef   long    _iq5;
typedef   long    _iq4;
typedef   long    _iq3;
typedef   long    _iq2;
typedef   long    _iq1;

//---------------------------------------------------------------------------
#define _IQmpy2(A)          ((A)<<1)
#define _IQmpy4(A)          ((A)<<2)
#define _IQmpy8(A)          ((A)<<3)
#define _IQmpy16(A)         ((A)<<4)
#define _IQmpy32(A)         ((A)<<5)
#define _IQmpy64(A)         ((A)<<6)

#define _IQdiv2(A)          ((A)>>1)
#define _IQdiv4(A)          ((A)>>2)
#define _IQdiv8(A)          ((A)>>3)
#define _IQdiv16(A)         ((A)>>4)
#define _IQdiv32(A)         ((A)>>5)
#define _IQdiv64(A)         ((A)>>6)
//---------------------------------------------------------------------------
#define   _IQ30(A)      (long) ((A) * 1073741824.0L)
#define   _IQ29(A)      (long) ((A) * 536870912.0L)
#define   _IQ28(A)      (long) ((A) * 268435456.0L)
#define   _IQ27(A)      (long) ((A) * 134217728.0L)
#define   _IQ26(A)      (long) ((A) * 67108864.0L)
#define   _IQ25(A)      (long) ((A) * 33554432.0L)
#define   _IQ24(A)      (long) ((A) * 16777216.0L)
#define   _IQ23(A)      (long) ((A) * 8388608.0L)
#define   _IQ22(A)      (long) ((A) * 4194304.0L)
#define   _IQ21(A)      (long) ((A) * 2097152.0L)
#define   _IQ20(A)      (long) ((A) * 1048576.0L)
#define   _IQ19(A)      (long) ((A) * 524288.0L)
#define   _IQ18(A)      (long) ((A) * 262144.0L)
#define   _IQ17(A)      (long) ((A) * 131072.0L)
#define   _IQ16(A)      (long) ((A) * 65536.0L)
#define   _IQ15(A)      (long) ((A) * 32768.0L)
#define   _IQ14(A)      (long) ((A) * 16384.0L)
#define   _IQ13(A)      (long) ((A) * 8192.0L)
#define   _IQ12(A)      (long) ((A) * 4096.0L)
#define   _IQ11(A)      (long) ((A) * 2048.0L)
#define   _IQ10(A)      (long) ((A) * 1024.0L)
#define   _IQ9(A)       (long) ((A) * 512.0L)
#define   _IQ8(A)       (long) ((A) * 256.0L)
#define   _IQ7(A)       (long) ((A) * 128.0L)
#define   _IQ6(A)       (long) ((A) * 64.0L)
#define   _IQ5(A)       (long) ((A) * 32.0L)
#define   _IQ4(A)       (long) ((A) * 16.0L)
#define   _IQ3(A)       (long) ((A) * 8.0L)
#define   _IQ2(A)       (long) ((A) * 4.0L)
#define   _IQ1(A)       (long) ((A) * 2.0L)

#if GLOBAL_Q == 30
#define   _IQ(A)  _IQ30(A)
#endif
#if GLOBAL_Q == 29
#define   _IQ(A)  _IQ29(A)
#endif
#if GLOBAL_Q == 28
#define   _IQ(A)  _IQ28(A)
#endif
#if GLOBAL_Q == 27
#define   _IQ(A)  _IQ27(A)
#endif
#if GLOBAL_Q == 26
#define   _IQ(A)  _IQ26(A)
#endif
#if GLOBAL_Q == 25
#define   _IQ(A)  _IQ25(A)
#endif
#if GLOBAL_Q == 24
#define   _IQ(A)  _IQ24(A)
#endif
#if GLOBAL_Q == 23
#define   _IQ(A)  _IQ23(A)
#endif
#if GLOBAL_Q == 22
#define   _IQ(A)  _IQ22(A)
#endif
#if GLOBAL_Q == 21
#define   _IQ(A)  _IQ21(A)
#endif
#if GLOBAL_Q == 20
#define   _IQ(A)  _IQ20(A)
#endif
#if GLOBAL_Q == 19
#define   _IQ(A)  _IQ19(A)
#endif
#if GLOBAL_Q == 18
#define   _IQ(A)  _IQ18(A)
#endif
#if GLOBAL_Q == 17
#define   _IQ(A)  _IQ17(A)
#endif
#if GLOBAL_Q == 16
#define   _IQ(A)  _IQ16(A)
#endif
#if GLOBAL_Q == 15
#define   _IQ(A)  _IQ15(A)
#endif
#if GLOBAL_Q == 14
#define   _IQ(A)  _IQ14(A)
#endif
#if GLOBAL_Q == 13
#define   _IQ(A)  _IQ13(A)
#endif
#if GLOBAL_Q == 12
#define   _IQ(A)  _IQ12(A)
#endif
#if GLOBAL_Q == 11
#define   _IQ(A)  _IQ11(A)
#endif
#if GLOBAL_Q == 10
#define   _IQ(A)  _IQ10(A)
#endif
#if GLOBAL_Q == 9
#define   _IQ(A)  _IQ9(A)
#endif
#if GLOBAL_Q == 8
#define   _IQ(A)  _IQ8(A)
#endif
#if GLOBAL_Q == 7
#define   _IQ(A)  _IQ7(A)
#endif
#if GLOBAL_Q == 6
#define   _IQ(A)  _IQ6(A)
#endif
#if GLOBAL_Q == 5
#define   _IQ(A)  _IQ5(A)
#endif
#if GLOBAL_Q == 4
#define   _IQ(A)  _IQ4(A)
#endif
#if GLOBAL_Q == 3
#define   _IQ(A)  _IQ3(A)
#endif
#if GLOBAL_Q == 2
#define   _IQ(A)  _IQ2(A)
#endif
#if GLOBAL_Q == 1
#define   _IQ(A)  _IQ1(A)
#endif

//---------------------------------------------------------------------------

#define   _IQ30toF(A)      ((float) ((A) / 1073741824.0L))
#define   _IQ29toF(A)      ((float) ((A) / 536870912.0L))
#define   _IQ28toF(A)      ((float) ((A) / 268435456.0L))
#define   _IQ27toF(A)      ((float) ((A) / 134217728.0L))
#define   _IQ26toF(A)      ((float) ((A) / 67108864.0L))
#define   _IQ25toF(A)      ((float) ((A) / 33554432.0L))
#define   _IQ24toF(A)      ((float) ((A) / 16777216.0L))
#define   _IQ23toF(A)      ((float) ((A) / 8388608.0L))
#define   _IQ22toF(A)      ((float) ((A) / 4194304.0L))
#define   _IQ21toF(A)      ((float) ((A) / 2097152.0L))
#define   _IQ20toF(A)      ((float) ((A) / 1048576.0L))
#define   _IQ19toF(A)      ((float) ((A) / 524288.0L))
#define   _IQ18toF(A)      ((float) ((A) / 262144.0L))
#define   _IQ17toF(A)      ((float) ((A) / 131072.0L))
#define   _IQ16toF(A)      ((float) ((A) / 65536.0L))
#define   _IQ15toF(A)      ((float) ((A) / 32768.0L))
#define   _IQ14toF(A)      ((float) ((A) / 16384.0L))
#define   _IQ13toF(A)      ((float) ((A) / 8192.0L))
#define   _IQ12toF(A)      ((float) ((A) / 4096.0L))
#define   _IQ11toF(A)      ((float) ((A) / 2048.0L))
#define   _IQ10toF(A)      ((float) ((A) / 1024.0L))
#define   _IQ9toF(A)       ((float) ((A) / 512.0L))
#define   _IQ8toF(A)       ((float) ((A) / 256.0L))
#define   _IQ7toF(A)       ((float) ((A) / 128.0L))
#define   _IQ6toF(A)       ((float) ((A) / 64.0L))
#define   _IQ5toF(A)       ((float) ((A) / 32.0L))
#define   _IQ4toF(A)       ((float) ((A) / 16.0L))
#define   _IQ3toF(A)       ((float) ((A) / 8.0L))
#define   _IQ2toF(A)       ((float) ((A) / 4.0L))
#define   _IQ1toF(A)       ((float) ((A) / 2.0L))

#if GLOBAL_Q == 30
#define   _IQtoF(A)  _IQ30toF(A)
#endif
#if GLOBAL_Q == 29
#define   _IQtoF(A)  _IQ29toF(A)
#endif
#if GLOBAL_Q == 28
#define   _IQtoF(A)  _IQ28toF(A)
#endif
#if GLOBAL_Q == 27
#define   _IQtoF(A)  _IQ27toF(A)
#endif
#if GLOBAL_Q == 26
#define   _IQtoF(A)  _IQ26toF(A)
#endif
#if GLOBAL_Q == 25
#define   _IQtoF(A)  _IQ25toF(A)
#endif
#if GLOBAL_Q == 24
#define   _IQtoF(A)  _IQ24toF(A)
#endif
#if GLOBAL_Q == 23
#define   _IQtoF(A)  _IQ23toF(A)
#endif
#if GLOBAL_Q == 22
#define   _IQtoF(A)  _IQ22toF(A)
#endif
#if GLOBAL_Q == 21
#define   _IQtoF(A)  _IQ21toF(A)
#endif
#if GLOBAL_Q == 20
#define   _IQtoF(A)  _IQ20toF(A)
#endif
#if GLOBAL_Q == 19
#define   _IQtoF(A)  _IQ19toF(A)
#endif
#if GLOBAL_Q == 18
#define   _IQtoF(A)  _IQ18toF(A)
#endif
#if GLOBAL_Q == 17
#define   _IQtoF(A)  _IQ17toF(A)
#endif
#if GLOBAL_Q == 16
#define   _IQtoF(A)  _IQ16toF(A)
#endif
#if GLOBAL_Q == 15
#define   _IQtoF(A)  _IQ15toF(A)
#endif
#if GLOBAL_Q == 14
#define   _IQtoF(A)  _IQ14toF(A)
#endif
#if GLOBAL_Q == 13
#define   _IQtoF(A)  _IQ13toF(A)
#endif
#if GLOBAL_Q == 12
#define   _IQtoF(A)  _IQ12toF(A)
#endif
#if GLOBAL_Q == 11
#define   _IQtoF(A)  _IQ11toF(A)
#endif
#if GLOBAL_Q == 10
#define   _IQtoF(A)  _IQ10toF(A)
#endif
#if GLOBAL_Q == 9
#define   _IQtoF(A)  _IQ9toF(A)
#endif
#if GLOBAL_Q == 8
#define   _IQtoF(A)  _IQ8toF(A)
#endif
#if GLOBAL_Q == 7
#define   _IQtoF(A)  _IQ7toF(A)
#endif
#if GLOBAL_Q == 6
#define   _IQtoF(A)  _IQ6toF(A)
#endif
#if GLOBAL_Q == 5
#define   _IQtoF(A)  _IQ5toF(A)
#endif
#if GLOBAL_Q == 4
#define   _IQtoF(A)  _IQ4toF(A)
#endif
#if GLOBAL_Q == 3
#define   _IQtoF(A)  _IQ3toF(A)
#endif
#if GLOBAL_Q == 2
#define   _IQtoF(A)  _IQ2toF(A)
#endif
#if GLOBAL_Q == 1
#define   _IQtoF(A)  _IQ1toF(A)
#endif

#define   _IQsat(A, Pos, Neg)  ((A > Pos) ? Pos : (A < Neg) ? Neg : A)
//---------------------------------------------------------------------------
#define   _IQtoIQ30(A)  ((long) (A) << (30 - GLOBAL_Q))
#define   _IQ30toIQ(A)  ((long) (A) >> (30 - GLOBAL_Q))

#if (GLOBAL_Q >= 29)
#define   _IQtoIQ29(A) ((long) (A) >> (GLOBAL_Q - 29))
#define   _IQ29toIQ(A) ((long) (A) << (GLOBAL_Q - 29))
#else
#define   _IQtoIQ29(A) ((long) (A) << (29 - GLOBAL_Q))
#define   _IQ29toIQ(A) ((long) (A) >> (29 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 28)
#define   _IQtoIQ28(A) ((long) (A) >> (GLOBAL_Q - 28))
#define   _IQ28toIQ(A) ((long) (A) << (GLOBAL_Q - 28))
#else
#define   _IQtoIQ28(A) ((long) (A) << (28 - GLOBAL_Q))
#define   _IQ28toIQ(A) ((long) (A) >> (28 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 27)
#define   _IQtoIQ27(A) ((long) (A) >> (GLOBAL_Q - 27))
#define   _IQ27toIQ(A) ((long) (A) << (GLOBAL_Q - 27))
#else
#define   _IQtoIQ27(A) ((long) (A) << (27 - GLOBAL_Q))
#define   _IQ27toIQ(A) ((long) (A) >> (27 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 26)
#define   _IQtoIQ26(A) ((long) (A) >> (GLOBAL_Q - 26))
#define   _IQ26toIQ(A) ((long) (A) << (GLOBAL_Q - 26))
#else
#define   _IQtoIQ26(A) ((long) (A) << (26 - GLOBAL_Q))
#define   _IQ26toIQ(A) ((long) (A) >> (26 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 25)
#define   _IQtoIQ25(A) ((long) (A) >> (GLOBAL_Q - 25))
#define   _IQ25toIQ(A) ((long) (A) << (GLOBAL_Q - 25))
#else
#define   _IQtoIQ25(A) ((long) (A) << (25 - GLOBAL_Q))
#define   _IQ25toIQ(A) ((long) (A) >> (25 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 24)
#define   _IQtoIQ24(A) ((long) (A) >> (GLOBAL_Q - 24))
#define   _IQ24toIQ(A) ((long) (A) << (GLOBAL_Q - 24))
#else
#define   _IQtoIQ24(A) ((long) (A) << (24 - GLOBAL_Q))
#define   _IQ24toIQ(A) ((long) (A) >> (24 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 23)
#define   _IQtoIQ23(A) ((long) (A) >> (GLOBAL_Q - 23))
#define   _IQ23toIQ(A) ((long) (A) << (GLOBAL_Q - 23))
#else
#define   _IQtoIQ23(A) ((long) (A) << (23 - GLOBAL_Q))
#define   _IQ23toIQ(A) ((long) (A) >> (23 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 22)
#define   _IQtoIQ22(A) ((long) (A) >> (GLOBAL_Q - 22))
#define   _IQ22toIQ(A) ((long) (A) << (GLOBAL_Q - 22))
#else
#define   _IQtoIQ22(A) ((long) (A) << (22 - GLOBAL_Q))
#define   _IQ22toIQ(A) ((long) (A) >> (22 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 21)
#define   _IQtoIQ21(A) ((long) (A) >> (GLOBAL_Q - 21))
#define   _IQ21toIQ(A) ((long) (A) << (GLOBAL_Q - 21))
#else
#define   _IQtoIQ21(A) ((long) (A) << (21 - GLOBAL_Q))
#define   _IQ21toIQ(A) ((long) (A) >> (21 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 20)
#define   _IQtoIQ20(A) ((long) (A) >> (GLOBAL_Q - 20))
#define   _IQ20toIQ(A) ((long) (A) << (GLOBAL_Q - 20))
#else
#define   _IQtoIQ20(A) ((long) (A) << (20 - GLOBAL_Q))
#define   _IQ20toIQ(A) ((long) (A) >> (20 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 19)
#define   _IQtoIQ19(A) ((long) (A) >> (GLOBAL_Q - 19))
#define   _IQ19toIQ(A) ((long) (A) << (GLOBAL_Q - 19))
#else
#define   _IQtoIQ19(A) ((long) (A) << (19 - GLOBAL_Q))
#define   _IQ19toIQ(A) ((long) (A) >> (19 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 18)
#define   _IQtoIQ18(A) ((long) (A) >> (GLOBAL_Q - 18))
#define   _IQ18toIQ(A) ((long) (A) << (GLOBAL_Q - 18))
#else
#define   _IQtoIQ18(A) ((long) (A) << (18 - GLOBAL_Q))
#define   _IQ18toIQ(A) ((long) (A) >> (18 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 17)
#define   _IQtoIQ17(A) ((long) (A) >> (GLOBAL_Q - 17))
#define   _IQ17toIQ(A) ((long) (A) << (GLOBAL_Q - 17))
#else
#define   _IQtoIQ17(A) ((long) (A) << (17 - GLOBAL_Q))
#define   _IQ17toIQ(A) ((long) (A) >> (17 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 16)
#define   _IQtoIQ16(A) ((long) (A) >> (GLOBAL_Q - 16))
#define   _IQ16toIQ(A) ((long) (A) << (GLOBAL_Q - 16))
#else
#define   _IQtoIQ16(A) ((long) (A) << (16 - GLOBAL_Q))
#define   _IQ16toIQ(A) ((long) (A) >> (16 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 15)
#define   _IQtoIQ15(A) ((long) (A) >> (GLOBAL_Q - 15))
#define   _IQ15toIQ(A) ((long) (A) << (GLOBAL_Q - 15))
#define   _IQtoQ15(A)  ((long) (A) >> (GLOBAL_Q - 15))
#define   _Q15toIQ(A)  ((long) (A) << (GLOBAL_Q - 15))
#else
#define   _IQtoIQ15(A) ((long) (A) << (15 - GLOBAL_Q))
#define   _IQ15toIQ(A) ((long) (A) >> (15 - GLOBAL_Q))
#define   _IQtoQ15(A)  ((long) (A) << (15 - GLOBAL_Q))
#define   _Q15toIQ(A)  ((long) (A) >> (15 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 14)
#define   _IQtoIQ14(A) ((long) (A) >> (GLOBAL_Q - 14))
#define   _IQ14toIQ(A) ((long) (A) << (GLOBAL_Q - 14))
#define   _IQtoQ14(A)  ((long) (A) >> (GLOBAL_Q - 14))
#define   _Q14toIQ(A)  ((long) (A) << (GLOBAL_Q - 14))
#else
#define   _IQtoIQ14(A) ((long) (A) << (14 - GLOBAL_Q))
#define   _IQ14toIQ(A) ((long) (A) >> (14 - GLOBAL_Q))
#define   _IQtoQ14(A)  ((long) (A) << (14 - GLOBAL_Q))
#define   _Q14toIQ(A)  ((long) (A) >> (14 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 13)
#define   _IQtoIQ13(A) ((long) (A) >> (GLOBAL_Q - 13))
#define   _IQ13toIQ(A) ((long) (A) << (GLOBAL_Q - 13))
#define   _IQtoQ13(A)  ((long) (A) >> (GLOBAL_Q - 13))
#define   _Q13toIQ(A)  ((long) (A) << (GLOBAL_Q - 13))
#else
#define   _IQtoIQ13(A) ((long) (A) << (13 - GLOBAL_Q))
#define   _IQ13toIQ(A) ((long) (A) >> (13 - GLOBAL_Q))
#define   _IQtoQ13(A)  ((long) (A) << (13 - GLOBAL_Q))
#define   _Q13toIQ(A)  ((long) (A) >> (13 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 12)
#define   _IQtoIQ12(A) ((long) (A) >> (GLOBAL_Q - 12))
#define   _IQ12toIQ(A) ((long) (A) << (GLOBAL_Q - 12))
#define   _IQtoQ12(A)  ((long) (A) >> (GLOBAL_Q - 12))
#define   _Q12toIQ(A)  ((long) (A) << (GLOBAL_Q - 12))
#else
#define   _IQtoIQ12(A) ((long) (A) << (12 - GLOBAL_Q))
#define   _IQ12toIQ(A) ((long) (A) >> (12 - GLOBAL_Q))
#define   _IQtoQ12(A)  ((long) (A) << (12 - GLOBAL_Q))
#define   _Q12toIQ(A)  ((long) (A) >> (12 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 11)
#define   _IQtoIQ11(A) ((long) (A) >> (GLOBAL_Q - 11))
#define   _IQ11toIQ(A) ((long) (A) << (GLOBAL_Q - 11))
#define   _IQtoQ11(A)  ((long) (A) >> (GLOBAL_Q - 11))
#define   _Q11toIQ(A)  ((long) (A) << (GLOBAL_Q - 11))
#else
#define   _IQtoIQ11(A) ((long) (A) << (11 - GLOBAL_Q))
#define   _IQ11toIQ(A) ((long) (A) >> (11 - GLOBAL_Q))
#define   _IQtoQ11(A)  ((long) (A) << (11 - GLOBAL_Q))
#define   _Q11toIQ(A)  ((long) (A) >> (11 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 10)
#define   _IQtoIQ10(A) ((long) (A) >> (GLOBAL_Q - 10))
#define   _IQ10toIQ(A) ((long) (A) << (GLOBAL_Q - 10))
#define   _IQtoQ10(A)  ((long) (A) >> (GLOBAL_Q - 10))
#define   _Q10toIQ(A)  ((long) (A) << (GLOBAL_Q - 10))
#else
#define   _IQtoIQ10(A) ((long) (A) << (10 - GLOBAL_Q))
#define   _IQ10toIQ(A) ((long) (A) >> (10 - GLOBAL_Q))
#define   _IQtoQ10(A)  ((long) (A) << (10 - GLOBAL_Q))
#define   _Q10toIQ(A)  ((long) (A) >> (10 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 9)
#define   _IQtoIQ9(A) ((long) (A) >> (GLOBAL_Q - 9))
#define   _IQ9toIQ(A) ((long) (A) << (GLOBAL_Q - 9))
#define   _IQtoQ9(A)  ((long) (A) >> (GLOBAL_Q - 9))
#define   _Q9toIQ(A)  ((long) (A) << (GLOBAL_Q - 9))
#else
#define   _IQtoIQ9(A) ((long) (A) << (9 - GLOBAL_Q))
#define   _IQ9toIQ(A) ((long) (A) >> (9 - GLOBAL_Q))
#define   _IQtoQ9(A)  ((long) (A) << (9 - GLOBAL_Q))
#define   _Q9toIQ(A)  ((long) (A) >> (9 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 8)
#define   _IQtoIQ8(A) ((long) (A) >> (GLOBAL_Q - 8))
#define   _IQ8toIQ(A) ((long) (A) << (GLOBAL_Q - 8))
#define   _IQtoQ8(A)  ((long) (A) >> (GLOBAL_Q - 8))
#define   _Q8toIQ(A)  ((long) (A) << (GLOBAL_Q - 8))
#else
#define   _IQtoIQ8(A) ((long) (A) << (8 - GLOBAL_Q))
#define   _IQ8toIQ(A) ((long) (A) >> (8 - GLOBAL_Q))
#define   _IQtoQ8(A)  ((long) (A) << (8 - GLOBAL_Q))
#define   _Q8toIQ(A)  ((long) (A) >> (8 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 7)
#define   _IQtoIQ7(A) ((long) (A) >> (GLOBAL_Q - 7))
#define   _IQ7toIQ(A) ((long) (A) << (GLOBAL_Q - 7))
#define   _IQtoQ7(A)  ((long) (A) >> (GLOBAL_Q - 7))
#define   _Q7toIQ(A)  ((long) (A) << (GLOBAL_Q - 7))
#else
#define   _IQtoIQ7(A) ((long) (A) << (7 - GLOBAL_Q))
#define   _IQ7toIQ(A) ((long) (A) >> (7 - GLOBAL_Q))
#define   _IQtoQ7(A)  ((long) (A) << (7 - GLOBAL_Q))
#define   _Q7toIQ(A)  ((long) (A) >> (7 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 6)
#define   _IQtoIQ6(A) ((long) (A) >> (GLOBAL_Q - 6))
#define   _IQ6toIQ(A) ((long) (A) << (GLOBAL_Q - 6))
#define   _IQtoQ6(A)  ((long) (A) >> (GLOBAL_Q - 6))
#define   _Q6toIQ(A)  ((long) (A) << (GLOBAL_Q - 6))
#else
#define   _IQtoIQ6(A) ((long) (A) << (6 - GLOBAL_Q))
#define   _IQ6toIQ(A) ((long) (A) >> (6 - GLOBAL_Q))
#define   _IQtoQ6(A)  ((long) (A) << (6 - GLOBAL_Q))
#define   _Q6toIQ(A)  ((long) (A) >> (6 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 5)
#define   _IQtoIQ5(A) ((long) (A) >> (GLOBAL_Q - 5))
#define   _IQ5toIQ(A) ((long) (A) << (GLOBAL_Q - 5))
#define   _IQtoQ5(A)  ((long) (A) >> (GLOBAL_Q - 5))
#define   _Q5toIQ(A)  ((long) (A) << (GLOBAL_Q - 5))
#else
#define   _IQtoIQ5(A) ((long) (A) << (5 - GLOBAL_Q))
#define   _IQ5toIQ(A) ((long) (A) >> (5 - GLOBAL_Q))
#define   _IQtoQ5(A)  ((long) (A) << (5 - GLOBAL_Q))
#define   _Q5toIQ(A)  ((long) (A) >> (5 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 4)
#define   _IQtoIQ4(A) ((long) (A) >> (GLOBAL_Q - 4))
#define   _IQ4toIQ(A) ((long) (A) << (GLOBAL_Q - 4))
#define   _IQtoQ4(A)  ((long) (A) >> (GLOBAL_Q - 4))
#define   _Q4toIQ(A)  ((long) (A) << (GLOBAL_Q - 4))
#else
#define   _IQtoIQ4(A) ((long) (A) << (4 - GLOBAL_Q))
#define   _IQ4toIQ(A) ((long) (A) >> (4 - GLOBAL_Q))
#define   _IQtoQ4(A)  ((long) (A) << (4 - GLOBAL_Q))
#define   _Q4toIQ(A)  ((long) (A) >> (4 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 3)
#define   _IQtoIQ3(A) ((long) (A) >> (GLOBAL_Q - 3))
#define   _IQ3toIQ(A) ((long) (A) << (GLOBAL_Q - 3))
#define   _IQtoQ3(A)  ((long) (A) >> (GLOBAL_Q - 3))
#define   _Q3toIQ(A)  ((long) (A) << (GLOBAL_Q - 3))
#else
#define   _IQtoIQ3(A) ((long) (A) << (3 - GLOBAL_Q))
#define   _IQ3toIQ(A) ((long) (A) >> (3 - GLOBAL_Q))
#define   _IQtoQ3(A)  ((long) (A) << (3 - GLOBAL_Q))
#define   _Q3toIQ(A)  ((long) (A) >> (3 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 2)
#define   _IQtoIQ2(A) ((long) (A) >> (GLOBAL_Q - 2))
#define   _IQ2toIQ(A) ((long) (A) << (GLOBAL_Q - 2))
#define   _IQtoQ2(A)  ((long) (A) >> (GLOBAL_Q - 2))
#define   _Q2toIQ(A)  ((long) (A) << (GLOBAL_Q - 2))
#else
#define   _IQtoIQ2(A) ((long) (A) << (2 - GLOBAL_Q))
#define   _IQ2toIQ(A) ((long) (A) >> (2 - GLOBAL_Q))
#define   _IQtoQ2(A)  ((long) (A) << (2 - GLOBAL_Q))
#define   _Q2toIQ(A)  ((long) (A) >> (2 - GLOBAL_Q))
#endif

#if (GLOBAL_Q >= 1)
#define   _IQtoQ1(A)  ((long) (A) >> (GLOBAL_Q - 1))
#define   _Q1toIQ(A)  ((long) (A) << (GLOBAL_Q - 1))
#else
#define   _IQtoQ1(A)  ((long) (A) << (1 - GLOBAL_Q))
#define   _Q1toIQ(A)  ((long) (A) >> (1 - GLOBAL_Q))
#endif

#define   _IQtoIQ1(A)   ((long) (A) >> (GLOBAL_Q - 1))
#define   _IQ1toIQ(A)   ((long) (A) << (GLOBAL_Q - 1))

/////////////////////////////////////////////////////////////
long multiply(long x, long y);
long long multiply_fixed_base_select(long long x, long long y, int base);
long divide(long num, long den);
long divide19(long num, long den);
long divideN(long num, long den, unsigned int d);
long sin_fixed(long x);
long cos_fixed(long x);
long sqrt_fixed(long x);
long exp_fixed(long x);
long exp_fixedN(long x, unsigned int n);

#define   _IQabs(A)     ((A) > 0 ? (A): -(A))
#define   _IQmpy(A,B)   multiply(A,B)
#define   _IQ19mpy(A,B) multiply_fixed_base_select(A,B,19)
#define   _IQ18mpy(A,B) multiply_fixed_base_select(A,B,18)

#define   _IQdiv(A,B)  	divide(A,B)
#define   _IQ19div(A,B)  	divide19(A,B)
#define   _IQ18div(A,B)  	divideN(A,B,18)
#define   _IQsin(A)  	sin_fixed(A)
#define   _IQcos(A)  	cos_fixed(A)
#define   _IQsinPU(A)  	sin_fixed(A)
#define   _IQcosPU(A)  	cos_fixed(A)
#define   _IQsqrt(A)    sqrt_fixed(A)
#define   _IQexp(A)    exp_fixed(A)
#define   _IQ18exp(A)    exp_fixedN(A,18)


#define   _IQmpyI32(A,B)    ((A)*(B))


#define PI 3.1415926535897932384626433832795
#define PI_2 1.5707963267948966192313216916398
#define TWO_PI 6.283185307179586476925286766559

#ifndef ONE_24
#define ONE_24 16777216
#endif
#ifndef ONE_27
#define ONE_27 134217728
#endif
#ifndef ONE_28
#define ONE_28 268435456
#endif

// #ifndef FIXED_PI
// #define FIXED_PI 52707179
// #endif

// #ifndef FIXED_2PI
// #define FIXED_2PI 105414357
// #endif

#ifndef FIXED_PI_30
#define FIXED_PI_30 3373259426
#endif

#ifndef FIXED_2PI_30
#define FIXED_2PI_30 6746518852
#endif

#ifndef FIXED_3PIna2
#define FIXED_3PIna2 79060768
#endif

#ifndef FIXED_PIna3
#define FIXED_PIna3 17569059
#endif

#ifndef FIXED_PIna6
#define FIXED_PIna6 8784529
#endif

//###########################################################################
// If FLOAT_MATH is used, the IQmath library function are replaced by
// equivalent floating point operations:
//===========================================================================

#define   _IQ15sqrt(A)        sqrt(A)

#endif //IQ_MATH_LIB