summaryrefslogtreecommitdiffstats
path: root/pi_bbp.c
blob: 40c1ca7bd3a10392fdf5c59cc37665d377045056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * http://www.cut-the-knot.org/Curriculum/Algorithms/SpigotForPi.shtml
 *
 * Original code is available in public domain
 * Pi Decimal Point Calculation using BBP (named after Bailey-Borwein-Plouffe)
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
#include <time.h>
#include <sys/time.h>

/* globals */
int printed = 0;
int line = 1;
int mulof2 = 1;

/* function prototypes */
inline void out_dig(long unsigned, struct timeval *);
inline void printTime(struct timeval *);
int do_calc(char **);

inline void out_dig(long unsigned dig, struct timeval *before)
{
    putchar(dig);
    printed++;

    if ((printed % 50) == 0)
    {
        if ((line % (20 * mulof2)) == 0)
        {
            printf("\n%dK: ", mulof2);
            mulof2 *= 2;

            printTime(before);
        }

        printf("\nd %06d:   ", 50 * line);
        line++;
    }
    else if ((printed % 10) == 0)
        putchar(' ');
}

inline void printTime(struct timeval *before)
{
    struct timeval after;
    gettimeofday(&after, 0);
    long unsigned int elapsed_ms = (after.tv_sec - before->tv_sec) * 1000 +
                                   (after.tv_usec - before->tv_usec) / 1000;
    unsigned int seconds_n = elapsed_ms / 1000;
    unsigned int milliseconds_n = elapsed_ms % 1000;

    printf("time elapsed: %u.%u seconds\n", seconds_n, milliseconds_n);
}

int do_calc(char **argv)
{
    long unsigned int i, j, nines, predigit;
    long unsigned int q, x, digits_n, len;
    long unsigned int *pi;
    long unsigned int alloc;

    digits_n = atol(argv[1]);
    if (digits_n < 1)
        return -1;

    len = (digits_n * 10) / 3;
    alloc = sizeof(long unsigned int) * (len+1);

    pi = (long unsigned int *) malloc(alloc);
    if (!pi)
    {
        perror("malloc");
        exit(-1);
    }
    else
        printf("\n\tPi Decimal Digit Calculation\n"
               "allocated %ldKb of heap memory\n", alloc / 1024);

    for (i = 0; i < len; i++)
        pi[i] = 2;

    /* wchar_t pi_c = 0x03c0; */
    printf("Pi value: 3.");

    nines = 0;
    predigit = 0;

    struct timeval before;
    gettimeofday(&before, 0);

    for (j = 0; j <= digits_n; j++)
    {
        q = 0;

        for (i = len; i > 0; i--)
        {
            x = 10 * pi[i] + q * i;
            pi[i] = x % (2 * i - 1);
            q = x / (2 * i - 1);
        }

        pi[1] = q % 10;
        q = q / 10;

        if (q == 9)
            nines++;
        else if (q == 10)
        {
            out_dig('1' + predigit, &before);

            while (nines)
            {
                out_dig('0', &before);
                nines--;
            }

            predigit = 0;
            fflush(stdout);
        }
        else
        {
            if (j > 1)
                out_dig('0' + predigit, &before);

            while (nines)
            {
                out_dig('9', &before);
                nines--;
            }

            predigit = q;
            fflush(stdout);
        }
    }

    out_dig(predigit + '0', &before);
    puts("");
    free(pi);

    struct timeval after;
    gettimeofday(&after, 0);
    long unsigned int elapsed_ms = (after.tv_sec - before.tv_sec) * 1000 +
                                   (after.tv_usec - before.tv_usec) / 1000;
    unsigned int seconds_n = elapsed_ms / 1000;
    unsigned int milliseconds_n = elapsed_ms % 1000;

    printf("\nPi calculation to %lu'th decimal point finished\n"
           "\tTotal Time: %u.%u seconds\n", digits_n, seconds_n, milliseconds_n);

    return 1;
}

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        fprintf(stderr, "%s <number of decimal digits to compute>\n", argv[0]);
        return 1;
    }

    if (!do_calc(argv))
        printf("do_calc() failed to execute\n");

    return 0;
}