Skip to content

Commit e551a2d

Browse files
committed
randomly speed up mandelbrot demo :-)
1 parent fc0101e commit e551a2d

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

scanvideo/mandelbrot/mandelbrot.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#if PICO_ON_DEVICE
1818

1919
#include "hardware/clocks.h"
20-
#include "hardware/structs/vreg_and_chip_reset.h"
20+
#include "hardware/vreg.h"
2121

2222
#endif
2323

@@ -44,10 +44,38 @@ static inline fixed float_to_fixed(float x) {
4444
return (fixed) (x * (float) (1u << FRAC_BITS));
4545
}
4646

47+
#if !PICO_ON_DEVICE || (FRAC_BITS != 25)
4748
static inline fixed fixed_mult(fixed a, fixed b) {
4849
int64_t r = ((int64_t) a) * b;
4950
return (int32_t) (r >> FRAC_BITS);
5051
}
52+
#else
53+
// Since we're trying to go fast, do a better multiply of 32x32 preserving the bits we want
54+
static inline fixed fixed_mult(fixed a, fixed b) {
55+
uint32_t tmp1, tmp2, tmp3;
56+
__asm__ volatile (
57+
".syntax unified\n"
58+
"asrs %[r_tmp1], %[r_b], #16 \n" // r_tmp1 = BH
59+
"uxth %[r_tmp2], %[r_a] \n" // r_tmp2 = AL
60+
"muls %[r_tmp2], %[r_tmp1] \n" // r_tmp2 = BH * AL
61+
"asrs %[r_tmp3], %[r_a], #16 \n" // r_tmp3 = AH
62+
"muls %[r_tmp1], %[r_tmp3] \n" // r_tmp1 = BH * AH
63+
"uxth %[r_b], %[r_b] \n" // r_b = BL
64+
"uxth %[r_a], %[r_a] \n" // r_a = AL
65+
"muls %[r_a], %[r_b] \n" // r_a = AL * BL
66+
"muls %[r_b], %[r_tmp3] \n" // r_b = BL * AH
67+
"add %[r_b], %[r_tmp2] \n" // r_b = BL * AH + BH * AL
68+
"lsls %[r_tmp1], #32 - 25 \n" // r_tmp1 = (BH * AH) >> (32 - FRAC_BITS)
69+
"lsrs %[r_a], #16 \n" // r_a = (AL & BL) H
70+
"add %[r_a], %[r_b] \n"
71+
"asrs %[r_a], #25- 16 \n" // r_a = (BL * AH + BH * AL) H | (AL & BL) H >> (32 - FRAC_BITS)
72+
"add %[r_a], %[r_tmp1] \n"
73+
: [r_a] "+l" (a), [r_b] "+l" (b), [r_tmp1] "=&l" (tmp1), [r_tmp2] "=&l" (tmp2), [r_tmp3] "=&l" (tmp3)
74+
:
75+
);
76+
return a;
77+
}
78+
#endif
5179

5280
#endif
5381

@@ -247,7 +275,7 @@ int main(void) {
247275
#endif
248276
#if PICO_ON_DEVICE
249277
#ifdef TURBO_BOOST
250-
hw_set_bits(&mm_vreg_and_chip_reset->vreg, VREG_AND_CHIP_RESET_VREG_VSEL_BITS);
278+
vreg_set_voltage(VREG_VOLTAGE_1_30);
251279
sleep_ms(10);
252280
set_sys_clock_khz(base_freq*6, true);
253281
#else

0 commit comments

Comments
 (0)