Skip to content

Commit

Permalink
Use hardware-accelerated _Float16 type with x86 GCC
Browse files Browse the repository at this point in the history
  • Loading branch information
Maratyszcza committed Jun 16, 2024
1 parent bc497e2 commit c2c86b8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ jobs:
run: cmake --build build --parallel
- name: Test
run: ctest --test-dir build --parallel --output-on-failure
cmake-linux-x86_64-f16c:
runs-on: ubuntu-24.04 # required for gcc >= 12
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Update apt
run: sudo apt update
- name: Install ninja
run: sudo apt install ninja-build
- name: Configure
run: cmake -Bbuild -S. -G Ninja -DCMAKE_BUILD_TYPE=Release -DFP16_BUILD_COMPARATIVE_BENCHMARKS=ON
env:
CFLAGS: "-mf16c"
CXXFLAGS: "-mf16c"
- name: Build
run: cmake --build build --parallel
- name: Test
run: ctest --test-dir build --parallel --output-on-failure
cmake-linux-x86:
runs-on: ubuntu-20.04
timeout-minutes: 15
Expand Down
16 changes: 14 additions & 2 deletions include/fp16/fp16.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ static inline uint32_t fp16_ieee_to_fp32_bits(uint16_t h) {
* floating-point operations and bitcasts between integer and floating-point variables.
*/
static inline float fp16_ieee_to_fp32_value(uint16_t h) {
#if FP16_USE_FP16_TYPE
#if FP16_USE_FLOAT16_TYPE
union {
uint16_t as_bits;
_Float16 as_value;
} fp16 = { h };
return (float) fp16.as_value;
#elif FP16_USE_FP16_TYPE
union {
uint16_t as_bits;
__fp16 as_value;
Expand Down Expand Up @@ -230,7 +236,13 @@ static inline float fp16_ieee_to_fp32_value(uint16_t h) {
* floating-point operations and bitcasts between integer and floating-point variables.
*/
static inline uint16_t fp16_ieee_from_fp32_value(float f) {
#if FP16_USE_FP16_TYPE
#if FP16_USE_FLOAT16_TYPE
union {
_Float16 as_value;
uint16_t as_bits;
} fp16 = { (_Float16) f };
return fp16.as_bits;
#elif FP16_USE_FP16_TYPE
union {
__fp16 as_value;
uint16_t as_bits;
Expand Down
11 changes: 11 additions & 0 deletions include/fp16/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#define FP16_MACROS_H


#ifndef FP16_USE_FLOAT16_TYPE
#if !defined(__clang__) && !defined(__INTEL_COMPILER) && defined(__GNUC__) && (__GNUC__ >= 12)
#if defined(__F16C__)
#define FP16_USE_FLOAT16_TYPE 1
#endif
#endif
#if !defined(FP16_USE_FLOAT16_TYPE)
#define FP16_USE_FLOAT16_TYPE 0
#endif // !defined(FP16_USE_FLOAT16_TYPE)
#endif // !defined(FP16_USE_FLOAT16_TYPE)

#ifndef FP16_USE_FP16_TYPE
#if defined(__clang__)
#if defined(__F16C__) || defined(__aarch64__)
Expand Down

0 comments on commit c2c86b8

Please sign in to comment.