Page 1 of 1

bug?? swap 520lts (solved)

Posted: Sat Sep 21, 2013 7:31 am
by kernadec
hello

swap function works more
test system (x86) windows xp sp3

best regard

Code: Select all

Dim tableau1(5): Dim tableau2(5)

For i=0 To 5
  tableau1(i)=i
  tableau2(i)=i+10
Next i
;Swap tableau1(),tableau2()        ; works to  PB500 
;Swap tableau1(),tableau2()        ; no works to  PB520 
Swap tableau1(5),tableau2(5)      ; PB 520LTS Swaps not the content of the tables

For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i

;text in the doc  ---> Swap Tableau1() , Tableau2()    ; Swaps the contents of the tables

Re: bug?? swap 520lts

Posted: Sat Sep 21, 2013 7:42 am
by netmaestro
Your uncommented line is swapping correctly here, PB 5.20LTS x86. Your commented line is not documented to work, as only array elements are allowed, not the whole array:
PB doc for Swap command wrote:The both <expression> have to be a variable, array, linked-list or a map element
;text in the doc ---> Swap Tableau1() , Tableau2() ; Swaps the contents of the tables
I can't find that syntax anywhere in the doc for 5.20...

Re: bug?? swap 520lts

Posted: Sat Sep 21, 2013 8:02 am
by kernadec
thank you netmaestro

the French doc seems to say that the entire table is swapped
if I understand I have to write like this

Code: Select all

Dim tableau1(5): Dim tableau2(5) 
	 
	For i=0 To 5 
	tableau1(i)=i 
	tableau2(i)=i+10 
	Next i 
	;Swap tableau1(),tableau2() ; works to PB500  
	;Swap tableau1(),tableau2() ; no works to PB520  

	;Swap tableau1(5),tableau2(5)      ; PB 520LTS Swaps not the content of the tables 

	For i=0 To 5 
    Swap tableau1(i),tableau2(i)      ; PB 520LTS Swaps ok
	Debug tableau1(i) 
	Debug tableau2(i) 
	Next i 
	 
	;text in the doc ---> Swap Tableau1() , Tableau2() ; Swaps the contents of the tables 

Re: bug?? swap 520lts

Posted: Sat Sep 21, 2013 8:34 am
by Demivec
kernadec wrote:the French doc seems to say that the entire table is swapped
if I understand I have to write like this
It was written in the help file before version 5.10 and would allow you to swap arrays that were the same size (and only the same size) using that syntax.

Fred changed this because he says it wasn't supported and was never intended to function that way (even though the example found its way into the Help file).

Now it no longer works for arrays in any situation and the example has been removed from the help file.

Re: bug?? swap 520lts (résolu)

Posted: Sat Sep 21, 2013 8:42 am
by kernadec
thank you Demivec

PB5.20 bug: 'Swap' does not swaps pointers

Posted: Sat Oct 05, 2013 10:14 am
by Psychophanta
In that tip you are internally swapping pointers but in the tip there is not used the pointers in a syntactic way for PB, but just using the reference of the base of the array.

However, there is a bug which is critical as i see in the 5.20 version:
When there is swapped two pointers (native vartype is integer '.i' ) in a syntaxically way, i.e. using '@' or '*' as prefix, it should work.
... but it does not.
Next tip display (under PB5.20):
'Swap' only works with 2 elements of the same native type, (not structured)
But the type is native as it is just pointers.

Code: Select all

Dim tableau1(5):Dim tableau2(5)

For i=0 To 5
  tableau1(i)=i
  tableau2(i)=i+10
Next i

Swap @tableau1(),@tableau2() ; <- no works to  PB520 !!

For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i
Sorry to say that it is dissapointing to me and to others when a newer PB version introduces some bug that didn't exist in before versions. Thanks! :|

@kernadec:
You can use this functional alternative for x86 32bit (if need it for 64bit just modify the macro accordingly or ask me for it):

Code: Select all

Macro mySwap(a,b); 32bit x86
  !mov eax,dword[a_#a]
  !xchg eax,dword[a_#b]
  !mov dword[a_#a],eax
EndMacro

Dim tableau1(5):Dim tableau2(5)
For i=0 To 5
  tableau1(i)=i
  tableau2(i)=i+10
Next i

; Swap tableau1(),tableau2()
mySwap(tableau1,tableau2)
For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i

Re: bug?? swap 520lts (solved)

Posted: Sat Oct 05, 2013 12:05 pm
by kernadec
coool!!! thank you Psychophanta

I have not tried more than that
but with this macro is ok for me.

best regard

Re: bug?? swap 520lts (solved)

Posted: Sat Oct 05, 2013 12:15 pm
by Psychophanta
Thanks, welcome!
You can also use this one, which is slighly slower:

Code: Select all

Macro mySwap(a,b); 32bit x86
  !pushd [a_#a] [a_#b]
  !popd [a_#a] [a_#b]
EndMacro
:wink:

Re: bug?? swap 520lts (solved)

Posted: Sat Oct 05, 2013 2:55 pm
by Demivec
@kernadec: Here is link to a routine written by Thorium that will swap memory contents. It may be useful to you.

After renaming the procedure from MemorySwapASM3() to SwapMemory() it would be used in a statement like so:

Code: Select all

SwapMemory(@tableau1(0), @tableau2(0), (ArraySize(tableu1()) + 1) * SizeOf(Integer))  ;assumes tableu1() and tableau2() are the same size

Re: bug?? swap 520lts (solved)

Posted: Sat Oct 05, 2013 6:14 pm
by kernadec
thank you very much for this interesting link Demivec.
I put all that aside.
thank you Thorium for your work.

best regard