float16 icon indicating copy to clipboard operation
float16 copied to clipboard

Create a type instead of using short

Open nemequ opened this issue 5 years ago • 3 comments

If nothing else it would probably be more appropriate to use int16_t instead of short. But even that should only be used internally IMHO. I think you should create your own type so it's clearer to people reading code using the API what is going on.

A simple typedef to int16_t would be an improvement, but I think a better solution would be something like typedef struct { int16_t val; } foo_float16; since it would trigger an error if people try to use arithmetic operators (+, -, *, /, etc.).

That said, some compilers have at least some support for 16-bit floats. Clang 6+ and GCC 4.5+ for starters. It would be nice if you could take advantage of that where available.

I'd also suggest you be careful about the name. float16_t is already taken by NEON, for example. Putting everything in a namespace would be an easy solution, but unfortunately calling the library "float16" is somewhat limiting there. FWIW, I probably would have been tempted to use something falcon-themed as a joke about the F-16 airplane. f16_t, maybe?

nemequ avatar Feb 18 '20 03:02 nemequ

Since the library is actually addressed for retrocomputers as well particlularly z80, the common compiler z88dk for z80 and cc64 for 6502 do not support returning structures by value :-(

Also typedef - you are right

artyom-beilis avatar Feb 18 '20 19:02 artyom-beilis

Since the library is actually addressed for retrocomputers as well particlularly z80, the common compiler z88dk for z80 and cc64 for 6502 do not support returning structures by value :-(

Also typedef - you are right

And how is it that I am going to pass a short to the functions and expect them to be treated as a half float? Where will the decimal point be?

Vinni-Cedraz avatar Aug 30 '23 13:08 Vinni-Cedraz

And how is it that I am going to pass a short to the functions and expect them to be treated as a half float? Where will the decimal point be?

Half float is IEEE 754 16 bit float... There is no decimal digit since it is binary format.

https://github.com/artyom-beilis/float16/blob/master/include/float16.h

You can convert to to and from integer - there are few constants defined.

artyom-beilis avatar Aug 30 '23 14:08 artyom-beilis