gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Can't Add To A Pointer

#72259 - tonkinfash - Fri Feb 17, 2006 6:43 am

I am trying to substitute the multiplication by 240 by using bit shifts, but GCC won't let me add a cast integer to a pointer. It says 'invalid operands to binary +'.

Code:

static unsigned short *pointer;
unsigned int offset;

pointer = VIDEO_BUFFER;

y <<= 4;
offset = y;
y <<= 1;
offset += y;
y <<= 1;
offset += y;
y <<= 1;
offset += y;
offset >>= 1;
pointer += (unsigned short *) offset;

#72261 - tepples - Fri Feb 17, 2006 6:47 am

Bit shifts to replace multiplication by 240 won't buy you much on ARM7TDMI, the CPU architecture used in the Game Boy Advance system, because ARM7TDMI has a fast multiplier.

What are you planning to use a bitmap mode for?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#72263 - gauauu - Fri Feb 17, 2006 6:57 am

why are you casting offset to a pointer? When adding or subtracting to a pointer, you use a pointer and an integer. The resulting pointer is the original moved by the integer times the size of the object pointed for.

For example, if I have:
Code:

int dummy = 9;  //give x somewhere to point

int * x = &dummy;
int offset  = 3;

x += offset;


Then x will point to a memory location (3 * sizeof(int)) beyond dummy. Or, in other words, point to a spot "3 integers later".

edit: yuck...sorry for the messed up tags. phpBB's tag javascript drives me crazy.

#72265 - DekuTree64 - Fri Feb 17, 2006 7:03 am

Remove the cast. You can add an integer to a pointer, but not a pointer to a pointer.

And yes, bitshifting won't get you a whole lot, especially in THUMB mode. You'll save a couple cycles in ARM though, if you do it like this instead of that long shifting sequence:
Code:
pointer += (y << 8) - (y >> 4);

_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#72271 - poslundc - Fri Feb 17, 2006 8:11 am

If it's more efficient to multiply by a constant using shifting and adding, the optimizing compiler will usually do it for you.

On the other hand, if you wind up multiplying by the wrong number because your code is all obfuscated, the compiler won't be able to detect that for you.

Dan.

#72391 - LOst? - Sat Feb 18, 2006 6:40 am

DekuTree64 wrote:
Remove the cast. You can add an integer to a pointer, but not a pointer to a pointer.

And yes, bitshifting won't get you a whole lot, especially in THUMB mode. You'll save a couple cycles in ARM though, if you do it like this instead of that long shifting sequence:
Code:
pointer += (y << 8) - (y >> 4);


I must ask a question DekuTree64, even if it hasn't to do with this topic, it has to do with C optimations.

I have always wondered if this code:
Code:

index = y << 8;
index -= y >> 4;
pointer += index;


... will be optimized the same way as this code:
Code:
pointer += (y << 8) - (y >> 4);

_________________
Exceptions are fun

#72397 - gladius - Sat Feb 18, 2006 8:07 am

Lost: Depends on the compiler and whether you use the index variable later on. But usually yes, they would end up optimizing to the exact same thing.

#72404 - DekuTree64 - Sat Feb 18, 2006 9:17 am

I tend not to trust compilers in general when it comes to optimizing. I think doing the calculation all on one line like that looks a lot nicer, and if the compiler wastes an extra cycle or two making a temp variable (or doing a real multiply instead of optimizing to shifts, for that matter), it's probably wasting a lot more cycles than that elsewhere, making the first one insignificantly wasteful :)

Besides, if you know what assembly code you want the compiler to generate, and have to screw around compiling and checking the output to coax it into doing it right, you might as well just write the assembly yourself in the first place.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#72439 - tepples - Sat Feb 18, 2006 4:45 pm

DekuTree64 wrote:
Besides, if you know what assembly code you want the compiler to generate, and have to screw around compiling and checking the output to coax it into doing it right, you might as well just write the assembly yourself in the first place.

Unless you're trying to use the game logic for both a GBA version and a PC version, such as if you're prototyping on the PC or if you're making a PC demo to promote a commercial GBA game.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.