📄 c_dotp.c
字号:
/****************************************************************************//* Copyright (C) 1996-2000 Texas Instruments Incorporated *//* All Rights Reserved *//* *//* C_DOTP.C - Dot product example. *//* Example code from Programmer's Guide on optimizing C code. *//* *//****************************************************************************/#include <stdio.h>#include <time.h>int dotprod (short * restrict a, short * restrict b, unsigned int N);int dotprod1(short * restrict a, short * restrict b, unsigned int N);int dotprod2(short * restrict a, short * restrict b, unsigned int N);short a[] = { 0x011D, 0xFFFF, 0x007A, 0x018C, 0xFFAD, 0xF515, 0x0369, 0x0017, 0x03DA, 0xFAC0, 0x000C, 0xEADD, 0xFFAE, 0x0A70, 0xFEFF, 0xFFFE, 0x13FE, 0xFFF9, 0xFFEB, 0x0000, 0xFA82, 0xFFFF, 0x0005, 0x0070, 0x010D, 0xFEE3, 0xF9E7, 0x00A7, 0x13D1, 0xDB90, 0xFFFF, 0xFFFE, 0xFF00, 0x030F, 0x00FE, 0xFFF8, 0xFFFD, 0xFFFF, 0xFFF7, 0x0010};short b[] = { 0x0E8F, 0x002F, 0x9C74, 0xFFFF, 0xFF0B, 0xFF78, 0xD09C, 0x0188, 0x0001, 0x07BF, 0xFD64, 0x00C7, 0x0000, 0x0452, 0xFF8E, 0x0001, 0xFFFE, 0xFF3C, 0x0007, 0xFFF8, 0x0233, 0xF72B, 0x0004, 0x0000, 0xFF6B, 0x0370, 0x017D, 0xFD07, 0x0000, 0xFFCE, 0x0000, 0x6504, 0xFFFD, 0xFFFF, 0xFFFB, 0x01AA, 0xFF3E, 0x04AA, 0x00D4, 0xFF95 };int ret, ret1, ret2;/****************************************************************************//* TOP LEVEL DRIVER FOR THE TEST. *//****************************************************************************/int main(){ clock_t t_overhead, t_start, t_stop; /************************************************************************/ /* COMPUTE THE OVERHEAD OF CALLING CLOCK TWICE TO GET TIMING INFO. */ /************************************************************************/ t_start = clock(); t_stop = clock(); t_overhead = t_stop - t_start; /************************************************************************/ /* TIME DOTPROD */ /************************************************************************/ t_start = clock(); ret = dotprod(a, b, 40); t_stop = clock(); printf("DOTPROD: %d cycles\n", t_stop - t_start - t_overhead); /************************************************************************/ /* TIME DOTPROD1 */ /************************************************************************/ t_start = clock(); ret1 = dotprod1(a, b, 40); t_stop = clock(); printf("DOTPROD1: %d cycles\n", t_stop - t_start - t_overhead); if (ret != ret1) printf("Result failure dotprod1()\n"); else printf("Correct result dotprod1()\n"); /************************************************************************/ /* TIME DOTPROD2 */ /************************************************************************/ t_start = clock(); ret2 = dotprod2(a, b, 40); t_stop = clock(); printf("DOTPROD2: %d cycles\n", t_stop - t_start - t_overhead); if (ret1 != ret2) printf("Result failure dotprod2()\n"); else printf("Correct result dotprod2()\n");}/****************************************************************************//* DOTPROD - BASIC FORM. *//****************************************************************************/int dotprod(short * restrict a, short * restrict b, unsigned int N){ int i, sum = 0; for (i = 0; i < N; i++) sum += a[i] * b[i]; return sum;}/****************************************************************************//* DOTPROD1 - GENERATING A DOT PRODUCT WITH WORD ACCESSES *//****************************************************************************/int dotprod1(short * restrict a, short * restrict b, unsigned int N){ int i, sum = 0; /* a and b are aligned to a word boundary */ _nassert(((int)(a) & 0x3) == 0); _nassert(((int)(b) & 0x3) == 0); #pragma MUST_ITERATE(40, 40) for (i = 0; i < N; i++) sum += a[i] * b[i]; return sum;}/****************************************************************************//* DOTPROD2 - USING INTRINSICS AND WORD ACCESSES *//****************************************************************************/int dotprod2(short * restrict a, short * restrict b, unsigned int N){ int i, sum1 = 0, sum2 = 0; const int *i_a = (const int *)a; const int *i_b = (const int *)b; for (i = 0; i < (N >> 1); i++) { sum1 = sum1 + _mpy (i_a[i], i_b[i]); sum2 = sum2 + _mpyh(i_a[i], i_b[i]); } return sum1 + sum2;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -