Challenge: Make a data type for holding one of 8 directions allowing increment and overflow
Liam McGillivray
yoshi.pit.link.mario at gmail.com
Thu Mar 14 23:39:33 UTC 2024
On Thursday, 14 March 2024 at 01:58:46 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> The cost of an add + increment then a bitwise and is only 2-4
> cycles on a Haswell cpu. Depending upon if its working solely
> in registers (via inlining) or its operating on ram.
>
> Whereas if you need to do branching (if statement, loops), this
> is an unpredictable cost and loops where a simple bitwise
> operation can be done is out right non-optimized.
>
> As for exceptions, totally not required. You can solve this by
> simply making your state private so nobody else can mutate it.
> Bounds checking will ensure if the state is corrupted it'll
> error out if you use the lookup method I suggested above.
I tried to rework the functions to use bitwise operations, but it
was difficult to figure out the correct logic. I decided that
it's not worth the hassle, so I just changed the value storage
from `bool[3]` to `ubyte`. Now it works much more like your
version.
https://github.com/LiamM32/Open_Emblem/blob/c2014ab3f77e89c0cedcd6dbf7f8362ebfac33a9/source/common.d
I did a little reading, so now I understand what it means when
you have `&= 7`. But I want to ask, is this faster than `%= 8`?
If not, I would like to change it to the latter for readability.
More information about the Digitalmars-d-learn
mailing list