Converting from Bytes to Ulong.

A forums to allow bugs and problems with Flowcode v6 to be reported and resolved.

Moderator: Benj

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Converting from Bytes to Ulong.

Post by medelec35 »

If converting 1234567890 from Bytes (MSB to LSB 0x49 0x96 0x02 0xD2)
Then all three methods show correct value in Flowcode simulator:

Code: Select all

Cycles = TotCycle1 * 16777216 + TotCycle2 * 65536 + TotCycle3 * 256 + TotCycle4

Code: Select all

Cycles = TotCycle1 * (1 << 24) + TotCycle2 * (1 << 16) + TotCycle3 * (1 << 8) + TotCycle4
&

Code: Select all

Cycles = (TotCycle1 << 24) | (TotCycle2 << 16) | (TotCycle3 << 8) | TotCycle4
However only the top version works with hardware.
Second and third method totaly ignore 16 and 24 bytes so they are not used in the final calculations.
Instead of Cycles = 1234567890
Cycles = 722

Even if it's my error, the simulation should reflect results on hardware hence posting as a bug.
Have I made a mistake or is it a bug?

Martin
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Converting from Bytes to Ulong.

Post by Benj »

Hi Martin,

I have experienced this problem a few times specifically with the FAT. It's down to the compiler not necessarily performing 32-bit maths when shifting.

One way around is to typecast however doing this via a calculation is not really currently possible. We did go to some lengths to try and automatically add a typecast here but I can't remember what the outcome of the investigation was.

Code: Select all

longvar = (long) longvar | (B2 << 16);
longvar = (long) longvar | (B2 << 24);
The (long) is the typecast which tells the compiler to deal in 32-bit when shifting.

Another approach is to use the C unions I added to the TypeDefs CAL files. The v5 and v6 FAT component currently uses these as they are known to work reliably on every target family without having to remember to typecast everything. This is also much more efficient as each bit shift consumes 1 clock cycle whereas the union pops the byte directly into the correct place in the variable without shifting.

MX_Union32 unionvar;
unionvar.AsByte[0] = B0;
unionvar.AsByte[1] = B1;
unionvar.AsByte[2] = B2;
unionvar.AsByte[3] = B3;
longvar = unionvar.AsLong;

I wanted to get these functions into the calculation icon but so far they have not made it in. I can certainly try and bump them up in priority if this is something people would like.

The MX_Union32 union has the values AsLong, AsInt[2] and AsByte[4].

There is also another union for dealing with 16-bit values, The MX_Union16 union has the values AsInt and AsByte[2].

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting from Bytes to Ulong.

Post by medelec35 »

Hi Ben,
Benj wrote:I wanted to get these functions into the calculation icon but so far they have not made it in. I can certainly try and bump them up in priority if this is something people would like.
Since

Code: Select all

Cycles = TotCycle1 * 16777216 + TotCycle2 * 65536 + TotCycle3 * 256 + TotCycle4
has proven to work with hardware, to me its not a big deal.
I just thought I would bring it to matrix's attention just in case.

Thanks

Martin
Martin


medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Converting from Bytes to Ulong.

Post by medelec35 »

Hi STibor,
To be honest I had forgotten about your post.
Thanks for reminding me, it's an age thing :lol:
Martin

Post Reply