Has anybody implemented a function to do Logical Shift Right operations on an integer in GNU PASCAL? SHR operator will do an arithmetic shift right. Or any suggestions on how to do this?
I am using gpc version 20010623, based on 2.95.2 19991024 (release) on Solaris 7 in an Intel box..
Thanks in advance for any assistance.
Jing Gloria Texas Instruments
Gloria, Jing wrote:
Has anybody implemented a function to do Logical Shift Right operations on an integer in GNU PASCAL? SHR operator will do an arithmetic shift right. Or any suggestions on how to do this?
Declare the variable as cardinal (or word) instead of integer.
SHR will then be a logical shift.
Maurice
On Fri, 7 Dec 2001, Maurice Lombardi wrote:
Gloria, Jing wrote:
Has anybody implemented a function to do Logical Shift Right operations on an integer in GNU PASCAL? SHR operator will do an arithmetic shift right. Or any suggestions on how to do this?
Declare the variable as cardinal (or word) instead of integer.
SHR will then be a logical shift.
Very clever!
I was wondering for a period of time however why no language implemented ROR and ROL operators (rotate right and left) - it may seem unnecessary, but sometimes you have to go to assembler just because C and Pascal miss this operator - it could be very useful, for example in filters, convolutions, CRC checksum calculation and graphics.
Of course, if a range of bytes is rotated, it's very useful to preserve carry flag somewhere - but I guess one has to do asm for that, doesn't he?
Mirsad
-- This message has been made up using recycled ideas and language constructs. No plant or animal has been injured in process of making this message.
Mirsad Todorovac wrote:
I was wondering for a period of time however why no language implemented ROR and ROL operators (rotate right and left) - it may seem unnecessary, but sometimes you have to go to assembler just because C and Pascal miss this operator - it could be very useful, for example in filters, convolutions, CRC checksum calculation and graphics.
Yes, I've thought about this when I translated the MD5 unit for GPC. Currently it does (left) rotation as (w shl s) or (w shr (32 - s)) (which is translated from similar C code).
The backend seems to have support for rotation, so it might be easy to add it in GPC (I haven't checked yet, but it appears so).
If we do it, I think it should not be operators (because that would change the syntax more than necessary), but rather simple predefines functions (`RotateLeft', `RotateRight'?).
Of course, if a range of bytes is rotated, it's very useful to preserve carry flag somewhere - but I guess one has to do asm for that, doesn't he?
I'm not even sure how easy it is in asm. At least on IA32, if my information is correct, "The Carry Flag will contain the value of the last bit rotated out", but the old value of CF is not rotated in, so this would take some extra work, anyway.
So I guess for the (few, I assume) situations where someone wants to rotate more than one work (not byte), they'll have to do some manual coding, anyway, whether in Pascal or asm. Am I missing something?
Frank
On Sat, 8 Dec 2001, Frank Heckenbach wrote:
Mirsad Todorovac wrote:
Yes, I've thought about this when I translated the MD5 unit for GPC. Currently it does (left) rotation as (w shl s) or (w shr (32 - s)) (which is translated from similar C code).
The backend seems to have support for rotation, so it might be easy to add it in GPC (I haven't checked yet, but it appears so).
If we do it, I think it should not be operators (because that would change the syntax more than necessary), but rather simple predefines functions (`RotateLeft', `RotateRight'?).
On the other hand, it belongs to the same family of operations as ``shl'' and ``shr'', doesn't it?
Of course, if a range of bytes is rotated, it's very useful to preserve carry flag somewhere - but I guess one has to do asm for that, doesn't he?
I'm not even sure how easy it is in asm. At least on IA32, if my information is correct, "The Carry Flag will contain the value of the last bit rotated out", but the old value of CF is not rotated in, so this would take some extra work, anyway.
On some processors there are both flavors of rotation (through carry, where old carry is rotated in - and rotation where the bit rotated out is rotated directly in).
If i wanted to scroll a large b&w bitmap, I would do it with one instruction per 32 points, while extracting the bit that travels between 32-bit words manually would take at least two additional instruction (masking it out from first word and or-ing it into second), which can be 3x slower.
But this is perhaps only a specific example.
So I guess for the (few, I assume) situations where someone wants to rotate more than one work (not byte), they'll have to do some manual coding, anyway, whether in Pascal or asm. Am I missing something?
well, if I had RORC: (which even ancient Z80 had)
+--+ +--+--+--+--+--+--+--+--+ +-> | | -> | | | | | | | | | -+ | +--+ +--+--+--+--+--+--+--+--+ | | | +---------------------------------------+
Then I could do form of loop unrolled shifting of neghboring words (for example a bitmap picture), sort of:
rorc (r0)+ rorc (r0)+ rorc (r0)+ rorc (r0)+ . . . rorc (r0)+
(in VAX-like assembler) Now, address register is auto-incremented, while carry flag transfers bits that travel between words.
But this is digression from PASCAL thematics - since I can't think of efficient and elegant syntax and semantics for carry flag in pascal.
But calculating a checksum that would be xor-ed and rotated with each new word isn't hard ot imagine, and isn't that rare use.
Marvin (I feel better as Marvin, my nickname from college than my real name).
-- This message has been made up using recycled ideas and language constructs. No plant or animal has been injured in process of making this message.
Mirsad Todorovac wrote:
On Sat, 8 Dec 2001, Frank Heckenbach wrote:
Mirsad Todorovac wrote:
Yes, I've thought about this when I translated the MD5 unit for GPC. Currently it does (left) rotation as (w shl s) or (w shr (32 - s)) (which is translated from similar C code).
The backend seems to have support for rotation, so it might be easy to add it in GPC (I haven't checked yet, but it appears so).
If we do it, I think it should not be operators (because that would change the syntax more than necessary), but rather simple predefines functions (`RotateLeft', `RotateRight'?).
On the other hand, it belongs to the same family of operations as ``shl'' and ``shr'', doesn't it?
And it's quite doubtful whether they should have been operators (Borland's decision) ...
Other operations like `Inc', `Dec' (also Borland extensions, incidentally) are also functions syntactically.
Since rotation is probably not used that much, I'd still prefer functions, even if it looks a little strange in the few cases, because it doesn't affect programs that use identifiers `ror' and `rol'.
Frank
"Gloria, Jing" wrote:
Has anybody implemented a function to do Logical Shift Right operations on an integer in GNU PASCAL? SHR operator will do an arithmetic shift right. Or any suggestions on how to do this?
I am using gpc version 20010623, based on 2.95.2 19991024 (release) on Solaris 7 in an Intel box..
Please don't use HTML.
Not especially fast, but portable on 2's complement machines:
FUNCTION lsr(i : integer) : integer; (* Avoiding all integer overflows *)
BEGIN (* lsr *) IF i >= 0 THEN lsr := i DIV 2 (* usual case *) ELSE IF (i + 1) = -maxint THEN lsr := maxint ELSE BEGIN i := (i + maxint + 1) DIV 2; (* now positive *) lsr := i + (maxint DIV 2) + 1; END; END; (* lsr - UNTESTED *)