MMALIB User Guide
MMALIB_utility.h
Go to the documentation of this file.
1 #ifndef COMMON_MMALIB_UTILITY_H_
2 #define COMMON_MMALIB_UTILITY_H_ 1
3 
4 /******************************************************************************/
8 /* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the
20  * distribution.
21  *
22  * Neither the name of Texas Instruments Incorporated nor the names of
23  * its contributors may be used to endorse or promote products derived
24  * from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  *
38  ******************************************************************************/
39 
40 
41 /*******************************************************************************
42  *
43  * INCLUDES
44  *
45  ******************************************************************************/
46 
47 #include <float.h> // for max float, double values
48 #include <limits.h> // for min, max integer values
49 #include <math.h>
50 
51 #include "MMALIB_bufParams.h"
52 #include "MMALIB_types.h"
53 
54 #if defined (__C7100__) || defined (__device__) || defined (__RESERVED__) || defined (__RESERVED__) || defined (__RESERVED__)
55 #include "c71/MMALIB_utility.h"
56 #if MMALIB_DEBUGPRINT >= 1
57 #include "c71/MMALIB_debug.h"
58 #endif
59 #else
60 #error invalid target
61 #endif
62 
63 #if defined (__C7100__)
64 #include "c7100/MMALIB_utility.h"
65 #elif defined (__device__)
66 #include "c7120/MMALIB_utility.h"
67 #elif defined (__RESERVED__)
68 #include "RESERVED/MMALIB_utility.h"
69 #elif defined (__RESERVED__)
70 #include "RESERVED/MMALIB_utility.h"
71 #elif defined (__RESERVED__)
72 #include "RESERVED/MMALIB_utility.h"
73 #else
74 #error invalid target
75 #endif
76 
77 /*******************************************************************************
78  *
79  * EXTERNAL VARIABLES
80  *
81  ******************************************************************************/
82 #ifdef __cplusplus
83 extern "C" {
84 #endif /* __cplusplus */
85 extern uint64_t beg_count; /* Begin cycle count for profiling */
86 extern uint64_t end_count; /* End cycle count for profiling */
87 extern uint64_t overhead; /* Cycle profiling overhead */
88 #ifdef __cplusplus
89 }
90 #endif /* __cplusplus */
91 
92 /*******************************************************************************
93  *
94  * Inline functions
95  *
96  ******************************************************************************/
97 
98 /*******************************************************************************
99  *
100  * Arithmetic with 128-bit signed integers
101  *
102  ******************************************************************************/
103 static inline void MMALIB_UTIL_mult(int64_t *ph, int64_t *pl, int64_t a, int64_t b);
104 
105 static inline void MMALIB_UTIL_mult(int64_t *ph , // result
106  int64_t *pl ,
107  int64_t a , // left operand
108  int64_t b // right operand
109  ){
110  //sum += A[k + m*K] * B[n + k*N];
111  *pl = a * b ;
112 
113  // sign extend the product
114  *ph = ((((uint64_t)*pl) & 0x8000000000000000ULL) != 0ULL)?(int64_t)0xffffffffffffffffULL:(int64_t)0ULL ;
115 }
116 
117 
118 #ifdef __cplusplus
119 
120 /*******************************************************************************
121 *
122 * Definition and arithmetic for MMALIB_int128_t class
123 *
124 ******************************************************************************/
125 
126 // Define a 128-bit integer class to allow natural-c implementations of MMALIB
127 // 32-bit input/output functions to be templated. The class is implemented in
128 // a header file for easy sharing. All member functions, including constructors
129 // are declared inline for two reasons: (1) performance and (2) necessary for
130 // implementing the class in a multiple-inclusion header file.
131 
132 class MMALIB_int128_t
133 {
134 public:
135  int64_t hi;
136  int64_t lo;
137  MMALIB_int128_t(int64_t h, int64_t l); // constructor for both high and low specified
138  MMALIB_int128_t(int64_t l); // constructor for just low specified (sign extends to high)
139  MMALIB_int128_t(); // constructor for neither field specified
140  MMALIB_int128_t operator+ (const MMALIB_int128_t&) const; // operator +
141  MMALIB_int128_t operator>> (const int8_t&) const; // operator >>
142 };
143 
144 // define constructor
145 inline MMALIB_int128_t::MMALIB_int128_t(int64_t h, int64_t l)
146 {
147  hi = h;
148  lo = l;
149 }
150 
151 // define constructor
152 inline MMALIB_int128_t::MMALIB_int128_t(int64_t l)
153 {
154  // sign extend l
155  hi = (((uint64_t)l & 0x8000000000000000ULL) != 0LL)?(int64_t)0xffffffffffffffffULL:(int64_t)0x0000000000000000ULL;
156  lo = l;
157 }
158 
159 // define constructor
160 inline MMALIB_int128_t::MMALIB_int128_t()
161 {
162  hi = 0x0000000000000000LL;
163  lo = 0x0000000000000000LL;
164 }
165 
166 static inline void MMALIB_UTIL_shiftRight128(
167  uint64_t *rh , // result
168  uint64_t *rl ,
169  uint64_t ah , // operand
170  uint64_t al ,
171  int32_t sh , // shift amount
172  int32_t s ) // signed
173 {
174  uint64_t h ;
175  uint64_t l ;
176  int32_t i;
177 
178  h = ah ;
179  l = al ;
180  for(i = 0 ; i < sh ; i++ ) {
181  l = __shift_right(l, (uint32_t)1) | __shift_left(h, (uint32_t)63) ;
182  h = __shift_right(h, (uint32_t)1) | ((s!=0)?(h&0x8000000000000000ULL):0ULL) ;
183  }
184 
185  *rh = h ;
186  *rl = l ;
187 }
188 
189 static inline void MMALIB_UTIL_Add128(
190  uint64_t *rh , // result
191  uint64_t *rl ,
192  uint64_t ah , // left operand
193  uint64_t al ,
194  uint64_t bh , // right operand
195  uint64_t bl )
196 {
197  // break up the operands into 4 32b chunks packed into 64b uints
198  uint64_t all ;
199  uint64_t alh ;
200  uint64_t ahl ;
201  uint64_t ahh ;
202  uint64_t bll ;
203  uint64_t blh ;
204  uint64_t bhl ;
205  uint64_t bhh ;
206  uint64_t s0 ;
207  uint64_t s1 ;
208  uint64_t s2 ;
209  uint64_t s3 ;
210  uint64_t sh ;
211  uint64_t sl ;
212 
213  all = __shift_right(al, (uint32_t) 0) & 0x0ffffffffULL ;
214  alh = __shift_right(al, (uint32_t)32) & 0x0ffffffffULL ;
215  ahl = __shift_right(ah, (uint32_t) 0) & 0x0ffffffffULL ;
216  ahh = __shift_right(ah, (uint32_t)32) & 0x0ffffffffULL ;
217 
218  bll = __shift_right(bl, (uint32_t) 0) & 0x0ffffffffULL ;
219  blh = __shift_right(bl, (uint32_t)32) & 0x0ffffffffULL ;
220  bhl = __shift_right(bh, (uint32_t) 0) & 0x0ffffffffULL ;
221  bhh = __shift_right(bh, (uint32_t)32) & 0x0ffffffffULL ;
222 
223  // the adds
224  s0 = all + bll ;
225  s1 = alh + blh + __shift_right(s0, (uint32_t)32) ;
226  s2 = ahl + bhl + __shift_right(s1, (uint32_t)32) ;
227  s3 = ahh + bhh + __shift_right(s2, (uint32_t)32) ;
228 
229  // pack the results
230  sl = __shift_left(s1, (uint32_t)32) | (s0 & 0x0ffffffffULL) ;
231  sh = __shift_left(s3, (uint32_t)32) | (s2 & 0x0ffffffffULL) ;
232 
233  *rl = sl ;
234  *rh = sh ;
235 }
236 
237 // define overloaded + (plus) operator
238 inline MMALIB_int128_t MMALIB_int128_t::operator+ (const MMALIB_int128_t& b) const
239 {
240  MMALIB_int128_t result;
241 
242  MMALIB_UTIL_Add128((uint64_t *)&(result.hi), (uint64_t *)&(result.lo), this->hi, this->lo, b.hi, b.lo);
243 
244  return result;
245 }
246 
247 // define overloaded >> (bit shift right) operator
248 inline MMALIB_int128_t MMALIB_int128_t::operator>> (const int8_t& shift) const
249 {
250  MMALIB_int128_t result;
251 
252  MMALIB_UTIL_shiftRight128((uint64_t *)&result.hi, (uint64_t *)&result.lo, this->hi, this->lo, (int32_t)shift, 1);
253  return result;
254 }
255 
256 /*******************************************************************************
257  *
258  * We need special utility to do negation because range of values is from
259  * -2^(bit-width-1) to 2^(bit-width-1)-1. For example, with int16_t, the
260  * range is from -32768 to 32767 - that is, -0x8000 to 0x7FFF. Now, if we want
261  * to evaluate negation of -32768 and we try simply -(-32768) and store the
262  * result in int16_t, we would get -32768 itself. Instead, we want to get 32767.
263  *
264  ******************************************************************************/
265 
266 static inline int16_t MMALIB_UTIL_negate(int16_t a)
267 {
268  int16_t result;
269 
270  result = (a == -32768) ? 32767 : -a;
271  return result;
272 }
273 
274 static inline int32_t MMALIB_UTIL_negate(int32_t a)
275 {
276  int32_t result;
277 
278  result = (a == -2147483648) ? 2147483647 : -a;
279  return result;
280 }
281 
282 /*******************************************************************************
283  *
284  * Inline multiply with higher bit-width output type
285  *
286  ******************************************************************************/
287 
288 static inline int32_t MMALIB_UTIL_mult(int8_t a, int8_t b)
289 {
290  return (int16_t)a * (int16_t)b;
291 }
292 
293 static inline int32_t MMALIB_UTIL_mult(uint8_t a, int8_t b)
294 {
295  return (int16_t)a * (int16_t)b;
296 }
297 
298 
299 static inline int64_t MMALIB_UTIL_mult(int16_t a, int16_t b)
300 {
301  return (int32_t)a * (int32_t)b;
302 }
303 
304 static inline int64_t MMALIB_UTIL_mult(uint16_t a, int16_t b)
305 {
306  return (int32_t)a * (int32_t)b;
307 }
308 
309 static inline MMALIB_int128_t MMALIB_UTIL_mult(int32_t a, int32_t b)
310 {
311  MMALIB_int128_t result(0,0);
312 
313  result.lo = (int64_t)a * (int64_t)b ;
314  // sign extend the product
315  result.hi = (((uint64_t)result.lo & 0x8000000000000000ULL) != 0LL)?(int64_t)0xffffffffffffffffULL:(int64_t)0ULL ;
316 
317  return result;
318 }
319 
320 /*******************************************************************************
321  *
322  * Inline saturate with ReLU operation
323  *
324  ******************************************************************************/
325 
326 static inline void MMALIB_UTIL_saturate_relu(int32_t x, int8_t *y)
327 {
328  if (x > 0x7F) {
329  *y = 0x7F;
330  } else if (x < 0) {
331  *y = 0;
332  } else {
333  *y = (int8_t)x;
334  }
335 
336  return;
337 }
338 
339 static inline void MMALIB_UTIL_saturate_relu(int32_t x, uint8_t *y)
340 {
341  if (x > 0xFF) {
342  *y = 0xFF;
343  } else if (x < 0) {
344  *y = 0;
345  } else {
346  *y = (uint8_t)x;
347  }
348 
349  return;
350 }
351 
352 static inline void MMALIB_UTIL_saturate_relu(uint32_t x, uint8_t *y)
353 {
354  if (x > 0xFF) {
355  *y = 0xFF;
356  } else {
357  *y = (uint8_t)x;
358  }
359 
360  return;
361 }
362 
363 static inline void MMALIB_UTIL_saturate_relu(int64_t x, int16_t *y)
364 {
365  if (x > 0x7FFF) {
366  *y = 0x7FFF;
367  } else if (x < 0x0000) {
368  *y = 0x0000;
369  } else {
370  *y = (int16_t)x;
371  }
372 
373  return;
374 }
375 
376 static inline void MMALIB_UTIL_saturate_relu(int64_t x, uint16_t *y)
377 {
378  if (x > 0xFFFF) {
379  *y = 0xFFFF;
380  } else if (x < 0x0000) {
381  *y = 0x0000;
382  } else {
383  *y = (uint16_t)x;
384  }
385 
386  return;
387 }
388 
389 static inline void MMALIB_UTIL_saturate_relu(uint64_t x, uint16_t *y)
390 {
391  if (x > 0xFFFF) {
392  *y = 0xFFFF;
393  } else {
394  *y = (uint16_t)x;
395  }
396 
397  return;
398 }
399 
400 /*******************************************************************************
401  *
402  * Inline shift, round and ReLU operation
403  *
404  ******************************************************************************/
405 
406 template <typename dataType, typename returnType>
407 static inline returnType MMALIB_UTIL_shiftRoundAndReLU(dataType inVal, uint8_t shift){
408  returnType result;
409 
410  if(shift == 0){
411  // remove the rounding, which doesn't make sense with no shift but causes C code problems
412  MMALIB_UTIL_saturate_relu(inVal, &result);
413  } else {
414  // round and shift
415  // Method requires right shift of signed integers be an arithmetic shift, but right
416  // shift >> on signed integer types is implementation dependent on whether the shift is
417  // arithmetic or logical. There's no simple way in C to specify the shift type as arithmetic.
418  // Instead, we use the __shift_right intrinsic, which is defined to be arithmetic shift.
419  dataType temp;
420  temp = __shift_right(inVal, (shift - 1)) + 1;
421  temp = __shift_right(temp, 1);
422  MMALIB_UTIL_saturate_relu(temp, &result);
423  }
424 
425  return result;
426 }
427 
428 template int8_t MMALIB_UTIL_shiftRoundAndReLU<int32_t, int8_t> (int32_t inVal, uint8_t shift);
429 template int16_t MMALIB_UTIL_shiftRoundAndReLU<int64_t, int16_t> (int64_t inVal, uint8_t shift);
430 // added for unsigned C matrix values inside MMA
431 //template uint8_t MMALIB_UTIL_shiftRoundAndReLU<uint32_t, uint8_t> (uint32_t inVal, uint8_t shift);
432 //template uint16_t MMALIB_UTIL_shiftRoundAndReLU<uint64_t, uint16_t> (uint64_t inVal, uint8_t shift);
433 
434 template <>
435 inline uint8_t MMALIB_UTIL_shiftRoundAndReLU<int32_t, uint8_t>(int32_t inVal, uint8_t shift){
436  uint8_t result;
437 
438  if(shift == 0){
439  // remove the rounding, which doesn't make sense with no shift but causes C code problems
440  MMALIB_UTIL_saturate_relu(inVal, &result);
441  } else {
442  // round and shift
443  // Method requires right shift of signed integers be an arithmetic shift, but right
444  // shift >> on signed integer types is implementation dependent on whether the shift is
445  // arithmetic or logical. There's no simple way in C to specify the shift type as arithmetic.
446  // Instead, we use the __shift_right intrinsic, which is defined to be arithmetic shift.
447  int32_t temp;
448  temp = __shift_right( inVal, (shift - 1) ) + 1;
449  temp = __shift_right(temp, 1);
450  MMALIB_UTIL_saturate_relu(temp, &result);
451 
452  }
453 
454  return result;
455 }
456 
457 template <>
458 inline uint8_t MMALIB_UTIL_shiftRoundAndReLU<uint32_t, uint8_t>(uint32_t inVal, uint8_t shift){
459  uint8_t result;
460 
461  if(shift == 0){
462  // remove the rounding, which doesn't make sense with no shift but causes C code problems
463  MMALIB_UTIL_saturate_relu(inVal, &result);
464  } else {
465  uint32_t temp;
466  //Subtracting two unsigned values of the same size will result in an unsigned value.
467  //If the first operand is less than the second the result will be arithmetically in correct.
468  //But if the size of the unsigned types is less than that of an unsigned int, C/C++ will promote the types to
469  //signed int before subtracting resulting in an correct result. In either case,
470  //there is no indication of an error.
471  uint32_t shift32_t = (uint32_t) shift;
472  temp = (inVal >> (shift32_t - (uint32_t)1) ) + 1;
473  temp = temp >> 1;
474  MMALIB_UTIL_saturate_relu(temp, &result);
475  }
476 
477  return result;
478 }
479 
480 template <>
481 inline uint16_t MMALIB_UTIL_shiftRoundAndReLU<int64_t, uint16_t>(int64_t inVal, uint8_t shift){
482  uint16_t result;
483 
484  if(shift == 0){
485  // remove the rounding, which doesn't make sense with no shift but causes C code problems
486  MMALIB_UTIL_saturate_relu(inVal, &result);
487  } else {
488  // round and shift
489  // Method requires right shift of signed integers be an arithmetic shift, but right
490  // shift >> on signed integer types is implementation dependent on whether the shift is
491  // arithmetic or logical. There's no simple way in C to specify the shift type as arithmetic.
492  // Instead, we use the __shift_right intrinsic, which is defined to be arithmetic shift.
493  int64_t temp;
494  temp = __shift_right( inVal, (shift - 1) ) + 1;
495  temp = __shift_right(temp, 1);
496  MMALIB_UTIL_saturate_relu(temp, &result);
497  }
498 
499  return result;
500 }
501 
502 template <>
503 inline uint16_t MMALIB_UTIL_shiftRoundAndReLU<uint64_t, uint16_t>(uint64_t inVal, uint8_t shift){
504  uint16_t result;
505 
506  if(shift == 0){
507  // remove the rounding, which doesn't make sense with no shift but causes C code problems
508  MMALIB_UTIL_saturate_relu(inVal, &result);
509  } else {
510  uint64_t temp;
511  uint32_t shift32_t = (uint32_t) shift;
512  temp = (inVal >> (shift32_t - (uint32_t)1) ) + 1;
513  temp = (temp >> 1);
514  MMALIB_UTIL_saturate_relu(temp, &result);
515  }
516 
517  return result;
518 }
519 
520 
521 /*******************************************************************************
522  *
523  * Inline saturate operation
524  *
525  ******************************************************************************/
526 
527 static inline int8_t MMALIB_UTIL_saturate(int32_t x)
528 {
529  int8_t retVal;
530  if (x > 0x7F) {
531  retVal = 0x7F;
532  } else if (x < -0x80) {
533  retVal = -0x80;
534  } else {
535  retVal = (int8_t)x;
536  }
537  return retVal;
538 }
539 
540 static inline uint8_t MMALIB_UTIL_saturate(uint32_t x)
541 {
542  uint8_t retVal;
543  if (x > 0xFF) {
544  retVal = 0xFF;
545  } else {
546  retVal = (uint8_t)x;
547  }
548  return retVal;
549 }
550 
551 static inline int16_t MMALIB_UTIL_saturate(int64_t x)
552 {
553  int16_t retVal;
554  if (x > 0x7FFF) {
555  retVal = 0x7FFF;
556  } else if (x < -0x8000) {
557  retVal = -0x8000;
558  } else {
559  retVal = (int16_t)x;
560  }
561  return retVal;
562 }
563 
564 static inline uint16_t MMALIB_UTIL_saturate(uint64_t x)
565 {
566  uint16_t retVal;
567  if (x > 0xFFFF) {
568  retVal = 0xFFFF;
569  } else {
570  retVal = (uint16_t)x;
571  }
572  return retVal;
573 }
574 
575 static inline int32_t MMALIB_UTIL_saturate(int64_t xh, int64_t xl)
576 {
577  int32_t retVal;
578  //printf("%s: xh = %" PRIx64 ", xl = %" PRIx64 "\n", __FUNCTION__, xh, xl);
579  // if negative
580  if(((uint64_t)xh & 0x8000000000000000ULL) != 0LL){
581  if( ((~(uint64_t)xh & 0xFFFFFFFFFFFFFFFFULL) != 0LL) || ((~(uint64_t)xl & 0xFFFFFFFF80000000ULL) != 0LL)){
582  retVal = ((int32_t)0x80000000U);
583  } else {
584  retVal = (int32_t)xl;
585  }
586  } else if (((uint64_t)xl & 0xFFFFFFFF80000000ULL) != 0LL){
587  //(xl > 0x000000007FFFFFFFLL){ // positive and saturated
588  retVal = ((int32_t)0x7FFFFFFFU);
589  } else {
590  retVal = (int32_t)xl;
591  }
592  return retVal;
593 }
594 
595 static inline int32_t MMALIB_UTIL_saturate(MMALIB_int128_t x)
596 {
597  return MMALIB_UTIL_saturate(x.hi, x.lo);
598 }
599 
600 /*******************************************************************************
601  *
602  * Inline shift and round operation
603  *
604  ******************************************************************************/
605 
606 template <typename dataType, typename returnType>
607 inline returnType MMALIB_UTIL_shiftAndRound(dataType inVal, uint8_t shift){
608  returnType result;
609 
610  if(shift == 0){
611  // remove the rounding, which doesn't make sense with no shift but causes C code problems
612  result = MMALIB_UTIL_saturate(inVal);
613  } else {
614  // round and shift
615  dataType temp;
616  temp = (__shift_right(inVal, (shift - 1)) + 1);
617  temp = __shift_right(temp , 1);
618  result = MMALIB_UTIL_saturate(temp);
619  }
620 
621  return result;
622 }
623 
624 template int8_t MMALIB_UTIL_shiftAndRound<int32_t, int8_t> (int32_t inVal, uint8_t shift);
625 template int16_t MMALIB_UTIL_shiftAndRound<int64_t, int16_t> (int64_t inVal, uint8_t shift);
626 
627 
628 template <>
629 inline uint8_t MMALIB_UTIL_shiftAndRound(uint32_t inVal, uint8_t shift){
630  uint8_t result;
631 
632  if(shift == 0){
633  // remove the rounding, which doesn't make sense with no shift but causes C code problems
634  result = MMALIB_UTIL_saturate(inVal);
635  } else {
636  // round and shift
637  uint32_t temp;
638  uint32_t shift32_t = (uint32_t) shift;
639  temp = (inVal >> (shift32_t - (uint32_t)1) ) + 1;
640  temp = (temp >> 1);
641  result = MMALIB_UTIL_saturate(temp);
642  }
643 
644  return result;
645 }
646 
647 template <>
648 inline uint16_t MMALIB_UTIL_shiftAndRound(uint64_t inVal, uint8_t shift){
649  uint16_t result;
650 
651  if(shift == 0){
652  // remove the rounding, which doesn't make sense with no shift but causes C code problems
653  result = MMALIB_UTIL_saturate(inVal);
654  } else {
655  // round and shift
656  uint64_t temp;
657  uint32_t shift32_t = (uint32_t) shift;
658  temp = (inVal >> (shift32_t - (uint32_t)1) ) + 1;
659  temp = (temp >> 1);
660  result = MMALIB_UTIL_saturate(temp);
661  }
662 
663  return result;
664 }
665 
666 // MISRA-C prohibits using >> on signed integers because it is implementation dependent on whether
667 // that shift is arithmetic or logical. However, for MMALIB_int128_t, this code implements the shift in software
668 // and ensures that it is arithmetic. To avoid the MISRA-C violation, we use the function version of the shift
669 // rather than the >> operator.
670 template <>
671 inline int32_t MMALIB_UTIL_shiftAndRound<MMALIB_int128_t, int32_t>(MMALIB_int128_t inVal, uint8_t shift){
672  int32_t result;
673 
674  if(shift == 0){
675  // remove the rounding, which doesn't make sense with no shift but causes C code problems
676  result = MMALIB_UTIL_saturate(inVal);
677  } else {
678  // round and shift
679  //result = MMALIB_UTIL_saturate(((inVal >> ((uint8_t)(shift - 1))) + 1) >> 1);
680  MMALIB_int128_t temp;
681  // temp = inVal >> (shift - 1)
682  MMALIB_UTIL_shiftRight128((uint64_t *)&temp.hi, (uint64_t *)&temp.lo, inVal.hi, inVal.lo, (int32_t)(shift - 1), 1);
683  temp = temp + 1;
684  // temp = temp >> 1
685  MMALIB_UTIL_shiftRight128((uint64_t *)&temp.hi, (uint64_t *)&temp.lo, temp.hi, temp.lo, 1, 1);
686  result = MMALIB_UTIL_saturate(temp);
687  }
688 
689  return result;
690 }
691 
692 
693 /*******************************************************************************
694  *
695  * Convert a double-precision floating point number to 16-bit integer
696  *
697  ******************************************************************************/
698 
699 template <typename returnType>
700 static inline returnType MMALIB_UTIL_typeConv_i64f_oxX(MMALIB_D64 x)
701 {
702  int64_t xLocal, maxValue;
703  returnType returnValue;
704 
705  /* Set maxValue to the maximumum possible value for the returnType */
706 
707  // original code
708  // maxValue = (1 << (sizeof(returnType)*8-2)) - 1;
709  // maxValue += (1 << (sizeof(returnType)*8-2));
710  maxValue = ((int64_t)( (uint32_t)1 << ((uint32_t)(sizeof(returnType)*8-2)))) - 1;
711  maxValue += (int64_t)( (uint32_t)1 << ((uint32_t)(sizeof(returnType)*8-2)));
712 
713  xLocal = (int64_t)floor(0.5 + x); /* Explicit rounding to integer */
714  if (xLocal >= maxValue) {
715  returnValue = (returnType)maxValue;
716  } else if (xLocal <= -maxValue-1) {
717  returnValue = (returnType)(-maxValue-1);
718  } else {
719  returnValue = (returnType)xLocal;
720  }
721  return returnValue;
722 }
723 
724 template int16_t MMALIB_UTIL_typeConv_i64f_oxX<int16_t>(MMALIB_D64 x);
725 template int32_t MMALIB_UTIL_typeConv_i64f_oxX<int32_t>(MMALIB_D64 x);
726 
727 /*******************************************************************************
728  *
729  * Evaluate cos function, and apply appropriate scale factor
730  *
731  ******************************************************************************/
732 template <typename returnType>
733 static returnType MMALIB_UTIL_cos_i64f_oxX(MMALIB_D64 x,
734  MMALIB_D64 scaleFactor)
735 {
736  return MMALIB_UTIL_typeConv_i64f_oxX<returnType>(scaleFactor*cos(x));
737 }
738 
739 template int16_t MMALIB_UTIL_cos_i64f_oxX<int16_t>(MMALIB_D64 x, MMALIB_D64 scaleFactor);
740 template int32_t MMALIB_UTIL_cos_i64f_oxX<int32_t>(MMALIB_D64 x, MMALIB_D64 scaleFactor);
741 /*******************************************************************************
742  *
743  * Evaluate sin function, and apply appropriate scale factor
744  *
745  ******************************************************************************/
746 template <typename returnType>
747 static inline returnType MMALIB_UTIL_sin_i64f_oxX(MMALIB_D64 x,
748  MMALIB_D64 scaleFactor)
749 {
750  return MMALIB_UTIL_typeConv_i64f_oxX<returnType>(scaleFactor*sin(x));
751 }
752 
753 template int16_t MMALIB_UTIL_sin_i64f_oxX<int16_t>(MMALIB_D64 x, MMALIB_D64 scaleFactor);
754 template int32_t MMALIB_UTIL_sin_i64f_oxX<int32_t>(MMALIB_D64 x, MMALIB_D64 scaleFactor);
755 
756 #endif
757 
758 #endif
File to hold buffer parameter related info for MMALIB.
File to hold common structure, enums, macros and functions for MMALIB.
double MMALIB_D64
Double precision floating point.
Definition: MMALIB_types.h:172
uint64_t end_count
static void MMALIB_UTIL_mult(int64_t *ph, int64_t *pl, int64_t a, int64_t b)
uint64_t overhead
uint64_t beg_count