FastLED 3.9.15
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages

◆ scale8()

LIB8STATIC_ALWAYS_INLINE uint8_t scale8 ( uint8_t i,
fract8 scale )

Scale one byte by a second one, which is treated as the numerator of a fraction whose denominator is 256.

In other words, it computes i * (scale / 256)

Parameters
iinput value to scale
scalescale factor, in n/256 units
Returns
scaled value
Note
Takes 4 clocks on AVR with MUL, 2 clocks on ARM
Examples
Fire2012WithPalette.ino, NoisePlusPalette.ino, Pacifica.ino, and XYMatrix.ino.

Definition at line 40 of file scale8.h.

40 {
41#if SCALE8_C == 1
42#if (FASTLED_SCALE8_FIXED == 1)
43 return (((uint16_t)i) * (1 + (uint16_t)(scale))) >> 8;
44#else
45 return ((uint16_t)i * (uint16_t)(scale)) >> 8;
46#endif
47#elif SCALE8_AVRASM == 1
48#if defined(LIB8_ATTINY)
49#if (FASTLED_SCALE8_FIXED == 1)
50 uint8_t work = i;
51#else
52 uint8_t work = 0;
53#endif
54 uint8_t cnt = 0x80;
55 asm volatile(
56#if (FASTLED_SCALE8_FIXED == 1)
57 " inc %[scale] \n\t"
58 " breq DONE_%= \n\t"
59 " clr %[work] \n\t"
60#endif
61 "LOOP_%=: \n\t"
62 /*" sbrc %[scale], 0 \n\t"
63 " add %[work], %[i] \n\t"
64 " ror %[work] \n\t"
65 " lsr %[scale] \n\t"
66 " clc \n\t"*/
67 " sbrc %[scale], 0 \n\t"
68 " add %[work], %[i] \n\t"
69 " ror %[work] \n\t"
70 " lsr %[scale] \n\t"
71 " lsr %[cnt] \n\t"
72 "brcc LOOP_%= \n\t"
73 "DONE_%=: \n\t"
74 : [work] "+r"(work), [cnt] "+r"(cnt)
75 : [scale] "r"(scale), [i] "r"(i)
76 :);
77 return work;
78#else
79 asm volatile(
80#if (FASTLED_SCALE8_FIXED == 1)
81 // Multiply 8-bit i * 8-bit scale, giving 16-bit r1,r0
82 "mul %0, %1 \n\t"
83 // Add i to r0, possibly setting the carry flag
84 "add r0, %0 \n\t"
85 // load the immediate 0 into i (note, this does _not_ touch any flags)
86 "ldi %0, 0x00 \n\t"
87 // walk and chew gum at the same time
88 "adc %0, r1 \n\t"
89#else
90 /* Multiply 8-bit i * 8-bit scale, giving 16-bit r1,r0 */
91 "mul %0, %1 \n\t"
92 /* Move the high 8-bits of the product (r1) back to i */
93 "mov %0, r1 \n\t"
94 /* Restore r1 to "0"; it's expected to always be that */
95#endif
96 "clr __zero_reg__ \n\t"
97
98 : "+d"(i) /* writes to i; r16-r31, restricted by ldi */
99 : "r"(scale) /* uses scale */
100 : "r0", "r1" /* clobbers r0, r1 */
101 );
102 /* Return the result */
103 return i;
104#endif
105#else
106#error "No implementation for scale8 available."
107#endif
108}
UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f)

References LIB8STATIC_ALWAYS_INLINE, and scale.

Referenced by DATA_NOP::adjust(), beatsin8(), brighten8_lin(), brighten8_raw(), fl::ColorFromPalette(), fl::ColorFromPalette(), dim8_lin(), dim8_raw(), fl::Fire2012::draw(), ease8InOutQuad(), fl::fadeUsingColor(), fill_raw_2dnoise16into8(), fill_raw_2dnoise8(), fillnoise8(), fl::NoisePalette::fillnoise8(), Fire2012WithPalette(), Fire2023(), hsv2rgb_rainbow(), hsv2rgb_spectrum(), PixelController< RGB_ORDER, LANES, MASK >::init_binary_dithering(), lerp7by8(), lerp8by8(), PixelController< RGB_ORDER, LANES, MASK >::loadAndScale(), PixelController< RGB_ORDER, LANES, MASK >::loadAndScale(), loop(), map8(), fl::nblend(), fl::Pacifica::pacifica_add_whitecaps(), pacifica_add_whitecaps(), fl::Pacifica::pacifica_deepen_colors(), pacifica_deepen_colors(), rgb2hsv_approximate(), rgb_2_rgbw_exact(), rgb_2_rgbw_max_brightness(), rgb_2_rgbw_null_white_pixel(), rgb_2_rgbw_white_boosted(), PixelController< RGB_ORDER, LANES, MASK >::scale(), and PixelController< RGB_ORDER, LANES, MASK >::scale().

+ Here is the caller graph for this function: