### Author Topic: Rotate Bits SSE  (Read 4876 times)

#### guga

• Member
• Posts: 1357
• Assembly is a state of art.
##### Rotate Bits SSE
« on: April 27, 2020, 06:30:22 AM »
Hi Guys

I made a couple of routines to rotate bits from rigth and left using MMX instructions, but i´m failing to do it for Se2. Can someone help ? Also, can these be optimized ?

The MMX instruction are as this:

Rotate Left
Code: [Select]
`      rotl64 -  Rotate Left bits in MM0 register N times.       Parameters:                   Count (Input). The total amount of times the register should be shifted left       Return Value:                    The shifted value is stored in MM0 register. (See also, remarks below)       Remarks:                  The function uses MM0 register to perform the rotation. So, on input, MM0 must already be filled.       Example of usage:      [tttt: Q\$ 1] ; A qword variable that hold "1" as value      movq MM0 Q\$Value      call rotl64 1 ; rotate 1 left by bitProc rotl64:    Arguments @Count    mov eax D@Count    movq MM2 MM0    movd MM3 eax    sub eax 64    neg eax    movd MM4 eax    psllq MM0 MM3    psrlq MM2 MM4    por MM0 MM2EndP`

Rotate Right
Code: [Select]
`      rotr64 -  Rotate Right bits in MM0 register N times.       Parameters:                   Count (Input). The total amount of times the register should be shifted right       Return Value:                    The shifted value is stored in MM0 register. (See also, remarks below)       Remarks:                  The function uses MM0 register to perform the rotation. So, on input, MM0 must already be filled.      [tttt: Q\$ 1] ; A qword variable that hold "1" as value      movq MM0 Q\$Value      call rotr64 1 ; rotate 1 right by bitProc rotr64:    Arguments @Count    mov eax D@Count    movq MM2 MM0    movd MM3 eax    sub eax 64    neg eax    movd MM4 eax    psrlq MM0 MM3    psllq MM2 MM4    por MM0 MM2EndP`

Now...How to do a similar thing (left and right) for 128 Bits ? I tried this one below, but it is not working :(

Code: [Select]
`Proc rot128:    Arguments @Count    mov eax D@Count    movdqu XMM2 XMM0    movd XMM3 eax    sub eax 128    neg eax    movd XMM4 eax    psllq XMM0 XMM3    psrlq XMM2 XMM4    por XMM0 XMM2EndP`
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

#### daydreamer

• Member
• Posts: 1586
• building nextdoor
##### Re: Rotate Bits SSE
« Reply #1 on: April 27, 2020, 03:55:20 PM »

Now...How to do a similar thing (left and right) for 128 Bits ? I tried this one below, but it is not working :(

Code: [Select]
`Proc rot128:    Arguments @Count    mov eax D@Count    movdqu XMM2 XMM0    movd XMM3 eax    sub eax 128    neg eax    movd XMM4 eax    psllq XMM0 XMM3    psrlq XMM2 XMM4    por XMM0 XMM2EndP`
you should use PSLLDQ (double quadword) instead of PSLLQ (only 64bit quadword) versions instead
SIMD fan and macro fan
why assembly is fastest is because its switch has no (brakes) breaks
:P

#### jj2007

• Member
• Posts: 11440
• Assembler is fun ;-)
##### Re: Rotate Bits SSE
« Reply #2 on: April 27, 2020, 08:24:14 PM »
you should use PSLLDQ (double quadword) instead of PSLLQ (only 64bit quadword) versions instead

Marinus, Magnus, PSLLDQ shifts bytes while PSLLQ shifts bits.
« Last Edit: April 28, 2020, 02:53:33 AM by jj2007 »

#### mineiro

• Member
• Posts: 685
##### Re: Rotate Bits SSE
« Reply #3 on: April 27, 2020, 10:19:56 PM »
The version below is not SSE2. Sorry, I didn't check for optimization or others instructions.

mov rax,0123456789abcdefh   ;high
mov rdx,0123456789abcdefh   ;low
mov ecx,8         ;count
shld rax,rdx,cl         ;shift left rotate higher
shl rdx,cl         ;shift left rotate lower

Rotating to the left is to multiply the number N times by 2. Depending, you may need two variables for the carry even to create a rcl or rol version at each step.
Maybe this can be usefull to others tries.

---edit---
I assembled your source file and now understand. It's something like:
Code: [Select]
` mov rax,0f123456789abcdefh ;high mov rdx,0123456789abcdefh ;low mov rbx,0 mov ecx,8 ;count shld rbx,rax,cl                 ;rbx=high bits of rax shld rax,rdx,cl ;rotate n bits agregating shl rdx,cl ;shift left rotate lower, inserting zeros at right side or rdx,rbx                ;join, logical add`
« Last Edit: April 27, 2020, 11:24:47 PM by mineiro »
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

#### Siekmanski

• Member
• Posts: 2360
##### Re: Rotate Bits SSE
« Reply #4 on: April 27, 2020, 11:01:39 PM »
you should use PSLLDQ (double quadword) instead of PSLLQ (only 64bit quadword) versions instead

Marinus, PSLLDQ shifts bytes while PSLLQ shifts bits.

Am I PSHUFD with Magnus?
Creative coders use backward thinking techniques as a strategy.

#### mineiro

• Member
• Posts: 685
##### Re: Rotate Bits SSE
« Reply #5 on: April 28, 2020, 12:46:41 AM »
hello sir guga,
This is a quick try;  psllq deals with 2 qwords instead of 1 oword(128)

Code: [Select]
`.dataalign 32number    dq 0123456789abcdefh,0fedcba9876543210hlow_mask dq 0ffffffffffffffffh,0000000000000000h ;reversed because using qwordhigh_mask  dq 0000000000000000h,0ffffffffffffffffh.codemov eax,4 ;countmovdqu xmm0,oword ptr [number] ;xmm0=fedcba98765432100123456789abcdefmovdqu xmm1,xmm0movdqu xmm2,xmm0pand xmm1,oword ptr [high_mask] ;xmm1=FEDCBA98765432100000000000000000pand xmm2,oword ptr [low_mask] ;xmm2=00000000000000000123456789ABCDEFmovd xmm3,eax ;xmm3=counterpsllq xmm0,xmm3 ;xmm0=EDCBA98765432100123456789ABCDEF0sub eax,64neg eaxmovd xmm4,eax ;xmm4=3cpsrlq xmm1,xmm4 ;xmm1=000000000000000F0000000000000000psrlq xmm2,xmm4 ;xmm2=00000000000000000000000000000000pxor xmm3,xmm3 ;zeromovhlps xmm3,xmm1 ;high part of 1 to lower part of 3movlhps xmm3,xmm2 ;lower part of 2 to higher part of 3 ;xmm3=0000000000000000000000000000000Fpor xmm0,xmm3 ;concatenate`
--edit---
I measure both procedures and the first that don't uses SSE perform better in my machine.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

#### jj2007

• Member
• Posts: 11440
• Assembler is fun ;-)
##### Re: Rotate Bits SSE
« Reply #6 on: April 28, 2020, 02:54:05 AM »
Am I PSHUFD with Magnus?

Corrected, sorry

#### Siekmanski

• Member
• Posts: 2360
##### Re: Rotate Bits SSE
« Reply #7 on: April 28, 2020, 04:26:37 AM »
No worries, 'The mouth speaks what the heart is full of'
Creative coders use backward thinking techniques as a strategy.

#### guga

• Member
• Posts: 1357
• Assembly is a state of art.
##### Re: Rotate Bits SSE
« Reply #8 on: April 28, 2020, 06:15:59 AM »
Hi Mineiro, tks, but not working. Try with Rotating 64 and 65 Bits. The result will be 0.

