Close

Two Additions in one operation!

A project log for limited-code hacks/ideas

When you're running out of code-space... sometimes yah's gots to hack. Here are some ideas.

eric-hertzEric Hertz 04/12/2023 at 07:472 Comments

Maybe everyone knows this already, but I just thought of it for my first time...

Say you've got a 64bit processor, but you're working with two 32bit numbers... if they're stored in a single 64bit variable, and you're sure they won't overflow, you can do two additions simultaneously! Or 4 16bit additions, or 8 8bit!

So, what could this be used for? How often would that really be useful?

I dunno... a screen is significantly smaller than 65536x65536 pixels... 

....

Presently I'm using an 8bitter, I have a function that I would like to return two TRUE/FALSE values. I want to keep running sums of those two values from each time it's called.

//Returns:
//0x01 if button A pressed
//0x10 if button B pressed
//0x11 if both are pressed
uint8_t getButtons(void);

main()
{
   uint8_t countsCombined=0;

   for(i=0; i<15; i++)
      countsCombined+=getButtons();

   printf("A presses = %d\n"
          "B presses = %d\n",
          countsCombined&0x0f,
          countsCombined>>4);
}

But, holy moly, this seems a little cheesy on an 8bitter, but just think what could be done on a 16bitter, or 64bitter!

Maybe you're designing PONG on a 16bit computer in a 256x256 window, the ball moves two pixels up, one left:

//Upper byte is X, lower byte is Y

uint16_t ballPosition = 0x0000;

uint16_t ballStep = 0x0102;

while(wallNotHit)
{
   ballPosition += ballStep;
}

Discussions

Dylan Brophy wrote 04/12/2023 at 14:43 point

We need a compiler that automatically does these operations - I bet the code would be faster too!

  Are you sure? yes | no

Eric Hertz wrote 04/12/2023 at 20:55 point

Exactly! maybe they already do? 'Cause I never really could imagine it's really /that/ often software needs 64bit integers otherwise, aside from addressing.

  Are you sure? yes | no