Recent

Author Topic: rrc and rlc operations  (Read 525 times)

bobihot

  • New Member
  • *
  • Posts: 44
rrc and rlc operations
« on: November 19, 2025, 03:33:15 pm »
Hi Dears,
I and other often need of bit operations multi word. For this is good to can transfer by processor flags. I consider that good to have rrc & rlc  - Rotate Right by Carry & Rotate Left by Carry. This made no need of use mask of bit, assign to variable and set to next word. This is build in may be any processor & assembler instructions.

p:= (v and 1);
n:= (n shr 1) or (p shl 63);
v:= (v shr 1);

- 67 machine cycles and non readable

Will be:

v := v rrc 1;
n := n rrc 1;

- 2 machine cycles

Also to can use flags as boolean values:
if (Flag_c) then ... ;
IfThen (not Flag_z;5;7);

Thanks

creaothceann

  • Sr. Member
  • ****
  • Posts: 263
Re: rrc and rlc operations
« Reply #1 on: November 19, 2025, 05:38:58 pm »
Free Pascal has Ror* and Rol*.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: rrc and rlc operations
« Reply #2 on: November 20, 2025, 11:15:34 am »
For a lack of a compilable example - as usual by creaothceann, there are none  -, this is what the syntax is and what the compiler generates:
Code: Pascal  [Select][+][-]
  1. // full program
  2. var
  3.   v,n:byte;
  4. begin
  5.   v := %10000000; n := %00000001;
  6.   v := rorbyte(v, 1);
  7.   n := rorbyte(n, 1);
  8.   writeln(v :4, n:4);
  9. end.
Code: ASM  [Select][+][-]
  1. # Var n located in register al
  2. # Var v located in register al
  3. # [6] v := rorbyte(v, 1);
  4.         movb    $64,%al
  5. # Var n located in register sil
  6. # [7] n := rorbyte(n, 1);
  7.         movb    $128,%sil
  8.  

The compiler makes it one immediate instead of performing the ror, depending on optimization.
Which is even better than you requested... :D

This depends on your code of course, but otherwise the compiler performs  a straight ror instruction if supported by the particular cpu.
Code: Pascal  [Select][+][-]
  1. //full program
  2. {$mode objfpc}
  3. uses
  4.   sysutils;
  5. var
  6.   v,n:byte;
  7. begin
  8.   v := random(256); n := random(256);
  9.   writeln('v:',v.tobinstring:10);
  10.   writeln('n:',n.tobinstring:10);
  11.   v := rorbyte(v, 1);
  12.   n := rorbyte(n, 1);
  13.   writeln('v:',v.tobinstring:10);
  14.   writeln('n:',n.tobinstring:10);
  15. end.
Code: ASM  [Select][+][-]
  1. # [11] v := rorbyte(v, 1);
  2.         rorb    $1,U_$P$UNTITLED_$$_V(%rip)
  3. # [12] n := rorbyte(n, 1);
  4.         rorb    $1,U_$P$UNTITLED_$$_N(%rip)



« Last Edit: November 20, 2025, 04:34:49 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: rrc and rlc operations
« Reply #3 on: November 20, 2025, 08:45:18 pm »
In all cases, with the important caveat that the various shifting etc. instructions typically have a "number of bits" limitation that one can only find out by referring to CPU documentation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: rrc and rlc operations
« Reply #4 on: November 21, 2025, 03:08:35 pm »
True.
The limits are rolbyte,word,dword,qword.

But it depends on the cpu if a ror/rol instruction can be done in a single operation.
But at least Intels and AMDs and ARMs are covered.
The operators work on all platforms, but the optimizations may differ between cpu's
[edit]
forgot to mention that shift arithmatic etc are also supported.

(Not to mention that all this is simply in the manuals)
« Last Edit: November 21, 2025, 04:12:06 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: rrc and rlc operations
« Reply #5 on: November 21, 2025, 04:25:56 pm »
I don't think we get an answer from a troll.....
Just as unlikely as compilable code from creaothceann. (a troll)
« Last Edit: November 21, 2025, 04:28:13 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018