sipxmedialib  Version 3.3
MpDspUtilsSum.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 _MpDspUtilsSum_h_
11 #define _MpDspUtilsSum_h_
12 
20 /* ============================ INLINE METHODS ============================ */
21 
22 int16_t MpDspUtils::add(int16_t a, int16_t b)
23 {
24  return MPF_EXTRACRT16(MPF_SATURATE16(a+b));
25 }
26 
27 void MpDspUtils::add_I(int16_t &a, int16_t b)
28 {
29  a = add(a, b);
30 }
31 
32 void MpDspUtils::addMul_I(int32_t &a, int16_t b, int16_t c)
33 {
34  a = add(a, b*c);
35 }
36 
37 int32_t MpDspUtils::add(int32_t a, int32_t b)
38 {
39 #ifdef ARMv5E_ASM
40  int32_t tmp;
41  __asm__ __volatile__ ("qadd %0,%1,%2;\n"
42  : "=&r"(tmp)
43  : "r"(a),"r"(b));
44  return tmp;
45 #else
46  int32_t c;
47  c = a + b;
48 
49  if (((a ^ b) & MPF_SIGN_BIT32) == 0)
50  {
51  // a and b have same sign
52  if ((c ^ a) & MPF_SIGN_BIT32)
53  {
54  // c have different signs then a and b, that is addition wrapped.
55  c = (a < 0) ? (INT32_MIN+1) : INT32_MAX;
56  }
57  }
58  if (c == INT32_MIN)
59  {
60  // Saturate to (INT32_MIN+1). We do not work with INT32_MIN,
61  // because -INT32_MIN=INT32_MIN and thus break negation.
62  c = INT32_MIN+1;
63  }
64  return c;
65 #endif
66 }
67 
68 void MpDspUtils::add_I(int32_t &a, int32_t b)
69 {
70  a = add(a, b);
71 }
72 
73 #ifndef MP_FIXED_POINT // [
74 
75 float MpDspUtils::maximum(float a, float b)
76 {
77  return (a > b) ? a : b;
78 }
79 
80 float MpDspUtils::add(float a, float b)
81 {
82  return a+b;
83 }
84 
85 void MpDspUtils::add_I(float &a, float b)
86 {
87  a = add(a, b);
88 }
89 
90 void MpDspUtils::addMul_I(float &a, float b, float c)
91 {
92  a = add(a, b*c);
93 }
94 
95 #endif // MP_FIXED_POINT ]
96 
97 int16_t MpDspUtils::abs(int16_t a)
98 {
99  return (a < 0) ?
100  ((a == INT16_MIN) ? INT16_MAX : -a) : a;
101 }
102 
103 int16_t MpDspUtils::minimum(int16_t a, int16_t b)
104 {
105  return (a < b) ? a : b;
106 }
107 
108 int16_t MpDspUtils::maximum(int16_t a, int16_t b)
109 {
110  return (a > b) ? a : b;
111 }
112 
113 int32_t MpDspUtils::abs(int32_t a)
114 {
115  return (a < 0) ?
116  ((a == INT32_MIN) ? INT32_MAX : -a) : a;
117 }
118 
119 int32_t MpDspUtils::minimum(int32_t a, int32_t b)
120 {
121  return (a < b) ? a : b;
122 }
123 
124 int32_t MpDspUtils::maximum(int32_t a, int32_t b)
125 {
126  return (a > b) ? a : b;
127 }
128 
129 #endif // _MpDspUtilsSum_h_
#define MPF_SIGN_BIT32
Definition: MpDspUtils.h:36
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 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 int16_t maximum(int16_t a, int16_t b)
Perform maximum value calculation.
Definition: MpDspUtilsSum.h:108
#define MPF_SATURATE16(val)
Saturate val to fit 16-bit signed integer.
Definition: MpDspUtils.h:60
static int16_t add(int16_t a, int16_t b)
Return (a+b) saturated (16-bit).
Definition: MpDspUtilsSum.h:22
#define MPF_EXTRACRT16(val)
Return 16 bit integer.
Definition: MpDspUtils.h:71