17

The __fp16 floating point data-type is a well known extension to the C standard used notably on ARM processors. I would like to run the IEEE version of them on my x86_64 processor. While I know they typically do not have that, I would be fine with emulating them with "unsigned short" storage (they have the same alignment requirement and storage space), and (hardware) float arithmetic.

Is there a way to request that in gcc?

I assume the rounding might be slightly "incorrect", but that is ok to me.

If this were to work in C++ too that would be ideal.

11
  • 1
    I don't think it has this for x86 targets. If it did, it would be very slow, because it would all have to be run in software emulation, rather than using FP hardware. Why would you want to do this?
    – Cody Gray
    Jul 14, 2017 at 18:39
  • 8
    @CodyGray: half-precision floats are natively supported by reasonably recent (Intel since Ivy Bridge, AMD since Piledriver) x86 CPUs (as a storage format only, conversion to single precision is required to do actual computation).
    – user784668
    Jul 14, 2017 at 18:41
  • 3
    Ah yes, so they are, @Fanael. Thanks for pointing that out. I had missed their introduction. So what you would use would be _mm256_cvtph_ps as the "load" (convert half-float to float), and _mm256_cvtps_ph as the "store" (convert float to half-float). It turns out this is reasonably fast, and is actually useful in situations where you're memory-constrained. Would it be acceptable, Nonyme, to implement this using intrinsics in something like a platform abstraction library? Or are you dead-set on having the compiler generate this code implicitly?
    – Cody Gray
    Jul 14, 2017 at 18:50
  • 5
    The goal is to run a huge code-base designed for ARM, on an x86_64 server farm. If the "platform abstraction library" do not need any modification of the code, then that is ok. But I doubt that is doable. Note: I managed to trick Clang in doing just that by tricking the semantic parser to define __fp16 and accept it as function argument/return values on x86_64. It then managed to use the aforementioned intrinsic to do the conversions and compute using floats instead.
    – Nonyme
    Jul 16, 2017 at 18:53
  • 2
    I edited clang source code to add the __fp16 built-in type on X86 targets (by default it is only enabled on ARM). Then the rest of the compiler dealt with it by itself.
    – Nonyme
    Jul 20, 2018 at 18:49

3 Answers 3

6

I did not find a way to do so in gcc (as of gcc 8.2.0).

As for clang, in 6.0.0 the following options showed some success:

clang -cc1 -fnative-half-type -fallow-half-arguments-and-returns

The option -fnative-half-type enable the use of __fp16 type (instead of promoting them to float). While the option -fallow-half-arguments-and-returns allows to pass __fp16 by value, the API being non-standard be careful not to mix different compilers.

That being said, it does not provide math functions using __fp16 types (it will promote them to/from float or double).

It was sufficient for my use case.

2
  • 2
    There's good reason for the lack of __fp16 math functions: x86 support for half-precision is limited to conversion to float (vcvtph2ps and the reverse, and only for SIMD vectors, not scalar). So it's useful only for reducing the cache footprint of an array at the cost of an ALU conversion when loading and storing. Even conversion to double takes 2 steps. You definitely don't want to be passing around __fp16 data in registers on x86 because every computation would have to convert to float and back. Oct 31, 2018 at 22:34
  • (Update: Sapphire Rapids has full scalar and SIMD support for AVX-512 FP16 math instructions, as well as BF16 which appeared in some earlier CPUs too. Half-precision floating-point arithmetic on Intel chips) Feb 7 at 2:48
2

C++23 introduces std::float16_t

#include <stdfloat> // C++23
 
int main()
{
    std::float16_t f = 0.1F16;
}
0

_Float16 is the type you should be looking for now in recent versions of clang and gcc.

At least in the compilers I've worked with __fp16 was a limited type that you could only convert to/from binary32 (using hardware where supported) while _Float16 is more like a "real" arithmetic type, not that you should attempt too much in such limited precision.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.