Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: Clyde on June 01, 2006

Title: SAR Equivalant command in FB
Post by: Clyde on June 01, 2006
wondered if anyone knows about doing this in Freebasic.

Here's the syntax.

Quote from: BB Sar Command
Sar 'repetitions'

Parameters
repetitions = number of shifts to make right 

Description
This performs a left binary shift on the value the specified number of times. This basically is a faster method of dividing the value exponentially. By shifting right once, you are dividing the value by 2. By shifting right twice, you divide by 4, etc.

Sar command varies from Shr whereas it fills blank bits shifted with copies of the sign bit, 0 for positive numbers and 1 for negative.
The usefulness of this command is basically faster math execution. 

Cheers and many many thanks,
Clyde.
Title: Re: SAR Equivalant command in FB
Post by: Stonemonkey on June 02, 2006
I don't think there's an equivalent in FB but you can use asm, there's 2 ways it could be done.

If you just want to shift right by a fixed number of bits then you can use:
Code: [Select]
asm sar dword ptr[my_value],2
would do an arithmetic shift right on my_value by 2 bits

If you want to shift by the value stored in another variable then you have to do something like
Code: [Select]
asm mov cl,byte ptr[shift_by]
asm sar dword ptr[my_value],cl

in these cases the shift_by is a byte and and my_value must be an integer. It can work on other data types but the code will need some modification.
Title: Re: SAR Equivalant command in FB
Post by: Clyde on June 02, 2006
Would I use that as a function dude. or just where I need a SAR?

Cheers matey,
Clyde.
Title: Re: SAR Equivalant command in FB
Post by: Stonemonkey on June 02, 2006
You could do either, although as a function you'd be adding a lot more instructions to deal with the function call which would defeat the purpose of using that for faster math.

Could be done with #define though to make things easier, give me a min and i'll see what i can come up with.
Title: Re: SAR Equivalant command in FB
Post by: Clyde on June 02, 2006
Cheers dude. All new to me, and really appreciate the help etc.
Nice one mate.
Title: Re: SAR Equivalant command in FB
Post by: Stonemonkey on June 02, 2006
Sorry, can't get it working with #define atm. Not sure if that's possible after all but i'll look into it.
Title: Re: SAR Equivalant command in FB
Post by: Stonemonkey on June 02, 2006
Just a point on using this and some other math, after a bit of investigation into how FB divides

a/b
uses the FPU even if they're both integers which means they have to be converted to floats and then the result has to be converted back to an integer

a\b
uses integer divide unless b is a power of 2 (and not a variable) in which case it uses SAR but there's a few other instructions to take care of the case of -1 which i never realised before

so something to watch out for would be that:

sar 1,any_number_of_shifts = 0

sar -1,any_number_of_shifts = -1

if that's going to cause any problems then use integer division (a\b)
Title: Re: SAR Equivalant command in FB
Post by: Clyde on June 02, 2006
No worries buddy :)

at the mo though dude, I'd simple use it like this:

asm sar dword ptr[fred],2

Willma=Fred + Pebbles

asm sar dword ptr[Barney],20

Betty=Barney+BamBam

Or am i on the wrong track?

Cheers,
Clyde.
Title: Re: SAR Equivalant command in FB
Post by: Stonemonkey on June 02, 2006
That's it, just remember that a negative number won't reach 0 no matter how much you shift it, it'll get to -1 and stay there.
Title: Re: SAR Equivalant command in FB
Post by: Clyde on June 02, 2006
Ok dude! Cheers for your help bud.
I'll tackle this some more in the morning.

Thankyou,
Clyde.
Title: Re: SAR Equivalant command in FB
Post by: Stonemonkey on June 02, 2006
Got it working, put this at the start of your code:

Code: [Select]
#define fb_sar(shift_variable,shift_constant) asm sar dword ptr[shift_variable],shift_constant

and then you can use

fb_sar(fred,10)

or whatever.
Title: Re: SAR Equivalant command in FB
Post by: TinDragon on June 02, 2006
What is the practical difference between using shr and sar ?
The only thing I can see it mention in the blitz docs is the bit about "Sar command varies from Shr whereas it fills blank bits shifted with copies of the sign bit, 0 for positive numbers and 1 for negative.
The usefulness of this command is basically faster math execution."

But what does that actually mean "fills blank bits shifted with copies of the sign bit"?

Is it saying that if you had for example in binary 10000111  that it would do 11000011 or am I way off ?
Title: Re: SAR Equivalant command in FB
Post by: Stonemonkey on June 02, 2006
Yep, your example is right.If you're working with signed ints/words/bytes the highest bit is the sign (1 is negative, 0 positive) If you were to use SHR 1 then all the bits are shifted right 1 place, the lowest bit of the original data is dropped and the highest (sign bit) of the result is filled with 0 so if it started as a negative number the result will no longer be negative as the sign bit has been filled with 0. with SAR 1 all bits are shifted right, the lowest bit is dropped but this time the sign bit is not changed.
Title: Re: SAR Equivalant command in FB
Post by: Clyde on June 02, 2006
That's just fab Stonemonkey mate. Welldone and thankyou.

Cheers,
Clyde.
Title: Re: SAR Equivalant command in FB
Post by: TinDragon on June 02, 2006
Seems all that binary stuff I did back in school on a BBC model B was useful after all  :D

Not sure what I would use SAR for as I dont recall using it in blitz, but I am sure someone can answer that, maybe Clyde if the project isnt a secret :)
Title: Re: SAR Equivalant command in FB
Post by: Shockwave on June 02, 2006
One thought that springs to mind would be colour cycling :)