modernc.org/ccgo/v3@v3.16.14/lib/testdata/CompCert-3.6/test/c/fannkuch.c (about) 1 /* 2 * The Computer Lannguage Shootout 3 * http://shootout.alioth.debian.org/ 4 * Contributed by Heiner Marxen 5 * 6 * "fannkuch" for C gcc 7 * 8 * $Id: fannkuch-gcc.code,v 1.33 2006/02/25 16:38:58 igouy-guest Exp $ 9 */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 14 #define Int int 15 #define Aint int 16 17 static long 18 fannkuch( int n ) 19 { 20 Aint* perm; 21 Aint* perm1; 22 Aint* count; 23 long flips; 24 long flipsMax; 25 Int r; 26 Int i; 27 Int k; 28 Int didpr; 29 const Int n1 = n - 1; 30 31 if( n < 1 ) return 0; 32 33 perm = calloc(n, sizeof(*perm )); 34 perm1 = calloc(n, sizeof(*perm1)); 35 count = calloc(n, sizeof(*count)); 36 37 for( i=0 ; i<n ; ++i ) perm1[i] = i; /* initial (trivial) permu */ 38 39 r = n; didpr = 0; flipsMax = 0; 40 for(;;) { 41 if( didpr < 30 ) { 42 for( i=0 ; i<n ; ++i ) printf("%d", (int)(1+perm1[i])); 43 printf("\n"); 44 ++didpr; 45 } 46 for( ; r!=1 ; --r ) { 47 count[r-1] = r; 48 } 49 50 #define XCH(x,y) { Aint t_mp; t_mp=(x); (x)=(y); (y)=t_mp; } 51 52 if( ! (perm1[0]==0 || perm1[n1]==n1) ) { 53 flips = 0; 54 for( i=1 ; i<n ; ++i ) { /* perm = perm1 */ 55 perm[i] = perm1[i]; 56 } 57 k = perm1[0]; /* cache perm[0] in k */ 58 do { /* k!=0 ==> k>0 */ 59 Int j; 60 for( i=1, j=k-1 ; i<j ; ++i, --j ) { 61 XCH(perm[i], perm[j]) 62 } 63 ++flips; 64 /* 65 * Now exchange k (caching perm[0]) and perm[k]... with care! 66 * XCH(k, perm[k]) does NOT work! 67 */ 68 j=perm[k]; perm[k]=k ; k=j; 69 }while( k ); 70 if( flipsMax < flips ) { 71 flipsMax = flips; 72 } 73 } 74 75 for(;;) { 76 if( r == n ) { 77 return flipsMax; 78 } 79 /* rotate down perm[0..r] by one */ 80 { 81 Int perm0 = perm1[0]; 82 i = 0; 83 while( i < r ) { 84 k = i+1; 85 perm1[i] = perm1[k]; 86 i = k; 87 } 88 perm1[r] = perm0; 89 } 90 if( (count[r] -= 1) > 0 ) { 91 break; 92 } 93 ++r; 94 } 95 } 96 } 97 98 int 99 main( int argc, char* argv[] ) 100 { 101 int n = (argc>1) ? atoi(argv[1]) : 10; 102 103 printf("Pfannkuchen(%d) = %ld\n", n, fannkuch(n)); 104 return 0; 105 } 106 /***** 107 build & benchmark results 108 109 BUILD COMMANDS FOR: fannkuch.gcc 110 111 Thu Sep 14 17:44:44 PDT 2006 112 113 /usr/bin/gcc -pipe -Wall -O3 -fomit-frame-pointer -funroll-loops -march=pentium4 fannkuch.c -o fannkuch.gcc_run 114 115 ================================================================= 116 COMMAND LINE (%A is single numeric argument): 117 118 fannkuch.gcc_run %A 119 N=10 120 121 PROGRAM OUTPUT 122 ============== 123 12345678910 124 21345678910 125 23145678910 126 32145678910 127 31245678910 128 13245678910 129 23415678910 130 32415678910 131 34215678910 132 43215678910 133 42315678910 134 24315678910 135 34125678910 136 43125678910 137 41325678910 138 14325678910 139 13425678910 140 31425678910 141 41235678910 142 14235678910 143 12435678910 144 21435678910 145 24135678910 146 42135678910 147 23451678910 148 32451678910 149 34251678910 150 43251678910 151 42351678910 152 24351678910 153 Pfannkuchen(10) = 38 154 *****/