/*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/* Nonsense version of escape-time algorithm for calculating a mandelbrot55* set. Just for benchmarking. */56static void57color_point(mpd_t *x0, mpd_t *y0, long maxiter, mpd_context_t *ctx)58{59mpd_t *x, *y, *sq_x, *sq_y;60mpd_t *two;6162x = new_mpd();63y = new_mpd();64mpd_set_u32(x, 0, ctx);65mpd_set_u32(y, 0, ctx);6667sq_x = new_mpd();68sq_y = new_mpd();69mpd_set_u32(sq_x, 0, ctx);70mpd_set_u32(sq_y, 0, ctx);7172two = new_mpd();73mpd_set_u32(two, 2, ctx);7475for (long i = 0; i < maxiter; i++) {76mpd_mul(y, x, y, ctx);77mpd_mul(y, y, two, ctx);78mpd_add(y, y, y0, ctx);7980mpd_sub(x, sq_x, sq_y, ctx);81mpd_add(x, x, x0, ctx);8283mpd_mul(sq_x, x, x, ctx);84mpd_mul(sq_y, y, y, ctx);85}8687mpd_copy(x0, x, ctx);8889mpd_del(two);90mpd_del(sq_y);91mpd_del(sq_x);92mpd_del(y);93mpd_del(x);94}959697int98main(int argc, char **argv)99{100mpd_context_t ctx;101mpd_t *x0, *y0;102uint32_t prec = 19;103long iter = 10000000;104clock_t start_clock, end_clock;105106if (argc != 3) {107err_exit("usage: bench prec iter\n");108}109prec = strtoul(argv[1], NULL, 10);110iter = strtol(argv[2], NULL, 10);111112mpd_init(&ctx, prec);113/* no more MPD_MINALLOC changes after here */114115x0 = new_mpd();116y0 = new_mpd();117mpd_set_string(x0, "0.222", &ctx);118mpd_set_string(y0, "0.333", &ctx);119if (ctx.status & MPD_Errors) {120mpd_del(y0);121mpd_del(x0);122err_exit("unexpected error during conversion");123}124125start_clock = clock();126color_point(x0, y0, iter, &ctx);127end_clock = clock();128129mpd_print(x0);130fprintf(stderr, "time: %f\n\n", (double)(end_clock-start_clock)/(double)CLOCKS_PER_SEC);131132mpd_del(y0);133mpd_del(x0);134135return 0;136}137138139