??? 02/21/10 22:18 Read: times |
#173403 - add a cast here? Responding to: ???'s previous message |
Hi Jan,
thanks for the code!! Just a note: by inserting a cast at some places some probably unneeded (and likely unexpected) promotions to integer can be avoided: idx = (uint8_t)(idx + snakeTailIdx) % SNAKE_MAX_LEN; instead of: idx = (idx + snakeTailIdx) % SNAKE_MAX_LEN; The integer promotion then causes a call of a 16 bit modulus library function. Avoiding this with the appended patch the compiled binary uses 198 bytes less code memory:) Greetings, Frieder --- main.c.orig 2010-02-21 22:45:08.000000000 +0100 +++ main.c 2010-02-21 22:47:38.000000000 +0100 @@ -172,13 +172,13 @@ // ---- logical snake // -- snake utils uint8_t GetSnakePiece(uint8_t idx) { // get direction of a snake's piece - idx = (idx + snakeTailIdx) % SNAKE_MAX_LEN; + idx = (uint8_t)(idx + snakeTailIdx) % SNAKE_MAX_LEN; return ( ( (snake[idx / 4]) >> (2 * (idx % 4)) ) & 0x03 ); } void SetSnakePiece(uint8_t idx, uint8_t dir) { // store direction of a snake's piece dir &= 0x03; - idx = (idx + snakeTailIdx) % SNAKE_MAX_LEN; + idx = (uint8_t)(idx + snakeTailIdx) % SNAKE_MAX_LEN; snake[idx / 4] = (snake[idx / 4] & (~(0x03 << (2 * (idx % 4))))) | (dir << (2 * (idx % 4))); } @@ -360,7 +360,7 @@ tmpPos.all = snakeTailPos.all; SnakeMove(&snakeTailPos); // we don't need to check for result, as tail is guaranteed free movement towards body ;-) snakeLen--; - snakeTailIdx = (snakeTailIdx + 1) % SNAKE_MAX_LEN; + snakeTailIdx = (uint8_t)(snakeTailIdx + 1) % SNAKE_MAX_LEN; if (MoveTail(&tmpPos)) return true; // this is the normal course of things: we ate one piece of tail and then break to continue; } } |
Topic | Author | Date |
Snake on 8052.com SBC | 01/01/70 00:00 | |
As a C noob... | 01/01/70 00:00 | |
C-hatred | 01/01/70 00:00 | |
upgrade | 01/01/70 00:00 | |
8 bit LCD | 01/01/70 00:00 | |
LCD, 4 vs 8 bit | 01/01/70 00:00 | |
add a cast here?![]() | 01/01/70 00:00 |