News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Problem with shift left << and LONGs

Started by kcsl, Feb 02, 2025, 07:16 PM

Previous topic - Next topic

kcsl

I think I've found a problem with Shift Left << It only seems to work for the first 16 bits.
Shift right seems to be ok.

Unless it's me... I'm tired and making silly mistakes now.

Regards,
Joe

    Dim lTest                    As Long  = 1
   
    SendDebugData("Shift Left")
    For nLoop = 0 To 23
        SendDebugData("Loop:" + Str$(Dec2 nLoop) + ", lTest:" + Str$(Bin24 lTest))
        lTest = lTest << 1
    Next nLoop
   
    SendDebugData("Shift Right")
    lTest = $800000
    For nLoop = 23 To 0 Step - 1
        SendDebugData("Loop:" + Str$(Dec2 nLoop) + ", lTest:" + Str$(Bin24 lTest))
        lTest = lTest >> 1
    Next nLoop

Shift Left
Loop:00, lTest:000000000000000000000001
Loop:01, lTest:000000000000000000000010
Loop:02, lTest:000000000000000000000100
Loop:03, lTest:000000000000000000001000
Loop:04, lTest:000000000000000000010000
Loop:05, lTest:000000000000000000100000
Loop:06, lTest:000000000000000001000000
Loop:07, lTest:000000000000000010000000
Loop:08, lTest:000000000000000100000000
Loop:09, lTest:000000000000001000000000
Loop:10, lTest:000000000000010000000000
Loop:11, lTest:000000000000100000000000
Loop:12, lTest:000000000001000000000000
Loop:13, lTest:000000000010000000000000
Loop:14, lTest:000000000100000000000000
Loop:15, lTest:000000001000000000000000
Loop:16, lTest:000000010000000000000000
Loop:17, lTest:000000000000000000000000
Loop:18, lTest:000000000000000000000000
Loop:19, lTest:000000000000000000000000
Loop:20, lTest:000000000000000000000000
Loop:21, lTest:000000000000000000000000
Loop:22, lTest:000000000000000000000000
Loop:23, lTest:000000000000000000000000
Shift Right
Loop:23, lTest:100000000000000000000000
Loop:22, lTest:010000000000000000000000
Loop:21, lTest:001000000000000000000000
Loop:20, lTest:000100000000000000000000
Loop:19, lTest:000010000000000000000000
Loop:18, lTest:000001000000000000000000
Loop:17, lTest:000000100000000000000000
Loop:16, lTest:000000010000000000000000
Loop:15, lTest:000000001000000000000000
Loop:14, lTest:000000000100000000000000
Loop:13, lTest:000000000010000000000000
Loop:12, lTest:000000000001000000000000
Loop:11, lTest:000000000000100000000000
Loop:10, lTest:000000000000010000000000
Loop:09, lTest:000000000000001000000000
Loop:08, lTest:000000000000000100000000
Loop:07, lTest:000000000000000010000000
Loop:06, lTest:000000000000000001000000
Loop:05, lTest:000000000000000000100000
Loop:04, lTest:000000000000000000010000
Loop:03, lTest:000000000000000000001000
Loop:02, lTest:000000000000000000000100
Loop:01, lTest:000000000000000000000010
Loop:00, lTest:000000000000000000000001
There's no room for optimism in software or hardware engineering.

trastikata

#1
Hello kcsl,

I think it's an anomaly, Byte 2 of the Long gets cleared before rotation. So the 17th rotation should be carried to Bit 0 of Byte 2, which happens, but then the Byte 2 gets cleared and only 0's remain to rotate.

P.s. Les, is it possible to do rotation with Carry directly storing the result back to the register avoiding the additional cycles of storing in WREG and then to file?

RA.jpg

top204

This has now been corrected, and I have created new code for Long variable left shifts. This problem has been with the compiler since I first added the 24-bit Long variables, nearly 10 years ago, and it has only now been spotted. Even when I ran beta testing for quite a few months, it was not spotted by me or others, so many thanks for spotting the problem, so it can be corrected.

It was shifting into a Word sized variable and not a Long type.

I am just running some more tests on the new compiler version and will place an update on the forum ASAP.

kcsl

Hi Les,
I'm really sorry, but I don't think this is quite right.

This code:
    Dim nTemp As Byte
    Dim lBuf As Long    
   
    For nTemp = 0 To 23
        lBuf = 1
        lBuf = (lBuf << nTemp.Byte0)
        SendDebugData("Loop:" + Str$(Dec2 nTemp) + ", Shift:" + Str$(Bin24 lBuf))
    Next nTemp

Loop:00, Shift:000000000000000000000001
Loop:01, Shift:000000000000000000000010
Loop:02, Shift:000000000000000000000100
Loop:03, Shift:000000000000000000001000
Loop:04, Shift:000000000000000000010000
Loop:05, Shift:000000000000000000100000
Loop:06, Shift:000000000000000001000000
Loop:07, Shift:000000000000000010000000
Loop:08, Shift:000000000000000000000000
Loop:09, Shift:000000000000000000000000
Loop:10, Shift:000000000000000000000000
Loop:11, Shift:000000000000000000000000
Loop:12, Shift:000000000000000000000000
Loop:13, Shift:000000000000000000000000
Loop:14, Shift:000000000000000000000000
Loop:15, Shift:000000000000000000000000
Loop:16, Shift:000000000000000000000000
Loop:17, Shift:000000000000000000000000
Loop:18, Shift:000000000000000000000000
Loop:19, Shift:000000000000000000000000
Loop:20, Shift:000000000000000000000000
Loop:21, Shift:000000000000000000000000
Loop:22, Shift:000000000000000000000000
Loop:23, Shift:000000000000000000000000

I've tried with and without the .byte0, and I tried making nTemp a long but to no avail.

Sorry.
Regards,
Joe
There's no room for optimism in software or hardware engineering.

trastikata

Hi kcsl,

the problem is in the generated assembler code where some arguments are missing, Les can advise on it:

AN.jpg

top204

#5
Sorry about that.

I went through dozens of the functions for Left Shifts and altered the code to make them better and more efficient etc, but I made a slip up in one function, where I used the code:

split_longin(pLongin);
split_dwordout(pLongout);


Instead of:

split_longin(pLongin);
split_longout(pLongout);


And it converted the wrong pieces of the pLongout parameter variable name. See how easy it is for a single line of code to cause so much of a problem with large source codes?

I was in such a hurry to correct the RAM bank Bit comparison anomaly with the new 18FxxQ devices, that I missed the anomaly. However, the assembler should have thrown up an error message with a parameter to a mnemonic missing, but it doesn't?

I have attached a zip file that contains the 4.0.5.4 version of the "Pos8.exe" file, that corrects the anomaly.

Drop the unzipped "Pos8.exe into the compiler's "PDS" folder: "C:\Program Files (x86)\ProtonIDE\PDS\". Replacing the existing file.

I will create a new update installer ASAP.

kcsl

There's no room for optimism in software or hardware engineering.