Optimize bit operations, style fix, cleanup

This commit is contained in:
Rickey Fehr
2025-10-17 16:55:07 -07:00
parent e00c51502b
commit 72c0a6c4a9
7 changed files with 49 additions and 39 deletions
+4 -4
View File
@@ -14,10 +14,10 @@
#define MAX_CARDS_PER_HAND (NUM_SUITS * NUM_RANKS)
#define MAX_CARDS_ON_SCREEN 16
DECLARE_POOL_TYPE(Card);
DEFINE_POOL_TYPE(Card, MAX_CARDS_PER_HAND);
DECLARE_POOL_TYPE(CardObject);
DEFINE_POOL_TYPE(CardObject, MAX_CARDS_ON_SCREEN);
POOL_DECLARE_TYPE(Card);
POOL_DEFINE_TYPE(Card, MAX_CARDS_PER_HAND);
POOL_DECLARE_TYPE(CardObject);
POOL_DEFINE_TYPE(CardObject, MAX_CARDS_ON_SCREEN);
// Card sprites lookup table. First index is the suit, second index is the rank. The value is the tile index.
const static u16 card_sprite_lut[NUM_SUITS][NUM_RANKS] = {
+1 -1
View File
@@ -586,7 +586,7 @@ void sort_cards()
if (hand[i] != NULL)
{
// card_object_get_sprite() will not work here since we need the address
sprite_destroy(&hand[i]->sprite_object->sprite);
sprite_destroy(&(hand[i]->sprite_object->sprite));
}
}
+7 -6
View File
@@ -29,13 +29,14 @@ static const unsigned short *joker_gfxPal[] =
#undef DEF_JOKER_GFX
};
// Jokers in deck
DECLARE_POOL_TYPE(Joker)
DEFINE_POOL_TYPE(Joker, 8); // 8 should be fine, we only have a max of 2
// This won't be more than the number of jokers in your current deck
// plus the amount that can fit in the shop, 8 should be fine. For now...
#define MAX_ACTIVE_JOKERS 8
// Jokers on screen
DECLARE_POOL_TYPE(JokerObject)
DEFINE_POOL_TYPE(JokerObject, 8) // 8 should be fine here as well, max of 2 from the shop in game.c
POOL_DECLARE_TYPE(Joker)
POOL_DEFINE_TYPE(Joker, MAX_ACTIVE_JOKERS)
POOL_DECLARE_TYPE(JokerObject)
POOL_DEFINE_TYPE(JokerObject, MAX_ACTIVE_JOKERS)
const static u8 edition_price_lut[MAX_EDITIONS] =
{
+15 -9
View File
@@ -1,14 +1,19 @@
#include "pool.h"
void pool_clear_idx(PoolBitmap *bm, int idx) {
u32 i = idx / POOL_BITS_PER_WORD; // word offset
u32 b = idx % POOL_BITS_PER_WORD; // bit offset
bm->w[i] &= ~((POOL_WORD_T)1 << b);
void pool_bm_clear_idx(PoolBitmap *bm, int idx)
{
// Divide by 32 to get the word index
u32 i = idx >> 5;
// Get last 5-bits, same as a modulo (% 32) operation on positive numbers
u32 b = idx & 31;
bm->w[i] &= ~((u32)1 << b);
}
int pool_get_free_idx(PoolBitmap *bm) {
for (u32 i = 0; i < bm->nwords; i++) {
POOL_WORD_T inv = ~bm->w[i];
int pool_bm_get_free_idx(PoolBitmap *bm)
{
for (u32 i = 0; i < bm->nwords; i++)
{
u32 inv = ~bm->w[i];
// guard so we don't cal `ctz` with 0, since __builtin_ctz(0) is undefined
// https://gcc.gnu.org/onlinedocs/gcc/Bit-Operation-Builtins.html#index-_005f_005fbuiltin_005fctz
@@ -18,9 +23,10 @@ int pool_get_free_idx(PoolBitmap *bm) {
// than 0 indicates there is a free slot. Then, when counting the trailing 0's, you can test very quickly
// where the first free slot is. This operation prevents looping through every bit of filled flags, and
// will instead operate only on the first word with free slots.
if (inv) {
if (inv)
{
int bit = __builtin_ctz(inv);
bm->w[i] |= ((POOL_WORD_T)1 << bit);
bm->w[i] |= ((u32)1 << bit);
int idx = i * POOL_BITS_PER_WORD + bit;
return idx;
}
+4 -4
View File
@@ -16,11 +16,11 @@
OBJ_ATTR obj_buffer[MAX_SPRITES];
OBJ_AFFINE *obj_aff_buffer = (OBJ_AFFINE*)obj_buffer;
DECLARE_POOL_TYPE(SpriteObject)
DEFINE_POOL_TYPE(SpriteObject, MAX_SPRITES)
POOL_DECLARE_TYPE(SpriteObject)
POOL_DEFINE_TYPE(SpriteObject, MAX_SPRITES)
DECLARE_POOL_TYPE(Sprite)
DEFINE_POOL_TYPE(Sprite, MAX_SPRITES)
POOL_DECLARE_TYPE(Sprite)
POOL_DEFINE_TYPE(Sprite, MAX_SPRITES)
static bool free_affines[MAX_AFFINES] = {false};