Use utility functions

This commit is contained in:
Lucas 2020-03-08 21:24:10 +00:00
parent b13f35e0e6
commit dbd7618c9f
7 changed files with 48 additions and 201 deletions

View File

@ -19,19 +19,20 @@ FRACTAL_CFLAGS = -O3 -ffast-math -march=native -mtune=native \
${CFLAGS} ${CFLAGS}
BIN = fractal fun-gen BIN = fractal fun-gen
FRACTAL_OBJ = criticals.o fractal.o julia.o palette.o pnmout.o util.o FRACTAL_OBJ = criticals.o fractal.o julia.o palette.o pnmout.o
FUN_GEN_OBJ = fun-gen.o FUN_GEN_OBJ = fun-gen.o
OBJ = ${FRACTAL_OBJ} ${FUN_GEN_OBJ} UTIL_OBJ = util.o
OBJ = ${FRACTAL_OBJ} ${FUN_GEN_OBJ} ${UTIL_OBJ}
all: fractal all: fractal
julia.o: fun.h julia.o: fun.h
fractal: ${FRACTAL_OBJ} fractal: ${FRACTAL_OBJ} ${UTIL_OBJ}
${CC} ${LDFLAGS} -o $@ ${FRACTAL_OBJ} -lm ${CC} ${LDFLAGS} -o $@ ${FRACTAL_OBJ} ${UTIL_OBJ} -lm
fun-gen: ${FUN_GEN_OBJ} fun-gen: ${FUN_GEN_OBJ} ${UTIL_OBJ}
${CC} ${LDFLAGS} -o $@ ${FUN_GEN_OBJ} -lm ${CC} ${LDFLAGS} -o $@ ${FUN_GEN_OBJ} ${UTIL_OBJ} -lm
clean: clean:
rm -f ${OBJ} ${BIN} fun.h rm -f ${OBJ} ${BIN} fun.h

View File

@ -62,9 +62,7 @@ ncriticals(void)
long double complex long double complex
get_critical(size_t i) get_critical(size_t i)
{ {
if (i >= idx) { if (i >= idx)
fprintf(stderr, "get_critical: index %zu out of range", i); errx(1, "get_critical: index %zu out of range", i);
exit(1);
}
return criticals[i]; return criticals[i];
} }

View File

