pret / pokeemerald

Decompilation of Pokémon Emerald

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"How the game works" overviews may need correction on u8 VS u32 use

purrfectdoodle opened this issue · comments

Hi there! I'm very new at all this so I might be totally wrong, but I was reading both this repo's "How the game works" pages and the GBA good/bad practices listed in the Tonc documentation, and I came across this bullet point on the Tonc website:

In a very real way, the 32bit integer is the only datatype the GBA has. The rest are essentially emulated, which carries a small performance penatly (2 extra shift instructions for bytes and halfwords). Do not use u8 or u16 for loop-indices for example, doing so can cut the speed of a loop in half! (The belief that using smaller types means lower memory-use only holds for aggregates and maybe globals; for local variables it actually costs memory). Likewise, if you have memory to copy or fill, using words can be about twice as fast as halfwords.

But on one of the pokeemerald "How the Game Works" articles (the one about the task system), I'm seeing this:

(A u8 just happens to be a lot smaller than a full u32 pointer, so it's more convenient to store for us.)

Who is right here? Thanks for bearing with me :)

It depends. The native size of a register is 32 bit and it does not make sense to use other types to store local variables. For accessing memory smaller types do make sense because you will get waitstates. For this context its probably fine to phrase it like that because the value is stored in memory (though the actual pointer will still be a word in size)

Alright, so does this mean I should always use u32 local variables, except for variables that are only used to store an address, which should be u8 ?

No, its not that easy. However the issue at hand is also very minor. Using a smaller type for a local variable will make your compiler emit a lsl/lsr pair to ensure the size. Unless your code is on a hot path this is most likely not an issue.

Okay, thank you! I still have a lot to learn then 😁