arm_dotproduct_example_f32.c
1 /* ---------------------------------------------------------------------- 2 * Copyright (C) 2010-2012 ARM Limited. All rights reserved. 3 * 4 * $Date: 17. January 2013 5 * $Revision: V1.4.0 6 * 7 * Project: CMSIS DSP Library 8 * Title: arm_dotproduct_example_f32.c 9 * 10 * Description: Example code computing dot product of two vectors. 11 * 12 * Target Processor: Cortex-M4/Cortex-M3 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * - Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * - Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * - Neither the name of ARM LIMITED nor the names of its contributors 24 * may be used to endorse or promote products derived from this 25 * software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 * -------------------------------------------------------------------- */ 40 41 /** 42 * @ingroup groupExamples 43 */ 44 45 /** 46 * @defgroup DotproductExample Dot Product Example 47 * 48 * \par Description: 49 * \par 50 * Demonstrates the use of the Multiply and Add functions to perform the dot product. 51 * The dot product of two vectors is obtained by multiplying corresponding elements 52 * and summing the products. 53 54 * \par Algorithm: 55 * \par 56 * The two input vectors \c A and \c B with length \c n, are multiplied element-by-element 57 * and then added to obtain dot product. 58 * \par 59 * This is denoted by the following equation: 60 * <pre> dotProduct = A[0] * B[0] + A[1] * B[1] + ... + A[n-1] * B[n-1]</pre> 61 * 62 * \par Block Diagram: 63 * \par 64 * \image html dotProduct.gif 65 * 66 * \par Variables Description: 67 * \par 68 * \li \c srcA_buf_f32 points to first input vector 69 * \li \c srcB_buf_f32 points to second input vector 70 * \li \c testOutput stores dot product of the two input vectors. 71 * 72 * \par CMSIS DSP Software Library Functions Used: 73 * \par 74 * - arm_mult_f32() 75 * - arm_add_f32() 76 * 77 * <b> Refer </b> 78 * \link arm_dotproduct_example_f32.c \endlink 79 * 80 */ 81 82 83 /** \example arm_dotproduct_example_f32.c 84 */ 85 86 #include <math.h> 87 #include "arm_math.h" 88 89 #if defined(SEMIHOSTING) 90 #include <stdio.h> 91 #endif 92 93 /* ---------------------------------------------------------------------- 94 * Defines each of the tests performed 95 * ------------------------------------------------------------------- */ 96 #define MAX_BLOCKSIZE 32 97 #define DELTA (0.000001f) 98 99 /* ---------------------------------------------------------------------- 100 * Test input data for Floating point Dot Product example for 32-blockSize 101 * Generated by the MATLAB randn() function 102 * ------------------------------------------------------------------- */ 103 /* ---------------------------------------------------------------------- 104 ** Test input data of srcA for blockSize 32 105 ** ------------------------------------------------------------------- */ 106 float32_t srcA_buf_f32[MAX_BLOCKSIZE] = 107 { 108 -0.4325648115282207, -1.6655843782380970, 0.1253323064748307, 109 0.2876764203585489, -1.1464713506814637, 1.1909154656429988, 110 1.1891642016521031, -0.0376332765933176, 0.3272923614086541, 111 0.1746391428209245, -0.1867085776814394, 0.7257905482933027, 112 -0.5883165430141887, 2.1831858181971011, -0.1363958830865957, 113 0.1139313135208096, 1.0667682113591888, 0.0592814605236053, 114 -0.0956484054836690, -0.8323494636500225, 0.2944108163926404, 115 -1.3361818579378040, 0.7143245518189522, 1.6235620644462707, 116 -0.6917757017022868, 0.8579966728282626, 1.2540014216025324, 117 -1.5937295764474768, -1.4409644319010200, 0.5711476236581780, 118 -0.3998855777153632, 0.6899973754643451 119 }; 120 121 /* ---------------------------------------------------------------------- 122 ** Test input data of srcB for blockSize 32 123 ** ------------------------------------------------------------------- */ 124 float32_t srcB_buf_f32[MAX_BLOCKSIZE] = 125 { 126 1.7491401329284098, 0.1325982188803279, 0.3252281811989881, 127 -0.7938091410349637, 0.3149236145048914, -0.5272704888029532, 128 0.9322666565031119, 1.1646643544607362, -2.0456694357357357, 129 -0.6443728590041911, 1.7410657940825480, 0.4867684246821860, 130 1.0488288293660140, 1.4885752747099299, 1.2705014969484090, 131 -1.8561241921210170, 2.1343209047321410, 1.4358467535865909, 132 -0.9173023332875400, -1.1060770780029008, 0.8105708062681296, 133 0.6985430696369063, -0.4015827425012831, 1.2687512030669628, 134 -0.7836083053674872, 0.2132664971465569, 0.7878984786088954, 135 0.8966819356782295, -0.1869172943544062, 1.0131816724341454, 136 0.2484350696132857, 0.0596083377937976 137 }; 138 139 /* Reference dot product output */ 140 float32_t refDotProdOut = 5.9273644806352142; 141 142 /* ---------------------------------------------------------------------- 143 * Declare Global variables 144 * ------------------------------------------------------------------- */ 145 float32_t multOutput[MAX_BLOCKSIZE]; /* Intermediate output */ 146 float32_t testOutput; /* Final ouput */ 147 148 arm_status status; /* Status of the example */ 149 150 int32_t main(void) 151 { 152 uint32_t i; /* Loop counter */ 153 float32_t diff; /* Difference between reference and test outputs */ 154 155 /* Multiplication of two input buffers */ 156 arm_mult_f32(srcA_buf_f32, srcB_buf_f32, multOutput, MAX_BLOCKSIZE); 157 158 /* Accumulate the multiplication output values to 159 get the dot product of the two inputs */ 160 for(i=0; i< MAX_BLOCKSIZE; i++) 161 { 162 arm_add_f32(&testOutput, &multOutput[i], &testOutput, 1); 163 } 164 165 /* absolute value of difference between ref and test */ 166 diff = fabsf(refDotProdOut - testOutput); 167 168 /* Comparison of dot product value with reference */ 169 status = (diff > DELTA) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS; 170 171 if (status != ARM_MATH_SUCCESS) 172 { 173 #if defined (SEMIHOSTING) 174 printf("FAILURE\n"); 175 #else 176 while (1); /* main function does not return */ 177 #endif 178 } 179 else 180 { 181 #if defined (SEMIHOSTING) 182 printf("SUCCESS\n"); 183 #else 184 while (1); /* main function does not return */ 185 #endif 186 } 187 } 188 189 /** \endlink */