特别说明:完整45期数字信号处理教程,原创高性能示波器代码全开源地址:链接
' T. G8 I9 z) U1 _! u第8章 BasicMathFunctions的使用(一)
- G, H- s- z: x 本期教程开始学习ARM官方的DSP库,这里我们先从基本数学函数开始。本期教程主要讲绝对值,加法,点乘和乘法四种运算。 8.1 绝对值(Vector Absolute Value) 8.2 求和(Vector Addition) 8.3 点乘(Vector Dot Product) 8.4 乘法(Vector Multiplication) ' F {: S! {( c' ]! c* V
8.1 绝对值(Vector Absolute Value) 这部分函数主要用于求绝对值,公式描述如下: pDst[n] = abs(pSrc[n]), 0 <= n < blockSize. 特别注意,这部分函数支持目标指针和源指针指向相同的缓冲区。 8.1.1 arm_abs_f32 这个函数用于求32位浮点数的绝对值,源代码分析如下: - /**
8 O+ _2 ^ D K - * @brief Floating-point vector absolute value. (1)8 Y* q) _4 Z$ n# e3 e' A: s: U, W
- * @param[in] *pSrc points to the input buffer
4 U* c) t5 s: ~/ f5 g; Y7 n - * @param[out] *pDst points to the output buffer / O. A' D) c, \( W: L" ?: z
- * @param[in] blockSize number of samples in each vector
& `; l! h4 R! z L - * @return none. M. L9 e9 p8 l9 D9 m# n
- */3 g5 w& q% g: D* X3 L
-
! l0 h7 s* G3 u3 w8 _ - void arm_abs_f32( (2)" j3 @) s1 z" v) w3 c, U3 r
- float32_t * pSrc,
: g. N& T' h' r3 s, A) L; p% T - float32_t * pDst,
8 q$ f4 z. G. \2 ?2 Z& B - uint32_t blockSize)& K2 o; L4 ]6 H4 ?. E
- {
$ a, l4 ?6 V g/ j - uint32_t blkCnt; /* loop counter */
+ P0 U/ l+ |, l( E8 ~7 n4 K -
7 V: v& b3 s6 X: l9 h. G0 C - #ifndef ARM_MATH_CM0_FAMILY (3)* O) K, }, J5 x$ m; e2 Z
- / D8 F) ~/ {( }; F$ X) e
- /* Run the below code for Cortex-M4 and Cortex-M3 */
7 m1 I' N4 j) t. W# h3 L - float32_t in1, in2, in3, in4; /* temporary variables */
$ ]8 r9 x# t% O7 o, e& ] - 1 p0 U" ]: N1 r6 {8 b: z
- /*loop Unrolling */& t* A; `& U. x2 {- a
- blkCnt = blockSize >> 2u; (4)
3 D/ l, h3 i" _5 t+ c -
: u; E; i2 G7 o$ _0 D* G6 X g4 g - /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
! k G4 \: m d - ** a second loop below computes the remaining 1 to 3 samples. */* V, l' @ ]# P+ J! i. |( ?
- while(blkCnt > 0u)
" h" t0 M+ \8 E/ e" h% K5 I - {
" \, J* P L {; ~* E - /* C = |A| */. ?0 O6 Y7 C9 g' o
- /* Calculate absolute and then store the results in the destination buffer. */
8 ?9 n( o+ d) J3 h- s - /* read sample from source */% _/ B% _7 k9 \4 @1 T6 ~
- in1 = *pSrc;
! Z; m' X- b& P. u - in2 = *(pSrc + 1);
0 U) W5 T) h6 @$ Y2 X; L0 p* }" ] - in3 = *(pSrc + 2);
( E0 u) i; O* [9 S: U - 1 U2 s4 X# g6 U2 f( u- r: o# P2 |
- /* find absolute value */# u& c. g1 s' S, S* K* T
- in1 = fabsf(in1); (5)
4 ^/ {9 }' `5 M7 m2 n -
2 l9 \4 U2 F5 q - /* read sample from source */# J! F. j6 a( R& o2 b% p
- in4 = *(pSrc + 3);2 s D) t' C9 s- n5 ?% m& Q2 d. U
- + u" h( f( e0 y3 m( Y9 Y' ^4 @
- /* find absolute value */+ |4 k( w+ G' I* e" l
- in2 = fabsf(in2); M, \/ I# d, T g/ @5 Q7 `
-
7 f4 T6 X" Y+ o+ g1 {- t6 Q3 _ - /* read sample from source */
6 [5 W2 `$ a) J - *pDst = in1;: i& j) F! T" E+ [. h, q
- 4 n% d' ]: i$ ]! f
- /* find absolute value */
2 d7 T0 G# j' X - in3 = fabsf(in3);2 b) t! q- k' ^
-
( Y4 l( w: u6 k) T# e% D - /* find absolute value */5 |, V% J! t; W- }5 b( J" G( ?
- in4 = fabsf(in4);
: ?( U* h/ l0 i, x- L; E' B3 d2 V - - e8 h2 E6 }+ n- X
- /* store result to destination */9 M) L P- g& Q7 j8 r$ l
- *(pDst + 1) = in2;# S, _3 B. \: n0 F
- 3 F% e" v1 u1 |
- /* store result to destination */
, d& c4 S1 B) T6 w) X: c - *(pDst + 2) = in3;
& \/ X b. K F$ y- }3 v2 r2 m+ K" ?" W - 7 B$ a) m- c" G4 U# [
- /* store result to destination */
' v- G# f9 D# a' o - *(pDst + 3) = in4;, C" g+ [' [# G
-
% p5 L0 g7 m z7 P- K -
& @: E) S2 C( y% [" m - /* Update source pointer to process next sampels */ (6)
! v3 c, A% e; u2 I4 b2 e - pSrc += 4u;* k" o* \; R; V M3 g4 G s) \9 V
- " B. ~0 J* N3 x1 n1 i( C
- /* Update destination pointer to process next sampels */' { _ {) `6 }6 U1 Q: |' h9 c6 E
- pDst += 4u;
/ ] |( ?3 v$ F; Y* R, l -
2 q1 w- ?& Y# i$ x - /* Decrement the loop counter */
2 g1 R8 U' w4 E9 V! M. Z& V) E - blkCnt--;
. P: v' [. p2 N1 E - }
9 ~: @' u/ D, l- [- L - 8 m1 T! [4 w8 K, ^
- /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
( H" ^3 }4 R# K* u- W - ** No loop unrolling is used. */* v- q$ j) r ^% U
- blkCnt = blockSize % 0x4u;
! |6 q. w- V' a5 n* j5 z -
4 I9 U+ M. g A6 P - #else (7)
7 m0 A, |* O- g$ @ - ( C/ y1 |! q, B& W
- /* Run the below code for Cortex-M0 */; m+ I$ G3 N, i' k X- G: k* r
-
~4 A& P, B( k! t- ?7 R$ w - /* Initialize blkCnt with number of samples */$ J4 T$ Y l( z
- blkCnt = blockSize;
9 w( e0 Q: O" N0 D% G, @: ] -
( _6 D1 k3 q$ r n# ` - #endif /* #ifndef ARM_MATH_CM0_FAMILY */( S) I0 w( ~1 c
-
- b" N) E, A; Y$ U( i! Y - while(blkCnt > 0u) (8)" b6 \9 U' l6 z3 K8 i1 J
- {* B, o; m5 f4 P1 @, H1 `
- /* C = |A| */
4 E7 n% p! r0 b1 k( b! N) h- ? - /* Calculate absolute and then store the results in the destination buffer. */. |, }2 ?" _5 q& w9 u) i
- *pDst++ = fabsf(*pSrc++);
1 o9 @ c+ R3 Q5 }: j -
* P) j$ u: y% l/ S! a1 Z - /* Decrement the loop counter */
6 `: }4 Q! T& f& [0 Q8 e - blkCnt--;. t1 } e- C2 d2 G
- }
5 S, F. r/ ?4 D* N - }
复制代码1. 在这里简单的跟大家介绍一下DSP库中函数的通用格式,后面就不再赘述了。 (1) 基本所有的函数都是可重入的。 (2) 大部分函数都支持一组数的计算,比如这个函数arm_abs_f32就可以计算一组数的绝对值。所以如果只是就几个数的绝对值,用这个库函数就没有什么优势了。 (3) 库函数基本是CM0,CM3和CM4都支持的(最新的DSP库已经添加CM7的支持)。 (4) 每组数据基本上都是以4个数为一个单位进行计算,不够四个再单独计算。 (5) 大部分函数都是配有f32,Q31,Q15和Q7四种格式。 2. 函数参数,支持输入一个数组进行计算绝对值。 3. 这部分代码是用于CM3和CM4内核。 4. 左移两位从而实现每4个数据为一组进行计算。 5. fabsf:这个函数不是用Cortex-M4F支持的DSP指令实现的,而是用C语言实现的,这个函数是被MDK封装起来的。 6. 切换到下一组数据。 7. 这部分代码用于CM0. 8. 用于不够4个数据的计算或者CM0内核。 8.1.2 arm_abs_q31 这个函数用于求32位定点数的绝对值,源代码分析如下: - /** " ~1 R- n; t* ]
- * @brief Q31 vector absolute value.
0 i: N6 C/ @( a3 ^ - * @param[in] *pSrc points to the input buffer
4 m5 r. [, s% K; i8 D - * @param[out] *pDst points to the output buffer ; ~0 B9 g8 k* d; ]
- * @param[in] blockSize number of samples in each vector 3 ]0 O( v- Y, A5 I' T# W! u" t
- * @return none.
( [# I0 ~" |% Y+ I" P - *
" C8 y ~9 {6 t }5 V& b7 I - * <b>Scaling and Overflow Behavior:</b> (1)
# `1 }" A- H$ C& @2 X8 J* ]: C" \ - * \par
; T! r2 C) _0 B1 p - * The function uses saturating arithmetic. & R; Q6 d6 Y' v2 t* h9 m
- * The Q31 value -1 (0x80000000) will be saturated to the maximum allowable positive value 0x7FFFFFFF.
; t* s9 r0 i( Q/ [' ` - */
' N9 {5 ?% b5 X7 g -
$ t0 Q( g$ W6 v - void arm_abs_q31(! a- {. o, s& P: [* l
- q31_t * pSrc,7 G! X# a* P2 ^" `
- q31_t * pDst,
" G! m; w# B1 ? B* N& d - uint32_t blockSize)! R) Q0 u% k0 y& n0 ?
- {9 x* F/ e! m2 E3 s3 u0 l
- uint32_t blkCnt; /* loop counter */
0 U E, d+ c5 l" e. g! J+ y - q31_t in; /* Input value */
) ?+ x: M0 z6 b) w -
& C5 f( {3 N! U) K' |" w - #ifndef ARM_MATH_CM0_FAMILY
) O8 o% S' j. B- D3 [9 Y9 Y - " ~. l6 N d' u; [3 s' w6 n% I f& {
- /* Run the below code for Cortex-M4 and Cortex-M3 */
# A: T* Q' F3 P - q31_t in1, in2, in3, in4;0 T* h* d& C( I$ @6 y
-
' ?" c5 x) \+ n9 g$ z, m9 s2 n& ~ - /*loop Unrolling */
0 d1 N2 s4 U7 X6 t0 Z. \ - blkCnt = blockSize >> 2u;
# y; C" W7 O/ y$ u% [! A! B' N - ; \- }( m6 N! F0 L6 f
- /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
3 Q9 c* G/ z8 M" k - ** a second loop below computes the remaining 1 to 3 samples. */7 J9 N! n$ E+ A0 W, n. N
- while(blkCnt > 0u)' K" `# p& J: _
- {
" \7 r l/ V6 p' B: u0 o/ h - /* C = |A| */
- w8 U$ ?0 A; b/ c - /* Calculate absolute of input (if -1 then saturated to 0x7fffffff) and then store the results in the destination buffer. */
/ w- u9 r+ |. b4 s3 ?9 e1 h" c - in1 = *pSrc++;6 i/ A0 [5 _ b( i$ @+ T
- in2 = *pSrc++;
+ `/ c. M7 I5 Q/ u8 Q3 S* a# } - in3 = *pSrc++;
' ]: b- v: k3 K% M9 W w# h: \% V - in4 = *pSrc++;2 k G O4 I# c; Q: H3 n: r
- ! B; S( A9 K" s; d
- *pDst++ = (in1 > 0) ? in1 : (q31_t)__QSUB(0, in1); (2)
$ l$ X6 f. M# _: v% Z& {# ]2 H - *pDst++ = (in2 > 0) ? in2 : (q31_t)__QSUB(0, in2);
' s" w" j! z1 u - *pDst++ = (in3 > 0) ? in3 : (q31_t)__QSUB(0, in3);/ H7 Q5 }7 Y- O) K/ v
- *pDst++ = (in4 > 0) ? in4 : (q31_t)__QSUB(0, in4);, l m% L3 N+ e: }* H" P2 @
- c: z8 R$ |! q L5 K
- /* Decrement the loop counter */
1 r0 W3 O4 }8 d' P/ m6 M8 j3 n s) } - blkCnt--;
; q- |% \' }6 j) M% e - }
7 X/ } b/ s3 t _ d# h5 G -
! I5 @/ @4 s/ G k" T, r! ] - /* If the blockSize is not a multiple of 4, compute any remaining output samples here. ' x' g2 y" A( d5 j) ]# \
- ** No loop unrolling is used. */
. W' n+ D' {/ ?7 |0 R2 Z4 @; G - blkCnt = blockSize % 0x4u;
; c3 y8 t% k6 W1 S$ _ - 2 W1 }/ k' Y6 D8 D
- #else
" z) W, b" L9 x - 5 h4 O. q& Q+ o9 R* b2 v0 L
- /* Run the below code for Cortex-M0 */1 G2 J: D5 Z, X7 [& r' ]3 U* T
- 9 Q. ~! d% d- L9 z7 p& e/ ?
- /* Initialize blkCnt with number of samples */6 [8 Q% _, W( S3 a6 G/ X% X% G
- blkCnt = blockSize;
0 O8 X+ F' O" a$ ? -
) N- W0 t4 ^+ f1 D, ]# B - #endif /* #ifndef ARM_MATH_CM0_FAMILY */
4 ?/ E9 {+ ~- V D" c0 _ - , o/ g& H0 _8 L& B
- while(blkCnt > 0u)# ~" K, c) J$ B0 }9 W
- {
" }' T4 ? e+ s- p - /* C = |A| */0 i" f' E1 S/ K w. t$ o" B. @2 w+ V
- /* Calculate absolute value of the input (if -1 then saturated to 0x7fffffff) and then store the results in the destination buffer. */
4 f& J" O) t+ R1 @ - in = *pSrc++;
% j$ n( k$ p+ `) `- j$ X/ t0 A - *pDst++ = (in > 0) ? in : ((in == INT32_MIN) ? INT32_MAX : -in);! T3 u" K) O c
-
! f4 ]3 c0 {# a - /* Decrement the loop counter */
2 f) I4 c4 d# i$ b, U5 c2 z - blkCnt--;
) |- Y& @4 c3 H e+ Z0 K - }
( U9 k( R# t% d! @+ s- b) M" b4 H -
Q" q, Y9 i% H4 C6 N! K" m, Q - }
复制代码1. 这个函数使用了饱和运算,其实不光这个函数,后面很多函数都是使用了饱和运算的,关于什么是饱和运算,大家看Cortex-M3权威指南中文版的4.3.6 小节:汇编语言:饱和运算即可。 对于Q31格式的数据,饱和运算会使得数据0x80000000变成0x7fffffff(这个数比较特殊,算是特殊处理,记住即可)。 2. 这里重点说一下函数__QSUB,其实这个函数算是Cortex-M4/M3的一个指令,用于实现饱和减法。 比如函数:__QSUB(0, in1) 的作用就是实现0 – in1并返回结果。这里__QSUB实现的是32位数的饱和减法。还有__QSUB16和__QSUB8实现的是16位和8位数的减法。 8.1.3 arm_abs_q15 这个函数用于求15位定点数的绝对值,源代码分析如下: - /** ; Z& p* {( }( g+ \
- * @brief Q15 vector absolute value.
& V: G9 R' y5 M! c! ~" j. W - * @param[in] *pSrc points to the input buffer
# L: E, W& {7 R) R7 W0 O+ ` - * @param[out] *pDst points to the output buffer
% e3 _7 i8 M1 H0 u: x$ g - * @param[in] blockSize number of samples in each vector $ F% d( y2 n u' X( V
- * @return none. 6 @' K: h4 ]. h2 y8 \6 n: E, G
- *
' J% |& [( Y3 M4 Q) d# l4 W- t7 u. E/ [ - * <b>Scaling and Overflow Behavior:</b> $ E0 o+ `: w" u
- * \par
% d9 p# T u: [0 g - * The function uses saturating arithmetic. , K6 W& v8 u* M2 R( |6 ^; f
- * The Q15 value -1 (0x8000) will be saturated to the maximum allowable positive value 0x7FFF. (1)
! V. Y0 V% r1 e' Y0 v7 r9 g" R$ {* { - */
% Y0 x$ c; ]; E T; V, u -
2 V# z4 t, H. f! [% R7 b& S4 R* C - void arm_abs_q15(& U: o6 K/ N B1 T( r/ W$ s% m% n
- q15_t * pSrc,5 r: p: C3 ^4 P# f* H
- q15_t * pDst,( d O- n* \, k) k
- uint32_t blockSize)
. ~" h9 Z* `' X' f% o7 U" v - {
8 C4 \, w& u: l5 `# P+ b$ ] - uint32_t blkCnt; /* loop counter */4 R% A& Z' g% d2 P* S
-
* U7 P; T5 y+ ^& } - #ifndef ARM_MATH_CM0_FAMILY% @( A! [ l r3 F) O
- __SIMD32_TYPE *simd; (2)4 S7 T ^$ l, b, G5 b
- * R6 r1 C# Q3 m" R
- /* Run the below code for Cortex-M4 and Cortex-M3 */2 n& Y3 s; o5 u: g
- + G1 o9 j$ a$ q t5 g3 k
- q15_t in1; /* Input value1 */
+ a: w" j/ t/ R) U7 } - q15_t in2; /* Input value2 */
+ \* Q3 V! B5 b5 C' T -
' V* B9 C% q- t* m' O( |/ V -
- Y, }! E9 g1 V6 j( S - /*loop Unrolling */
9 p' Y2 @7 `/ b$ V+ I6 I# v - blkCnt = blockSize >> 2u;
( r" x9 o/ \* C& S9 s z% _2 u; D - f T+ g5 {" [' D8 U
- /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 8 D( d4 G9 l+ V- w) ^3 `
- ** a second loop below computes the remaining 1 to 3 samples. */
9 d3 t$ k1 v7 I9 c - simd = __SIMD32_CONST(pDst); (3)
U0 f# T# F' j3 M; f - while(blkCnt > 0u)
9 S7 Y: x0 {4 ^* ?' v; @ - {
( M1 N$ {. U5 |) m+ M# v - /* C = |A| */
`" t/ f* ~. s - /* Read two inputs */' r, d) `' r/ z$ [
- in1 = *pSrc++;
( Y( K! V5 [8 b! D - in2 = *pSrc++;1 `% E. O& e9 u6 p
-
3 U U" G9 Y v8 [' }% m p) d! v1 m -
3 U+ z5 e/ X% `" T4 W - /* Store the Absolute result in the destination buffer by packing the two values, in a single cycle */
2 s# R: ^+ P- U - #ifndef ARM_MATH_BIG_ENDIAN
% ]' X" ?- O* ~4 ?9 b( L - *simd++ =4 N+ Z5 D! }7 n+ r; T( {' g: p
- __PKHBT(((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)), (4)" |- E" l \- y, T
- ((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)), 16);
+ m' O$ Z3 ^6 t - & g5 F! H# z& b- w, ]
- #else: S/ q: a; I5 i- ^& w. B
- " b( n. ^" o G- j5 H* v7 H: D
-
9 _! @; ^: m' G7 X& m - *simd++ =
7 K+ `" }, ^2 I9 p - __PKHBT(((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)),
- J9 p/ u6 s9 `3 p8 c - ((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)), 16);
+ r; @# n6 V2 _1 @! O# w5 b -
9 {7 E# B/ @' u" n0 ^7 u - #endif /* #ifndef ARM_MATH_BIG_ENDIAN */) H3 ]0 D H; s/ ]- s
- 7 y$ z M5 s2 z* w
- in1 = *pSrc++;7 O, S. ~6 s7 Y2 [! i$ O8 P
- in2 = *pSrc++;, `" W$ G7 ]( o3 V L! F7 ^
-
: I5 k, [! u9 i( y3 | d5 i! z( ] -
: ~# W& o, R$ _( j# n e( h# X - #ifndef ARM_MATH_BIG_ENDIAN
; v2 V; ]1 d+ b( ]" N9 y& a -
6 ?1 x2 p' F1 y% f) w - *simd++ =7 F2 Q0 t: R( V! x' n) S% q5 \8 ]
- __PKHBT(((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)),* w. Q( r- k9 o3 {
- ((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)), 16);
6 l+ X5 u8 _0 O, B" T N - 7 @' q; X- |9 ~" h5 @; h( ?
- #else
4 Y8 Y/ t9 J* A0 H! o. R1 ^ - 1 i7 Q0 _& [; Q7 ]4 J" b/ G8 r
- % q7 g$ B8 B! |/ B
- *simd++ =$ U2 S- {' G$ { X
- __PKHBT(((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)),
( e6 o& V1 m. @ O8 I; u% d* } - ((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)), 16);/ _: Y/ f/ E) M" E4 m! I- e1 y0 n
-
/ j! W. a k8 T3 ~$ k& N - #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
9 w8 }+ ?& @2 l* U -
3 _1 N' z( z& r& r* M, z( R - /* Decrement the loop counter */; x. v9 ]( v0 s/ d z& W9 m
- blkCnt--;
8 p) b, f1 w& L. ?+ u4 ^0 N* S - }0 Q: H% R, @$ N5 i7 |, B9 B
- pDst = (q15_t *)simd;- `, b; B; I0 K3 x" |: v
- /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
* h& H1 R- b0 v9 i2 s2 Z& E - ** No loop unrolling is used. */- S- j9 I U8 u) \
- blkCnt = blockSize % 0x4u;7 @/ i1 V: ^+ P/ B& t# L
- 6 _2 Q4 v- E" R- S) n1 `
- while(blkCnt > 0u)
: |5 T7 y7 j: M$ V4 n& j - {. x. M- Z/ Q# n: L" k
- /* C = |A| */
: a) y, R: m! \: ]% q7 x) v4 U+ I0 N - /* Read the input */1 J7 `. H% c5 R* K; d# k/ E. \
- in1 = *pSrc++;% C6 ~/ T; F' ?# D. W3 ]
- : s0 \) }4 d# K6 s& u
- /* Calculate absolute value of input and then store the result in the destination buffer. */( ~, [3 ?0 h3 H2 s9 L5 ^
- *pDst++ = (in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1);
" V: l% K2 u0 r -
2 `' B. n6 a0 w1 p% f - /* Decrement the loop counter */' i/ S: M7 q6 j9 I% ^3 ]$ }
- blkCnt--;
% H2 {! a- i3 N& F, l! ~6 _ - }3 H/ R1 f' A% l6 C! _. Q$ k
- & U5 q8 q; l3 E4 l: g! j% I$ G
- #else: n) X/ y! ~: E+ l( C# O
- / w- x0 V( ?% V( a6 o
- /* Run the below code for Cortex-M0 */; w1 G8 K6 Z6 T+ p7 ]4 r2 P" ^0 G
- 6 _4 o& C, M& s- f
- q15_t in; /* Temporary input variable */4 K6 r& }2 Q. P/ N ?) Q8 P# d. K
-
+ f7 F6 R, K/ \8 ?6 j0 r6 L( ^: Q - /* Initialize blkCnt with number of samples *// y; P$ d$ d. @% P
- blkCnt = blockSize;& C( J# {, `1 l _: ^( w
- 0 X6 [+ A4 [2 [8 i ^1 l- S; d
- while(blkCnt > 0u)* g9 D% s# }, B" y
- {: S& J7 Y& ?5 O
- /* C = |A| */9 E4 S7 k4 Z* i# n
- /* Read the input */
z) z5 l$ X/ q$ B3 {$ p0 R - in = *pSrc++;3 h, K* C4 r6 g. R& W
-
* K" Z w) @. k, d- H/ Y6 R - /* Calculate absolute value of input and then store the result in the destination buffer. */9 x6 N3 v# r# x; T' `( E( m/ F) Y
- *pDst++ = (in > 0) ? in : ((in == (q15_t) 0x8000) ? 0x7fff : -in);
/ m4 ?6 N3 w9 z: D - 7 ^; x5 [- g% j# R+ R2 ]
- /* Decrement the loop counter */
4 s g: v; C7 ~" v& o) j$ J - blkCnt--;6 D ~- R @3 e0 R
- }' {% H& |+ [" y1 q' U- l* W i2 d
-
, \' H, t) E& W - #endif /* #ifndef ARM_MATH_CM0_FAMILY */
7 \( n' T' l5 A1 ]" ~9 | - ! H9 a( j0 h* a% U% t |5 |* B
- }
复制代码1. 对于Q15格式的数据,饱和运算会使得数据0x8000变成0x7fff。 2. __SIMD32_TYPE的定义在文件arm_math.h中,具体定义如下: #define __SIMD32_TYPE int32_t __packed SIMD就是咱们上期教程所将的单指令多数据流。简单的理解就是__SIMD32_TYPE就是定义了一个int32_t类型的数据,__packed的含义就是实现字节的对齐功能,方便两个16位数据的都存入到这个数据类型中。 3. 函数__SIMD32_CONST的定义如下: #define __SIMD32_CONST(addr) ((__SIMD32_TYPE *)(addr)) 4. 函数__PKHBT的定义在文件core_cm4_simd.h,定义如下: #define __PKHBT(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0x0000FFFFUL) | \ ((((uint32_t)(ARG2)) << (ARG3)) & 0xFFFF0000UL) ) 这个宏定义的作用就是将将两个16位的数据合并成32位数据。但是有一点要特别说明__PKHBT也是CM4内核支持的SIMD指令,上面的宏定义的C函数会被MDK自动识别并调用相应的PKHBT指令。 __QSUB16用于实现16位数据的饱和减法。 8.1.4 arm_abs_q7 这个函数用于求8位定点数的绝对值,源代码分析如下: - /** % d* F w! n3 P* a
- * @brief Q7 vector absolute value.
6 h0 E2 F; D8 b3 W: n - * @param[in] *pSrc points to the input buffer , S$ y8 x# c- R+ \( ?/ S1 s
- * @param[out] *pDst points to the output buffer
. |4 b9 R& Q+ T( N* |% ^ - * @param[in] blockSize number of samples in each vector
$ u3 p$ [1 n0 ]8 R2 C - * @return none.
3 j( o% k; `* }, G) W - * & Z+ a2 R; ~* q T, V! @2 j3 T$ n
- * \par Conditions for optimum performance
" C; z" d7 m& w5 d L6 o - * Input and output buffers should be aligned by 32-bit
, {* B' R6 C# s$ u H! C - *
, `9 W1 d/ w. q7 S# j - *
. e4 [6 g: A7 u" i/ n& _ - * <b>Scaling and Overflow Behavior:</b> (1)
( M, o/ G& j4 O8 ^1 h- h! L$ a - * \par ) ?/ |& h6 e) U: K; c& Z' ^( r
- * The function uses saturating arithmetic. # o1 k7 w( E: D9 g
- * The Q7 value -1 (0x80) will be saturated to the maximum allowable positive value 0x7F.
+ T8 Z, p, Q) r/ i4 C - */* q' z4 g& f/ ]( d9 v# z
- 4 V! W3 [; Q8 p
- void arm_abs_q7(
7 ^8 g U- a) ~ - q7_t * pSrc,2 w+ J8 `3 K' |; |; q# c
- q7_t * pDst,
. R0 v, f7 Q+ q ] - uint32_t blockSize)" v' V5 j) L: j) F3 {7 O' Z
- {
) A+ z1 `, K1 r! {- b - uint32_t blkCnt; /* loop counter */2 `6 k2 K9 c- }
- q7_t in; /* Input value1 */
# G6 ~ g. r( \ - ' p m5 T6 ~ W* s1 B
- #ifndef ARM_MATH_CM0_FAMILY( X" s- r+ R' Z+ b: J0 Q
-
" V/ H- O3 }+ S+ Y - /* Run the below code for Cortex-M4 and Cortex-M3 */$ k) U) ]; Q- }: N
- q31_t in1, in2, in3, in4; /* temporary input variables */
$ ^/ d$ n" N0 O' v - q31_t out1, out2, out3, out4; /* temporary output variables */
9 R0 p' @# F+ p0 G2 c5 C' H+ \ -
' T' p. Z3 I% M, q1 u& G - /*loop Unrolling */! q* p9 ?1 u2 a, A* L9 e- v+ Z
- blkCnt = blockSize >> 2u;
9 _( z3 A1 _% {; n -
! Y: r6 y5 T6 N. N - /* First part of the processing with loop unrolling. Compute 4 outputs at a time. / t+ f: p0 p8 p: E- H0 |
- ** a second loop below computes the remaining 1 to 3 samples. */9 |* g6 X2 h j( \- U0 G
- while(blkCnt > 0u). H' q/ i( _' p9 ]- |& A. a
- {1 a+ N2 F( _! o- l1 d5 p9 `
- /* C = |A| */' A& S0 _0 _0 i7 t8 B* ]! s0 b' T( a
- /* Read inputs */
; _/ B- j5 D2 i; U8 J; G) S - in1 = (q31_t) * pSrc;
6 F ~5 o$ k0 H Y - in2 = (q31_t) * (pSrc + 1);1 b- ~9 ~1 B; @2 @5 t; W
- in3 = (q31_t) * (pSrc + 2);
6 v, Z2 m7 k5 h1 S - 0 a6 {9 S4 D$ f8 C0 h
- /* find absolute value */
/ |# U, H9 ~5 A3 V0 O - out1 = (in1 > 0) ? in1 : (q31_t)__QSUB8(0, in1); (2)
1 x1 s. _0 \ `2 { -
% r* K7 Z( z) ~ - /* read input */
9 m2 [9 U! T) v# ?. l - in4 = (q31_t) * (pSrc + 3);) V0 v) ?2 k$ A' d! [& y2 t
- ) k3 a1 a1 c! E* n
- /* find absolute value */
3 B1 U k7 V N! i7 k - out2 = (in2 > 0) ? in2 : (q31_t)__QSUB8(0, in2);+ } f3 x* }( V5 @- ^& {
- ! }6 Y3 O! K2 `" c
- /* store result to destination */
% e- G# ^6 \0 R) f& b0 { J - *pDst = (q7_t) out1;
+ N$ n2 D& |2 h: ?& X& n. A" G - ' f7 o( h' Q2 ]5 z/ @
- /* find absolute value *// ?% V: d. q' y2 u& G% w& Z. l
- out3 = (in3 > 0) ? in3 : (q31_t)__QSUB8(0, in3);0 u9 c: W2 f* X: e; j" ?0 p7 C
-
3 `+ X8 Z3 F; @ - /* find absolute value */
4 i6 G& l5 r' h5 @ - out4 = (in4 > 0) ? in4 : (q31_t)__QSUB8(0, in4);
) a$ J1 G4 L+ A( e* P: \* C* T - % V" Q5 R! N4 @5 ~7 H2 X$ X
- /* store result to destination */
/ y9 i N7 j0 C( F6 N/ R* \2 l1 t - *(pDst + 1) = (q7_t) out2;
; `( H! W' O6 p/ J& S - 1 d2 M. P8 | C2 L0 y. F
- /* store result to destination */; n' }9 y2 m9 c& j6 Q( W
- *(pDst + 2) = (q7_t) out3;* O% _3 N2 R8 r
-
, ~2 v( r# E1 K - /* store result to destination */
: G$ X' e* z, B1 x2 D; _. d9 |( M- { - *(pDst + 3) = (q7_t) out4;
1 X. F6 u" Q# q -
7 X* g5 L% ?; D - /* update pointers to process next samples */
3 j' @: B/ m- h) x. J ^+ p% W - pSrc += 4u;! i" _9 o6 n# \( l C6 Y
- pDst += 4u;- u4 r. }- L' k/ q
-
: ^& W* v. X7 W& Q) ]; z - /* Decrement the loop counter */& ? B. r a# i. w0 u- g# V
- blkCnt--;
, F" V- N5 P& P, I - }
4 T0 l+ ^0 P+ L) ?/ w* x; |0 Z2 _ - 7 N# F1 I# e' F6 O2 I. N
- /* If the blockSize is not a multiple of 4, compute any remaining output samples here. * n9 W5 e8 }; ^* A& j. ?
- ** No loop unrolling is used. */
" D$ m# d1 @6 b6 {- \7 [ - blkCnt = blockSize % 0x4u;9 C: R& a c( B5 I) @
- #else# d" F2 ~! ^& k! L7 q
- ! z+ U: l9 F& i' {( }* B7 l
- /* Run the below code for Cortex-M0 */0 {9 m* Z9 E4 {) A) U
- blkCnt = blockSize;) _- b% ~3 k) {' J7 U8 F1 [
-
7 j+ j' N- L0 O* K, [6 l& M - #endif // #define ARM_MATH_CM0_FAMILY) L/ k, }% [- ]6 ]0 s
-
+ T* T' Z. o6 c X' ]" B9 g - while(blkCnt > 0u)
8 X! o' W3 c9 @: R* c. o - {
, c% T9 x& j2 } - /* C = |A| */5 W, T; |+ g' v$ Q1 U
- /* Read the input */0 Q; g+ D1 f9 _; J: @4 R
- in = *pSrc++;
& j0 q& Y0 }0 {0 r4 E1 x -
0 T/ y. S- P. l1 B: S& w, @, i, ` - /* Store the Absolute result in the destination buffer */+ y0 ?! _( p3 x" O4 X
- *pDst++ = (in > 0) ? in : ((in == (q7_t) 0x80) ? 0x7f : -in);
3 l3 S" G! Y: q -
! J+ }6 j* q2 @/ V$ A - /* Decrement the loop counter */
' _8 H7 Q! R1 O2 L7 P/ Q' e# F - blkCnt--;
# k) r9 T6 I0 r$ c+ _4 P0 B2 ` - }
o% t! k+ t+ l - }
复制代码1. 由于饱和运算,0x80求绝对值将变成数据0x7F。 2. __QSUB8用以实现8位数的饱和减法运算。 8.1.5 实例讲解实验目的: 1. 四种数据类型数据绝对值求解 实验内容: 1. 按下按键K1, 串口打印输出结果 实验现象: 通过窗口上位机软件SecureCRT(V5光盘里面有此软件)查看打印信息现象如下: 程序设计: - /*
8 s* ]; c! h2 D: ] R' x - *********************************************************************************************************. \) R4 G, i7 E W, s5 j2 }. O
- * 函 数 名: DSP_ABS
4 R5 u( d! I; X1 p/ T1 l2 Z @6 c- E - * 功能说明: 求绝对值9 t( C2 M) U9 }8 h8 S' t/ V/ p+ W
- * 形 参:无+ l$ X+ w4 a/ L
- * 返 回 值: 无
3 S! t9 \5 z" C$ j/ }! m - *********************************************************************************************************$ W% h$ Y4 q1 \: c5 P6 q! s
- */2 y; L: ~7 x; n
- static void DSP_ABS(void)$ P% z' f2 v6 p! A/ g
- {
" i T7 W- ]7 ]+ K2 ?8 g( ^ Z - static float32_t pSrc;
; H8 C( X ` N1 O - static float32_t pDst;
: `, ]! M8 f, r8 B( U! m - static q31_t pSrc1;, K8 J5 P& C1 k6 G, l' m- N1 u* l
- static q31_t pDst1;
; S: a4 `% E8 q: T6 ]7 H - static q15_t pSrc2;; O9 E( |& o: P! e
- static q15_t pDst2;# w4 ? Q+ `8 l. a
- static q7_t pSrc3 = 127; /* 为了说明问题,在这里设置初始值为127,然后查看0x80是否饱和为0x7F */" f/ T; @9 ~ H% j0 U8 [# o
- static q7_t pDst3;* T0 [$ N: H/ D! g* X
- pSrc -= 1.23f;( D+ V3 v) V' `, |
- arm_abs_f32(&pSrc, &pDst, 1); (1)
' m% r7 I- i7 X2 d. v) ~ - printf("arm_abs_f32 = %f\r\n", pDst);
; r5 B3 {* I! i2 j6 ` - $ W1 s& {" j( k! P: [
- pSrc1 -= 1;
& K3 @$ k5 W) Z- K! c2 o - arm_abs_q31(&pSrc1, &pDst1, 1); (2)
$ D6 [0 N* g* @ - printf("arm_abs_q31 = %d\r\n", pDst1);2 h, ?0 i( D# J" T
-
! ?1 ~! h( q* k - pSrc2 -= 1;" S0 H5 T* K6 t1 h3 C2 `+ J
- arm_abs_q15(&pSrc2, &pDst2, 1); (3)
+ q9 M! O9 Q$ s - printf("arm_abs_q15 = %d\r\n", pDst2);0 d& F3 ?- D2 e, T9 j9 C+ \% H
-
1 y# v; ?( |0 R. k! J% f c - pSrc3 += 1; + n. l2 _1 C6 ?0 U% g( W6 F% e1 P/ O
- printf("pSrc3 = %d\r\n", pSrc3); |1 v( v" l, g7 T6 r1 P# t/ z3 F
- arm_abs_q7(&pSrc3, &pDst3, 1); (4)
7 l0 Y8 M" p9 O y9 P! S! [8 Z - printf("arm_abs_q7 = %d\r\n", pDst3);
* }2 q; e: N6 _9 f - printf("***********************************\r\n");: A9 Y. @5 A+ v/ B4 U; P9 p
- }
复制代码(1)到(4)实现相应格式下绝对值的求解。这里只求了一个数,大家可以尝试求解一个数组的绝对值。 ) C( p8 T" o' G& D4 `# z0 D
|
8.5 总结