Add a built-in `POPCOUNT` function
- Dominant language
- C++
- Stars
- 1.6k
- Forks
- 193
- Avg merge
- 22h 17m
- Merged PRs (30d)
- 26
Description
Eventually:tm: we'll have user-defined functions (#201), and people can implement all kinds of inline numeric logic -- even `POPCOUNT`, assuming they support recursion and a short-circuiting ternary operator:
```asm
DEF POPCOUNT(x) := x ? (1 + POPCOUNT(x & (x - 1))) : 0
```
However, that's not an immediately obvious definition, and we won't have user-defined functions for a long while yet, so it would be helpful to provide `POPCOUNT` specifically as a built-in. Unlike some desired functions, you can't just "fake it" with a single expression (e.g. `DEF tiles EQUS "* 16"` instead of `DEF tiles(x) := x * 16`); you have to use a `REPT`/`FOR` loop.
I'm bringing this up again because someone in pret was figuring out a `mulhl` macro (an instruction macro to optimally "multiply `hl` by argument"; yes I know the arguments against such things, but when users are going to write them anyway, rgbasm shouldn't make them even *more* verbose).
`POPCOUNT` will make sense along with our `BITWIDTH` and `TZCOUNT` functions.
Contributor guide
Research direction
Start by locating the existing BITWIDTH and TZCOUNT built-in functions and their tests, since POPCOUNT is intended to fit alongside them. Add the built-in with the recursive behavior described in the issue, then verify its results across representative numeric inputs using the existing test structure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100