xref: /llvm-project/mlir/include/mlir/ExecutionEngine/Float16bits.h (revision 753dc0a01ccc3cbe87d5ee0fe0ec7f8db340966f)
1 //===--- Float16bits.h - supports 2-byte floats ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements f16 and bf16 to support the compilation and execution
10 // of programs using these types.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_EXECUTIONENGINE_FLOAT16BITS_H_
15 #define MLIR_EXECUTIONENGINE_FLOAT16BITS_H_
16 
17 #include <cstdint>
18 #include <iostream>
19 
20 #ifdef _WIN32
21 #ifdef mlir_float16_utils_EXPORTS // We are building this library
22 #define MLIR_FLOAT16_EXPORT __declspec(dllexport)
23 #define MLIR_FLOAT16_DEFINE_FUNCTIONS
24 #else // We are using this library
25 #define MLIR_FLOAT16_EXPORT __declspec(dllimport)
26 #endif // mlir_float16_utils_EXPORTS
27 #else  // Non-windows: use visibility attributes.
28 #define MLIR_FLOAT16_EXPORT __attribute__((visibility("default")))
29 #define MLIR_FLOAT16_DEFINE_FUNCTIONS
30 #endif // _WIN32
31 
32 // Implements half precision and bfloat with f16 and bf16, using the MLIR type
33 // names. These data types are also used for c-interface runtime routines.
34 extern "C" {
35 struct MLIR_FLOAT16_EXPORT f16 {
36   f16(float f = 0);
37   uint16_t bits;
38 };
39 
40 struct MLIR_FLOAT16_EXPORT bf16 {
41   bf16(float f = 0);
42   uint16_t bits;
43 };
44 }
45 
46 // Outputs a half precision value.
47 MLIR_FLOAT16_EXPORT std::ostream &operator<<(std::ostream &os, const f16 &f);
48 // Outputs a bfloat value.
49 MLIR_FLOAT16_EXPORT std::ostream &operator<<(std::ostream &os, const bf16 &d);
50 
51 MLIR_FLOAT16_EXPORT bool operator==(const f16 &f1, const f16 &f2);
52 MLIR_FLOAT16_EXPORT bool operator==(const bf16 &bf1, const bf16 &bf2);
53 
54 extern "C" MLIR_FLOAT16_EXPORT void printF16(uint16_t bits);
55 extern "C" MLIR_FLOAT16_EXPORT void printBF16(uint16_t bits);
56 
57 #undef MLIR_FLOAT16_EXPORT
58 #endif // MLIR_EXECUTIONENGINE_FLOAT16BITS_H_
59