-
-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] Count leading/trailing zeros and population count #527
Comments
What is it useful for? |
To give an example I just came across, this ugly macro here: https://github.com/pret/pokecrystal/blob/b50dd57cbb8e163cd269255c8f754e5631760a03/macros/code.asm#L26-L48 That could be trivially rewritten as:
|
I don't think this is appropriate as a built-in function, then. |
That case is a macro, but this would often be useful inline. And the truth is that there's no way to do this inline without invoking some sort of looping macro. The little example I posted in the chat is a great case of an inline use:
There's simply no way of achieving that behavior right now, short of hiding the entire comparison code in a looping macro. And that's bad for several reasons. |
With user-defined functions (#201) and a ternary operator (#621) (and
|
Also useful for counting the bits needed to store a number:
|
Converting a number to string just to count its digits is one of the oldest bad programming memes around... |
I'm not sure you'd need string conversion, either, but that might not be faster. Then again, we will see about that once #201 is implemented. |
You could also do:
|
Macro Assembler AS has these as |
Assuming user-defined functions will support recursion, and that we implement a short-circuiting ternary operator, these can be defined as follows:
|
We've decided to add Useful equivalences:
No plans to add Edit: This is an interesting list of use cases, none of which apply to GB ASM. :P |
So, ways to check whether
pokecrystal has this macro for checking it, which gets three uses: MACRO assert_power_of_2
assert (\1) & ((\1) - 1) == 0, "\1 must be a power of 2"
ENDM |
Having built-in functions to count the number of leading and trailing zeros in a number, as well as the number of set bits in it (population count), would be very useful, as this is something that is necessary every now and then and there's no way to do it without a loop.
Example:
...would output 26, 1, 3. (The leading zero count is based on 32-bit numbers. This is easy to adjust to narrower widths by subtraction; the function doesn't need to worry about guessing the correct width.)
The text was updated successfully, but these errors were encountered: