Blame SOURCES/papi-postfixcalc.patch

c7ea89
diff -up papi-5.2.0/src/papi_internal.c.postfix papi-5.2.0/src/papi_internal.c
c7ea89
--- papi-5.2.0/src/papi_internal.c.postfix	2016-07-25 14:25:09.331487358 -0400
c7ea89
+++ papi-5.2.0/src/papi_internal.c	2016-07-25 14:27:17.271715363 -0400
c7ea89
@@ -32,6 +32,7 @@
c7ea89
 #include <errno.h>
c7ea89
 #include <string.h>
c7ea89
 #include <ctype.h>
c7ea89
+#include <assert.h>
c7ea89
 
c7ea89
 #include "papi.h"
c7ea89
 #include "papi_internal.h"
c7ea89
@@ -1840,89 +1841,90 @@ handle_derived_add_ps( int *position, lo
c7ea89
 /* this function implement postfix calculation, it reads in a string where I use:
c7ea89
       |      as delimiter
c7ea89
       N2     indicate No. 2 native event in the derived preset
c7ea89
-      +, -, *, /, %  as operator
c7ea89
+      +, -, *, /  as operator
c7ea89
       #      as MHZ(million hz) got from  _papi_hwi_system_info.hw_info.cpu_max_mhz*1000000.0
c7ea89
 
c7ea89
   Haihang (you@cs.utk.edu)
c7ea89
 */ 
c7ea89
-static long long
c7ea89
-_papi_hwi_postfix_calc( EventInfo_t * evi, long long *hw_counter )
c7ea89
-{
c7ea89
-	char *point = evi->ops, operand[16];
c7ea89
-	double stack[PAPI_EVENTS_IN_DERIVED_EVENT];
c7ea89
-	int i, top = 0;
c7ea89
-
c7ea89
-	memset(&stack,0,PAPI_EVENTS_IN_DERIVED_EVENT*sizeof(double));
c7ea89
-
c7ea89
-	while ( *point != '\0' ) {
c7ea89
-		if ( *point == 'N' ) {	/* to get count for each native event */
c7ea89
-			i = 0;
c7ea89
-			point++;
c7ea89
-			do {
c7ea89
-				operand[i] = *point;
c7ea89
-				point++;
c7ea89
-				i++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			operand[i] = '\0';
c7ea89
-			stack[top] = ( double ) hw_counter[evi->pos[atoi( operand )]];
c7ea89
-			top++;
c7ea89
-			point++;
c7ea89
-		} else if ( *point == '#' ) {	/* to get mhz, ignore the rest char's */
c7ea89
-			stack[top] = _papi_hwi_system_info.hw_info.cpu_max_mhz * 1000000.0;
c7ea89
-			top++;
c7ea89
-			do {
c7ea89
-				point++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			point++;
c7ea89
-		} else if ( isdigit( *point ) ) {	/* to get integer, I suppose only integer will be used, 
c7ea89
-											   no error check here, please only use integer */
c7ea89
-			i = 0;
c7ea89
-			do {
c7ea89
-				operand[i] = *point;
c7ea89
-				point++;
c7ea89
-				i++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			operand[i] = '\0';
c7ea89
-			stack[top] = atoi( operand );
c7ea89
-			top++;
c7ea89
-			point++;
c7ea89
-		} else if ( *point == '+' ) {	/* + calculation */
c7ea89
-			stack[top - 2] += stack[top - 1];
c7ea89
-			top--;
c7ea89
-			do {
c7ea89
-				point++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			point++;
c7ea89
-		} else if ( *point == '-' ) {	/* - calculation */
c7ea89
-			stack[top - 2] -= stack[top - 1];
c7ea89
-			top--;
c7ea89
-			do {
c7ea89
-				point++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			point++;
c7ea89
-		} else if ( *point == '*' ) {	/* * calculation */
c7ea89
-			stack[top - 2] *= stack[top - 1];
c7ea89
-			top--;
c7ea89
-			do {
c7ea89
-				point++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			point++;
c7ea89
-		} else if ( *point == '/' ) {	/* / calculation */
c7ea89
-			stack[top - 2] /= stack[top - 1];
c7ea89
-			top--;
c7ea89
-			do {
c7ea89
-				point++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			point++;
c7ea89
-		} else {			 /* do nothing */
c7ea89
-			do {
c7ea89
-				point++;
c7ea89
-			} while ( *point != '|' );
c7ea89
-			point++;
c7ea89
-		}
c7ea89
-	}
c7ea89
-	return ( long long ) stack[0];
c7ea89
-}
c7ea89
+ static long long
c7ea89
+ _papi_hwi_postfix_calc( EventInfo_t * evi, long long *hw_counter )
c7ea89
+ {
c7ea89
+        char *point = evi->ops, operand[16];
c7ea89
+        double stack[PAPI_EVENTS_IN_DERIVED_EVENT];
c7ea89
+       int i, val, top = 0;
c7ea89
+
c7ea89
+       INTDBG("ENTER: evi: %p, evi->ops: %p (%s), evi->pos[0]: %d, evi->pos[1]: %d, hw_counter: %p (%lld %lld)\n",
c7ea89
+              evi, evi->ops, evi->ops, evi->pos[0], evi->pos[1], hw_counter, hw_counter[0], hw_counter[1]);
c7ea89
+
c7ea89
+        memset(&stack,0,PAPI_EVENTS_IN_DERIVED_EVENT*sizeof(double));
c7ea89
+
c7ea89
+        while ( *point != '\0' ) {
c7ea89
+               if ( *point == '|' ) {  /* consume '|' characters */
c7ea89
+                        point++;
c7ea89
+                } else if ( *point == 'N' ) {   /* to get count for each native event */
c7ea89
+                        point++;
c7ea89
+                       i = 0;
c7ea89
+                       while ( isdigit(*point) ) {
c7ea89
+                               assert(i<16);
c7ea89
+                                operand[i] = *point;
c7ea89
+                                point++;
c7ea89
+                                i++;
c7ea89
+                       }
c7ea89
+                       assert(0
c7ea89
+                        operand[i] = '\0';
c7ea89
+                       val = atoi( operand );
c7ea89
+                       assert( top < PAPI_EVENTS_IN_DERIVED_EVENT );
c7ea89
+                       assert( 0 <= val && val < PAPI_EVENTS_IN_DERIVED_EVENT );
c7ea89
+                       stack[top] = ( double ) hw_counter[evi->pos[val]];
c7ea89
+                        top++;
c7ea89
+               } else if ( *point == '#' ) {   /* to get mhz */
c7ea89
+                        point++;
c7ea89
+                       assert( top < PAPI_EVENTS_IN_DERIVED_EVENT );
c7ea89
+                        stack[top] = _papi_hwi_system_info.hw_info.cpu_max_mhz * 1000000.0;
c7ea89
+                        top++;
c7ea89
+               } else if ( isdigit( *point ) ) {
c7ea89
+                        i = 0;
c7ea89
+                       while ( isdigit(*point) ) {
c7ea89
+                               assert(i<16);
c7ea89
+                                operand[i] = *point;
c7ea89
+                                point++;
c7ea89
+                                i++;
c7ea89
+                       }
c7ea89
+                       assert(0
c7ea89
+                        operand[i] = '\0';
c7ea89
+                       assert( top < PAPI_EVENTS_IN_DERIVED_EVENT );
c7ea89
+                        stack[top] = atoi( operand );
c7ea89
+                        top++;
c7ea89
+                } else if ( *point == '+' ) {   /* + calculation */
c7ea89
+                       point++;
c7ea89
+                       assert(top >= 2);
c7ea89
+                        stack[top - 2] += stack[top - 1];
c7ea89
+                        top--;
c7ea89
+                } else if ( *point == '-' ) {   /* - calculation */
c7ea89
+                       point++;
c7ea89
+                       assert(top >= 2);
c7ea89
+                        stack[top - 2] -= stack[top - 1];
c7ea89
+                        top--;
c7ea89
+                } else if ( *point == '*' ) {   /* * calculation */
c7ea89
+                       point++;
c7ea89
+                       assert(top >= 2);
c7ea89
+                        stack[top - 2] *= stack[top - 1];
c7ea89
+                        top--;
c7ea89
+                } else if ( *point == '/' ) {   /* / calculation */
c7ea89
+                       point++;
c7ea89
+                       assert(top >= 2);
c7ea89
+                       /* FIXME should handle runtime divide by zero */
c7ea89
+                        stack[top - 2] /= stack[top - 1];
c7ea89
+                        top--;
c7ea89
+               } else { /* flag an error parsing the preset */
c7ea89
+                       PAPIERROR( "BUG! Unable to parse \"%s\"", evi->ops );
c7ea89
+                       return ( long long ) stack[0];
c7ea89
+                }
c7ea89
+        }
c7ea89
+        assert(top == 1);
c7ea89
+        INTDBG("EXIT: stack[0]: %lld\n", (long long)stack[0]);
c7ea89
+        return ( long long ) stack[0];
c7ea89
+ }
c7ea89
 
c7ea89
 static long long
c7ea89
 handle_derived( EventInfo_t * evi, long long *from )