Shifts and masks

Topics related to the use of Oberon language features
Post Reply
Helpdesk
Posts: 33
Joined: Sat Jan 01, 2011 5:43 am
Contact:

Shifts and masks

Post by Helpdesk » Mon Jan 03, 2011 6:01 am

How is the best way to do the Pascal equivalent of:

Code: Select all

R := ( R shr 6) and $3FF;

cfbsoftware
Site Admin
Posts: 493
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Shifts and masks

Post by cfbsoftware » Mon Jan 03, 2011 6:12 am

There are several possible ways. The style most often seen in Oberon code is:

Code: Select all

R := LSR(R, 6) MOD 0400H;

The code generated is:

Code: Select all

E1B0B32BH   MOV  S  R11,R11 LSR 6    
E1A0BB0BH   MOV     R11,R11 LSL 22    
E1B0BB2BH   MOV  S  R11,R11 LSR 22
An alternative is:

Code: Select all

R := LSR(LSL(value, 16));
Initially this might appear less readable. However, it does makes it clearer that you are completely ignoring the top 16 bits. The code generated uses one less instruction than the previous example:

Code: Select all

E1B0B80BH   MOV  S  R11,R11 LSL 16    
E1B0BB2BH   MOV  S  R11,R11 LSR 22
Choose the one that you think most clearly illustrates the problem being solved.

pompey
Posts: 3
Joined: Tue Jan 04, 2011 12:44 pm

Re: Shifts and masks

Post by pompey » Sat Mar 26, 2011 8:25 pm

I'm sure its obvious to everyone else - but just in case:

I think

Code: Select all

R := LSR(LSL(value, 16));
Should be

Code: Select all

R := LSR(LSL(value, 16), 22);

cfbsoftware
Site Admin
Posts: 493
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Shifts and masks

Post by cfbsoftware » Sat Mar 26, 2011 11:07 pm

Yes - of course. Sorry about the blooper!

With the benefit of hindsight, it might be clearer to write it as two separate statements:

Code: Select all

R := LSL(value, 16);
R := LSR(R, 22);
If this form is used in a leaf procedure you do not need to worry about efficiency as it generates code which is identical to the single-line version.

Post Reply