WizziLab / modem_ref_v5_3_217

Dependents:   modem_ref_helper_for_v5_3_217

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers kal_math.h Source File

kal_math.h

00001 /// @copyright
00002 /// ========================================================================={{{
00003 /// Copyright (c) 2013-2014 WizziLab                                           /
00004 /// All rights reserved                                                        /
00005 ///                                                                            /
00006 /// IMPORTANT: This Software may not be modified, copied or distributed unless /
00007 /// embedded on a WizziLab product. Other than for the foregoing purpose, this /
00008 /// Software and/or its documentation may not be used, reproduced, copied,     /
00009 /// prepared derivative works of, modified, performed, distributed, displayed  /
00010 /// or sold for any purpose. For the sole purpose of embedding this Software   /
00011 /// on a WizziLab product, copy, modification and distribution of this         /
00012 /// Software is granted provided that the following conditions are respected:  /
00013 ///                                                                            /
00014 /// *  Redistributions of source code must retain the above copyright notice,  /
00015 ///    this list of conditions and the following disclaimer                    /
00016 ///                                                                            /
00017 /// *  Redistributions in binary form must reproduce the above copyright       /
00018 ///    notice, this list of conditions and the following disclaimer in the     /
00019 ///    documentation and/or other materials provided with the distribution.    /
00020 ///                                                                            /
00021 /// *  The name of WizziLab can not be used to endorse or promote products     /
00022 ///    derived from this software without specific prior written permission.   /
00023 ///                                                                            /
00024 /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS        /
00025 /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED  /
00026 /// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR /
00027 /// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR          /
00028 /// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,      /
00029 /// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,        /
00030 /// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,            /
00031 /// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     /
00032 /// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING    /
00033 /// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS         /
00034 /// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.               /
00035 /// WIZZILAB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,       /
00036 /// ENHANCEMENTS OR MODIFICATIONS.                                             /
00037 ///                                                                            /
00038 /// Should you have any questions regarding your right to use this Software,   /
00039 /// contact WizziLab at www.wizzilab.com.                                      /
00040 ///                                                                            /
00041 /// =========================================================================}}}
00042 /// @endcopyright
00043 
00044 #ifndef __KAL_MATH_H__
00045 #define __KAL_MATH_H__
00046 
00047 #include "hal_types.h"
00048 
00049 // =============================================================================
00050 // General purpose constants
00051 // =============================================================================
00052 
00053 #define MIN_S8                          ((s8 )(0x80))
00054 #define MAX_S8                          ((s8 )(0x7F))
00055 #define MIN_U8                          ((u8 )(0x0))
00056 #define MAX_U8                          ((u8 )(0xFF))
00057 
00058 #define MIN_S12                         ((s16)(0xF800))
00059 #define MAX_S12                         ((s16)(0x7FF))
00060 #define MIN_U12                         ((u16)(0x0))
00061 #define MAX_U12                         ((u16)(0xFFF))
00062 
00063 #define MIN_S16                         ((s16)(0x8000))
00064 #define MAX_S16                         ((s16)(0x7FFF))
00065 #define MIN_U16                         ((u16)(0x0))
00066 #define MAX_U16                         ((u16)(0xFFFF))
00067 
00068 #define MIN_U32                         ((u32)(0x0))
00069 #define MAX_U32                         ((u32)(0xFFFFFFFF))
00070 #define MIN_S32                         ((s32)(0x80000000))
00071 #define MAX_S32                         ((s32)(0x7FFFFFFF))
00072 
00073 // =============================================================================
00074 // Compare operators
00075 // =============================================================================
00076 
00077 #define KAL_MAX(a,b)                    (((a) > (b)) ? (a) : (b))
00078 #define KAL_MIN(a,b)                    (((a) < (b)) ? (a) : (b))
00079 
00080 #define KAL_ABS(a)                      (((a) > 0) ? (a) : (-(a)))
00081 #define KAL_ABS16_FAST(a)               ((a) ^ ((a) >> 15))
00082 #define KAL_ABS32_FAST(a)               ((a) ^ ((a) >> 31))
00083 
00084 #define KAL_SAT_RANGE(a,min,max)        (((a) < (min)) ? (min) : ((a) > (max)) ? (max) : (a))
00085 #define KAL_SAT_S16(a)                  (((a) < MIN_S16) ? MIN_S16 : ((a) > MAX_S16) ? MAX_S16 : (s16)(a))
00086 #define KAL_SAT_U16(a)                  (((a) > MAX_U16) ? MAX_U16 : (u16)(a))
00087 #define KAL_SAT_S8(a)                   (((a) < MIN_S8)  ? MIN_S8  : ((a) > MAX_S8)  ? MAX_S8 :  (s8)(a))
00088 #define KAL_SAT_U8(a)                   (((a) > MAX_U8)  ? MAX_U8 :  (u8)(a))
00089 
00090 #define KAL_COMP16_FAST(a,b)            (((b) - (a)) >> 15)
00091 
00092 // =============================================================================
00093 // Bit manipulation operators
00094 // =============================================================================
00095 
00096 #define KAL_BIT_CLR(d, j)               (d)[(j)/8] &= (~(1 << ((j) & 7)))
00097 #define KAL_BIT_SET(d, j)               (d)[(j)/8] |= (((1) << ((j) & 7)))
00098 #define KAL_BIT_MOV(s, i, d, j)         (d)[(j)/8] |= ((((s)[(i)/8]>>((i)&7))&1)<<((j)&7))
00099 #define KAL_BIT_IS_ONE(s, i)            ((s)[(i)/8] & (1 << ((i) & 7)))
00100 #define KAL_BIT_GET(s, i)               ((s)[(i)/8] >> ((i) & 7) & 1)
00101 
00102 #define KAL_2BIT_SH(i)                  (2 * ((i) & 3))
00103 #define KAL_2BIT_GET(d, i)              (((d)[(i)/4] >> KAL_2BIT_SH(i)) & 3)  
00104 #define KAL_2BIT_CLR(d, i)              ((d)[(i)/4] &=~ (3 << KAL_2BIT_SH(i)))
00105 #define KAL_2BIT_SET(d, i, c)           ((d)[(i)/4] |= ((c) << KAL_2BIT_SH(i)))  
00106 #define KAL_2BIT_MOV(s, i, d, j)        KAL_2BIT_SET(d, j, KAL_2BIT_GET(s, i))
00107 
00108 // =============================================================================
00109 // Simple arithmetic operators
00110 // =============================================================================
00111 
00112 #define KAL_MUL(x,y)                    ((x)*(y))
00113 #define KAL_MLF(x,y,frac)               (((x)*(y)) >> (frac))
00114 #define KAL_SHIFT_LEFT_SIGNED(a,s)      (((s) > 0) ? ((a) << (s)) : ((a) >> (-(s))))
00115 #define KAL_SQR(x)                      KAL_MUL(x,x)
00116 #define KAL_DIV_INT(n,d)                (((n) + ((d)/2))/(d))
00117 #define KAL_DIV_CEILING(n,d)            (((n) + (d) - 1)/(d))
00118 #define KAL_DIV_FLOOR(n,d)              (((n)          )/(d))
00119 
00120 // =============================================================================
00121 // Complex operators
00122 // =============================================================================
00123 
00124 #define KAL_COMPLEX_ADD(a,b,out)        { (out).I = (a).I + (b).I; (out).Q = (a).Q + (b).Q; }
00125 #define KAL_COMPLEX_SUB(a,b,out)        { (out).I = (a).I - (b).I; (out).Q = (a).Q - (b).Q; }
00126 
00127 #define KAL_COMPLEX_MUL(a,b,out)        { (out).I = KAL_MUL((a).I,(b).I) - KAL_MUL((a).Q,(b).Q); \
00128                                           (out).Q = KAL_MUL((a).I,(b).Q) + KAL_MUL((a).Q,(b).I); }
00129 
00130 #define KAL_COMPLEX_MLF(a,b,out,frac)   { (out).I = KAL_MLF((a).I,(b).I,(frac)) - KAL_MLF((a).Q,(b).Q,(frac)); \
00131                                           (out).Q = KAL_MLF((a).I,(b).Q,(frac)) + KAL_MLF((a).Q,(b).I,(frac)); }
00132 
00133 #define KAL_COMPLEX_NORM(a)             (KAL_MUL((a).I,(a).I) + KAL_MUL((a).Q,(a).Q))
00134 
00135 #endif // __KAL_MATH_H__