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