-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1247 lines (1082 loc) · 42.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ################
// # INTRODUCTION #
// ################
//
// Welcome to Mandelbrot, with big floats. This program allows you to
// interactively zoom into the fractal, or render a deep-zoom movie up to a max
// limit of 10^57 zoom.
//
// Youtube link:
//
// This is the CPU bigfloat version.
// CPU doubles version (faster but 10^15 limit): https://github.com/limdingwen/Mandelbrot/tree/fast
// GPU OpenCL version (slower): https://github.com/limdingwen/Mandelbrot/tree/bigfloat-gpu
// GPU Metal version (slower and may crash your Mac): https://github.com/limdingwen/Mandelbrot/tree/bigfloat-metal
//
// #############
// # DOWNLOADS #
// #############
//
// M1 Mac: https://github.com/limdingwen/Mandelbrot/releases/download/v1.0.0/M1.Mac.zip
// (If you want Intel Mac or Intel Windows version just contact me.)
//
// All of these downloads are for interactive zooming; if you want to use the
// movie rendering mode, you'll need to compile the code yourself, and edit the
// configuration (search for #define MOVIE to find it).
//
// ###########
// # OUTLINE #
// ###########
//
// This version of the program has two modes; preview and full-sized rendering.
// Both modes are nearly identical except for the resolution in which they are
// rendered at. In an earlier version of this program, the preview mode was able
// to run at 60fps, navigatable by keyboard. However, ever since the switch from
// doubles to big floats, the program does not run fast enough to enjoy that
// level of interactivity. As such, both modes operate on click-to-zoom.
//
// Controls:
// H - Return home
// Left click - Zoom in
// Right click - Zoom out
// Tab - Change between preview and full resolution (may take a long time!)
//
// ###############
// # COMPILATION #
// ###############
//
// This program was originally written for Clang+Make, then ported to XCode.
// As such, I'll only provide general instructions for building this.
//
// First of all, this program only supports ARM due to use of inline ARM asm.
// If you would like x86 support, please contact me and I'll add it in; I just
// don't want to waste my time if no one cares anyway.
//
// You only need to compile main.c, and remember to enable -Ofast optimisation.
// You'll need to link SDL2 and SDL2_image, as well as tell the compiler where
// to find their header files. Finally, remember that zoom.png needs to
// accompany the binary.
//
// #############
// # BIG FLOAT #
// #############
//
// One of the core parts of Mandelbrot is the floating point precision, so it
// makes sense to go over them first.
//
// We do not use any floating points in our program; instead, we have an array
// of uint64s, and then we simply imagine the decimal point to be in-between
// the first uint64 and the second. That way our calculations are immensely
// simplified. Adding and subtracting can ignore the decimal point entirely,
// while multiplication just involves taking a slice of the resulting bits.
//
// While we can technically achieve abitrary precision, it was easier to limit
// ourselves to 256-bits and 512-bits for now. You may also notice that we are
// wasting quite a lot of bits by having 64 bits be part of the "whole number".
// However, doing so makes the program a lot simpler and seems to be good enough
// for now.
//
// Finally, we use a sign-magnitude representation instead of 2s complement.
// While this makes adding and subtracting much harder, it makes multiplication
// much easier. I'm unsure if this was a good tradeoff, but it seems like most
// bignum libraries use sign-magnitude as well.
#include <stdint.h>
enum sign
{
SIGN_NEG,
SIGN_ZERO,
SIGN_POS
};
struct fp256
{
enum sign sign;
uint64_t man[4];
};
struct fp512
{
enum sign sign;
uint64_t man[8];
};
// Let's start with unsigned addition. This is the exciting bit; we get to use
// some easy inline assembly! As you can see, we simply add the LSB (Least
// Significant Bit) of a and b, saving the carry, and then repeat all the way
// to the MSB (Most Significant Bit) while propogating the carry.
//
// We do use quite a lot of registers here, which is another reason for fixing
// ourselves at 512-bits max. ARM only has about 30 usage registers, so we're
// quite near the limit. Any more, and we'll have to start writing code to spill
// registers mid-way through the calculation.
struct fp512 fp_uadd512(struct fp512 a, struct fp512 b)
{
struct fp512 c;
asm("ADDS %7, %15, %23\n"
"ADCS %6, %14, %22\n"
"ADCS %5, %13, %21\n"
"ADCS %4, %12, %20\n"
"ADCS %3, %11, %19\n"
"ADCS %2, %10, %18\n"
"ADCS %1, %9, %17\n"
"ADC %0, %8, %16"
:
"=&r"(c.man[0]), // 0
"=&r"(c.man[1]), // 1
"=&r"(c.man[2]), // 2
"=&r"(c.man[3]), // 3
"=&r"(c.man[4]), // 4
"=&r"(c.man[5]), // 5
"=&r"(c.man[6]), // 6
"=&r"(c.man[7]) // 7
:
"r" (a.man[0]), // 8
"r" (a.man[1]), // 9
"r" (a.man[2]), // 10
"r" (a.man[3]), // 11
"r" (a.man[4]), // 12
"r" (a.man[5]), // 13
"r" (a.man[6]), // 14
"r" (a.man[7]), // 15
"r" (b.man[0]), // 16
"r" (b.man[1]), // 17
"r" (b.man[2]), // 18
"r" (b.man[3]), // 19
"r" (b.man[4]), // 20
"r" (b.man[5]), // 21
"r" (b.man[6]), // 22
"r" (b.man[7]) // 23
:
"cc");
return c;
}
// We do the same thing for 256-bits, but we will use a macro to help us reuse
// the same code for both adding (ADD/ADC) and subtracting (SUB/SBC).
//
// Do note that since we are using signed-magnitude comparison, we must ensure
// that a > b before subtracting, so as to ensure that the resulting magnitude
// does not wrap around in 2s complement representation. The only exception is
// if we're comparing a and b, in which we will throw away the results after
// (and not use it as a magnitude).
#define DEF_FP_UADDSUB256(name, op, opc) struct fp256 fp_u##name##256(struct fp256 a, struct fp256 b) \
{ \
struct fp256 c; \
asm(#op "S %3, %7, %11\n" \
#opc "S %2, %6, %10\n" \
#opc "S %1, %5, %9\n" \
#opc " %0, %4, %8" \
: \
"=&r"(c.man[0]), /* 0 */ \
"=&r"(c.man[1]), /* 1 */ \
"=&r"(c.man[2]), /* 2 */ \
"=&r"(c.man[3]) /* 3 */ \
: \
"r" (a.man[0]), /* 4 */ \
"r" (a.man[1]), /* 5 */ \
"r" (a.man[2]), /* 6 */ \
"r" (a.man[3]), /* 7 */ \
"r" (b.man[0]), /* 8 */ \
"r" (b.man[1]), /* 9 */ \
"r" (b.man[2]), /* 10 */ \
"r" (b.man[3]) /* 11 */ \
: \
"cc"); \
return c; \
}
DEF_FP_UADDSUB256(add, ADD, ADC);
DEF_FP_UADDSUB256(sub, SUB, SBC);
#undef DEF_FP_UADDSUB256
// This allows us to compare the magnitudes of two numbers (note that this
// function is unsigned and ignores the signs of the numbers)! We can do so
// by simply performing (a - b). Then, if it's negative, b must be more than a.
// If it's non-negative, it can either be 0 (same) or a is more than b.
enum cmp
{
CMP_SAME,
CMP_A_BIG,
CMP_B_BIG
};
#include <string.h>
#include <stdbool.h>
enum cmp fp_ucmp256(struct fp256 a, struct fp256 b)
{
static const uint64_t zero[4]; // Static variables are 0 by default
struct fp256 c = fp_usub256(a, b);
bool is_negative = (c.man[0] >> 63) == 1;
if (is_negative)
return CMP_B_BIG;
else
if (memcmp(c.man, zero, 4 * sizeof(uint64_t)) == 0)
return CMP_SAME;
else
return CMP_A_BIG;
}
// With unsigned add, sub and cmp functions, we can use this to build a signed
// addition function. First, we check for the special case in which one or both
// of the inputs are 0:
#include <assert.h>
struct fp256 fp_sadd256(struct fp256 a, struct fp256 b)
{
if (a.sign == SIGN_ZERO && b.sign == SIGN_ZERO)
return a;
if (b.sign == SIGN_ZERO)
return a;
if (a.sign == SIGN_ZERO)
return b;
// First, if both are of the same sign, we can simply add the magnitudes and
// inherit the sign.
if ((a.sign == SIGN_POS && b.sign == SIGN_POS) ||
(a.sign == SIGN_NEG && b.sign == SIGN_NEG))
{
struct fp256 c = fp_uadd256(a, b);
c.sign = a.sign;
return c;
}
// At this point, there are only 2 possibilities: -a and +b, or +a and -b.
assert((a.sign == SIGN_POS && b.sign == SIGN_NEG) ||
(a.sign == SIGN_NEG && b.sign == SIGN_POS));
// It should be obvious that if we have a - b or b - a, if a = b, then the
// answer is 0.
enum cmp cmp = fp_ucmp256(a, b);
if (cmp == CMP_SAME)
return (struct fp256) { SIGN_ZERO, {0} };
// And then we can simply follow this chart for the remaining possibilities:
// https://www.tutorialspoint.com/explain-the-performance-of-addition-and-subtraction-with-signed-magnitude-data-in-computer-architecture
if (a.sign == SIGN_POS && b.sign == SIGN_NEG)
{
if (cmp == CMP_A_BIG)
{
struct fp256 c = fp_usub256(a, b);
c.sign = SIGN_POS;
return c;
}
else
{
struct fp256 c = fp_usub256(b, a);
c.sign = SIGN_NEG;
return c;
}
}
else
{
if (cmp == CMP_A_BIG)
{
struct fp256 c = fp_usub256(a, b);
c.sign = SIGN_NEG;
return c;
}
else
{
struct fp256 c = fp_usub256(b, a);
c.sign = SIGN_POS;
return c;
}
}
}
// We can build upon signed addition to created signed subtraction, by simply
// inverting the second operand's sign.
struct fp256 fp_sinv256(struct fp256 a)
{
if (a.sign == SIGN_POS) a.sign = SIGN_NEG;
else if (a.sign == SIGN_NEG) a.sign = SIGN_POS;
return a;
}
struct fp256 fp_ssub256(struct fp256 a, struct fp256 b)
{
return fp_sadd256(a, fp_sinv256(b));
}
// Now for the big gun; multiplication. There exists harder and more efficient
// multiplication algorithms, but I'll go with the naive, elementary-school-like
// way of doing it.
struct fp256 fp_smul256(struct fp256 a, struct fp256 b)
{
// First, the obvious x * 0 = 0.
if (a.sign == SIGN_ZERO || b.sign == SIGN_ZERO)
return (struct fp256) { SIGN_ZERO, {0} };
// Next, we calculate the magnitude of a * the magnitude of b to create our
// final magnitude (c). We create a 512-bit number to hold all the possible
// bits of a 256 * 256 bit multiplication.
struct fp512 c = {0};
// Just like in elementary school, we literally just multiply each word (digit)
// with all the words in the other operand.
for (int i = 3; i >= 0; i--) // a
{
for (int j = 3; j >= 0; j--) // b
{
// First, we use a 128-bit number to multiply our two 64-bit words (digit). We
// do this because ARM's 128-bit multiplication was so hard to understand. Plus,
// it's portable!
//
// Also yes, we could have done this for adding as well, but 1) inline assembly
// is kinda fun and 2) I'm unsure if the compiler would have generated the
// optimal ADDS/ADCS code.
#ifndef __SIZEOF_INT128__
#error Your compiler or platform does not support 128 bits.
#endif
__uint128_t mult = (__uint128_t)a.man[i] * (__uint128_t)b.man[j];
// We then put this 128-bit number into 2 64-bit words (digits), and that makes
// up 1 "row", like you normally see in pencil-and-paper multiplication. Then
// we can simply repeatedly accumulate this into the final answer.
int low_offset = 7 - (3 - i) - (3 - j);
assert(low_offset >= 1);
int high_offset = low_offset - 1;
struct fp512 temp = {0};
temp.man[low_offset] = (uint64_t)mult;
temp.man[high_offset] = mult >> 64;
c = fp_uadd512(c, temp);
}
}
// With the magnitude calculated, finding the sign of the result is trivial:
enum sign sign;
if (a.sign == SIGN_NEG && b.sign == SIGN_NEG)
sign = SIGN_POS;
else if (a.sign == SIGN_NEG || b.sign == SIGN_NEG)
sign = SIGN_NEG;
else
sign = SIGN_POS;
// Finally, we need to convert the 512-bit result back into 256-bits. Take note
// that 1.234 * 5.678 = 12.345678 (the numbers represent the word position),
// and so we need to take the slice [2, 5].
struct fp256 c256;
c256.sign = sign;
memcpy(c256.man, c.man + 1, 4 * sizeof(uint64_t));
return c256;
}
struct fp256 fp_ssqr256(struct fp256 a)
{
return fp_smul256(a, a);
}
// There's an easier way to multiply or divide by 2 however; bit shifting!
// For shifting to the right (ASR), we first shift the least significant word
// to the right, then take the least signifiant bit of the second word, and then
// put it at the most significant bit of the least significant word.
//
// In effect, it's as if we take the entire 256 bits and shift it once to the
// right, where the original least significant bit gets ignored, while the
// new most significant bit is 0.
struct fp256 fp_asr256(struct fp256 a)
{
a.man[3] >>= 1;
a.man[3] |= (a.man[2] & 0x1) << 63;
a.man[2] >>= 1;
a.man[2] |= (a.man[1] & 0x1) << 63;
a.man[1] >>= 1;
a.man[1] |= (a.man[0] & 0x1) << 63;
a.man[0] >>= 1;
return a;
}
struct fp256 fp_asl256(struct fp256 a)
{
a.man[0] <<= 1;
a.man[0] |= (a.man[1] & 0x8000000000000000) >> 63;
a.man[1] <<= 1;
a.man[1] |= (a.man[2] & 0x8000000000000000) >> 63;
a.man[2] <<= 1;
a.man[2] |= (a.man[3] & 0x8000000000000000) >> 63;
a.man[3] <<= 1;
return a;
}
// Finally, to convert a signed into a number, first we remove the sign (and
// store it as the sign enum), then we put the magnitude into the first word
// of the mantissa (which is defined to be the whole number).
struct fp256 int_to_fp256(int a)
{
if (a == 0)
return (struct fp256){ SIGN_ZERO, {0} };
struct fp256 b = {0};
if (a < 0)
{
b.sign = SIGN_NEG;
a = -a;
}
else
b.sign = SIGN_POS;
b.man[0] = (uint64_t)a;
return b;
}
// ##############
// # MANDELBROT #
// ##############
//
// The heart of the fractal. This function returns if a particular math
// coordinate is in the set, and if not, returns how many iterations it took to
// escape.
struct mb_result
{
bool is_in_set;
unsigned long long escape_iterations;
};
// This is a direct replica of this psuedocode from Wikipedia:
//
// x2 := 0
// y2 := 0
//
// while (x2 + y2 ≤ 4 and iteration < max_iteration) do
// y := 2 × x × y + y0
// x := x2 - y2 + x0
// x2 := x × x
// y2 := y × y
// iteration := iteration + 1
//
// https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set#Optimized_escape_time_algorithms
struct mb_result process_mandelbrot(struct fp256 math_x, struct fp256 math_y, unsigned long long iterations)
{
struct fp256 x2 = { SIGN_ZERO, {0} };
struct fp256 y2 = { SIGN_ZERO, {0} };
struct fp256 x = { SIGN_ZERO, {0} };
struct fp256 y = { SIGN_ZERO, {0} };
for (unsigned long long i = 0; i < iterations; i++)
{
y = fp_sadd256(fp_asl256(fp_smul256(x, y)), math_y);
x = fp_sadd256(fp_ssub256(x2, y2), math_x);
x2 = fp_ssqr256(x);
y2 = fp_ssqr256(y);
if (fp_sadd256(x2, y2).man[0] >= 4) // Escape radius is 2.
return (struct mb_result){ false, i };
}
return (struct mb_result){ true, -1ULL };
}
// Next, we will be using the "square root" algorithm (chosen for its simplicity
// and consistency in deep zooms) for colouring the set.
//
// The gradient is set to loop for every sqrt(GRADIENT_ITERATION_SIZE)
// number of iterations. It used to be that 2^x would give better performance,
// but since we're now using fmod(), I'm no longer so sure.
#define GRADIENT_ITERATION_SIZE 16
// We define the actual gradient, in 0-255 RGB format. We also duplicate
// the first and last stops, so that it will be easier for the program to loop.
struct color
{
float r;
float g;
float b;
};
#define GRADIENT_STOP_COUNT 4
const struct color gradient_stops[GRADIENT_STOP_COUNT + 1] =
{
{ 0x44, 0x44, 0xFF },
{ 0x44, 0xFF, 0x44 },
{ 0xFF, 0xFF, 0x44 },
{ 0xFF, 0x44, 0x44 },
{ 0x44, 0x44, 0xFF }, // For looping
};
// We define a trivial function for lerping between two colors for the
// gradient to use. We also clamp x so that colors may not end up as more than
// 255 or less than 0.
struct color color_lerp(struct color a, struct color b, float x)
{
if (x > 1) x = 1;
if (x < 0) x = 0;
return (struct color)
{
a.r + (b.r - a.r)*x,
a.g + (b.g - a.g)*x,
a.b + (b.b - a.b)*x
};
}
// We use a gradient struct instead of the configuration seen above so we do not
// tie our implementation to this source file. Rather, any caller may pass in
// any gradient configuration to our function.
struct gradient
{
int stop_count;
int size;
const struct color *stops;
};
#include <math.h>
struct color gradient_color(struct gradient gradient, float x)
{
// We make the gradient loop...
x = fmodf(x, (float)gradient.size);
// Then we calculate the two color stop indexes that we'll be lerping between.
// We do this by first calculating where on the gradient we are on a floating-
// point range from 0 (inclusive) to stop_count (exclusive). Then we floor it
// into an int, producing an int value from 0 to stop_count - 1.
//
// Then we just +1 that to get the next color stop index. You can see from this
// why it was important to duplicate the first color stop as stops[stop_count],
// to loop.
int stop_prev = (int)((float)x / (float)gradient.size * (float)gradient.stop_count);
int stop_next = stop_prev + 1;
// Then, using a bit of duplicated code, we determine where between the two
// color stops we are, in a range from 0 to 1.
float stop_x = (float)x / (float)gradient.size * (float)gradient.stop_count - (float)stop_prev;
// Then we may use that information to lerp between the two color stops.
return color_lerp(gradient.stops[stop_prev], gradient.stops[stop_next], stop_x);
}
// #############
// # RENDERING #
// #############
//
// First, let's define some properties for full-sized rendering. We assume that
// full-size is 1:1 to the window, and thus window size = full-size dimensions.
#define WINDOW_WIDTH 1920
#define WINDOW_HEIGHT 1080
// Then, we'll need to have a way to convert from height to width.
// In this case, ratio = width / height and thus width = height * ratio.
#define SIZE_RATIO_X (struct fp256){ SIGN_POS, { 1, 0xC71C71C71C71C71C, 0x71C71C71C71C71C7, 0x1C71C71C71C71C71 } } // 1920/1080
// Here, we also define reciprocals (1/x) of the width and height, as our big
// float implementation is currently unable to divide; only multiply. We will
// elaborate on the big float implementation later. Currently, this represents
// the raw hexadecimal value of the big float as converting from a decimal
// value is too difficult.
//
// Note that you may use the dec2hex.py program that came alongside to help you
// convert between decimals and hex. Alternatively, use Wolfram Alpha.
#define WINDOW_WIDTH_RECIPROCAL (struct fp256){ SIGN_POS, { 0, 0x0022222222222222, 0x2222222222222222, 0x2222222222222222 } }
#define WINDOW_HEIGHT_RECIPROCAL (struct fp256){ SIGN_POS, { 0, 0x003CAE759203CAE7, 0x59203CAE759203CA, 0xE759203CAE759203 } }
// This defines how many columns of pixels we draw before presenting a frame
// to the user. Doing this after too little columns results in slower rendering.
// This should be a factor of FULL_THREAD_X_SIZE below.
#define FULL_SHOW_X_INTERVAL 10
// We are going to render multicore, and the simplest way to do so is to divide
// up fixed blocks of pixels for each core to render.
#define THREADS 8
struct thread_block
{
int x_start;
int x_end;
int y_start;
int y_end;
};
const struct thread_block full_thread_blocks[THREADS] =
{
{ 0, 479, 0, 539 },
{ 480, 959, 0, 539 },
{ 960, 1439, 0, 539 },
{ 1440, 1919, 0, 539 },
{ 0, 479, 540, 1079 },
{ 480, 959, 540, 1079 },
{ 960, 1439, 540, 1079 },
{ 1440, 1919, 540, 1079 },
};
// Since this is multicore, the program must be able to know how many columns
// it should render for each block; it can't just loop through the entire
// window.
//
// This must be the difference between x_start and x_end in full_thread_blocks.
// And yes, all of them must have the same width.
#define FULL_THREAD_X_SIZE 480
// When switching from preview to full-mode rendering, the user might want to
// see a black screen so the progress of the render might be easier to see,
// or they might want to see the preview behind the partly-done full-mode render
// so the image progressively looks clearer.
#define SHOW_PREVIEW_WHEN_RENDERING 1
// Next, we define the same rendering settings, but for the preview mode. The
// preview mode differs in that it is much smaller and faster to render, but
// is almost identical in every other way.
// TODO: Need new resolution... but seems to still work for now?
#define PREVIEW_WIDTH 240
#define PREVIEW_HEIGHT 160
#define PREVIEW_WIDTH_RECIPROCAL (struct fp256){ SIGN_POS, { 0, 0x0111111111111111, 0x1111111111111111, 0x1111111111111111 } }
#define PREVIEW_HEIGHT_RECIPROCAL (struct fp256){ SIGN_POS, { 0, 0x0199999999999999, 0x9999999999999999, 0x9999999999999999 } }
#define PREVIEW_SHOW_X_INTERVAL 5
#define PREVIEW_THREAD_X_SIZE 60
const struct thread_block preview_thread_blocks[THREADS] =
{
{ 0, 59, 0, 79 },
{ 60, 119, 0, 79, },
{ 120, 179, 0, 79 },
{ 180, 239, 0, 79 },
{ 0, 59, 80, 159 },
{ 60, 119, 80, 159, },
{ 120, 179, 80, 159 },
{ 180, 239, 80, 159 },
};
// This function allows us to turn a screen position + size + center into a math
// position.
//
// math_pos = (screen_pos / width) * size + (center - 2 * size)
//
// Where width may be width or height, and size may be size_y or size_x. This
// function is supposed to be called once per dimension.
struct fp256 calculateMathPos(int screen_pos, struct fp256 width_reciprocal, struct fp256 size, struct fp256 center)
{
struct fp256 offset = fp_ssub256(center, fp_asr256(size));
return fp_sadd256(fp_smul256(fp_smul256(int_to_fp256(screen_pos), width_reciprocal), size), offset);
}
// This is the entry-point for each thread that will be spawned. Note that we
// pass all the data needed for it to render the block it is allocated to.
// Width/height etc may change depending on the resolution of render. We also
// pass a pointer to the pixels buffer, that will be write-only for the duration
// of the thread.
//
// Also note that the thread is one-off; that is, each thread only lives for 1
// frame, before another one is created for the next frame. There may be an
// overhead from doing so but I have not noticed any significant slowdowns.
struct thread_data
{
int x_start;
int x_end;
int y_start;
int y_end;
int width;
int height;
struct fp256 width_reciprocal;
struct fp256 height_reciprocal;
struct fp256 size;
struct fp256 center_x;
struct fp256 center_y;
unsigned long long iterations;
uint8_t *pixels;
};
#include <pthread.h>
void *thread(void *arg)
{
// First, we loop through all of the pixels we're responsible for, and calculate
// the math coordinates as necessary.
struct thread_data *data = (struct thread_data*)arg;
struct fp256 size_x = fp_smul256(data->size, SIZE_RATIO_X);
for (int screen_x = data->x_start; screen_x <= data->x_end; screen_x++)
{
struct fp256 math_x = calculateMathPos(screen_x, data->width_reciprocal, size_x, data->center_x);
for (int screen_y = data->y_start; screen_y <= data->y_end; screen_y++)
{
struct fp256 math_y = calculateMathPos(data->height - screen_y, data->height_reciprocal, data->size, data->center_y);
// From there, we can calculate the Mandelbrot result from the math coordinates,
// and assign it a color using our gradient function (or just black).
struct mb_result result = process_mandelbrot(math_x, math_y, data->iterations);
struct color color;
if (result.is_in_set)
color = (struct color){ 0, 0, 0 };
else
{
const static struct gradient gradient =
{
GRADIENT_STOP_COUNT,
GRADIENT_ITERATION_SIZE,
gradient_stops
};
color = gradient_color(gradient, (float)sqrt((double)result.escape_iterations));
}
// Pixels are stored in a RGBA format, with 1 byte per channel.
int r_offset = (screen_y*data->width + screen_x)*4;
data->pixels[r_offset + 0] = (uint8_t) color.r;
data->pixels[r_offset + 1] = (uint8_t) color.g;
data->pixels[r_offset + 2] = (uint8_t) color.b;
data->pixels[r_offset + 3] = 255;
}
}
// Finally, it's good practice to exit using this function just in case we need
// to return anything.
pthread_exit(NULL);
}
// ########
// # MAIN #
// ########
//
// I did intend to factor out various functions from main, but the way I
// structured the code makes it not really worth the time. Let this be a lesson
// to factor and comment early and not leave it to the last minute.
//
// Though... tell me if you like pure code more than semi-literate programming
// as seen above?
#include "SDL2/SDL_video.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#pragma clang diagnostic pop
#include <stdio.h>
// First, the initial mathematical center and size of the view.
#define INITIAL_CENTER_X (struct fp256){ SIGN_NEG, { 0, 0x8000000000000000, 0, 0 } } // -0.5
#define INITIAL_CENTER_Y (struct fp256){ SIGN_ZERO, {0} } // 0
#define INITIAL_SIZE (struct fp256){ SIGN_POS, { 2, 0, 0, 0 }} // 2
// The program uses a click-to-zoom mechanic, and thus we need to show the user
// an image so the user knows where they will be zooming into. As with all
// resources, the image may be found in the same folder as the executable.
#define ZOOM_IMAGE_PATH "zoom.png"
// Here, we may configure how much zoom we want to apply per click; it shall be
// calculated as size *= ZOOM when zooming in, and size /= ZOOM when zooming
// out. For instance, a zoom of 0.25 will zoom the user in by 4x every click.
// The zoom should divide WINDOW_WIDTH and WINDOW_HEIGHT, as we shall soon see.
#define ZOOM (struct fp256){ SIGN_POS, { 0, 0x4000000000000000, 0, 0 } } // 0.25
#define ZOOM_RECIPROCAL (struct fp256){ SIGN_POS, { 4, 0, 0, 0 } }
// Here, we define not the size of the image as seen on disk, but rather how big
// the image should be when rendered on screen. It should be obvious now why
// ZOOM should divide the width and height; it is so that we may have nice
// values for the zoom image, such as WINDOW_WIDTH / 4 and WINDOW_HEIGHT / 4.
#define ZOOM_IMAGE_SIZE_X 225
#define ZOOM_IMAGE_SIZE_Y 150
// We can also define how the iteration count increases. The iteration count
// increases linearly.
#define INITIAL_ITERATIONS 64
#define ITERATIONS_PER_CLICK 64
// If MOVIE is 1, then the program will become non-interactive and render a
// movie to disk instead. Some of the settings will be overriden with those seen
// here.
//
// You may use the dec2hex.py program to generate your own hexadecimal
// coordinates.
#define MOVIE 0
#define MOVIE_FULL_SHOW_X_INTERVAL 480
// Coordinates from "Eye of the Universe"
#define MOVIE_INITIAL_CENTER_X (struct fp256){ SIGN_POS, { 0, 0x5C38B7BB42D6E499, 0x134BFE5798655AA0, 0xCB8925EC9853B954 } }
#define MOVIE_INITIAL_CENTER_Y (struct fp256){ SIGN_NEG, { 0, 0xA42D17BFC55EFB99, 0x9B8E8100EB7161E1, 0xCA1080A9F02EBC2A } }
#define MOVIE_ZOOM_PER_FRAME (struct fp256){ SIGN_POS, { 0, 0xfa2727db62aebb76, 0x126ec75985ae7fe5, 0x1be434c7706da711 } }
//#define MOVIE_ZOOM_PER_FRAME (struct fp256){ SIGN_POS, { 0, 0xFD0F413D0D9C5EF1, 0xDBE485CFBA44A80F, 0x30D9409A2D2212AF } } // 0.5 / 60
#define MOVIE_PREFIX "movie/frame"
#define MOVIE_PREFIX_LEN 11
#define MOVIE_INITIAL_FRAME 2245
#define MOVIE_INITIAL_ITERATIONS 64
#define MOVIE_ITERATIONS_PER_FRAME 2
int main()
{
int err;
int exit_code = EXIT_SUCCESS;
// Start SDL
uint8_t *full_stored_pixels = NULL, *preview_stored_pixels = NULL;
SDL_Renderer *renderer = NULL;
SDL_Window *window = NULL;
SDL_Texture *full_texture = NULL, *preview_texture = NULL, *zoom_image = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "Unable to init SDL2: %s\n", SDL_GetError());
exit_code = EXIT_FAILURE;
goto cleanup;
}
if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) == -1)
{
fprintf(stderr, "Unable to create window and renderer: %s\n", SDL_GetError());
exit_code = EXIT_FAILURE;
goto cleanup;
}
SDL_SetWindowTitle(window, "Mandelbrot: Your CPU is on fire");
full_texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, WINDOW_WIDTH, WINDOW_HEIGHT);
if (full_texture == NULL)
{
fprintf(stderr, "Unable to create full texture: %s\n", SDL_GetError());
exit_code = EXIT_FAILURE;
goto cleanup;
}
if (SHOW_PREVIEW_WHEN_RENDERING)
{
if (SDL_SetTextureBlendMode(full_texture, SDL_BLENDMODE_BLEND) == -1)
{
fputs("Blend blendmode not supported on this platform.", stderr);
fputs("Preview may not be shown during the render.", stderr);
fputs("You may wish to disable SHOW_PREVIEW_WHEN_RENDERING.", stderr);
}
}
preview_texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, PREVIEW_WIDTH, PREVIEW_HEIGHT);
if (preview_texture == NULL)
{
fprintf(stderr, "Unable to create preview texture: %s\n", SDL_GetError());
exit_code = EXIT_FAILURE;
goto cleanup;
}
if (IMG_Init(IMG_INIT_PNG) == 0)
{
fprintf(stderr, "Unable to initialise SDL2 image: %s\n", IMG_GetError());
exit_code = EXIT_FAILURE;
goto cleanup;
}
zoom_image = IMG_LoadTexture(renderer, ZOOM_IMAGE_PATH);
if (zoom_image == NULL)
{
fprintf(stderr, "Unable to load zoom image: %s\n", IMG_GetError());
exit_code = EXIT_FAILURE;
goto cleanup;
}
if (SDL_SetTextureBlendMode(zoom_image, SDL_BLENDMODE_BLEND) == -1)
{
fputs("Blend blendmode not supported on this platform.", stderr);
fputs("Zoom image may not show correctly.", stderr);
}
puts("Mandelbrot started.");
// Main loop
struct fp256 size = INITIAL_SIZE;
struct fp256 center_x = MOVIE ? MOVIE_INITIAL_CENTER_X : INITIAL_CENTER_X;
struct fp256 center_y = MOVIE ? MOVIE_INITIAL_CENTER_Y : INITIAL_CENTER_Y;
unsigned long long iterations = MOVIE ? MOVIE_INITIAL_ITERATIONS : INITIAL_ITERATIONS;
unsigned long long zoom = 0;
int movie_current_frame = MOVIE_INITIAL_FRAME;
if (MOVIE)
{
for (int i = 0; i < MOVIE_INITIAL_FRAME - 1; i++)
{
size = fp_smul256(size, MOVIE_ZOOM_PER_FRAME);
iterations += MOVIE_ITERATIONS_PER_FRAME;
printf("Fast forward to frame %d...\n", i + 2);
}
}
static const int full_pixels_size = WINDOW_WIDTH * WINDOW_HEIGHT * 4;
static const int preview_pixels_size = PREVIEW_WIDTH * PREVIEW_HEIGHT * 4;
full_stored_pixels = calloc(1, full_pixels_size * sizeof(uint8_t));
preview_stored_pixels = calloc(1, preview_pixels_size * sizeof(uint8_t));
if (full_stored_pixels == NULL || preview_stored_pixels == NULL)
{
fputs("Unable to allocate memory for storing pixels.", stderr);
exit_code = EXIT_FAILURE;
goto cleanup;
}
enum state
{
STATE_PREVIEW,
STATE_FULL
};
enum state state = MOVIE ? STATE_FULL : STATE_PREVIEW;
bool haveToRender = true;
bool running = true;
while (running)
{
int mouse_x, mouse_y;
SDL_PumpEvents();
SDL_GetMouseState(&mouse_x, &mouse_y);
SDL_Window *window_cursor = SDL_GetMouseFocus();
bool cursor_in_window = window_cursor != NULL;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
{
running = false;
}
break;
case SDL_KEYDOWN:
{
if (MOVIE)
break;
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_TAB:
{
state = (state == STATE_FULL) ? STATE_PREVIEW : STATE_FULL;
// State init logic
haveToRender = true;
if (state == STATE_FULL)
memset(full_stored_pixels, 0, full_pixels_size);
else if (state == STATE_PREVIEW)
memset(preview_stored_pixels, 0, preview_pixels_size);
}
break;
case SDL_SCANCODE_H: