Skip to content

Commit edac675

Browse files
committed
rainbowio: optimize for code size
.. this saves 76 bytes on trinket_m0
1 parent 9646d3f commit edac675

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

shared-module/rainbowio/__init__.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@
2727
#include "shared-bindings/rainbowio/__init__.h"
2828

2929
int32_t colorwheel(mp_float_t pos) {
30-
if (pos > 255) {
31-
pos = pos - ((uint32_t)(pos / 256) * 256);
32-
}
30+
pos = pos - ((uint32_t)(pos / 256) * 256);
31+
int shift1, shift2;
3332
if (pos < 85) {
34-
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
33+
shift1 = 8;
34+
shift2 = 16;
3535
} else if (pos < 170) {
3636
pos -= 85;
37-
return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
37+
shift1 = 0;
38+
shift2 = 8;
3839
} else {
3940
pos -= 170;
40-
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
41+
shift1 = 16;
42+
shift2 = 0;
4143
}
44+
int p = (int)(pos * 3);
45+
return (p << shift1) | ((255 - p) << shift2);
4246
}

0 commit comments

Comments
 (0)