Path: blob/main/Modules/_decimal/libmpdec/bench_full.c
12 views
/*1* Copyright (c) 2008-2020 Stefan Krah. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/262728#include "mpdecimal.h"2930#include <stdint.h>31#include <stdio.h>32#include <stdlib.h>33#include <time.h>343536static void37err_exit(const char *msg)38{39fprintf(stderr, "%s\n", msg);40exit(1);41}4243static mpd_t *44new_mpd(void)45{46mpd_t *x = mpd_qnew();47if (x == NULL) {48err_exit("out of memory");49}5051return x;52}5354/*55* Example from: http://en.wikipedia.org/wiki/Mandelbrot_set56*57* Escape time algorithm for drawing the set:58*59* Point x0, y0 is deemed to be in the Mandelbrot set if the return60* value is maxiter. Lower return values indicate how quickly points61* escaped and can be used for coloring.62*/63static int64color_point(const mpd_t *x0, const mpd_t *y0, const long maxiter, mpd_context_t *ctx)65{66mpd_t *x, *y, *sq_x, *sq_y;67mpd_t *two, *four, *c;68long i;6970x = new_mpd();71y = new_mpd();72mpd_set_u32(x, 0, ctx);73mpd_set_u32(y, 0, ctx);7475sq_x = new_mpd();76sq_y = new_mpd();77mpd_set_u32(sq_x, 0, ctx);78mpd_set_u32(sq_y, 0, ctx);7980two = new_mpd();81four = new_mpd();82mpd_set_u32(two, 2, ctx);83mpd_set_u32(four, 4, ctx);8485c = new_mpd();86mpd_set_u32(c, 0, ctx);8788for (i = 0; i < maxiter && mpd_cmp(c, four, ctx) <= 0; i++) {89mpd_mul(y, x, y, ctx);90mpd_mul(y, y, two, ctx);91mpd_add(y, y, y0, ctx);9293mpd_sub(x, sq_x, sq_y, ctx);94mpd_add(x, x, x0, ctx);9596mpd_mul(sq_x, x, x, ctx);97mpd_mul(sq_y, y, y, ctx);98mpd_add(c, sq_x, sq_y, ctx);99}100101mpd_del(c);102mpd_del(four);103mpd_del(two);104mpd_del(sq_y);105mpd_del(sq_x);106mpd_del(y);107mpd_del(x);108109return i;110}111112int113main(int argc, char **argv)114{115mpd_context_t ctx;116mpd_t *x0, *y0;117mpd_t *sqrt_2, *xstep, *ystep;118mpd_ssize_t prec = 19;119120long iter = 1000;121int points[40][80];122int i, j;123clock_t start_clock, end_clock;124125126if (argc != 3) {127fprintf(stderr, "usage: ./bench prec iter\n");128exit(1);129}130prec = strtoll(argv[1], NULL, 10);131iter = strtol(argv[2], NULL, 10);132133mpd_init(&ctx, prec);134/* no more MPD_MINALLOC changes after here */135136sqrt_2 = new_mpd();137xstep = new_mpd();138ystep = new_mpd();139x0 = new_mpd();140y0 = new_mpd();141142mpd_set_u32(sqrt_2, 2, &ctx);143mpd_sqrt(sqrt_2, sqrt_2, &ctx);144mpd_div_u32(xstep, sqrt_2, 40, &ctx);145mpd_div_u32(ystep, sqrt_2, 20, &ctx);146147start_clock = clock();148mpd_copy(y0, sqrt_2, &ctx);149for (i = 0; i < 40; i++) {150mpd_copy(x0, sqrt_2, &ctx);151mpd_set_negative(x0);152for (j = 0; j < 80; j++) {153points[i][j] = color_point(x0, y0, iter, &ctx);154mpd_add(x0, x0, xstep, &ctx);155}156mpd_sub(y0, y0, ystep, &ctx);157}158end_clock = clock();159160#ifdef BENCH_VERBOSE161for (i = 0; i < 40; i++) {162for (j = 0; j < 80; j++) {163if (points[i][j] == iter) {164putchar('*');165}166else if (points[i][j] >= 10) {167putchar('+');168}169else if (points[i][j] >= 5) {170putchar('.');171}172else {173putchar(' ');174}175}176putchar('\n');177}178putchar('\n');179#else180(void)points; /* suppress gcc warning */181#endif182183printf("time: %f\n\n", (double)(end_clock-start_clock)/(double)CLOCKS_PER_SEC);184185mpd_del(y0);186mpd_del(x0);187mpd_del(ystep);188mpd_del(xstep);189mpd_del(sqrt_2);190191return 0;192}193194195