- 1) FB takes care of the memory management and
2) I don't know a faster and easier way to access a single byte than an indexed string access
How to "reverse" a byte?
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: How to "reverse" a byte?
I like to misappropriate strings as byte arrays because
Re: How to "reverse" a byte?
Well, behind the scenes, its exactly the same as a Byte-Array access.grindstone wrote:I like to misappropriate strings as byte arrays ...
So, if a Array-Size is fixed (as in above case), you're just creating unneeded overhead (String-Header).
Re: How to "reverse" a byte?
One can also work with a fix-len string:
instead of:
Code: Select all
Dim Shared As String * 256 table
Code: Select all
Dim Shared As String table
table = String(256,Chr(0)) 'allocate the memory and fill with "0"s
Re: How to "reverse" a byte?
Execution time is equivalent if one does not compile with option -exx.MrSwiss wrote:Well, behind the scenes, its exactly the same as a Byte-Array access.grindstone wrote:I like to misappropriate strings as byte arrays ...
Re: How to "reverse" a byte?
Maybe I missed something but can't you just do this?
-Vicne
Code: Select all
Dim value as long= &hFFFF0000
dim rev as long = value XOR &hFFFFFFFF
print hex(value,8), hex(rev,8)
sleep
Re: How to "reverse" a byte?
Hi Luis,
a fast way:
a fast way:
Code: Select all
'bitweise gespiegelt (abcde... -> ...edcba)
#Macro mirror8(Variable)
Asm
mov al, [Variable]
mov bl, 0
mov cl, 8
1:
rcr al
rcl bl
dec cl
jnz 1b
mov [Variable], bl
End Asm
#EndMacro
Dim As UByte zahl = &b00111011
Print Bin(zahl, 8), zahl
mirror8(zahl)
Print Bin(zahl, 8), zahl
mirror8(zahl)
Print Bin(zahl, 8), zahl
Sleep
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: How to "reverse" a byte?
Sorry guys, I do ot understand so much your discussion.
I tried the suggestion from MrSwiss (the last post of page 1) and works nice..... but not know how to see how fast it really is.
This about I3I2UI/I0 is faster?
VDcampo, I think this is not what I want, I want to "flip" the byte horizontally to say it in other words.
I tried the suggestion from MrSwiss (the last post of page 1) and works nice..... but not know how to see how fast it really is.
This about I3I2UI/I0 is faster?
VDcampo, I think this is not what I want, I want to "flip" the byte horizontally to say it in other words.
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: How to "reverse" a byte?
I´m compiling my program with "Windows console " option saying below "fbc -s console"fxm wrote:Execution time is equivalent if one does not compile with option -exx.MrSwiss wrote:Well, behind the scenes, its exactly the same as a Byte-Array access.grindstone wrote:I like to misappropriate strings as byte arrays ...
Not know too much what all this meaning. :-(
Re: How to "reverse" a byte?
@Luis,
assembly language may be faster but, on the other hand, less compatible:
you'd have to do separate ASM routines for FBC 32bit and FBC 64bit.
With routines written in FB, you are able to use any of the compilers.
You'll have to decide on which option to choose.
assembly language may be faster but, on the other hand, less compatible:
you'd have to do separate ASM routines for FBC 32bit and FBC 64bit.
With routines written in FB, you are able to use any of the compilers.
You'll have to decide on which option to choose.
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: How to "reverse" a byte?
Mmmmm, may be is not enough with this invByte for 8 bits bytes... :-(
I´m afraid I need more help to understand if the following, that seems to be made in c++, is possible to be doing in FB.
Note: all is from here: https://chessprogramming.wikispaces.com ... ce+Attacks
(The idea behind is to represent a chessboard (8x8 grid) as a 64 bits byte with 1s for occuped squares and 0s for empty squares).
First thing (I solved):
in positive ray direction:
occupied 11000101
slider 00000100 subset of occupied
o-s 11000001 resets the slider in occupied
o-2s 10111101 borrows the one from nearest blocker
occupied 11000101
o^(o-2s) 01111000 all flipped bits are the attacks in most significant bit direction
Second thing (you solved for me):
For negative directions:
with o' = reverse (o)
and s' = reverse (s)
negativeRayAttacks = reverse (o' ^ (o' - 2s'));
But for the next I´m not sure if it is possible to solve it in FB (or at least for sure not by me!!):
Diagonal or vertical line occupancies need leading and trailing intersection with line-masks. Thanks to the little-endian versus big-endian war, recent processors have appropriate instructions to swap bytes (ranks) inside a 64-bit word for the reverse arithmetic, note that masked files or diagonals have only max one bit per rank:
U64 diagonalAttacks(U64 occ, int sqOfSlider) {
U64 forward, reverse, slider, lineMask;
lineMask = diagonalMaskEx[sqOfSlider]; // excludes square of slider
slider = singleBitboard[sqOfSlider]; // single bit 1 << sq, 2^sq
forward = occ & lineMask; // also performs the first subtraction by clearing the s in o
reverse = byteswap( forward ); // o'-s'
forward -= ( slider ); // o -2s
reverse -= byteswap( slider ); // o'-2s'
forward ^= byteswap( reverse );
return forward & lineMask; // mask the line again
}
I´m afraid I need more help to understand if the following, that seems to be made in c++, is possible to be doing in FB.
Note: all is from here: https://chessprogramming.wikispaces.com ... ce+Attacks
(The idea behind is to represent a chessboard (8x8 grid) as a 64 bits byte with 1s for occuped squares and 0s for empty squares).
First thing (I solved):
in positive ray direction:
occupied 11000101
slider 00000100 subset of occupied
o-s 11000001 resets the slider in occupied
o-2s 10111101 borrows the one from nearest blocker
occupied 11000101
o^(o-2s) 01111000 all flipped bits are the attacks in most significant bit direction
Second thing (you solved for me):
For negative directions:
with o' = reverse (o)
and s' = reverse (s)
negativeRayAttacks = reverse (o' ^ (o' - 2s'));
But for the next I´m not sure if it is possible to solve it in FB (or at least for sure not by me!!):
Diagonal or vertical line occupancies need leading and trailing intersection with line-masks. Thanks to the little-endian versus big-endian war, recent processors have appropriate instructions to swap bytes (ranks) inside a 64-bit word for the reverse arithmetic, note that masked files or diagonals have only max one bit per rank:
U64 diagonalAttacks(U64 occ, int sqOfSlider) {
U64 forward, reverse, slider, lineMask;
lineMask = diagonalMaskEx[sqOfSlider]; // excludes square of slider
slider = singleBitboard[sqOfSlider]; // single bit 1 << sq, 2^sq
forward = occ & lineMask; // also performs the first subtraction by clearing the s in o
reverse = byteswap( forward ); // o'-s'
forward -= ( slider ); // o -2s
reverse -= byteswap( slider ); // o'-2s'
forward ^= byteswap( reverse );
return forward & lineMask; // mask the line again
}
Last edited by Luis Babboni on Jan 18, 2017 20:01, edited 1 time in total.
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: How to "reverse" a byte?
I´m wrong or it saids that it is needed to do the same than you did with invByte for 8 bits byte now with 64 bits byte??!! :-(
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: How to "reverse" a byte?
Just now I saw his suggestion is in assembler! I´ll kept in FB for the moment :-DMrSwiss wrote:@Luis,
assembly language may be faster but, on the other hand, less compatible:
you'd have to do separate ASM routines for FBC 32bit and FBC 64bit.
With routines written in FB, you are able to use any of the compilers.
You'll have to decide on which option to choose.
Thanks!
Re: How to "reverse" a byte?
Just coded an Endian-Inverter for 64bit INT ...
64bit conversion Big-/Little- Endian (here)
This is however, a Byte by Byte conversion (not bit by bit).
64bit conversion Big-/Little- Endian (here)
This is however, a Byte by Byte conversion (not bit by bit).
-
- Posts: 375
- Joined: Mar 15, 2015 12:41
Re: How to "reverse" a byte?
Mmmm, I need to combine this with invByte to do it bit by bit?MrSwiss wrote:Just coded an Endian-Inverter for 64bit INT ...
64bit conversion Big-/Little- Endian (here)
This is however, a Byte by Byte conversion (not bit by bit).