Exportable version of WizziLab's modem driver.
include/kal_math.h@46:9b83866cef2c, 2019-07-31 (annotated)
- Committer:
- Jeej
- Date:
- Wed Jul 31 16:48:48 2019 +0000
- Revision:
- 46:9b83866cef2c
- Parent:
- 31:517fc900afba
- Child:
- 56:67e3d9608403
Updated for mbed-os 5.13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jeej | 31:517fc900afba | 1 | /// @copyright |
Jeej | 31:517fc900afba | 2 | /// ========================================================================={{{ |
Jeej | 31:517fc900afba | 3 | /// Copyright (c) 2013-2014 WizziLab / |
Jeej | 31:517fc900afba | 4 | /// All rights reserved / |
Jeej | 31:517fc900afba | 5 | /// / |
Jeej | 31:517fc900afba | 6 | /// IMPORTANT: This Software may not be modified, copied or distributed unless / |
Jeej | 31:517fc900afba | 7 | /// embedded on a WizziLab product. Other than for the foregoing purpose, this / |
Jeej | 31:517fc900afba | 8 | /// Software and/or its documentation may not be used, reproduced, copied, / |
Jeej | 31:517fc900afba | 9 | /// prepared derivative works of, modified, performed, distributed, displayed / |
Jeej | 31:517fc900afba | 10 | /// or sold for any purpose. For the sole purpose of embedding this Software / |
Jeej | 31:517fc900afba | 11 | /// on a WizziLab product, copy, modification and distribution of this / |
Jeej | 31:517fc900afba | 12 | /// Software is granted provided that the following conditions are respected: / |
Jeej | 31:517fc900afba | 13 | /// / |
Jeej | 31:517fc900afba | 14 | /// * Redistributions of source code must retain the above copyright notice, / |
Jeej | 31:517fc900afba | 15 | /// this list of conditions and the following disclaimer / |
Jeej | 31:517fc900afba | 16 | /// / |
Jeej | 31:517fc900afba | 17 | /// * Redistributions in binary form must reproduce the above copyright / |
Jeej | 31:517fc900afba | 18 | /// notice, this list of conditions and the following disclaimer in the / |
Jeej | 31:517fc900afba | 19 | /// documentation and/or other materials provided with the distribution. / |
Jeej | 31:517fc900afba | 20 | /// / |
Jeej | 31:517fc900afba | 21 | /// * The name of WizziLab can not be used to endorse or promote products / |
Jeej | 31:517fc900afba | 22 | /// derived from this software without specific prior written permission. / |
Jeej | 31:517fc900afba | 23 | /// / |
Jeej | 31:517fc900afba | 24 | /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS / |
Jeej | 31:517fc900afba | 25 | /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED / |
Jeej | 31:517fc900afba | 26 | /// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR / |
Jeej | 31:517fc900afba | 27 | /// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR / |
Jeej | 31:517fc900afba | 28 | /// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, / |
Jeej | 31:517fc900afba | 29 | /// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, / |
Jeej | 31:517fc900afba | 30 | /// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, / |
Jeej | 31:517fc900afba | 31 | /// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY / |
Jeej | 31:517fc900afba | 32 | /// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING / |
Jeej | 31:517fc900afba | 33 | /// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS / |
Jeej | 31:517fc900afba | 34 | /// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. / |
Jeej | 31:517fc900afba | 35 | /// WIZZILAB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, / |
Jeej | 31:517fc900afba | 36 | /// ENHANCEMENTS OR MODIFICATIONS. / |
Jeej | 31:517fc900afba | 37 | /// / |
Jeej | 31:517fc900afba | 38 | /// Should you have any questions regarding your right to use this Software, / |
Jeej | 31:517fc900afba | 39 | /// contact WizziLab at www.wizzilab.com. / |
Jeej | 31:517fc900afba | 40 | /// / |
Jeej | 31:517fc900afba | 41 | /// =========================================================================}}} |
Jeej | 31:517fc900afba | 42 | /// @endcopyright |
Jeej | 31:517fc900afba | 43 | |
Jeej | 31:517fc900afba | 44 | #ifndef __KAL_MATH_H__ |
Jeej | 31:517fc900afba | 45 | #define __KAL_MATH_H__ |
Jeej | 31:517fc900afba | 46 | |
Jeej | 31:517fc900afba | 47 | #include "hal_types.h" |
Jeej | 31:517fc900afba | 48 | |
Jeej | 31:517fc900afba | 49 | // ============================================================================= |
Jeej | 31:517fc900afba | 50 | // General purpose constants |
Jeej | 31:517fc900afba | 51 | // ============================================================================= |
Jeej | 31:517fc900afba | 52 | |
Jeej | 31:517fc900afba | 53 | #define MIN_S8 ((s8 )(0x80)) |
Jeej | 31:517fc900afba | 54 | #define MAX_S8 ((s8 )(0x7F)) |
Jeej | 31:517fc900afba | 55 | #define MIN_U8 ((u8 )(0x0)) |
Jeej | 31:517fc900afba | 56 | #define MAX_U8 ((u8 )(0xFF)) |
Jeej | 31:517fc900afba | 57 | |
Jeej | 31:517fc900afba | 58 | #define MIN_S12 ((s16)(0xF800)) |
Jeej | 31:517fc900afba | 59 | #define MAX_S12 ((s16)(0x7FF)) |
Jeej | 31:517fc900afba | 60 | #define MIN_U12 ((u16)(0x0)) |
Jeej | 31:517fc900afba | 61 | #define MAX_U12 ((u16)(0xFFF)) |
Jeej | 31:517fc900afba | 62 | |
Jeej | 31:517fc900afba | 63 | #define MIN_S16 ((s16)(0x8000)) |
Jeej | 31:517fc900afba | 64 | #define MAX_S16 ((s16)(0x7FFF)) |
Jeej | 31:517fc900afba | 65 | #define MIN_U16 ((u16)(0x0)) |
Jeej | 31:517fc900afba | 66 | #define MAX_U16 ((u16)(0xFFFF)) |
Jeej | 31:517fc900afba | 67 | |
Jeej | 31:517fc900afba | 68 | #define MIN_U32 ((u32)(0x0)) |
Jeej | 31:517fc900afba | 69 | #define MAX_U32 ((u32)(0xFFFFFFFF)) |
Jeej | 31:517fc900afba | 70 | #define MIN_S32 ((s32)(0x80000000)) |
Jeej | 31:517fc900afba | 71 | #define MAX_S32 ((s32)(0x7FFFFFFF)) |
Jeej | 31:517fc900afba | 72 | |
Jeej | 31:517fc900afba | 73 | // ============================================================================= |
Jeej | 31:517fc900afba | 74 | // Compare operators |
Jeej | 31:517fc900afba | 75 | // ============================================================================= |
Jeej | 31:517fc900afba | 76 | |
Jeej | 31:517fc900afba | 77 | #define KAL_MAX(a,b) (((a) > (b)) ? (a) : (b)) |
Jeej | 31:517fc900afba | 78 | #define KAL_MIN(a,b) (((a) < (b)) ? (a) : (b)) |
Jeej | 31:517fc900afba | 79 | |
Jeej | 31:517fc900afba | 80 | #define KAL_ABS(a) (((a) > 0) ? (a) : (-(a))) |
Jeej | 31:517fc900afba | 81 | #define KAL_ABS16_FAST(a) ((a) ^ ((a) >> 15)) |
Jeej | 31:517fc900afba | 82 | #define KAL_ABS32_FAST(a) ((a) ^ ((a) >> 31)) |
Jeej | 31:517fc900afba | 83 | |
Jeej | 31:517fc900afba | 84 | #define KAL_SAT_RANGE(a,min,max) (((a) < (min)) ? (min) : ((a) > (max)) ? (max) : (a)) |
Jeej | 31:517fc900afba | 85 | #define KAL_SAT_S16(a) (((a) < MIN_S16) ? MIN_S16 : ((a) > MAX_S16) ? MAX_S16 : (s16)(a)) |
Jeej | 31:517fc900afba | 86 | #define KAL_SAT_U16(a) (((a) > MAX_U16) ? MAX_U16 : (u16)(a)) |
Jeej | 31:517fc900afba | 87 | #define KAL_SAT_S8(a) (((a) < MIN_S8) ? MIN_S8 : ((a) > MAX_S8) ? MAX_S8 : (s8)(a)) |
Jeej | 31:517fc900afba | 88 | #define KAL_SAT_U8(a) (((a) > MAX_U8) ? MAX_U8 : (u8)(a)) |
Jeej | 31:517fc900afba | 89 | |
Jeej | 31:517fc900afba | 90 | #define KAL_COMP16_FAST(a,b) (((b) - (a)) >> 15) |
Jeej | 31:517fc900afba | 91 | |
Jeej | 31:517fc900afba | 92 | // ============================================================================= |
Jeej | 31:517fc900afba | 93 | // Bit manipulation operators |
Jeej | 31:517fc900afba | 94 | // ============================================================================= |
Jeej | 31:517fc900afba | 95 | |
Jeej | 31:517fc900afba | 96 | #define KAL_BIT_CLR(d, j) (d)[(j)/8] &= (~(1 << ((j) & 7))) |
Jeej | 31:517fc900afba | 97 | #define KAL_BIT_SET(d, j) (d)[(j)/8] |= (((1) << ((j) & 7))) |
Jeej | 31:517fc900afba | 98 | #define KAL_BIT_MOV(s, i, d, j) (d)[(j)/8] |= ((((s)[(i)/8]>>((i)&7))&1)<<((j)&7)) |
Jeej | 31:517fc900afba | 99 | #define KAL_BIT_IS_ONE(s, i) ((s)[(i)/8] & (1 << ((i) & 7))) |
Jeej | 31:517fc900afba | 100 | #define KAL_BIT_GET(s, i) ((s)[(i)/8] >> ((i) & 7) & 1) |
Jeej | 31:517fc900afba | 101 | |
Jeej | 31:517fc900afba | 102 | #define KAL_2BIT_SH(i) (2 * ((i) & 3)) |
Jeej | 31:517fc900afba | 103 | #define KAL_2BIT_GET(d, i) (((d)[(i)/4] >> KAL_2BIT_SH(i)) & 3) |
Jeej | 31:517fc900afba | 104 | #define KAL_2BIT_CLR(d, i) ((d)[(i)/4] &=~ (3 << KAL_2BIT_SH(i))) |
Jeej | 31:517fc900afba | 105 | #define KAL_2BIT_SET(d, i, c) ((d)[(i)/4] |= ((c) << KAL_2BIT_SH(i))) |
Jeej | 31:517fc900afba | 106 | #define KAL_2BIT_MOV(s, i, d, j) KAL_2BIT_SET(d, j, KAL_2BIT_GET(s, i)) |
Jeej | 31:517fc900afba | 107 | |
Jeej | 31:517fc900afba | 108 | // ============================================================================= |
Jeej | 31:517fc900afba | 109 | // Simple arithmetic operators |
Jeej | 31:517fc900afba | 110 | // ============================================================================= |
Jeej | 31:517fc900afba | 111 | |
Jeej | 31:517fc900afba | 112 | #define KAL_MUL(x,y) ((x)*(y)) |
Jeej | 31:517fc900afba | 113 | #define KAL_MLF(x,y,frac) (((x)*(y)) >> (frac)) |
Jeej | 31:517fc900afba | 114 | #define KAL_SHIFT_LEFT_SIGNED(a,s) (((s) > 0) ? ((a) << (s)) : ((a) >> (-(s)))) |
Jeej | 31:517fc900afba | 115 | #define KAL_SQR(x) KAL_MUL(x,x) |
Jeej | 31:517fc900afba | 116 | #define KAL_DIV_INT(n,d) (((n) + ((d)/2))/(d)) |
Jeej | 31:517fc900afba | 117 | #define KAL_DIV_CEILING(n,d) (((n) + (d) - 1)/(d)) |
Jeej | 31:517fc900afba | 118 | #define KAL_DIV_FLOOR(n,d) (((n) )/(d)) |
Jeej | 31:517fc900afba | 119 | |
Jeej | 31:517fc900afba | 120 | // ============================================================================= |
Jeej | 31:517fc900afba | 121 | // Complex operators |
Jeej | 31:517fc900afba | 122 | // ============================================================================= |
Jeej | 31:517fc900afba | 123 | |
Jeej | 31:517fc900afba | 124 | #define KAL_COMPLEX_ADD(a,b,out) { (out).I = (a).I + (b).I; (out).Q = (a).Q + (b).Q; } |
Jeej | 31:517fc900afba | 125 | #define KAL_COMPLEX_SUB(a,b,out) { (out).I = (a).I - (b).I; (out).Q = (a).Q - (b).Q; } |
Jeej | 31:517fc900afba | 126 | |
Jeej | 31:517fc900afba | 127 | #define KAL_COMPLEX_MUL(a,b,out) { (out).I = KAL_MUL((a).I,(b).I) - KAL_MUL((a).Q,(b).Q); \ |
Jeej | 31:517fc900afba | 128 | (out).Q = KAL_MUL((a).I,(b).Q) + KAL_MUL((a).Q,(b).I); } |
Jeej | 31:517fc900afba | 129 | |
Jeej | 31:517fc900afba | 130 | #define KAL_COMPLEX_MLF(a,b,out,frac) { (out).I = KAL_MLF((a).I,(b).I,(frac)) - KAL_MLF((a).Q,(b).Q,(frac)); \ |
Jeej | 31:517fc900afba | 131 | (out).Q = KAL_MLF((a).I,(b).Q,(frac)) + KAL_MLF((a).Q,(b).I,(frac)); } |
Jeej | 31:517fc900afba | 132 | |
Jeej | 31:517fc900afba | 133 | #define KAL_COMPLEX_NORM(a) (KAL_MUL((a).I,(a).I) + KAL_MUL((a).Q,(a).Q)) |
Jeej | 31:517fc900afba | 134 | |
Jeej | 31:517fc900afba | 135 | #endif // __KAL_MATH_H__ |