• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008-2020 Stefan Krah. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 
29 #include "mpdecimal.h"
30 
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 
36 
37 static void
err_exit(const char * msg)38 err_exit(const char *msg)
39 {
40     fprintf(stderr, "%s\n", msg);
41     exit(1);
42 }
43 
44 static mpd_t *
new_mpd(void)45 new_mpd(void)
46 {
47     mpd_t *x = mpd_qnew();
48     if (x == NULL) {
49         err_exit("out of memory");
50     }
51 
52     return x;
53 }
54 
55 /*
56  * Example from: http://en.wikipedia.org/wiki/Mandelbrot_set
57  *
58  * Escape time algorithm for drawing the set:
59  *
60  * Point x0, y0 is deemed to be in the Mandelbrot set if the return
61  * value is maxiter. Lower return values indicate how quickly points
62  * escaped and can be used for coloring.
63  */
64 static int
color_point(const mpd_t * x0,const mpd_t * y0,const long maxiter,mpd_context_t * ctx)65 color_point(const mpd_t *x0, const mpd_t *y0, const long maxiter, mpd_context_t *ctx)
66 {
67     mpd_t *x, *y, *sq_x, *sq_y;
68     mpd_t *two, *four, *c;
69     long i;
70 
71     x = new_mpd();
72     y = new_mpd();
73     mpd_set_u32(x, 0, ctx);
74     mpd_set_u32(y, 0, ctx);
75 
76     sq_x = new_mpd();
77     sq_y = new_mpd();
78     mpd_set_u32(sq_x, 0, ctx);
79     mpd_set_u32(sq_y, 0, ctx);
80 
81     two = new_mpd();
82     four = new_mpd();
83     mpd_set_u32(two, 2, ctx);
84     mpd_set_u32(four, 4, ctx);
85 
86     c = new_mpd();
87     mpd_set_u32(c, 0, ctx);
88 
89     for (i = 0; i < maxiter && mpd_cmp(c, four, ctx) <= 0; i++) {
90         mpd_mul(y, x, y, ctx);
91         mpd_mul(y, y, two, ctx);
92         mpd_add(y, y, y0, ctx);
93 
94         mpd_sub(x, sq_x, sq_y, ctx);
95         mpd_add(x, x, x0, ctx);
96 
97         mpd_mul(sq_x, x, x, ctx);
98         mpd_mul(sq_y, y, y, ctx);
99         mpd_add(c, sq_x, sq_y, ctx);
100     }
101 
102     mpd_del(c);
103     mpd_del(four);
104     mpd_del(two);
105     mpd_del(sq_y);
106     mpd_del(sq_x);
107     mpd_del(y);
108     mpd_del(x);
109 
110     return i;
111 }
112 
113 int
main(int argc,char ** argv)114 main(int argc, char **argv)
115 {
116     mpd_context_t ctx;
117     mpd_t *x0, *y0;
118     mpd_t *sqrt_2, *xstep, *ystep;
119     mpd_ssize_t prec = 19;
120 
121     long iter = 1000;
122     int points[40][80];
123     int i, j;
124     clock_t start_clock, end_clock;
125 
126 
127     if (argc != 3) {
128         fprintf(stderr, "usage: ./bench prec iter\n");
129         exit(1);
130     }
131     prec = strtoll(argv[1], NULL, 10);
132     iter = strtol(argv[2], NULL, 10);
133 
134     mpd_init(&ctx, prec);
135     /* no more MPD_MINALLOC changes after here */
136 
137     sqrt_2 = new_mpd();
138     xstep = new_mpd();
139     ystep = new_mpd();
140     x0 = new_mpd();
141     y0 = new_mpd();
142 
143     mpd_set_u32(sqrt_2, 2, &ctx);
144     mpd_sqrt(sqrt_2, sqrt_2, &ctx);
145     mpd_div_u32(xstep, sqrt_2, 40, &ctx);
146     mpd_div_u32(ystep, sqrt_2, 20, &ctx);
147 
148     start_clock = clock();
149     mpd_copy(y0, sqrt_2, &ctx);
150     for (i = 0; i < 40; i++) {
151         mpd_copy(x0, sqrt_2, &ctx);
152         mpd_set_negative(x0);
153         for (j = 0; j < 80; j++) {
154             points[i][j] = color_point(x0, y0, iter, &ctx);
155             mpd_add(x0, x0, xstep, &ctx);
156         }
157         mpd_sub(y0, y0, ystep, &ctx);
158     }
159     end_clock = clock();
160 
161 #ifdef BENCH_VERBOSE
162     for (i = 0; i < 40; i++) {
163         for (j = 0; j < 80; j++) {
164             if (points[i][j] == iter) {
165                 putchar('*');
166             }
167             else if (points[i][j] >= 10) {
168                 putchar('+');
169             }
170             else if (points[i][j] >= 5) {
171                 putchar('.');
172             }
173             else {
174                 putchar(' ');
175             }
176         }
177         putchar('\n');
178     }
179     putchar('\n');
180 #else
181     (void)points; /* suppress gcc warning */
182 #endif
183 
184     printf("time: %f\n\n", (double)(end_clock-start_clock)/(double)CLOCKS_PER_SEC);
185 
186     mpd_del(y0);
187     mpd_del(x0);
188     mpd_del(ystep);
189     mpd_del(xstep);
190     mpd_del(sqrt_2);
191 
192     return 0;
193 }
194