OSDN Git Service

Merge branch 'trunk' of git://gcc.gnu.org/git/gcc into rework
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / bench / pidigits.c
1 /*
2 Redistribution and use in source and binary forms, with or without
3 modification, are permitted provided that the following conditions are met:
4
5     * Redistributions of source code must retain the above copyright
6     notice, this list of conditions and the following disclaimer.
7
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.
11
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.
16
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.
28 */
29
30 /* The Computer Language Benchmarks Game
31   http://shootout.alioth.debian.org/
32
33   contributed by Paolo Bonzini & Sean Bartlett
34   modified by Michael Mellor
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <gmp.h>
40
41 static mpz_t numer, accum, denom, tmp1, tmp2;
42
43 static int extract_digit()
44 {
45   if (mpz_cmp(numer, accum) > 0)
46     return -1;
47
48   /* Compute (numer * 3 + accum) / denom */
49   mpz_mul_2exp(tmp1, numer, 1);
50   mpz_add(tmp1, tmp1, numer);
51   mpz_add(tmp1, tmp1, accum);
52   mpz_fdiv_qr(tmp1, tmp2, tmp1, denom);
53
54   /* Now, if (numer * 4 + accum) % denom... */
55   mpz_add(tmp2, tmp2, numer);
56
57   /* ... is normalized, then the two divisions have the same result.  */
58   if (mpz_cmp(tmp2, denom) >= 0)
59     return -1;
60
61   return mpz_get_ui(tmp1);
62 }
63
64 static void next_term(unsigned int k)
65 {
66   unsigned int y2 = k*2 + 1;
67
68   mpz_mul_2exp(tmp1, numer, 1);
69   mpz_add(accum, accum, tmp1);
70   mpz_mul_ui(accum, accum, y2);
71   mpz_mul_ui(numer, numer, k);
72   mpz_mul_ui(denom, denom, y2);
73 }
74
75 static void eliminate_digit(unsigned int d)
76 {
77   mpz_submul_ui(accum, denom, d);
78   mpz_mul_ui(accum, accum, 10);
79   mpz_mul_ui(numer, numer, 10);
80 }
81
82 static void pidigits(unsigned int n)
83 {
84   int d;
85   unsigned int i = 0, k = 0, m;
86   mpz_init(tmp1);
87   mpz_init(tmp2);
88   mpz_init_set_ui(numer, 1);
89   mpz_init_set_ui(accum, 0);
90   mpz_init_set_ui(denom, 1);
91
92   for(;;)
93   {
94     do {
95       k++;
96       next_term(k);
97       d = extract_digit();
98     } while(d == -1);
99
100     putchar(d + '0');
101
102     i++;
103     m = i%10;
104     if(m == 0)
105       printf("\t:%d\n", i);
106     if(i >= n)
107       break;
108     eliminate_digit(d);
109   }
110
111   if(m) {
112     m = 10 - m;
113     while(m--)
114       putchar(' ');
115     printf("\t:%d\n", n);
116   }
117 }
118
119 int main(int argc, char **argv)
120 {
121   pidigits(argc > 1 ? atoi(argv[1]) : 27);
122   return 0;
123 }