Skip to content

Convert rand multiplication constants to shifts and adds. - #916

Merged
kriegsman merged 2 commits into
FastLED:masterfrom
grandinquisitor:master
Feb 11, 2020
Merged

kriegsman merged 2 commits into
FastLED:masterfrom
grandinquisitor:master

Conversation

@grandinquisitor

Copy link
Copy Markdown
Contributor

Since many popular low-cost chipsets do not have a native multiply operation,
this calculation can be executed much more efficiently using only add and left shift.

Since many popular low-cost chipsets do not have a native multiply operation,
this calculation can be executed much more efficiently using only add and left shift.
@kriegsman

Copy link
Copy Markdown
Member

Hi @grandinquisitor - Thanks for the PR, and for the help. And on a personal note, I really appreciate the focus on speed of execution!

I know that the ATtiny microcontroller line has no on-chip hardware multiplier, but other than that, I think all of the other chips that FastLED supports have a hardware multiplier. Am I forgetting some other of our supported chips that don't have a multiply unit?

Also, I find myself wondering if the compiler itself will turn multiplication-by-a-compile-time-constant into a combination of shifts and adds in this case. I know that it does when you do things like x *= 2, and other 'no-brainer' cases. And of course a lot of that depends on how 'new' the compiler version is, too.

Anyway, I'm interested in this PR with two main questions: first, which 'popular low-cost chipsets' do not have a hardware multiplier other than the ATtiny. And second, do you have benchmark data yet that you could share about the speed change resulting from this code change? Run it a million times in a loop, that sort of thing. Any actual benchmark data would be appreciated; I always like making our library faster.

Thanks-
-Mark

@kriegsman

Copy link
Copy Markdown
Member

Writing some notes here:
On ATmega (which has MUL instruction), the plain multiply is 16 instructions, but the shift-add implementation is 23 instructions.
https://godbolt.org/z/DkpTLm

Haven't checked ATtiny yet.

@grandinquisitor Are there other FastLED-supported platforms that don't have a hardware multiplier, other than ATtiny?

@kriegsman kriegsman self-assigned this Jan 20, 2020
@grandinquisitor

Copy link
Copy Markdown
Contributor Author

Hi Mark,

Thanks for verifying the number of instructions. That's a cool tool.

I was hoping that it would still come out ahead on the atmega because while it has a MUL instruction, it's only 1 or 2 instructions if it's a single byte, but looks like it would expand it to ~19 instructions if it's a 2 byte int, per doc AVR201. And in this case it's a 2 byte int.

http://ww1.microchip.com/downloads/en/AppNotes/Atmel-1631-Using-the-AVR-Hardware-Multiplier_ApplicationNote_AVR201.pdf

It looks like it still comes out behind according to that tool.

Per code size, I noticed that it's 10 fewer bytes of compiled hex for attiny85 but is 8 more bytes for the arduino uno. (though, flash space is an order of magnitude more precious for attiny's).

I was able to get Serial output working on an attiny. Here are my results.

arduino uno (16mhz, external crystal):

multiply = 1320ms / 1 million executions

shift&add = 1383ms / 1 million executions

attiny24a (8mhz internal oscillator):

multiply = 21338ms / 1 million executions

shift&add = 3321ms / 1 million executions

So, in my tests it was 4.78% slower on the arduino uno, but was 84.44% faster for the attiny. There was very little variance in the results for any of the conditions.

So this is obviously a huge improvement for the attiny but does have a small negative cost for the atmega, which is presumably much more popular.

Is this the list of supported platforms? https://github.com/FastLED/FastLED#supported-platforms . AFAICT, only the attiny-based boards would not have a native multiply.

@grandinquisitor

Copy link
Copy Markdown
Contributor Author

FWIW a slight alternative occurred to me:

#define APPLY_FASTLED_RAND16_2053(x) (x << 11) + x + x + x

This is now only comes at a cost of 4 bytes of hex (vs 8), 5 extra instructions in that compiler tool (vs. 7 extra), and is only 4.2% slower (vs. 4.8% slower above) in my tests for the arduino uno.

@kriegsman

Copy link
Copy Markdown
Member

All of this talk about how to do specific efficient multiplies on an 8-bit microcontroller without a hardware multiplier, and without a barrel shifter, reminds me of programming the 6502, back in the day.

So: I am tempted to make this new implementation the default on ATtiny (or other MUL-less), but leave it with the multiply method on other platforms. I checked ARM (and PowerPC, just for fun), and the multiply method is never slower or larger than the shift method, and it is often a little smaller and faster, as it is on ATmega. I didn't check ESP/xtensa but I suspect it's similar.

There are other ATtiny-specific optimizations that we could also make (the 'scale8' family of functions comes to mind), but I'm mostly tempted not to pursue that right now, given that all of our other supported MCUs have single-cycle hardware multipliers.

We already have LIB8_ATTINY as a manifest constant that we can #ifdef. Actually, now that I look at it, it looks like we may have introduced a bug into detection of ATtiny at compile time! Hrm.

@kriegsman
kriegsman merged commit 1e6e56b into FastLED:master Feb 11, 2020
@kriegsman

Copy link
Copy Markdown
Member

Thanks for the contribution, and for the help with FastLED!

zackees added a commit that referenced this pull request Jul 1, 2026
)

FastLED-side companion to FastLED/framework-arduino-lpc8xx#35 (adds
`DMA_Type` + `DMA0` to LPC804's vendor CMSIS PAL, merged as
`1179200a30`) and FastLED/fbuild#916 (bumps `ACLPC_COMMIT` to that
SHA, merged as `f43acc3d82`).

## Changes

- Widen gate from `FL_IS_ARM_LPC_845` to
  `(FL_IS_ARM_LPC_845 || FL_IS_ARM_LPC_804)`.
- Add per-chip `FASTLED_LPC_SPI_DMA_CHANNEL` default:
    - LPC845: 4 (SPI0_TX per UM11029 Table 80) — unchanged.
    - LPC804: 0 (SPI0_TX per UM11065 §12.4 DMA trigger source table).
- Clear `#error` diagnostic if a user opts into `FASTLED_LPC_SPI_DMA`
  on LPC804 while the toolchain is still on the pre-#35 vendor PAL —
  points at the fbuild release requirement rather than cascading into
  a wall of `'DMA0' was not declared` errors.
- Update class + file docstring to reflect dual-chip support.

## Compile matrix

- `bash compile lpc804 --examples Blink` → ✅ succeeds (default, no
  SPI DMA opt-in, unaffected by the gate widening).
- `bash compile lpc845 --examples AutoResearch --defines
  "FASTLED_LPC_SPI_DMA=1"` → ✅ succeeds.
- `bash compile lpc804 --examples Blink --defines
  "FASTLED_LPC_SPI_DMA=1"` → ❌ fails with the new #error until a
  fbuild release picks up #916. Expected — the diagnostic message
  tells the user exactly what they need.

## Host tests

`bash test --cpp` → 344/344 passed. C++ lint → clean.

## Closes/refs

- Closes the FastLED-side scope of #3499 (LPC804 vendor-header
  prereq). The remaining acceptance criterion — "Compile LPC804 with
  `-DFASTLED_LPC_SPI_DMA=1` succeeds" — flips green automatically the
  moment fbuild ships a release with #916.
- Refs #3453 Phase 1 (LPC async DMA drivers meta) and #2845 Stage 4
  (LPC family expansion).

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants