Skip to content

Commit 013fc74

Browse files
committed
Avoid always rebuilding: cache when deps are found.
1 parent 3c5cd0e commit 013fc74

File tree

4 files changed

+83
-53
lines changed

4 files changed

+83
-53
lines changed

GNUmakefile

+32-19
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ GD_LIBS := $(shell pkg-config --libs gdlib 2>/dev/null)
1414
IMAGEMAGICK := $(shell compare 2>/dev/null | grep ImageMagick)
1515
IMLIB2_CFLAGS := $(shell pkg-config --cflags imlib2 2>/dev/null)
1616
IMLIB2_LIBS := $(shell pkg-config --libs imlib2 2>/dev/null)
17-
18-
BUILD_WITH_MPC ?= no
17+
MPC := $(shell cpp -dM ${GD_CFLAGS} -include mpc.h </dev/null 2>/dev/null | grep MPC_VERSION_STRING)
18+
MPC_CFLAGS = ${GD_CFLAGS}
19+
MPC_LIBS = -lmpc -lmpfr -lgmp
1920

2021
SILENT = @
2122

@@ -24,57 +25,69 @@ all: check approval
2425
check: ${THE_TESTS}
2526
${SILENT}./${THE_TESTS}
2627

27-
approval: is-imagemagick-installed ${THE_PROGRAM}
28+
approval: .has_imagemagick ${THE_PROGRAM}
2829
${SILENT}./${THE_PROGRAM} gd pngelbrot.png 800 500 100 0.0 0.0 4.0
2930
${SILENT}./${APPROVAL_TESTS} pngelbrot.png
3031

3132
valgrind: ${THE_TESTS}
3233
${SILENT}valgrind --leak-check=full --show-leak-kinds=all ./${THE_TESTS}
3334

3435
clean:
35-
${SILENT}rm -f *.o ${THE_TESTS} ${THE_LIBRARY} ${THE_PROGRAM}
36+
${SILENT}rm -f .has_* *.o ${THE_TESTS} ${THE_LIBRARY} ${THE_PROGRAM}
3637
${SILENT}rm -rf *.dSYM
3738

38-
is-cairo-installed:
39+
.PHONY: all check approval valgrind clean
40+
41+
.has_cairo:
3942
ifeq (, ${CAIRO_LIBS})
4043
${SILENT}echo "Please install Cairo (https://www.cairographics.org)." && false
44+
else
45+
${SILENT}touch .has_cairo
4146
endif
4247

43-
is-check-installed:
48+
.has_check:
4449
ifeq (, ${CHECK_LIBS})
4550
${SILENT}echo "Please install Check (https://libcheck.github.io/check/)." && false
51+
else
52+
${SILENT}touch .has_check
4653
endif
4754

48-
is-gd-installed:
55+
.has_gd:
4956
ifeq (, ${GD_LIBS})
5057
${SILENT}echo "Please install GD (http://libgd.github.io)." && false
58+
else
59+
${SILENT}touch .has_gd
5160
endif
5261

53-
is-imagemagick-installed:
62+
.has_imagemagick:
5463
ifeq (, ${IMAGEMAGICK})
5564
${SILENT}echo "Please install ImageMagick (http://www.imagemagick.org)." && false
65+
else
66+
${SILENT}touch .has_imagemagick
5667
endif
5768

58-
is-imlib2-installed:
69+
.has_imlib2:
5970
ifeq (, ${IMLIB2_LIBS})
6071
${SILENT}echo "Please install Imlib 2 (http://docs.enlightenment.org/api/imlib2/html/)." && false
72+
else
73+
${SILENT}touch .has_imlib2
6174
endif
6275

63-
is-mpc-installed:
64-
ifeq (yes, ${BUILD_WITH_MPC}) # XXX a little too phony
65-
MPC_CFLAGS = -DUSE_MPC ${GD_CFLAGS}
66-
MPC_LIBS = -lmpc -lmpfr -lgmp
76+
.has_mpc:
77+
ifeq (, ${MPC})
78+
${SILENT}echo "Please install GNU MPC (http://www.multiprecision.org)." && false
79+
else
80+
${SILENT}touch .has_mpc
6781
endif
6882

69-
.PHONY: all check approval valgrind clean is-cairo-installed is-check-installed is-gd-installed is-imagemagick-installed is-imlib2-installed is-mpc-installed
70-
71-
${THE_TESTS}: is-check-installed ${THE_LIBRARY} check_mandelbrot.c
83+
${THE_TESTS}: .has_check ${THE_LIBRARY} check_mandelbrot.c
7284
${SILENT}${CC} ${CFLAGS} ${CHECK_CFLAGS} -o ${THE_TESTS} check_mandelbrot.c ${THE_LIBRARY} ${LIBS} ${CAIRO_LIBS} ${GD_LIBS} ${IMLIB2_LIBS} ${MPC_LIBS} ${CHECK_LIBS}
7385

