]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/go.test/test/bench/fannkuch.c
2 Redistribution and use in source and binary forms, with or without
3 modification, are permitted provided that the following conditions are met:
5 * Redistributions of source code must retain the above copyright
6 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
12 * Neither the name of "The Computer Language Benchmarks Game" nor the
13 name of "The Computer Language Shootout Benchmarks" nor the names of
14 its contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
31 * The Computer Language Shootout
32 * http://shootout.alioth.debian.org/
33 * Contributed by Heiner Marxen
35 * "fannkuch" for C gcc
37 * $Id: fannkuch.1.gcc.code,v 1.15 2009-04-28 15:39:31 igouy-guest Exp $
62 perm
= calloc(n
, sizeof(*perm
));
63 perm1
= calloc(n
, sizeof(*perm1
));
64 count
= calloc(n
, sizeof(*count
));
66 for( i
=0 ; i
<n
; ++i
) perm1
[i
] = i
; /* initial (trivial) permu */
68 r
= n
; didpr
= 0; flipsMax
= 0;
71 for( i
=0 ; i
<n
; ++i
) printf("%d", (int)(1+perm1
[i
]));
79 #define XCH(x,y) { Aint t_mp; t_mp=(x); (x)=(y); (y)=t_mp; }
81 if( ! (perm1
[0]==0 || perm1
[n1
]==n1
) ) {
83 for( i
=1 ; i
<n
; ++i
) { /* perm = perm1 */
86 k
= perm1
[0]; /* cache perm[0] in k */
87 do { /* k!=0 ==> k>0 */
89 for( i
=1, j
=k
-1 ; i
<j
; ++i
, --j
) {
94 * Now exchange k (caching perm[0]) and perm[k]... with care!
95 * XCH(k, perm[k]) does NOT work!
97 j
=perm
[k
]; perm
[k
]=k
; k
=j
;
99 if( flipsMax
< flips
) {
108 /* rotate down perm[0..r] by one */
110 Int perm0
= perm1
[0];
119 if( (count
[r
] -= 1) > 0 ) {
128 main( int argc
, char* argv
[] )
130 int n
= (argc
>1) ? atoi(argv
[1]) : 0;
132 printf("Pfannkuchen(%d) = %ld\n", n
, fannkuch(n
));