Changeset 626 for trunk/user/pgcd
- Timestamp:
- Apr 29, 2019, 7:25:09 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/user/pgcd/pgcd.c
r625 r626 12 12 #include <almosmkh.h> 13 13 14 #define INSTRUMENTATION 0 15 #define IDBG 0 16 14 17 ///////////////// 15 18 void main( void ) … … 17 20 int opx; 18 21 int opy; 22 int x; 23 int y; 19 24 unsigned long long cycle; 20 25 unsigned int cxy; … … 24 29 get_core( &cxy , &lid ); 25 30 26 printf( "\n \n[pgcd] starts on core[%x,%d] / cycle %d\n",31 printf( "\n[pgcd] starts on core[%x,%d] / cycle %d\n\n", 27 32 cxy , lid , (unsigned int)cycle ); 28 33 29 while (1) 34 // get operand X 35 printf("operand X = "); 36 opx = get_uint32(); 37 printf("\n"); 38 39 // get operand Y 40 printf("operand Y = "); 41 opy = get_uint32(); 42 printf("\n"); 43 44 // check operands 45 if( (opx == 0) || (opy == 0) ) 30 46 { 31 printf("\n*******************\n"); 32 printf("operand X = "); 33 opx = get_uint32(); 34 printf("\n"); 35 printf("operand Y = "); 36 opy = get_uint32(); 37 printf("\n"); 47 printf("\n[pgcd error] operands must be strictly positive\n"); 48 exit(0); 49 } 38 50 39 if( (opx == 0) || (opy == 0) ) 40 { 41 printf("operands must be positive and larger than 0 => exit\n"); 42 exit( 0 ); 43 } 44 else 45 { 46 while (opx != opy) 47 { 48 if(opx > opy) opx = opx - opy; 49 else opy = opy - opx; 50 } 51 printf("pgcd = %d", opx); 52 } 51 // compute PGCD 52 x = opx; 53 y = opy; 54 while (x != y) 55 { 56 if(x > y) x = x - y; 57 else y = y - x; 53 58 } 59 60 // display result 61 printf("pgcd = %d\n", x); 62 63 #if INSTRUMENTATION 64 65 char name[64]; 66 char path[128]; 67 68 // build a file name from X and Y values 69 snprintf( name , 64 , "pgcd_%d_%d", opx, opy ); 70 71 // build file pathname 72 snprintf( path , 128 , "home/%s" , name ); 73 74 #if IDBG 75 idbg(); 76 #endif 77 78 // open file 79 FILE * stream = fopen( path , NULL ); 80 81 if( stream == NULL ) 82 { 83 printf("\n[pgcd error] cannot open instrumentation file <%s>\n", name ); 84 exit(0); 85 } 86 87 printf("\n[pgcd] file %s successfully open\n", path); 88 89 #if IDBG 90 idbg(); 91 #endif 92 93 // register results to file 94 int ret = fprintf( stream , "pgcd( %d , %d ) = %d\n", opx, opy, x ); 95 96 if( ret < 0 ) 97 { 98 printf("\n[pgcd error] cannot write to instrumentation file <%s>\n", name ); 99 exit(0); 100 } 101 102 printf("\n[pgcd] file %s successfully written\n", path); 103 104 display_mapper( path , 0 , 64 ); 105 106 // close instrumentation file 107 108 #if IDBG 109 idbg(); 110 #endif 111 112 if( fclose( stream ) ) 113 { 114 printf("\n[pgcd error] cannot close the file <%s>\n", name ); 115 exit(0); 116 } 117 118 printf("\n[pgcd] file %s successfully closed\n", path); 119 120 #endif 121 122 exit(0); 123 54 124 } // end pgcd 55 125
Note: See TracChangeset
for help on using the changeset viewer.