sipxmedialib  Version 3.3
MpDspUtils.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007-2017 SIPez LLC. All rights reserved.
3 //
4 //
5 // $$
7 
8 // Author: Alexander Chemeris <Alexander DOT Chemeris AT SIPez DOT com>
9 
10 #ifndef _MpDspUtils_h_
11 #define _MpDspUtils_h_
12 
14 #define MP_FIXED_POINT
15 //#undef MP_FIXED_POINT
16 
18 #define MP_DSP_INLINE_VECTOR_FUNCTIONS
19 //#undef MP_DSP_INLINE_VECTOR_FUNCTIONS
20 #ifdef MP_DSP_INLINE_VECTOR_FUNCTIONS // [
21 # define MP_DSP_VECTOR_API inline
22 #else // MP_DSP_INLINE_VECTOR_FUNCTIONS ][
23 # define MP_DSP_VECTOR_API
24 #endif // MP_DSP_INLINE_VECTOR_FUNCTIONS ]
25 
26 // SYSTEM INCLUDES
27 // APPLICATION INCLUDES
28 #include <os/OsStatus.h>
29 #include <os/OsIntTypes.h>
30 #ifdef MP_FIXED_POINT // [
31 # include <math.h>
32 #endif // MP_FIXED_POINT ]
33 
34 // DEFINES
35 #define MPF_SIGN_BIT16 (1<<15)
36 #define MPF_SIGN_BIT32 (1<<31)
37 
38 // MACROS
39 #ifdef MP_FIXED_POINT // [
40 # define MPF_FLOAT(num, intLength, fracLength) (floor((num)*(float)(1<<(fracLength))))
41 #else // MP_FIXED_POINT ][
42 # define MPF_FLOAT(num, intLength, fracLength) (num)
43 #endif // MP_FIXED_POINT ]
44 
45 #define MPF_STEP(intLength, fracLength) MPF_FLOAT(1.0/(1<<(fracLength)), (intLength), (fracLength))
46 #define MPF_MAX(intLength, fracLength) MPF_FLOAT((1<<(intLength))-1.0/(1<<(fracLength)), (intLength), (fracLength))
47 
49 
55 #define MPF_SATURATE(val, maxval) ((val)>=(maxval) ? (maxval) : (val)<-(maxval) ? -(maxval) : (val))
56 
60 #define MPF_SATURATE16(val) MPF_SATURATE(val, INT16_MAX)
61 
65 #define MPF_SATURATE32(val) MPF_SATURATE(val, INT32_MAX)
66 
68 
71 #define MPF_EXTRACRT16(val) ((int16_t)(val))
72 
73 // EXTERNAL FUNCTIONS
74 // EXTERNAL VARIABLES
75 // CONSTANTS
76 // STRUCTS
77 // TYPEDEFS
78 // FORWARD DECLARATIONS
79 
121 {
122 /* //////////////////////////////// PUBLIC //////////////////////////////// */
123 public:
124 
125 /* ========================= Arithmetic Functions ========================= */
127 
128 
130  static inline
131  int16_t add(int16_t a, int16_t b);
132 
134  static inline
135  void add_I(int16_t &a, int16_t b);
136 
138  static inline
139  int32_t add(int32_t a, int32_t b);
140 
142  static inline
143  void add_I(int32_t &a, int32_t b);
144 
146  static inline
147  void addMul_I(int32_t &a, int16_t b, int16_t c);
148 
149 #ifndef MP_FIXED_POINT // [
150 
152  static inline
153  float maximum(float a, float b);
154 
156  static inline
157  float add(float a, float b);
158 
160  static inline
161  void add_I(float &a, float b);
162 
164  static inline
165  void addMul_I(float &a, float b, float c);
166 
167 #endif // !MP_FIXED_POINT ]
168 
170  static inline
171  int16_t abs(int16_t a);
179  static inline
181  int16_t minimum(int16_t a, int16_t b);
182 
184  static inline
185  int16_t maximum(int16_t a, int16_t b);
186 
188  static inline
189  int32_t abs(int32_t a);
197  static inline
199  int32_t minimum(int32_t a, int32_t b);
200 
202  static inline
203  int32_t maximum(int32_t a, int32_t b);
204 
206  static inline
207  uint32_t sqrt(uint32_t val);
208 
210 
211 /* ======================= Serial Number Arithmetic ======================= */
213 
214 
216  static inline
217  int compareSerials(uint32_t val1, uint32_t val2);
231  static inline
233  int compareSerials(uint16_t val1, uint16_t val2);
234 
236 
237 /* ================== Vector Logical and Shift Functions ================== */
239 
240 
241  static inline
242  void shl16_I(int32_t &a, unsigned scale);
247  static inline
248  int32_t shl16(int32_t a, unsigned scale);
253  static inline
254  void shl32_I(int32_t &a, unsigned scale);
259  static inline
260  int32_t shl32(int32_t a, unsigned scale);
266 
267 /* ====================== Vector Arithmetic Functions ===================== */
269 
270 
271 #ifdef MP_FIXED_POINT // [
272 
274  static MP_DSP_VECTOR_API
275  OsStatus add_I(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength);
276 
278  static MP_DSP_VECTOR_API
279  OsStatus add_IGain(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor);
280 
282  static MP_DSP_VECTOR_API
283  OsStatus add_IAtt(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor);
284 
286  static MP_DSP_VECTOR_API
287  OsStatus add(const int32_t *pSrc1, const int32_t *pSrc2, int32_t *pDst, int dataLength);
292  static MP_DSP_VECTOR_API
294  OsStatus addMul_I(const int16_t *pSrc1, int16_t val, int32_t *pSrc2Dst, int dataLength);
295 
298  static MP_DSP_VECTOR_API
299  OsStatus addMulLinear_I(const int16_t *pSrc1, int16_t valStart, int16_t valEnd,
300  int32_t *pSrc2Dst, int dataLength);
308  static MP_DSP_VECTOR_API
310  OsStatus mul(const int16_t *pSrc, const int16_t val, int32_t *pDst, int dataLength);
315  static MP_DSP_VECTOR_API
317  OsStatus mul_I(int16_t *pSrcDst, const int16_t val, int dataLength);
322  static MP_DSP_VECTOR_API
325  OsStatus mulLinear(const int16_t *pSrc, int16_t valStart, int16_t valEnd,
326  int32_t *pDst, int dataLength);
334 #else // MP_FIXED_POINT ][
335 
337  static MP_DSP_VECTOR_API
338  OsStatus add_I(const int16_t *pSrc1, float *pSrc2Dst, int dataLength);
339 
341  static inline
342  OsStatus add_IGain(const int16_t *pSrc1, float *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
343  {
344  return add_I(pSrc1, pSrc2Dst, dataLength);
345  }
346 
348  static inline
349  OsStatus add_IAtt(const int16_t *pSrc1, float *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
350  {
351  return add_I(pSrc1, pSrc2Dst, dataLength);
352  }
353 
355  static MP_DSP_VECTOR_API
356  OsStatus add(const float *pSrc1, const float *pSrc2, float *pDst, int dataLength);
361  static MP_DSP_VECTOR_API
363  OsStatus addMul_I(const int16_t *pSrc1, float val, float *pSrc2Dst, int dataLength);
364 
367  static MP_DSP_VECTOR_API
368  OsStatus addMulLinear_I(const int16_t *pSrc1, float valStart, float valEnd,
369  float *pSrc2Dst, int dataLength);
374  static MP_DSP_VECTOR_API
376  OsStatus mul(const int16_t *pSrc, const float val, float *pDst, int dataLength);
381  static MP_DSP_VECTOR_API
384  OsStatus mulLinear(const int16_t *pSrc, float valStart, float valEnd,
385  float *pDst, int dataLength);
393 #endif // MP_FIXED_POINT ]
394 
396  static inline
397  int maxAbs(const int16_t *pSrc, int dataLength);
402  static inline
404  int16_t maximum(const int16_t *pSrc, int dataLength);
405 
407  static inline
408  int32_t maximum(const int32_t *pSrc, int dataLength);
409 
411  static inline
412  int16_t minimum(const int16_t *pSrc, int dataLength);
413 
415  static inline
416  int32_t minimum(const int32_t *pSrc, int dataLength);
417 
419  static inline
420  int32_t countClippedValues(const int16_t *pSrc, int dataLength);
421 
423 
424 /* ====================== Vector Conversion Functions ===================== */
426 
427 
428 #ifdef MP_FIXED_POINT // [
429 
431  static MP_DSP_VECTOR_API
432  OsStatus convert(const int32_t *pSrc, int16_t *pDst, int dataLength);
433 
435  static MP_DSP_VECTOR_API
436  OsStatus convert_Gain(const int32_t *pSrc, int16_t *pDst, int dataLength, unsigned srcScaleFactor);
437 
439  static MP_DSP_VECTOR_API
440  OsStatus convert_Att(const int32_t *pSrc, int16_t *pDst, int dataLength, unsigned srcScaleFactor);
441 
443  static MP_DSP_VECTOR_API
444  OsStatus convert(const int16_t *pSrc, int32_t *pDst, int dataLength);
449  static MP_DSP_VECTOR_API
451  OsStatus convert_Gain(const int16_t *pSrc, int32_t *pDst, int dataLength, unsigned srcScaleFactor);
456  static MP_DSP_VECTOR_API
458  OsStatus convert_Att(const int16_t *pSrc, int32_t *pDst, int dataLength, unsigned srcScaleFactor);
463 #else // MP_FIXED_POINT ][
464 
466  static MP_DSP_VECTOR_API
467  OsStatus convert(const float *pSrc, int16_t *pDst, int dataLength);
468 
470  static inline
471  OsStatus convert_Gain(const float *pSrc, int16_t *pDst, int dataLength, unsigned srcScaleFactor)
472  {
473  return convert(pSrc, pDst, dataLength);
474  }
475 
477  static inline
478  OsStatus convert_Att(const float *pSrc, int16_t *pDst, int dataLength, unsigned srcScaleFactor)
479  {
480  return convert(pSrc, pDst, dataLength);
481  }
482 
484  static inline
485  OsStatus convert(const int16_t *pSrc, float *pDst, int dataLength)
489  {
490  const int16_t* srcIntPtr = pSrc;
491  float* dstFloatPtr = pDst;
492  for(int dataIndex = 0; dataIndex < dataLength; dataIndex++)
493  {
494  *dstFloatPtr = float(*srcIntPtr);
495  dstFloatPtr++;
496  srcIntPtr++;
497  }
498  return(OS_SUCCESS);
499  }
500 
502  static inline
503  OsStatus convert_Gain(const int16_t *pSrc, float *pDst, int dataLength, unsigned srcScaleFactor)
504  {
505  return convert(pSrc, pDst, dataLength);
506  }
507 
509  static inline
510  OsStatus convert_Att(const int16_t *pSrc, float *pDst, int dataLength, unsigned srcScaleFactor)
511  {
512  return convert(pSrc, pDst, dataLength);
513  }
514 
515 #endif // MP_FIXED_POINT ]
516 
518 
519 };
520 
521 /* ============================ INLINE METHODS ============================ */
522 
524 #include <mp/MpDspUtilsIntSqrt.h>
525 #include <mp/MpDspUtilsSum.h>
526 #include <mp/MpDspUtilsSumVect.h>
527 #include <mp/MpDspUtilsShift.h>
528 #include <mp/MpDspUtilsSerials.h>
529 
530 #endif // _MpDspUtils_h_
static void shl16_I(int32_t &a, unsigned scale)
Definition: MpDspUtilsShift.h:25
static uint32_t sqrt(uint32_t val)
Integer square root.
Definition: MpDspUtilsIntSqrt.h:33
static int maxAbs(const int16_t *pSrc, int dataLength)
Calculate absolute maximum value of array.
Definition: MpDspUtilsSumVect.h:217
static MP_DSP_VECTOR_API OsStatus mul_I(int16_t *pSrcDst, const int16_t val, int dataLength)
Multiply vector by constant with saturation.
static MP_DSP_VECTOR_API OsStatus convert_Gain(const int32_t *pSrc, int16_t *pDst, int dataLength, unsigned srcScaleFactor)
Gain vector of 32-bit integers and convert them to 16-bit integers.
static MP_DSP_VECTOR_API OsStatus add_IAtt(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
Attenuate source vector by 2^src1ScaleFactor factor and add it to accumulator.
#define MP_DSP_VECTOR_API
Definition: MpDspUtils.h:21
static int32_t countClippedValues(const int16_t *pSrc, int dataLength)
Find the number of values in vector that are at maximum or minimum 16 bit value.
Definition: MpDspUtilsSumVect.h:271
static MP_DSP_VECTOR_API OsStatus convert(const int32_t *pSrc, int16_t *pDst, int dataLength)
Convert vector of 32-bit integers to 16-bit integers.
static int compareSerials(uint32_t val1, uint32_t val2)
Is val1 bigger, equal or lesser then val2.
Definition: MpDspUtilsSerials.h:35
static void addMul_I(int32_t &a, int16_t b, int16_t c)
Perform (a+=b*c) saturated (32-bit accumulator, 16-bit operands).
Definition: MpDspUtilsSum.h:32
static int32_t shl16(int32_t a, unsigned scale)
Definition: MpDspUtilsShift.h:30
static void add_I(int16_t &a, int16_t b)
Perform (a+=b) saturated (16-bit).
Definition: MpDspUtilsSum.h:27
static int16_t minimum(int16_t a, int16_t b)
Perform minimum value calculation.
Definition: MpDspUtilsSum.h:103
static int16_t abs(int16_t a)
Perform absolute value calculation with saturation.
Definition: MpDspUtilsSum.h:97
static MP_DSP_VECTOR_API OsStatus addMulLinear_I(const int16_t *pSrc1, int16_t valStart, int16_t valEnd, int32_t *pSrc2Dst, int dataLength)
Multiply source vector by values linearly changing from valStart to valEnd and add it to accumulator ...
Definition: MpDspUtilsSumVect.h:174
static int16_t maximum(int16_t a, int16_t b)
Perform maximum value calculation.
Definition: MpDspUtilsSum.h:108
static MP_DSP_VECTOR_API OsStatus mulLinear(const int16_t *pSrc, int16_t valStart, int16_t valEnd, int32_t *pDst, int dataLength)
Multiply source vector by values linearly changing from valStart to valEnd and add it to accumulator ...
Definition: MpDspUtilsSumVect.h:197
Class for generic DSP functions.
Definition: MpDspUtils.h:120
static void shl32_I(int32_t &a, unsigned scale)
Definition: MpDspUtilsShift.h:38
static int16_t add(int16_t a, int16_t b)
Return (a+b) saturated (16-bit).
Definition: MpDspUtilsSum.h:22
static int32_t shl32(int32_t a, unsigned scale)
Definition: MpDspUtilsShift.h:43
static MP_DSP_VECTOR_API OsStatus convert_Att(const int32_t *pSrc, int16_t *pDst, int dataLength, unsigned srcScaleFactor)
Attenuate vector of 32-bit integers and convert them to 16-bit integers.
static MP_DSP_VECTOR_API OsStatus add_IGain(const int16_t *pSrc1, int32_t *pSrc2Dst, int dataLength, unsigned src1ScaleFactor)
Gain source vector by 2^src1ScaleFactor factor and add it to accumulator.
static MP_DSP_VECTOR_API OsStatus mul(const int16_t *pSrc, const int16_t val, int32_t *pDst, int dataLength)
Multiply vector by constant.
Definition: MpDspUtilsSumVect.h:187