@ -15,8 +15,6 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#endif #endif
#include <errno.h>
#include <inttypes.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -41,7 +39,6 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *end; char *end;
intmax_t v;
long seed; long seed;
int ch; int ch;
@ -49,15 +46,7 @@ main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "s:")) != -1) while ((ch = getopt(argc, argv, "s:")) != -1)
switch (ch) { switch (ch) {
case 's': case 's':
errno = 0; seed = parse_integer(optarg, LONG_MIN, LONG_MAX);
v = strtoimax(optarg, &end, 0);
if (*end != '\0' || errno != 0 || v > LONG_MAX ||
v < LONG_MIN) {
fprintf(stderr, "-s: Invalid value \"%s\".",
optarg);
exit(1);
}
seed = (long)v;
break; break;
default: default:
usage(); usage();

View File

@ -15,17 +15,18 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#endif #endif
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MAX_DEGREE 1000
static char *argv0; static char *argv0;
struct poly { struct poly {
@ -37,10 +38,8 @@ struct poly {
static void static void
poly_init(struct poly *p, unsigned int degree, const char *expr) poly_init(struct poly *p, unsigned int degree, const char *expr)
{ {
if ((p->coefs = malloc((degree + 1) * sizeof(double))) == NULL) { if ((p->coefs = malloc((degree + 1) * sizeof(double))) == NULL)
fprintf(stderr, "out of memory\n"); errx(1, "out of memory");
exit(1);
}
p->degree = degree; p->degree = degree;
p->expr = expr; p->expr = expr;
@ -158,9 +157,6 @@ print_funs(struct poly p, struct poly q)
"- q * (2.0L * dp * dq + p * ddq) " "- q * (2.0L * dp * dq + p * ddq) "
"+ 2.0L * p * dq * dq" "+ 2.0L * p * dq * dq"
") / (q * q * q);\n"); ") / (q * q * q);\n");
/*printf("\treturn (ddp "
"- 2.0 * ((dp * q - p * dq) / (q * q)) * dq "
"- (p / q) * ddq) / q;\n");*/
print_fun_footer(); print_fun_footer();
poly_free(&dp); poly_free(&dp);
@ -169,43 +165,13 @@ print_funs(struct poly p, struct poly q)
poly_free(&ddq); poly_free(&ddq);
} }
static unsigned int
parse_uint(const char *s)
{
char *end;
uintmax_t v;
errno = 0;
v = strtoumax(s, &end, 10);
if (s == end || *end != '\0' || errno != 0 || v > UINT_MAX) {
fprintf(stderr, "%s: invalid value: %s\n", argv0, s);
exit(1);
}
return (unsigned int)v;
}
static double
parse_double(const char *s)
{
char *end;
double d;
errno = 0;
d = strtod(s, &end);
if (s == end || *end != '\0' || errno != 0 || !isfinite(d)) {
fprintf(stderr, "%s: invalid value: %s\n", argv0, s);
exit(1);
}
return d;
}
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "Usage: %s p-degree q-degree p-coefs q-coefs\n" const char *p = xgetprogname();
" %s -n p-degree p-coefs\n", argv0, argv0); fprintf(stderr, "Usage:\n"
"\t%s p-degree q-degree p-coefs q-coefs\n"
"\t%s -n p-degree p-coefs\n", p, p);
exit(1); exit(1);
} }
@ -218,8 +184,8 @@ do_poly_div(int argc, char *argv[])
if (argc < 2) if (argc < 2)
usage(); usage();
p_degree = parse_uint(argv[0]); p_degree = parse_integer(argv[0], 0, MAX_DEGREE);
q_degree = parse_uint(argv[1]); q_degree = parse_integer(argv[1], 0, MAX_DEGREE);
if (argc != p_degree + q_degree + 2 + 2) if (argc != p_degree + q_degree + 2 + 2)
usage(); usage();
@ -249,7 +215,7 @@ do_newton(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
f_degree = parse_uint(argv[0]); f_degree = parse_integer(argv[0], 0, MAX_DEGREE);
if (argc != f_degree + 1 + 1) if (argc != f_degree + 1 + 1)
usage(); usage();
@ -275,7 +241,6 @@ main(int argc, char *argv[])
{ {
struct poly p, q; struct poly p, q;
char *end; char *end;
uintmax_t v;
unsigned int i, j, p_degree, q_degree; unsigned int i, j, p_degree, q_degree;
int ch, nflag; int ch, nflag;

112
julia.c
View File

@ -16,8 +16,6 @@
#endif #endif
#include <complex.h> #include <complex.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
@ -164,123 +162,43 @@ julia_main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "a:b:c:D:d:h:Ll:n:w:x:y:z:")) != -1) while ((ch = getopt(argc, argv, "a:b:c:D:d:h:Ll:n:w:x:y:z:")) != -1)
switch (ch) { switch (ch) {
case 'a': case 'a':
errno = 0; cen_x = parse_double(optarg);
cen_x = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-a: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'b': case 'b':
errno = 0; cen_y = parse_double(optarg);
cen_y = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-b: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'c': case 'c':
errno = 0; contour = parse_double(optarg);
contour = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0
|| contour <= 0) {
fprintf(stderr, "-c: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'D': case 'D':
errno = 0; displacement = parse_double(optarg);
displacement = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-d: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'd': case 'd':
errno = 0; density = parse_double(optarg);
density = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-d: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'h': case 'h':
errno = 0; height = parse_integer(optarg, 1, 65536);
v = strtoimax(optarg, &end, 0);
if (end == optarg || *end != '\0' || errno != 0 ||
v <= 0 || v >= 65536) {
fprintf(stderr, "-h: invalid value \"%s\"",
optarg);
exit(1);
}
height = (unsigned int)v + 1;
break; break;
case 'L': case 'L':
do_light = 1; do_light = 1;
break; break;
case 'l': case 'l':
errno = 0; light_intensity = parse_double(optarg);
light_intensity = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-l: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'n': case 'n':
errno = 0; niters = parse_integer(optarg, 1, ULONG_MAX);
v = strtoimax(optarg, &end, 0);
if (end == optarg || *end != '\0' || errno != 0 ||
v <= 0 || v >= ULONG_MAX) {
fprintf(stderr, "-n: invalid value \"%s\"",
optarg);
exit(1);
}
niters = (unsigned long)v;
break; break;
case 'w': case 'w':
errno = 0; width = parse_integer(optarg, 1, 65536);
v = strtoimax(optarg, &end, 0);
if (end == optarg || *end != '\0' || errno != 0 ||
v <= 0 || v >= 65536) {
fprintf(stderr, "-w: invalid value \"%s\"",
optarg);
exit(1);
}
width = (unsigned int)v + 1;
break; break;
case 'x': case 'x':
errno = 0; julia_x = parse_double(optarg);
julia_x = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-x: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'y': case 'y':
errno = 0; julia_y = parse_double(optarg);
julia_y = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0) {
fprintf(stderr, "-y: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
case 'z': case 'z':
errno = 0; zoom = parse_double(optarg);
zoom = strtold(optarg, &end);
if (end == optarg || *end != '\0' || errno != 0
|| zoom <= 0) {
fprintf(stderr, "-z: invalid value \"%s\"",
optarg);
exit(1);
}
break; break;
default: default:
usage(); usage();
@ -292,10 +210,8 @@ julia_main(int argc, char *argv[])
palette = palette_gen(palette_size); palette = palette_gen(palette_size);
if ((rescache = malloc(sizeof(*rescache) * width * height)) == NULL) { if ((rescache = malloc(sizeof(*rescache) * width * height)) == NULL)
fprintf(stderr, "out of memory\n"); errx(1, "out of memory");
exit(1);
}
if (do_light) { if (do_light) {
long double angle_xy, angle_z; long double angle_xy, angle_z;

View File

@ -15,8 +15,6 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#endif #endif
#include <errno.h>
#include <inttypes.h>
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -57,10 +55,8 @@ palette_gen(size_t n)
double r, g, b, t; double r, g, b, t;
size_t i; size_t i;
if (!(palette = malloc(n * sizeof(*palette)))) { if ((palette = malloc(n * sizeof(*palette))) == NULL)
fprintf(stderr, "out of memory"); errx(1, "out of memory");
exit(1);
}
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
t = i / (double)n; t = i / (double)n;
@ -97,25 +93,11 @@ palette_main(int argc, char *argv[])
while ((ch = getopt(argc, argv, "h:w:")) != -1) while ((ch = getopt(argc, argv, "h:w:")) != -1)
switch (ch) { switch (ch) {
case 'h': case 'h':
errno = 0; height = parse_integer(optarg, 1, 65535);
v = strtoumax(optarg, &end, 0); break;
if (*end != '\0' || errno != 0 || v == 0
|| v > UINT_MAX) {
fprintf(stderr, "-h: Invalid value \"%s\"",
optarg);
exit(1);
}
height = v;
case 'w': case 'w':
errno = 0; width = parse_integer(optarg, 1, 65535);
v = strtoumax(optarg, &end, 0); break;
if (*end != '\0' || errno != 0 || v == 0
|| v > UINT_MAX) {
fprintf(stderr, "-w: Invalid value \"%s\"",
optarg);
exit(1);
}
width = v;
default: default:
usage(); usage();
} }

View File

@ -15,14 +15,13 @@
#include "palette.h" #include "palette.h"
#include "pnmout.h" #include "pnmout.h"
#include "util.h"
void void
pnmout_header(FILE *fp, unsigned int width, unsigned int height) pnmout_header(FILE *fp, unsigned int width, unsigned int height)
{ {
if (fprintf(fp, "P6\n%u %u 255\n", width, height) < 0) { if (fprintf(fp, "P6\n%u %u 255\n", width, height) < 0)
perror("fprintf"); err(1, "fprintf");
exit(1);
}
} }
void void
@ -32,9 +31,6 @@ pnmout_pixel(FILE *fp, struct color c)
v[0] = c.r / 256; v[0] = c.r / 256;
v[1] = c.g / 256; v[1] = c.g / 256;
v[2] = c.b / 256; v[2] = c.b / 256;
if (fwrite(v, sizeof(v[0]), sizeof(v) / sizeof(v[0]), fp) != 3) { if (fwrite(v, sizeof(v[0]), sizeof(v) / sizeof(v[0]), fp) != 3)
perror("fwrite"); err(1, "fwrite");
exit(1);
}
} }