number    dq 0123456789abcdefh,0fedcba9876543210h
The input are:
0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0111_0110_0101_0100_0011_0010_0001_0000_1000_1001_1010_1011_1100_1101_1110_1111

and the output should be:

Rotate 64 Bts
1110_1100_1010_1000_0110_0100_0010_0001_0001_0011_0101_0111_1001_1011_1101_1110_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000

Rotate 65 bits
1101_1001_0101_0000_1100_1000_0100_0010_0010_0110_1010_1111_0011_0111_1011_1100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

#### guga

• Member
• Posts: 1357
• Assembly is a state of art.
##### Re: Rotate Bits SSE
« Reply #9 on: April 28, 2020, 06:17:25 AM »
Ops...edited, wrong
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

#### mineiro

• Member
• Posts: 685
##### Re: Rotate Bits SSE
« Reply #10 on: April 28, 2020, 06:29:27 AM »
That's working or not to your needs sir guga? I can change the code to fit your needs.
I suppose shl deals with N-1, so, maximum counter can hold will be 63. Well, now that you said that I need review result with counter being zero, 64 or above.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

#### guga

• Member
• Posts: 1357
• Assembly is a state of art.
##### Re: Rotate Bits SSE
« Reply #11 on: April 28, 2020, 06:56:11 AM »
Hi Mineiro

No. It´s not working. The goal is to rotate left and right all the bits on a 128 bit data. What the code was doing is shifting the bits (also only for 64) which causes the zeroing of the lo half of the 128 bits, rather then rotating all bits by XXX times.
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

#### mineiro

• Member
• Posts: 685
##### Re: Rotate Bits SSE
« Reply #12 on: April 28, 2020, 08:11:41 AM »
Maybe this can work, please check. If this works we can deal with rotate right.
Code: [Select]
` .data align 32 number    dq 3,0 low_mask dq 0ffffffffffffffffh,0000000000000000h ;reversed because using qword high_mask  dq 0000000000000000h,0ffffffffffffffffh .code  mov eax,127 ;count range 0 to 128  movdqu xmm0,oword ptr [number]  movdqu xmm1,xmm0  .if eax >= 64      movhlps xmm0,xmm1 ;switch high and low     movlhps xmm0,xmm1     sub eax,64  .endif    movdqu xmm1,xmm0    movdqu xmm2,xmm0    pand xmm1,oword ptr [high_mask]    pand xmm2,oword ptr [low_mask]    movd xmm3,eax    psllq xmm0,xmm3    sub eax,64    neg eax    movd xmm4,eax    psrlq xmm1,xmm4    psrlq xmm2,xmm4    pxor xmm3,xmm3    movhlps xmm3,xmm1    movlhps xmm3,xmm2    por xmm0,xmm3`
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

#### Siekmanski

• Member
• Posts: 2360
##### Re: Rotate Bits SSE
« Reply #13 on: April 28, 2020, 09:21:41 AM »
Cool challenge.

EDIT: Sorry, I misunderstood the question and posted the wrong code.
I'll try again and if it works, post some code that does the job.
Creative coders use backward thinking techniques as a strategy.

#### mineiro

• Member
• Posts: 685
##### Re: Rotate Bits SSE
« Reply #14 on: April 28, 2020, 11:15:17 AM »
by XXX times.
obscene

I figured it was not necessary to use masks, I cleaned the code.
Code: [Select]
`  mov eax,127 ;count range 0 to 128  movdqu xmm0,oword ptr [number]  .if eax >= 64     movdqu xmm1,xmm0      movhlps xmm0,xmm1 ;switch high and low     movlhps xmm0,xmm1     sub eax,64  .endif    movdqu xmm1,xmm0    movd xmm3,eax    psllq xmm0,xmm3    sub eax,64    neg eax    movd xmm4,eax    psrlq xmm1,xmm4    movhlps xmm3,xmm1    movlhps xmm3,xmm1    por xmm0,xmm3`
I'd rather be this ambulant metamorphosis than to have that old opinion about everything