int main(int argc, char **argv) { mpd_context_t ctx; mpd_t *x0, *y0; mpd_t *sqrt_2, *xstep, *ystep; uint32_t prec = 19; int iter = 1000; int points[40][80]; int i, j; clock_t start_clock, end_clock; if (argc != 3) { fprintf(stderr, "usage: ./bench prec iter\n"); exit(1); } prec = strtoul(argv[1], NULL, 10); iter = strtol(argv[2], NULL, 10); mpd_init(&ctx, prec); /* no more MPD_MINALLOC changes after here */ sqrt_2 = mpd_new(&ctx); xstep = mpd_new(&ctx); ystep = mpd_new(&ctx); x0 = mpd_new(&ctx); y0 = mpd_new(&ctx); mpd_set_u32(sqrt_2, 2, &ctx); mpd_sqrt(sqrt_2, sqrt_2, &ctx); mpd_div_u32(xstep, sqrt_2, 40, &ctx); mpd_div_u32(ystep, sqrt_2, 20, &ctx); start_clock = clock(); mpd_copy(y0, sqrt_2, &ctx); for (i = 0; i < 40; i++) { mpd_copy(x0, sqrt_2, &ctx); mpd_set_negative(x0); for (j = 0; j < 80; j++) { points[i][j] = color_point(x0, y0, iter, &ctx); mpd_add(x0, x0, xstep, &ctx); } mpd_sub(y0, y0, ystep, &ctx); } end_clock = clock(); #ifdef BENCH_VERBOSE for (i = 0; i < 40; i++) { for (j = 0; j < 80; j++) { if (points[i][j] == iter) { putchar('*'); } else if (points[i][j] >= 10) { putchar('+'); } else if (points[i][j] >= 5) { putchar('.'); } else { putchar(' '); } } putchar('\n'); } putchar('\n'); #endif printf("time: %f\n\n", (double)(end_clock-start_clock)/(double)CLOCKS_PER_SEC); mpd_del(x0); mpd_del(y0); mpd_del(sqrt_2); mpd_del(xstep); mpd_del(ystep); return 0; }
/* convert a character string to a decimal */ void mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, uint32_t *status) { mpd_ssize_t q, r, len; const char *coeff, *end; const char *dpoint = NULL, *exp = NULL; size_t digits; uint8_t sign = MPD_POS; mpd_set_flags(dec, 0); dec->len = 0; dec->exp = 0; /* sign */ if (*s == '+') { s++; } else if (*s == '-') { mpd_set_negative(dec); sign = MPD_NEG; s++; } if (_mpd_strneq(s, "nan", "NAN", 3)) { /* NaN */ s += 3; mpd_setspecial(dec, sign, MPD_NAN); if (*s == '\0') return; /* validate payload: digits only */ if ((coeff = scan_payload(s, &end)) == NULL) goto conversion_error; /* payload consists entirely of zeros */ if (*coeff == '\0') return; digits = end - coeff; /* prec >= 1, clamp is 0 or 1 */ if (digits > (size_t)(ctx->prec-ctx->clamp)) goto conversion_error; } /* sNaN */ else if (_mpd_strneq(s, "snan", "SNAN", 4)) { s += 4; mpd_setspecial(dec, sign, MPD_SNAN); if (*s == '\0') return; /* validate payload: digits only */ if ((coeff = scan_payload(s, &end)) == NULL) goto conversion_error; /* payload consists entirely of zeros */ if (*coeff == '\0') return; digits = end - coeff; if (digits > (size_t)(ctx->prec-ctx->clamp)) goto conversion_error; } else if (_mpd_strneq(s, "inf", "INF", 3)) { s += 3; if (*s == '\0' || _mpd_strneq(s, "inity", "INITY", 6)) { /* numeric-value: infinity */ mpd_setspecial(dec, sign, MPD_INF); return; } goto conversion_error; } else { /* scan for start of coefficient, decimal point, indicator, end */ if ((coeff = scan_dpoint_exp(s, &dpoint, &exp, &end)) == NULL) goto conversion_error; /* numeric-value: [exponent-part] */ if (exp) { /* exponent-part */ end = exp; exp++; dec->exp = strtoexp(exp); if (errno) { if (!(errno == ERANGE && (dec->exp == MPD_SSIZE_MAX || dec->exp == MPD_SSIZE_MIN))) goto conversion_error; } } digits = end - coeff; if (dpoint) { size_t fracdigits = end-dpoint-1; if (dpoint > coeff) digits--; if (fracdigits > MPD_MAX_PREC) { goto conversion_error; } if (dec->exp < MPD_SSIZE_MIN+(mpd_ssize_t)fracdigits) { dec->exp = MPD_SSIZE_MIN; } else { dec->exp -= (mpd_ssize_t)fracdigits; } } if (digits > MPD_MAX_PREC) { goto conversion_error; } if (dec->exp > MPD_EXP_INF) { dec->exp = MPD_EXP_INF; } if (dec->exp == MPD_SSIZE_MIN) { dec->exp = MPD_SSIZE_MIN+1; } } _mpd_idiv_word(&q, &r, (mpd_ssize_t)digits, MPD_RDIGITS); len = (r == 0) ? q : q+1; if (len == 0) { goto conversion_error; /* GCOV_NOT_REACHED */ } if (!mpd_qresize(dec, len, status)) { mpd_seterror(dec, MPD_Malloc_error, status); return; } dec->len = len; string_to_coeff(dec->data, coeff, dpoint, (int)r, len); mpd_setdigits(dec); mpd_qfinalize(dec, ctx, status); return; conversion_error: /* standard wants a positive NaN */ mpd_seterror(dec, MPD_Conversion_syntax, status); }