1 //===--- Float16bits.cpp - supports 2-byte floats ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements f16 and bf16 to support the compilation and execution
10 // of programs using these types.
12 //===----------------------------------------------------------------------===//
14 #include "mlir/ExecutionEngine/Float16bits.h"
16 #ifdef MLIR_FLOAT16_DEFINE_FUNCTIONS // We are building this library
23 // Union used to make the int/float aliasing explicit so we can access the raw
30 const uint32_t kF32MantiBits
= 23;
31 const uint32_t kF32HalfMantiBitDiff
= 13;
32 const uint32_t kF32HalfBitDiff
= 16;
33 const Float32Bits kF32Magic
= {113 << kF32MantiBits
};
34 const uint32_t kF32HalfExpAdjust
= (127 - 15) << kF32MantiBits
;
36 // Constructs the 16 bit representation for a half precision value from a float
37 // value. This implementation is adapted from Eigen.
38 uint16_t float2half(float floatValue
) {
39 const Float32Bits inf
= {255 << kF32MantiBits
};
40 const Float32Bits f16max
= {(127 + 16) << kF32MantiBits
};
41 const Float32Bits denormMagic
= {((127 - 15) + (kF32MantiBits
- 10) + 1)
43 uint32_t signMask
= 0x80000000u
;
44 uint16_t halfValue
= static_cast<uint16_t>(0x0u
);
47 uint32_t sign
= f
.u
& signMask
;
50 if (f
.u
>= f16max
.u
) {
51 const uint32_t halfQnan
= 0x7e00;
52 const uint32_t halfInf
= 0x7c00;
53 // Inf or NaN (all exponent bits set).
54 halfValue
= (f
.u
> inf
.u
) ? halfQnan
: halfInf
; // NaN->qNaN and Inf->Inf
56 // (De)normalized number or zero.
57 if (f
.u
< kF32Magic
.u
) {
58 // The resulting FP16 is subnormal or zero.
60 // Use a magic value to align our 10 mantissa bits at the bottom of the
61 // float. As long as FP addition is round-to-nearest-even this works.
64 halfValue
= static_cast<uint16_t>(f
.u
- denormMagic
.u
);
67 (f
.u
>> kF32HalfMantiBitDiff
) & 1; // Resulting mantissa is odd.
69 // Update exponent, rounding bias part 1. The following expressions are
70 // equivalent to `f.u += ((unsigned int)(15 - 127) << kF32MantiBits) +
71 // 0xfff`, but without arithmetic overflow.
73 // Rounding bias part 2.
75 halfValue
= static_cast<uint16_t>(f
.u
>> kF32HalfMantiBitDiff
);
79 halfValue
|= static_cast<uint16_t>(sign
>> kF32HalfBitDiff
);
83 // Converts the 16 bit representation of a half precision value to a float
84 // value. This implementation is adapted from Eigen.
85 float half2float(uint16_t halfValue
) {
86 const uint32_t shiftedExp
=
87 0x7c00 << kF32HalfMantiBitDiff
; // Exponent mask after shift.
89 // Initialize the float representation with the exponent/mantissa bits.
91 static_cast<uint32_t>((halfValue
& 0x7fff) << kF32HalfMantiBitDiff
)};
92 const uint32_t exp
= shiftedExp
& f
.u
;
93 f
.u
+= kF32HalfExpAdjust
; // Adjust the exponent
95 // Handle exponent special cases.
96 if (exp
== shiftedExp
) {
98 f
.u
+= kF32HalfExpAdjust
;
99 } else if (exp
== 0) {
101 f
.u
+= 1 << kF32MantiBits
;
105 f
.u
|= (halfValue
& 0x8000) << kF32HalfBitDiff
; // Sign bit.
109 const uint32_t kF32BfMantiBitDiff
= 16;
111 // Constructs the 16 bit representation for a bfloat value from a float value.
112 // This implementation is adapted from Eigen.
113 uint16_t float2bfloat(float floatValue
) {
114 if (std::isnan(floatValue
))
115 return std::signbit(floatValue
) ? 0xFFC0 : 0x7FC0;
117 Float32Bits floatBits
;
118 floatBits
.f
= floatValue
;
121 // Least significant bit of resulting bfloat.
122 uint32_t lsb
= (floatBits
.u
>> kF32BfMantiBitDiff
) & 1;
123 uint32_t roundingBias
= 0x7fff + lsb
;
124 floatBits
.u
+= roundingBias
;
125 bfloatBits
= static_cast<uint16_t>(floatBits
.u
>> kF32BfMantiBitDiff
);
129 // Converts the 16 bit representation of a bfloat value to a float value. This
130 // implementation is adapted from Eigen.
131 float bfloat2float(uint16_t bfloatBits
) {
132 Float32Bits floatBits
;
133 floatBits
.u
= static_cast<uint32_t>(bfloatBits
) << kF32BfMantiBitDiff
;
139 f16::f16(float f
) : bits(float2half(f
)) {}
141 bf16::bf16(float f
) : bits(float2bfloat(f
)) {}
143 std::ostream
&operator<<(std::ostream
&os
, const f16
&f
) {
144 os
<< half2float(f
.bits
);
148 std::ostream
&operator<<(std::ostream
&os
, const bf16
&d
) {
149 os
<< bfloat2float(d
.bits
);
153 bool operator==(const f16
&f1
, const f16
&f2
) { return f1
.bits
== f2
.bits
; }
155 bool operator==(const bf16
&f1
, const bf16
&f2
) { return f1
.bits
== f2
.bits
; }
157 // Mark these symbols as weak so they don't conflict when compiler-rt also
160 #ifdef __has_attribute
161 #if __has_attribute(weak) && !defined(__MINGW32__) && !defined(__CYGWIN__) && \
164 #define ATTR_WEAK __attribute__((__weak__))
168 #if defined(__x86_64__) || defined(_M_X64)
169 // On x86 bfloat16 is passed in SSE registers. Since both float and __bf16
170 // are passed in the same register we can use the wider type and careful casting
171 // to conform to x86_64 psABI. This only works with the assumption that we're
172 // dealing with little-endian values passed in wider registers.
173 // Ideally this would directly use __bf16, but that type isn't supported by all
175 using BF16ABIType
= float;
177 // Default to uint16_t if we have nothing else.
178 using BF16ABIType
= uint16_t;
181 // Provide a float->bfloat conversion routine in case the runtime doesn't have
183 extern "C" BF16ABIType ATTR_WEAK
__truncsfbf2(float f
) {
184 uint16_t bf
= float2bfloat(f
);
185 // The output can be a float type, bitcast it from uint16_t.
187 std::memcpy(&ret
, &bf
, sizeof(bf
));
191 // Provide a double->bfloat conversion routine in case the runtime doesn't have
193 extern "C" BF16ABIType ATTR_WEAK
__truncdfbf2(double d
) {
194 // This does a double rounding step, but it's precise enough for our use
196 return __truncsfbf2(static_cast<float>(d
));
199 // Provide these to the CRunner with the local float16 knowledge.
200 extern "C" void printF16(uint16_t bits
) {
202 std::memcpy(&f
, &bits
, sizeof(f16
));
205 extern "C" void printBF16(uint16_t bits
) {
207 std::memcpy(&f
, &bits
, sizeof(bf16
));
211 #endif // MLIR_FLOAT16_DEFINE_FUNCTIONS