//###########################################################################
//
// FILE: IQmathLib.h
//
// TITLE: IQ Math library functions definitions.
//
//###########################################################################
//
// Ver | dd-mmm-yyyy | Who | Description of changes
// =====|=============|=======|==============================================
// 1.3 | 19 Nov 2001 | A. T. | Original Release.
// -----|-------------|-------|----------------------------------------------
// 1.4 | 17 May 2002 | A. T. | Added new functions and support for
// | | | intrinsics IQmpy, IQxmpy, IQsat.
// -----|-------------|-------|----------------------------------------------
// 1.4a| 12 Jun 2002 | A. T. | Fixed problem with _IQ() operation on
// | | | variables.
// -----|-------------|-------|----------------------------------------------
// 1.4b| 18 Jun 2002 | A. T. | Fixed bug with _IQtoIQN() and _IQNtoIQ()
// | | | operations.
// -----|-------------|-------|----------------------------------------------
// 1.4d| 30 Mar 2003 | DA/SD | 1. Added macro parameters in parentheses
// | | | in number of places where it matters
// | | | 2. Added macro definition to include header
// | | | file multiple times in the program.
// -----|-------------|-------|----------------------------------------------
//
//###########################################################################
//
// User needs to configure "MATH_TYPE" and "GLOBAL_Q" values:
//
//---------------------------------------------------------------------------
// Select math type, IQ_MATH or FLOAT_MATH:
//
#ifndef __IQMATHLIB_H_INCLUDED__
#define __IQMATHLIB_H_INCLUDED__
#define FLOAT_MATH 1
#define IQ_MATH 0
#ifndef MATH_TYPE
#define MATH_TYPE IQ_MATH
#endif
//---------------------------------------------------------------------------
// Select global Q value and scaling. The Q value is limited to the
// following range for all functions:
//
// 30 <= GLOBAL_Q <= 1
//
#ifndef GLOBAL_Q
#define GLOBAL_Q 24
#endif
//---------------------------------------------------------------------------
// Various Usefull Constant Definitions:
//
#define QG GLOBAL_Q
#define Q30 30
#define Q29 29
#define Q28 28
#define Q27 27
#define Q26 26
#define Q25 25
#define Q24 24
#define Q23 23
#define Q22 22
#define Q21 21
#define Q20 20
#define Q19 19
#define Q18 18
#define Q17 17
#define Q16 16
#define Q15 15
#define Q14 14
#define Q13 13
#define Q12 12
#define Q11 11
#define Q10 10
#define Q9 9
#define Q8 8
#define Q7 7
#define Q6 6
#define Q5 5
#define Q4 4
#define Q3 3
#define Q2 2
#define Q1 1
#define MAX_IQ_POS 2147483647
#define MAX_IQ_NEG -2147483648
#define MIN_IQ_POS 1
#define MIN_IQ_NEG -1
//###########################################################################
#if MATH_TYPE == IQ_MATH
//###########################################################################
// If IQ_MATH is used, the following IQmath library function definitions
// are used:
//===========================================================================
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 _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
//---------------------------------------------------------------------------
extern float _IQ30toF(long A);
extern float _IQ29toF(long A);
extern float _IQ28toF(long A);
extern float _IQ27toF(long A);
extern float _IQ26toF(long A);
extern float _IQ25toF(long A);
extern float _IQ24toF(lon