TypeConversions from FP16 to FP

Post here to discuss any new features, components, chips, etc, that you would like to see in Flowcode.
stefan.erni
Valued Contributor
Posts: 997
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 190 times
Been thanked: 217 times

TypeConversions from FP16 to FP

Post by stefan.erni »

Hi Ben

I read from an IMU values in the format FP16.
I store them as 16bit integer.
There is a practical and useful component to convert types.
Maybe you can add an input to read FP16 and get it as a floating point from Flowcode (is a FP32 ? )



2024-11-01_15-06-37.PNG
2024-11-01_15-06-37.PNG (21.02 KiB) Viewed 6359 times
2024-11-01_15-08-43.PNG
2024-11-01_15-08-43.PNG (11.07 KiB) Viewed 6359 times
Half precision floating-point
2024-11-01_15-16-06.PNG
2024-11-01_15-16-06.PNG (33.54 KiB) Viewed 6359 times

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: TypeConversions from FP16 to FP

Post by mnfisher »

That looked interesting....

So a small program that converts 16bit floats to 32bit floats. The code is in supplementary code (and from https://stackoverflow.com/questions/165 ... 8#60047308

The code for 32 to 16bit is also there - but needs a FC 'glue' macro to handle it.

As it stands it tests three numbers (from https://en.wikipedia.org/wiki/Half-prec ... int_format )

These are output to UART (and seem correct :-) )

Martin
Attachments
fp16.fcfx
(14.24 KiB) Downloaded 101 times

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: TypeConversions from FP16 to FP

Post by mnfisher »

With the 'glue' for 32 -> 16bit fp conversions as well.

I just added one test (pi-ish)

Martin
Attachments
fp16.fcfx
(15.53 KiB) Downloaded 105 times

stefan.erni
Valued Contributor
Posts: 997
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 190 times
Been thanked: 217 times

Re: TypeConversions from FP16 to FP

Post by stefan.erni »

Hi Martin

That looks very good.
Now I still have a small error. I don't know if it comes from the STM32 board or from where.

I have heard that FP16 has advantages in many applications, especially with microprocessors and AI or Motion, because it is faster and needs less memory.

In my application I want to send the values from the IMU to the Flowcode App still FB16 and only convert them to FB32 in the app

But I can also send FP32 if necessary.



Project fp16 (1)
Device STM32F469xx
In file included from fp16 (1).c:20:0:
C:\c#_sourcecode\_#FC10\FP16toFP32\fp16 (1).h:246:19: error: conflicting types for 'ushort'
typedef MX_UINT32 ushort;
^
In file included from c:\program files (x86)\flowcode\compilers\starm\gcc\arm-none-eabi\include\stdio.h:61:0,
from c:\program files (x86)\flowcode\compilers\starm\boards\f469discovery.h:19,
from C:\c#_sourcecode\_#FC10\FP16toFP32\fp16 (1).h:32,
from fp16 (1).c:20:
c:\program files (x86)\flowcode\compilers\starm\gcc\arm-none-eabi\include\sys\types.h:107:24: note: previous declaration of 'ushort' was here
typedef unsigned short ushort; /* System V compatibility */
^
In file included from fp16 (1).c:20:0:
C:\c#_sourcecode\_#FC10\FP16toFP32\fp16 (1).h:247:19: error: conflicting types for 'uint'
typedef MX_UINT32 uint;
^
In file included from c:\program files (x86)\flowcode\compilers\starm\gcc\arm-none-eabi\include\stdio.h:61:0,
from c:\program files (x86)\flowcode\compilers\starm\boards\f469discovery.h:19,
from C:\c#_sourcecode\_#FC10\FP16toFP32\fp16 (1).h:32,
from fp16 (1).c:20:
c:\program files (x86)\flowcode\compilers\starm\gcc\arm-none-eabi\include\sys\types.h:108:22: note: previous declaration of 'uint' was here
typedef unsigned int uint; /* System V compatibility */
^
Error(s) in build


mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: TypeConversions from FP16 to FP

Post by mnfisher »

Looks like ushort is already defined.

So either remove the #define (in supplementary code) or
Change the ushorts on the code to MX_UINT32 (and remove the #define)
Try option 1 first and check the results - ushort seems to need to be 32 bit (and the results are wrong if it is a 16 bit value) so I assume that 64 bit is now common as I'd assumed it would be 16...

Martin

stefan.erni
Valued Contributor
Posts: 997
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 190 times
Been thanked: 217 times

Re: TypeConversions from FP16 to FP

Post by stefan.erni »

Hi Martin

So either remove the #define = No more error!


That doesn't make the whole thing any easier..64Bit or 32Bit
In Flowcode:
Floating point
A floating point value can represent a much wider range of values than an integer can, but at a loss of accuracy over large ranges. Floating point values when downloaded will be 64-bit if the target supports them, or 32-bit if it does not.

FLOAT
Icon: Image
Range: -Infinite to +Infinite
Bit Depth: 32-bit signed

Float is a keyword in Flowcode and so cannot be used for variable, property or component names.

Mathematical operations such as a = b + c will be processed as an integer calculation if a, b or c are integer values.

for example you would expect 2.5 * 2 to equal 5 but in the world of integers the calculation only sees 2.5 * 2 = 4, the real portion of the number is lost.


The keyword float can be used to force a calculation to be done using floating point maths instead of integer maths.

e.g. FloatVar1 = FloatVar2 + float IntVar1


Did you know numeric constants such as 35 are also considered as integer values.

Using the float keyword or changing the constant value to for example 35.0 would force the value to be recognised as a floating point type variable

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: TypeConversions from FP16 to FP

Post by mnfisher »

It's okay in FC.

Are you getting the right results?

MX_UINT32 is 32 bit and so are floats - so the 'trick' to load a value as a float from it's memory address works okay..

The C compiler used by the person who wrote the code - I'm guessing that had 64 bit ints? But I might be wrong - it's far easier to have a fixed set of types rather than them being define (or redefined) at the start of every program...

Martin

stefan.erni
Valued Contributor
Posts: 997
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 190 times
Been thanked: 217 times

Re: TypeConversions from FP16 to FP

Post by stefan.erni »

Hi Martin

Yes Martin, the result are perfect!

UART:
2024-11-04_16-26-09.PNG
2024-11-04_16-26-09.PNG (24.89 KiB) Viewed 6146 times

stefan.erni
Valued Contributor
Posts: 997
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 190 times
Been thanked: 217 times

Re: TypeConversions from FP16 to FP

Post by stefan.erni »

Hi Martin, Hi Ben

It is simply converting and saving to an array, perfect!
but it is not possible to send as Float_Array.
Receiving in the app is also not possible, only as INT_Array.
And if I send the values as a Float instead of FB16, I send twice as many bytes.
Question: can I also do the conversion from FP16 to Float in the app?
and also add a send/receive Float_Array?


no send Float Array available:
2024-11-05_10-06-00.PNG
2024-11-05_10-06-00.PNG (22.76 KiB) Viewed 6106 times
simply converting and saving to an Float_Array:
2024-11-05_10-18-44.PNG
2024-11-05_10-18-44.PNG (154.6 KiB) Viewed 6106 times

stefan.erni
Valued Contributor
Posts: 997
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 190 times
Been thanked: 217 times

Re: TypeConversions from FP16 to FP

Post by stefan.erni »

Hi Martin

I have tried to make a macro for the conversion FP16 to FP32 that works in emdeded and in the App.
I tested different values and it works. But three values don't work. Do you have an idea?

My App with the macro:
FP16_to_dez_App_post1.fcsx
(85.58 KiB) Downloaded 105 times
This link was helpfull:

https://evanw.github.io/float-toy/

https://en.wikipedia.org/wiki/Half-pre ... nt_format
And....
The problem:
2024-11-08_17-44-15.PNG
2024-11-08_17-44-15.PNG (401.31 KiB) Viewed 6079 times
2024-11-08_17-49-52.PNG
2024-11-08_17-49-52.PNG (12.24 KiB) Viewed 6079 times
2024-11-08_17-50-28.PNG
2024-11-08_17-50-28.PNG (11.97 KiB) Viewed 6079 times
2024-11-08_17-49-25.PNG
2024-11-08_17-49-25.PNG (13.42 KiB) Viewed 6079 times

Post Reply