74-
${THE_LIBRARY}: is-cairo-installed is-gd-installed is-imlib2-installed is-mpc-installed graph_cairo.h graph_gd.h graph_imlib2.h graph.h graph.c mandelbrot.h mandelbrot.c
86+
${THE_LIBRARY}: .has_cairo .has_gd .has_imlib2 .has_mpc graph_cairo.h graph_gd.h graph_imlib2.h graph.h graph.c mandelbrot.h mandelbrot.c mandelbrot_mpc.c
7587
${SILENT}${CC} ${CFLAGS} ${CAIRO_CFLAGS} ${GD_CFLAGS} ${IMLIB2_CFLAGS} -c graph.c
76-
${SILENT}${CC} ${CFLAGS} ${MPC_CFLAGS} -c mandelbrot.c
77-
${SILENT}ar rc ${THE_LIBRARY} graph.o mandelbrot.o
88+
${SILENT}${CC} ${CFLAGS} -c mandelbrot.c
89+
${SILENT}${CC} ${CFLAGS} ${MPC_CFLAGS} -c mandelbrot_mpc.c
90+
${SILENT}ar rc ${THE_LIBRARY} graph.o mandelbrot.o mandelbrot_mpc.o
7891
${SILENT}ranlib ${THE_LIBRARY}
7992

8093
${THE_PROGRAM}: ${THE_LIBRARY} mandelbrot.h main.c

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Now I'm drawing it in C:
1212

1313
## TODO
1414

15-
7. Why does the code always get rebuilt?
1615
7. Improve safety and style of `graph_t.image` declaration.
1716
7. Why did Cairo get slower with this refactoring? Where are we spending our time?
1817
7. Choose colormap size at runtime.

mandelbrot.c

-33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#include <complex.h>
22
#include <stdlib.h>
33

4-
#ifdef USE_MPC
5-
#include <mpc.h>
6-
#endif
7-
84
#include "graph.h"
95
#include "mandelbrot.h"
106

@@ -13,34 +9,6 @@ choose_escape_color(const complex double c, const size_t maximum_iterations)
139
{
1410
size_t escape = 0;
1511

16-
#ifdef USE_MPC
17-
const mpfr_prec_t precision = 53;
18-
mpc_rnd_t rounding_mode = MPC_RNDNN;
19-
20-
mpc_t my_z, my_temp, my_c;
21-
mpfr_t my_mpfr_absolute_value;
22-
mpc_init2(my_z, precision), mpc_init2(my_temp, precision),
23-
mpc_init2(my_c, precision);
24-
mpfr_init2(my_mpfr_absolute_value, precision);
25-
26-
mpc_set_dc(my_c, c, rounding_mode);
27-
mpc_set_dc(my_z, 0.0 + I * 0.0, rounding_mode);
28-
29-
for (; escape < maximum_iterations; escape++) {
30-
mpc_pow_d(my_temp, my_z, 2.0, rounding_mode);
31-
32-
mpc_abs(my_mpfr_absolute_value, my_temp, rounding_mode);
33-
if (mpfr_get_d(my_mpfr_absolute_value, rounding_mode) > 2)
34-
break;
35-
36-
mpc_add(my_z, my_temp, my_c, rounding_mode);
37-
}
38-
39-
mpfr_clear(my_mpfr_absolute_value);
40-
mpc_clear(my_c);
41-
mpc_clear(my_temp);
42-
mpc_clear(my_z);
43-
#else
4412
complex double z = 0.0 + I * 0.0;
4513
complex double temp;
4614

@@ -52,7 +20,6 @@ choose_escape_color(const complex double c, const size_t maximum_iterations)
5220

5321
z = temp + c;
5422
}
55-
#endif /* USE_MPC */
5623

5724
if (escape == maximum_iterations)
5825
escape = 0;

mandelbrot_mpc.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <complex.h>
2+
#include <stdlib.h>
3+
4+
#include <mpc.h>
5+
6+
size_t
7+
choose_escape_color_mpc(const complex double c, const size_t maximum_iterations)
8+
{
9+
size_t escape = 0;
10+
11+
const mpfr_prec_t precision = 53;
12+
mpc_rnd_t rounding_mode = MPC_RNDNN;
13+
14+
mpc_t my_z, my_temp, my_c;
15+
mpfr_t my_mpfr_absolute_value;
16+
mpc_init2(my_z, precision), mpc_init2(my_temp, precision),
17+
mpc_init2(my_c, precision);
18+
mpfr_init2(my_mpfr_absolute_value, precision);
19+
20+
mpc_set_dc(my_c, c, rounding_mode);
21+
mpc_set_dc(my_z, 0.0 + I * 0.0, rounding_mode);
22+
23+
for (; escape < maximum_iterations; escape++) {
24+
mpc_pow_d(my_temp, my_z, 2.0, rounding_mode);
25+
26+
mpc_abs(my_mpfr_absolute_value, my_temp, rounding_mode);
27+
if (mpfr_get_d(my_mpfr_absolute_value, rounding_mode) > 2)
28+
break;
29+
30+
mpc_add(my_z, my_temp, my_c, rounding_mode);
31+
}
32+
33+
mpfr_clear(my_mpfr_absolute_value);
34+
mpc_clear(my_c);
35+
mpc_clear(my_temp);
36+
mpc_clear(my_z);
37+
38+
if (escape == maximum_iterations)
39+
escape = 0;
40+
41+
if (escape == 0)
42+
return 0;
43+
else if (escape <= (maximum_iterations / 7))
44+
return 1;
45+
else if (escape <= (maximum_iterations / 5))
46+
return 2;
47+
else
48+
return 3;
49+
50+
return escape;
51+
}

0 commit comments

Comments
 (0)