artyom-beilis / float16

half float library for C and for z80
MIT License
34 stars 7 forks source link

Create a type instead of using short #2

Open nemequ opened 4 years ago

nemequ commented 4 years ago

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?

artyom-beilis commented 4 years ago

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

Vinni-Cedraz commented 1 year ago

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?

artyom-beilis commented 1 year ago

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.