Author Topic: [Resource] Fast bitwise (un)signed integer addition/subtraction  (Read 2235 times)

function add(%x, %y)
{
   %a = 1;

   while (%a)
   {
      %a = %x & %y;
      %b = %x ^ %y;
      %x = %a << 1;
      %y = %b;
   }

   return %b;
}


==>echo(add(5, -3));
2
==>echo(add(100, 50));
150
==>echo(add(-999999, -65000));
-1064999


Obligatory obvious fact: Numbers where abs(n) > 999999 work just fine.
Note: That other topic about addition has nothing to do with this.

Note: That other topic about addition has nothing to do with this.


i'm trying to wrap my brain around this. i think i get it.
what makes this faster than simply adding? surely torque has an efficient way of doing it? or is it bogged down by attempting to interpret expressions before determining it's a mathematical operation?

i'm trying to wrap my brain around this. i think i get it.
what makes this faster than simply adding? surely torque has an efficient way of doing it? or is it bogged down by attempting to interpret expressions before determining it's a mathematical operation?

The "fast" in the title indicates that it's a fast way of doing it, not that it's faster than the way Torque does it.
The whole point is being able to use the full 32-bit range, rather than being restricted to -999999~999999.


The "fast" in the title indicates that it's a fast way of doing it, not that it's faster than the way Torque does it.
The whole point is being able to use the full 32-bit range, rather than being restricted to -999999~999999.
Presumably you can.

($b is uninitialized)
echo(~$b >> 1);
returns
2147483647
When you add one to that, it starts representing it in scientific notation. I'm currently trying to figure out how many bits it uses to store that value.

Torque uses uint32 and float32 to store numeric values.

Torque uses uint32 and float32 to store numeric values.

I happen to know for a fact that uints do not represent negative values, ie -2147483648, and store double the positive range of their signed components, ie 4294967295.

I happen to know for a fact that uints do not represent negative values, ie -2147483648, and store double the positive range of their signed components, ie 4294967295.

The TS bytecode compiler seems to like using float32 for nearly everything, though.

function add(%a,%b)
{
   return ((%a | 0) + (%b | 0)) | 0;
}


?

function add(%a,%b)
{
   return ((%a | 0) + (%b | 0)) | 0;
}


?

Too many steps and too many parentheses.
function add(%a, %b) { return %a + %b | 0; }

Note: I'm pretty sure that EVERY OTHER LANGUAGE treats + as higher-precedence than |, but this is TorqueScript. Let's abuse the little quirks for all they're worth.

Yeah, that should work too. I was just being safe.

How is this useful though


Too many steps and too many parentheses.
function add(%a, %b) { return %a + %b | 0; }

Note: I'm pretty sure that EVERY OTHER LANGUAGE treats + as higher-precedence than |, but this is TorqueScript. Let's abuse the little quirks for all they're worth.
I did some more thorough testing on this, and doing it like that will actually cause some cases to return incorrect results. To guarentee that they'll be correct you have to use ( ( %a | 0 ) operator ( %b | 0 ) ) | 0