123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /***********************************************************************
- *
- * This file contains functions for the automatic complexity calculation
- * $Id $
- *************************************************************************/
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "typedef.h"
- #include "count.h"
- /* Global counter variable for calculation of complexity weight */
- BASIC_OP multiCounter[MAXCOUNTERS];
- int currCounter=0; /* Zero equals global counter */
- /*BASIC_OP counter;*/
- const BASIC_OP op_weight =
- {
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 3, 3, 3, 4, 15, 18, 30, 1, 2, 1, 2, 2
- };
- /* function prototypes */
- Word32 TotalWeightedOperation (void);
- Word32 DeltaWeightedOperation (void);
- /* local variable */
- #if WMOPS
- /* Counters for separating counting for different objects */
- static int maxCounter=0;
- static char* objectName[MAXCOUNTERS+1];
- static Word16 fwc_corr[MAXCOUNTERS+1];
- #define NbFuncMax 1024
- static Word16 funcid[MAXCOUNTERS], nbframe[MAXCOUNTERS];
- static Word32 glob_wc[MAXCOUNTERS], wc[MAXCOUNTERS][NbFuncMax];
- static float total_wmops[MAXCOUNTERS];
- static Word32 LastWOper[MAXCOUNTERS];
- static char* my_strdup(const char *s)
- /*
- * duplicates UNIX function strdup() which is not ANSI standard:
- * -- malloc() memory area big enough to hold the string s
- * -- copy string into new area
- * -- return pointer to new area
- *
- * returns NULL if either s==NULL or malloc() fails
- */
- {
- char *dup;
-
- if (s == NULL)
- return NULL;
- /* allocate memory for copy of ID string (including string terminator) */
- /* NOTE: the ID strings will never be deallocated because there is no
- way to "destroy" a counter that is not longer needed */
- if ((dup = (char *) wmalloc(strlen(s)+1)) == NULL)
- return NULL;
- return strcpy(dup, s);
- }
- #endif
- int getCounterId(char *objectNameArg)
- {
- #if WMOPS
- if(maxCounter>=MAXCOUNTERS-1) return 0;
- objectName[++maxCounter]=my_strdup(objectNameArg);
- return maxCounter;
- #else
- return 0; /* Dummy */
- #endif
- }
- void setCounter(int counterId)
- {
- #if WMOPS
- if(counterId>maxCounter || counterId<0)
- {
- currCounter=0;
- return;
- }
- currCounter=counterId;
- #endif
- }
- #if WMOPS
- static Word32 WMOPS_frameStat()
- /* calculate the WMOPS seen so far and update the global
- per-frame maximum (glob_wc)
- */
- {
- Word32 tot;
- tot = TotalWeightedOperation ();
- if (tot > glob_wc[currCounter])
- glob_wc[currCounter] = tot;
- /* check if fwc() was forgotten at end of last frame */
- if (tot > LastWOper[currCounter]) {
- if (!fwc_corr[currCounter]) {
- wfprintf(stderr,
- "count: operations counted after last fwc() for '%s'; "
- "-> fwc() called\n",
- objectName[currCounter]?objectName[currCounter]:"");
- }
- fwc();
- }
-
- return tot;
- }
- static void WMOPS_clearMultiCounter()
- {
- Word16 i;
-
- Word32 *ptr = (Word32 *) &multiCounter[currCounter];
- for (i = 0; i < (sizeof (multiCounter[currCounter])/ sizeof (Word32)); i++)
- {
- *ptr++ = 0;
- }
- }
- #endif
- Word32 TotalWeightedOperation ()
- {
- #if WMOPS
- Word16 i;
- Word32 tot, *ptr, *ptr2;
- tot = 0;
- ptr = (Word32 *) &multiCounter[currCounter];
- ptr2 = (Word32 *) &op_weight;
- for (i = 0; i < (sizeof (multiCounter[currCounter])/ sizeof (Word32)); i++)
- {
- tot += ((*ptr++) * (*ptr2++));
- }
- return ((Word32) tot);
- #else
- return 0; /* Dummy */
- #endif
- }
- Word32 DeltaWeightedOperation ()
- {
- #if WMOPS
- Word32 NewWOper, delta;
- NewWOper = TotalWeightedOperation ();
- delta = NewWOper - LastWOper[currCounter];
- LastWOper[currCounter] = NewWOper;
- return (delta);
- #else
- return 0; /* Dummy */
- #endif
- }
- void move16 (void)
- {
- #if WMOPS
- multiCounter[currCounter].DataMove16++;
- #endif
- }
- void move32 (void)
- {
- #if WMOPS
- multiCounter[currCounter].DataMove32++;
- #endif
- }
- void test (void)
- {
- #if WMOPS
- multiCounter[currCounter].Test++;
- #endif
- }
- void logic16 (void)
- {
- #if WMOPS
- multiCounter[currCounter].Logic16++;
- #endif
- }
- void logic32 (void)
- {
- #if WMOPS
- multiCounter[currCounter].Logic32++;
- #endif
- }
- void Init_WMOPS_counter (void)
- {
- #if WMOPS
- Word16 i;
- /* reset function weight operation counter variable */
- for (i = 0; i < NbFuncMax; i++)
- wc[currCounter][i] = (Word32) 0;
- glob_wc[currCounter] = 0;
- nbframe[currCounter] = 0;
- total_wmops[currCounter] = 0.0;
- /* initially clear all counters */
- WMOPS_clearMultiCounter();
- LastWOper[currCounter] = 0;
- funcid[currCounter] = 0;
- #endif
- }
- void Reset_WMOPS_counter (void)
- {
- #if WMOPS
- Word32 tot = WMOPS_frameStat();
-
- /* increase the frame counter --> a frame is counted WHEN IT BEGINS */
- nbframe[currCounter]++;
- /* add wmops used in last frame to count, then reset counter */
- /* (in first frame, this is a no-op */
- total_wmops[currCounter] += ((float) tot) * 0.00005;
-
- /* clear counter before new frame starts */
- WMOPS_clearMultiCounter();
- LastWOper[currCounter] = 0;
- funcid[currCounter] = 0; /* new frame, set function id to zero */
- #endif
- }
- Word32 fwc (void) /* function worst case */
- {
- #if WMOPS
- Word32 tot;
- tot = DeltaWeightedOperation ();
- if (tot > wc[currCounter][funcid[currCounter]])
- wc[currCounter][funcid[currCounter]] = tot;
- funcid[currCounter]++;
- return (tot);
- #else
- return 0; /* Dummy */
- #endif
- }
- void WMOPS_output (Word16 dtx_mode)
- {
- #if WMOPS
- Word16 i;
- Word32 tot, tot_wm, tot_wc;
- /* get operations since last reset (or init),
- but do not update the counters (except the glob_wc[] maximum)
- so output CAN be called in each frame without problems.
- The frame counter is NOT updated!
- */
- tot = WMOPS_frameStat();
- tot_wm = total_wmops[currCounter] + ((float) tot) * 0.00005;
- wfprintf (stdout, "%10s:WMOPS=%.3f",
- objectName[currCounter]?objectName[currCounter]:"",
- ((float) tot) * 0.00005);
- if (nbframe[currCounter] != 0)
- wfprintf (stdout, " Average=%.3f",
- tot_wm / (float) nbframe[currCounter]);
-
- wfprintf (stdout, " WorstCase=%.3f",
- ((float) glob_wc[currCounter]) * 0.00005);
- /* Worst worst case printed only when not in DTX mode */
- if (dtx_mode == 0)
- {
- tot_wc = 0L;
- for (i = 0; i < funcid[currCounter]; i++)
- tot_wc += wc[currCounter][i];
- wfprintf (stdout, " WorstWC=%.3f", ((float) tot_wc) * 0.00005);
- }
- wfprintf (stdout, " (%d frames)\n", nbframe[currCounter]);
-
- #endif
- }
|