1 /* ======================================================================= */
2 /* TEXAS INSTRUMENTS, INC. */
3 /* */
4 /* DSPLIB DSP Signal Processing Library */
5 /* */
6 /* This library contains proprietary intellectual property of Texas */
7 /* Instruments, Inc. The library and its source code are protected by */
8 /* various copyrights, and portions may also be protected by patents or */
9 /* other legal protections. */
10 /* */
11 /* This software is licensed for use with Texas Instruments TMS320 */
12 /* family DSPs. This license was provided to you prior to installing */
13 /* the software. You may review this license by consulting the file */
14 /* TI_license.PDF which accompanies the files in this library. */
15 /* */
16 /* ----------------------------------------------------------------------- */
17 /* */
18 /* DSP_maxval.c -- Return maximum value out of a vector */
19 /* Intrinsic C Implementation */
20 /* */
21 /* REV 0.0.1 */
22 /* */
23 /* USAGE */
24 /* This routine has the following C prototype: */
25 /* */
26 /* short DSP_maxval ( */
27 /* const short *x, // Input Array */
28 /* int nx // Number of elements in input array */
29 /* ) */
30 /* */
31 /* The DSP_maxval routine accepts an array with 'nx' input data and */
32 /* searches for the maximum value. This maximum value is returned to */
33 /* the calling function. */
34 /* */
35 /* ASSUMPTIONS */
36 /* - Assumes 16-bit input data */
37 /* - nx > = 8 and is a multiple of 8 */
38 /* - The Kernel assumes the input array is aligned to a 8 byte */
39 /* boundary */
40 /* */
41 /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ */
42 /* */
43 /* */
44 /* Redistribution and use in source and binary forms, with or without */
45 /* modification, are permitted provided that the following conditions */
46 /* are met: */
47 /* */
48 /* Redistributions of source code must retain the above copyright */
49 /* notice, this list of conditions and the following disclaimer. */
50 /* */
51 /* Redistributions in binary form must reproduce the above copyright */
52 /* notice, this list of conditions and the following disclaimer in the */
53 /* documentation and/or other materials provided with the */
54 /* distribution. */
55 /* */
56 /* Neither the name of Texas Instruments Incorporated nor the names of */
57 /* its contributors may be used to endorse or promote products derived */
58 /* from this software without specific prior written permission. */
59 /* */
60 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
61 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
62 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
63 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
64 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
65 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
66 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
67 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
68 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
69 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
70 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
71 /* */
72 /* ======================================================================= */
74 #pragma CODE_SECTION(DSP_maxval, ".text:optimized");
76 #include "DSP_maxval.h"
78 short DSP_maxval (
79 const short *x, /* x[nx] = input vector */
80 int nx /* nx = number of elements */
81 )
82 {
83 int i;
85 const long long *xll;
87 double x0123, x4567;
88 int max01, max23, max45, max67;
90 /* Set all 8 intermediate max values to most negative */
91 /* Each 32bit var contains two shorts */
92 max01 = 0x80008000;
93 max23 = 0x80008000;
94 max45 = 0x80008000;
95 max67 = 0x80008000;
97 /* Convert the short pointer to a 64bit long long pointer */
98 xll = (const long long *)x;
100 /* In each loop iteration we will load 8 short values from the array. */
101 /* On the C64x+ we can do 4 max2 operations in one cycle. This will */
102 /* give us 8 results, that we keep seperated. Outside the loop we'll */
103 /* find the max out of these 8 intermediate values. */
105 _nassert((int)(xll) % 8 == 0);
106 #pragma MUST_ITERATE(1,,1);
107 for (i = 0; i < nx; i += 8) {
108 x0123 = _amemd8((void *)xll++); /* Use LDDW to load 4 shorts */
109 x4567 = _amemd8((void *)xll++); /* Use LDDW to load 4 shorts */
111 max01 = _max2(max01, _lo(x0123));
112 max23 = _max2(max23, _hi(x0123));
113 max45 = _max2(max45, _lo(x4567));
114 max67 = _max2(max67, _hi(x4567));
115 }
117 max01 = _max2(max01, max23); /* Calculate 2 maximums of max01 and max23 */
118 max45 = _max2(max45, max67); /* Calculate 2 maximums of max45 and max67 */
120 max01 = _max2(max01, max45); /* Get the 2 max values of the remaining 4 */
122 max45 = _rotl(max01, 16); /* Swap lower and higher 16 bit */
124 /* Find the final maximum value (will be in higher and lower part) */
125 max01 = _max2(max01, max45);
127 /* max01 is a 32-bit value with the result in the upper and lower 16 bit */
128 /* Use an AND operation to only return the lower 16 bit to the caller. */
129 return (max01 & 0xFFFF);
130 }
132 /* ======================================================================== */
133 /* End of file: DSP_maxval.c */
134 /* ------------------------------------------------------------------------ */
135 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
136 /* All Rights Reserved. */
137 /* ======================================================================== */