xref: /aosp_15_r20/external/FP16/include/fp16/fp16.h (revision 5f32b7105932ea8520a0e8811c640f936367d707)
1*5f32b710SXin Li #pragma once
2*5f32b710SXin Li #ifndef FP16_FP16_H
3*5f32b710SXin Li #define FP16_FP16_H
4*5f32b710SXin Li 
5*5f32b710SXin Li #if defined(__cplusplus) && (__cplusplus >= 201103L)
6*5f32b710SXin Li 	#include <cstdint>
7*5f32b710SXin Li 	#include <cmath>
8*5f32b710SXin Li #elif !defined(__OPENCL_VERSION__)
9*5f32b710SXin Li 	#include <stdint.h>
10*5f32b710SXin Li 	#include <math.h>
11*5f32b710SXin Li #endif
12*5f32b710SXin Li 
13*5f32b710SXin Li #ifdef _MSC_VER
14*5f32b710SXin Li 	#include <intrin.h>
15*5f32b710SXin Li #endif
16*5f32b710SXin Li 
17*5f32b710SXin Li #include <fp16/bitcasts.h>
18*5f32b710SXin Li 
19*5f32b710SXin Li 
20*5f32b710SXin Li /*
21*5f32b710SXin Li  * Convert a 16-bit floating-point number in IEEE half-precision format, in bit representation, to
22*5f32b710SXin Li  * a 32-bit floating-point number in IEEE single-precision format, in bit representation.
23*5f32b710SXin Li  *
24*5f32b710SXin Li  * @note The implementation doesn't use any floating-point operations.
25*5f32b710SXin Li  */
fp16_ieee_to_fp32_bits(uint16_t h)26*5f32b710SXin Li static inline uint32_t fp16_ieee_to_fp32_bits(uint16_t h) {
27*5f32b710SXin Li 	/*
28*5f32b710SXin Li 	 * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
29*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
30*5f32b710SXin Li 	 *      | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
31*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
32*5f32b710SXin Li 	 * Bits  31  26-30    16-25            0-15
33*5f32b710SXin Li 	 *
34*5f32b710SXin Li 	 * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
35*5f32b710SXin Li 	 */
36*5f32b710SXin Li 	const uint32_t w = (uint32_t) h << 16;
37*5f32b710SXin Li 	/*
38*5f32b710SXin Li 	 * Extract the sign of the input number into the high bit of the 32-bit word:
39*5f32b710SXin Li 	 *
40*5f32b710SXin Li 	 *      +---+----------------------------------+
41*5f32b710SXin Li 	 *      | S |0000000 00000000 00000000 00000000|
42*5f32b710SXin Li 	 *      +---+----------------------------------+
43*5f32b710SXin Li 	 * Bits  31                 0-31
44*5f32b710SXin Li 	 */
45*5f32b710SXin Li 	const uint32_t sign = w & UINT32_C(0x80000000);
46*5f32b710SXin Li 	/*
47*5f32b710SXin Li 	 * Extract mantissa and biased exponent of the input number into the bits 0-30 of the 32-bit word:
48*5f32b710SXin Li 	 *
49*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
50*5f32b710SXin Li 	 *      | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
51*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
52*5f32b710SXin Li 	 * Bits  30  27-31     17-26            0-16
53*5f32b710SXin Li 	 */
54*5f32b710SXin Li 	const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
55*5f32b710SXin Li 	/*
56*5f32b710SXin Li 	 * Renorm shift is the number of bits to shift mantissa left to make the half-precision number normalized.
57*5f32b710SXin Li 	 * If the initial number is normalized, some of its high 6 bits (sign == 0 and 5-bit exponent) equals one.
58*5f32b710SXin Li 	 * In this case renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note that if we shift
59*5f32b710SXin Li 	 * denormalized nonsign by renorm_shift, the unit bit of mantissa will shift into exponent, turning the
60*5f32b710SXin Li 	 * biased exponent into 1, and making mantissa normalized (i.e. without leading 1).
61*5f32b710SXin Li 	 */
62*5f32b710SXin Li #ifdef _MSC_VER
63*5f32b710SXin Li 	unsigned long nonsign_bsr;
64*5f32b710SXin Li 	_BitScanReverse(&nonsign_bsr, (unsigned long) nonsign);
65*5f32b710SXin Li 	uint32_t renorm_shift = (uint32_t) nonsign_bsr ^ 31;
66*5f32b710SXin Li #else
67*5f32b710SXin Li 	uint32_t renorm_shift = nonsign ? __builtin_clz(nonsign) : 32;
68*5f32b710SXin Li #endif
69*5f32b710SXin Li 	renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
70*5f32b710SXin Li 	/*
71*5f32b710SXin Li 	 * Iff half-precision number has exponent of 15, the addition overflows it into bit 31,
72*5f32b710SXin Li 	 * and the subsequent shift turns the high 9 bits into 1. Thus
73*5f32b710SXin Li 	 *   inf_nan_mask ==
74*5f32b710SXin Li 	 *                   0x7F800000 if the half-precision number had exponent of 15 (i.e. was NaN or infinity)
75*5f32b710SXin Li 	 *                   0x00000000 otherwise
76*5f32b710SXin Li 	 */
77*5f32b710SXin Li 	const int32_t inf_nan_mask = ((int32_t) (nonsign + 0x04000000) >> 8) & INT32_C(0x7F800000);
78*5f32b710SXin Li 	/*
79*5f32b710SXin Li 	 * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31 into 1. Otherwise, bit 31 remains 0.
80*5f32b710SXin Li 	 * The signed shift right by 31 broadcasts bit 31 into all bits of the zero_mask. Thus
81*5f32b710SXin Li 	 *   zero_mask ==
82*5f32b710SXin Li 	 *                0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
83*5f32b710SXin Li 	 *                0x00000000 otherwise
84*5f32b710SXin Li 	 */
85*5f32b710SXin Li 	const int32_t zero_mask = (int32_t) (nonsign - 1) >> 31;
86*5f32b710SXin Li 	/*
87*5f32b710SXin Li 	 * 1. Shift nonsign left by renorm_shift to normalize it (if the input was denormal)
88*5f32b710SXin Li 	 * 2. Shift nonsign right by 3 so the exponent (5 bits originally) becomes an 8-bit field and 10-bit mantissa
89*5f32b710SXin Li 	 *    shifts into the 10 high bits of the 23-bit mantissa of IEEE single-precision number.
90*5f32b710SXin Li 	 * 3. Add 0x70 to the exponent (starting at bit 23) to compensate the different in exponent bias
91*5f32b710SXin Li 	 *    (0x7F for single-precision number less 0xF for half-precision number).
92*5f32b710SXin Li 	 * 4. Subtract renorm_shift from the exponent (starting at bit 23) to account for renormalization. As renorm_shift
93*5f32b710SXin Li 	 *    is less than 0x70, this can be combined with step 3.
94*5f32b710SXin Li 	 * 5. Binary OR with inf_nan_mask to turn the exponent into 0xFF if the input was NaN or infinity.
95*5f32b710SXin Li 	 * 6. Binary ANDNOT with zero_mask to turn the mantissa and exponent into zero if the input was zero.
96*5f32b710SXin Li 	 * 7. Combine with the sign of the input number.
97*5f32b710SXin Li 	 */
98*5f32b710SXin Li 	return sign | ((((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) | inf_nan_mask) & ~zero_mask);
99*5f32b710SXin Li }
100*5f32b710SXin Li 
101*5f32b710SXin Li /*
102*5f32b710SXin Li  * Convert a 16-bit floating-point number in IEEE half-precision format, in bit representation, to
103*5f32b710SXin Li  * a 32-bit floating-point number in IEEE single-precision format.
104*5f32b710SXin Li  *
105*5f32b710SXin Li  * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
106*5f32b710SXin Li  * floating-point operations and bitcasts between integer and floating-point variables.
107*5f32b710SXin Li  */
fp16_ieee_to_fp32_value(uint16_t h)108*5f32b710SXin Li static inline float fp16_ieee_to_fp32_value(uint16_t h) {
109*5f32b710SXin Li 	/*
110*5f32b710SXin Li 	 * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
111*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
112*5f32b710SXin Li 	 *      | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
113*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
114*5f32b710SXin Li 	 * Bits  31  26-30    16-25            0-15
115*5f32b710SXin Li 	 *
116*5f32b710SXin Li 	 * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
117*5f32b710SXin Li 	 */
118*5f32b710SXin Li 	const uint32_t w = (uint32_t) h << 16;
119*5f32b710SXin Li 	/*
120*5f32b710SXin Li 	 * Extract the sign of the input number into the high bit of the 32-bit word:
121*5f32b710SXin Li 	 *
122*5f32b710SXin Li 	 *      +---+----------------------------------+
123*5f32b710SXin Li 	 *      | S |0000000 00000000 00000000 00000000|
124*5f32b710SXin Li 	 *      +---+----------------------------------+
125*5f32b710SXin Li 	 * Bits  31                 0-31
126*5f32b710SXin Li 	 */
127*5f32b710SXin Li 	const uint32_t sign = w & UINT32_C(0x80000000);
128*5f32b710SXin Li 	/*
129*5f32b710SXin Li 	 * Extract mantissa and biased exponent of the input number into the high bits of the 32-bit word:
130*5f32b710SXin Li 	 *
131*5f32b710SXin Li 	 *      +-----+------------+---------------------+
132*5f32b710SXin Li 	 *      |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
133*5f32b710SXin Li 	 *      +-----+------------+---------------------+
134*5f32b710SXin Li 	 * Bits  27-31    17-26            0-16
135*5f32b710SXin Li 	 */
136*5f32b710SXin Li 	const uint32_t two_w = w + w;
137*5f32b710SXin Li 
138*5f32b710SXin Li 	/*
139*5f32b710SXin Li 	 * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become mantissa and exponent
140*5f32b710SXin Li 	 * of a single-precision floating-point number:
141*5f32b710SXin Li 	 *
142*5f32b710SXin Li 	 *       S|Exponent |          Mantissa
143*5f32b710SXin Li 	 *      +-+---+-----+------------+----------------+
144*5f32b710SXin Li 	 *      |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
145*5f32b710SXin Li 	 *      +-+---+-----+------------+----------------+
146*5f32b710SXin Li 	 * Bits   | 23-31   |           0-22
147*5f32b710SXin Li 	 *
148*5f32b710SXin Li 	 * Next, there are some adjustments to the exponent:
149*5f32b710SXin Li 	 * - The exponent needs to be corrected by the difference in exponent bias between single-precision and half-precision
150*5f32b710SXin Li 	 *   formats (0x7F - 0xF = 0x70)
151*5f32b710SXin Li 	 * - Inf and NaN values in the inputs should become Inf and NaN values after conversion to the single-precision number.
152*5f32b710SXin Li 	 *   Therefore, if the biased exponent of the half-precision input was 0x1F (max possible value), the biased exponent
153*5f32b710SXin Li 	 *   of the single-precision output must be 0xFF (max possible value). We do this correction in two steps:
154*5f32b710SXin Li 	 *   - First, we adjust the exponent by (0xFF - 0x1F) = 0xE0 (see exp_offset below) rather than by 0x70 suggested
155*5f32b710SXin Li 	 *     by the difference in the exponent bias (see above).
156*5f32b710SXin Li 	 *   - Then we multiply the single-precision result of exponent adjustment by 2**(-112) to reverse the effect of
157*5f32b710SXin Li 	 *     exponent adjustment by 0xE0 less the necessary exponent adjustment by 0x70 due to difference in exponent bias.
158*5f32b710SXin Li 	 *     The floating-point multiplication hardware would ensure than Inf and NaN would retain their value on at least
159*5f32b710SXin Li 	 *     partially IEEE754-compliant implementations.
160*5f32b710SXin Li 	 *
161*5f32b710SXin Li 	 * Note that the above operations do not handle denormal inputs (where biased exponent == 0). However, they also do not
162*5f32b710SXin Li 	 * operate on denormal inputs, and do not produce denormal results.
163*5f32b710SXin Li 	 */
164*5f32b710SXin Li 	const uint32_t exp_offset = UINT32_C(0xE0) << 23;
165*5f32b710SXin Li #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
166*5f32b710SXin Li 	const float exp_scale = 0x1.0p-112f;
167*5f32b710SXin Li #else
168*5f32b710SXin Li 	const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
169*5f32b710SXin Li #endif
170*5f32b710SXin Li 	const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
171*5f32b710SXin Li 
172*5f32b710SXin Li 	/*
173*5f32b710SXin Li 	 * Convert denormalized half-precision inputs into single-precision results (always normalized).
174*5f32b710SXin Li 	 * Zero inputs are also handled here.
175*5f32b710SXin Li 	 *
176*5f32b710SXin Li 	 * In a denormalized number the biased exponent is zero, and mantissa has on-zero bits.
177*5f32b710SXin Li 	 * First, we shift mantissa into bits 0-9 of the 32-bit word.
178*5f32b710SXin Li 	 *
179*5f32b710SXin Li 	 *                  zeros           |  mantissa
180*5f32b710SXin Li 	 *      +---------------------------+------------+
181*5f32b710SXin Li 	 *      |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
182*5f32b710SXin Li 	 *      +---------------------------+------------+
183*5f32b710SXin Li 	 * Bits             10-31                0-9
184*5f32b710SXin Li 	 *
185*5f32b710SXin Li 	 * Now, remember that denormalized half-precision numbers are represented as:
186*5f32b710SXin Li 	 *    FP16 = mantissa * 2**(-24).
187*5f32b710SXin Li 	 * The trick is to construct a normalized single-precision number with the same mantissa and thehalf-precision input
188*5f32b710SXin Li 	 * and with an exponent which would scale the corresponding mantissa bits to 2**(-24).
189*5f32b710SXin Li 	 * A normalized single-precision floating-point number is represented as:
190*5f32b710SXin Li 	 *    FP32 = (1 + mantissa * 2**(-23)) * 2**(exponent - 127)
191*5f32b710SXin Li 	 * Therefore, when the biased exponent is 126, a unit change in the mantissa of the input denormalized half-precision
192*5f32b710SXin Li 	 * number causes a change of the constructud single-precision number by 2**(-24), i.e. the same ammount.
193*5f32b710SXin Li 	 *
194*5f32b710SXin Li 	 * The last step is to adjust the bias of the constructed single-precision number. When the input half-precision number
195*5f32b710SXin Li 	 * is zero, the constructed single-precision number has the value of
196*5f32b710SXin Li 	 *    FP32 = 1 * 2**(126 - 127) = 2**(-1) = 0.5
197*5f32b710SXin Li 	 * Therefore, we need to subtract 0.5 from the constructed single-precision number to get the numerical equivalent of
198*5f32b710SXin Li 	 * the input half-precision number.
199*5f32b710SXin Li 	 */
200*5f32b710SXin Li 	const uint32_t magic_mask = UINT32_C(126) << 23;
201*5f32b710SXin Li 	const float magic_bias = 0.5f;
202*5f32b710SXin Li 	const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
203*5f32b710SXin Li 
204*5f32b710SXin Li 	/*
205*5f32b710SXin Li 	 * - Choose either results of conversion of input as a normalized number, or as a denormalized number, depending on the
206*5f32b710SXin Li 	 *   input exponent. The variable two_w contains input exponent in bits 27-31, therefore if its smaller than 2**27, the
207*5f32b710SXin Li 	 *   input is either a denormal number, or zero.
208*5f32b710SXin Li 	 * - Combine the result of conversion of exponent and mantissa with the sign of the input number.
209*5f32b710SXin Li 	 */
210*5f32b710SXin Li 	const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
211*5f32b710SXin Li 	const uint32_t result = sign |
212*5f32b710SXin Li 		(two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
213*5f32b710SXin Li 	return fp32_from_bits(result);
214*5f32b710SXin Li }
215*5f32b710SXin Li 
216*5f32b710SXin Li /*
217*5f32b710SXin Li  * Convert a 32-bit floating-point number in IEEE single-precision format to a 16-bit floating-point number in
218*5f32b710SXin Li  * IEEE half-precision format, in bit representation.
219*5f32b710SXin Li  *
220*5f32b710SXin Li  * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
221*5f32b710SXin Li  * floating-point operations and bitcasts between integer and floating-point variables.
222*5f32b710SXin Li  */
fp16_ieee_from_fp32_value(float f)223*5f32b710SXin Li static inline uint16_t fp16_ieee_from_fp32_value(float f) {
224*5f32b710SXin Li #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
225*5f32b710SXin Li 	const float scale_to_inf = 0x1.0p+112f;
226*5f32b710SXin Li 	const float scale_to_zero = 0x1.0p-110f;
227*5f32b710SXin Li #else
228*5f32b710SXin Li 	const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
229*5f32b710SXin Li 	const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
230*5f32b710SXin Li #endif
231*5f32b710SXin Li 	float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
232*5f32b710SXin Li 
233*5f32b710SXin Li 	const uint32_t w = fp32_to_bits(f);
234*5f32b710SXin Li 	const uint32_t shl1_w = w + w;
235*5f32b710SXin Li 	const uint32_t sign = w & UINT32_C(0x80000000);
236*5f32b710SXin Li 	uint32_t bias = shl1_w & UINT32_C(0xFF000000);
237*5f32b710SXin Li 	if (bias < UINT32_C(0x71000000)) {
238*5f32b710SXin Li 		bias = UINT32_C(0x71000000);
239*5f32b710SXin Li 	}
240*5f32b710SXin Li 
241*5f32b710SXin Li 	base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
242*5f32b710SXin Li 	const uint32_t bits = fp32_to_bits(base);
243*5f32b710SXin Li 	const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
244*5f32b710SXin Li 	const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
245*5f32b710SXin Li 	const uint32_t nonsign = exp_bits + mantissa_bits;
246*5f32b710SXin Li 	return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);
247*5f32b710SXin Li }
248*5f32b710SXin Li 
249*5f32b710SXin Li /*
250*5f32b710SXin Li  * Convert a 16-bit floating-point number in ARM alternative half-precision format, in bit representation, to
251*5f32b710SXin Li  * a 32-bit floating-point number in IEEE single-precision format, in bit representation.
252*5f32b710SXin Li  *
253*5f32b710SXin Li  * @note The implementation doesn't use any floating-point operations.
254*5f32b710SXin Li  */
fp16_alt_to_fp32_bits(uint16_t h)255*5f32b710SXin Li static inline uint32_t fp16_alt_to_fp32_bits(uint16_t h) {
256*5f32b710SXin Li 	/*
257*5f32b710SXin Li 	 * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
258*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
259*5f32b710SXin Li 	 *      | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
260*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
261*5f32b710SXin Li 	 * Bits  31  26-30    16-25            0-15
262*5f32b710SXin Li 	 *
263*5f32b710SXin Li 	 * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
264*5f32b710SXin Li 	 */
265*5f32b710SXin Li 	const uint32_t w = (uint32_t) h << 16;
266*5f32b710SXin Li 	/*
267*5f32b710SXin Li 	 * Extract the sign of the input number into the high bit of the 32-bit word:
268*5f32b710SXin Li 	 *
269*5f32b710SXin Li 	 *      +---+----------------------------------+
270*5f32b710SXin Li 	 *      | S |0000000 00000000 00000000 00000000|
271*5f32b710SXin Li 	 *      +---+----------------------------------+
272*5f32b710SXin Li 	 * Bits  31                 0-31
273*5f32b710SXin Li 	 */
274*5f32b710SXin Li 	const uint32_t sign = w & UINT32_C(0x80000000);
275*5f32b710SXin Li 	/*
276*5f32b710SXin Li 	 * Extract mantissa and biased exponent of the input number into the bits 0-30 of the 32-bit word:
277*5f32b710SXin Li 	 *
278*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
279*5f32b710SXin Li 	 *      | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
280*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
281*5f32b710SXin Li 	 * Bits  30  27-31     17-26            0-16
282*5f32b710SXin Li 	 */
283*5f32b710SXin Li 	const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
284*5f32b710SXin Li 	/*
285*5f32b710SXin Li 	 * Renorm shift is the number of bits to shift mantissa left to make the half-precision number normalized.
286*5f32b710SXin Li 	 * If the initial number is normalized, some of its high 6 bits (sign == 0 and 5-bit exponent) equals one.
287*5f32b710SXin Li 	 * In this case renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note that if we shift
288*5f32b710SXin Li 	 * denormalized nonsign by renorm_shift, the unit bit of mantissa will shift into exponent, turning the
289*5f32b710SXin Li 	 * biased exponent into 1, and making mantissa normalized (i.e. without leading 1).
290*5f32b710SXin Li 	 */
291*5f32b710SXin Li #ifdef _MSC_VER
292*5f32b710SXin Li 	unsigned long nonsign_bsr;
293*5f32b710SXin Li 	_BitScanReverse(&nonsign_bsr, (unsigned long) nonsign);
294*5f32b710SXin Li 	uint32_t renorm_shift = (uint32_t) nonsign_bsr ^ 31;
295*5f32b710SXin Li #else
296*5f32b710SXin Li 	uint32_t renorm_shift = nonsign ? __builtin_clz(nonsign) : 32;
297*5f32b710SXin Li #endif
298*5f32b710SXin Li 	renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
299*5f32b710SXin Li 	/*
300*5f32b710SXin Li 	 * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31 into 1. Otherwise, bit 31 remains 0.
301*5f32b710SXin Li 	 * The signed shift right by 31 broadcasts bit 31 into all bits of the zero_mask. Thus
302*5f32b710SXin Li 	 *   zero_mask ==
303*5f32b710SXin Li 	 *                0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
304*5f32b710SXin Li 	 *                0x00000000 otherwise
305*5f32b710SXin Li 	 */
306*5f32b710SXin Li 	const int32_t zero_mask = (int32_t) (nonsign - 1) >> 31;
307*5f32b710SXin Li 	/*
308*5f32b710SXin Li 	 * 1. Shift nonsign left by renorm_shift to normalize it (if the input was denormal)
309*5f32b710SXin Li 	 * 2. Shift nonsign right by 3 so the exponent (5 bits originally) becomes an 8-bit field and 10-bit mantissa
310*5f32b710SXin Li 	 *    shifts into the 10 high bits of the 23-bit mantissa of IEEE single-precision number.
311*5f32b710SXin Li 	 * 3. Add 0x70 to the exponent (starting at bit 23) to compensate the different in exponent bias
312*5f32b710SXin Li 	 *    (0x7F for single-precision number less 0xF for half-precision number).
313*5f32b710SXin Li 	 * 4. Subtract renorm_shift from the exponent (starting at bit 23) to account for renormalization. As renorm_shift
314*5f32b710SXin Li 	 *    is less than 0x70, this can be combined with step 3.
315*5f32b710SXin Li 	 * 5. Binary ANDNOT with zero_mask to turn the mantissa and exponent into zero if the input was zero.
316*5f32b710SXin Li 	 * 6. Combine with the sign of the input number.
317*5f32b710SXin Li 	 */
318*5f32b710SXin Li 	return sign | (((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) & ~zero_mask);
319*5f32b710SXin Li }
320*5f32b710SXin Li 
321*5f32b710SXin Li /*
322*5f32b710SXin Li  * Convert a 16-bit floating-point number in ARM alternative half-precision format, in bit representation, to
323*5f32b710SXin Li  * a 32-bit floating-point number in IEEE single-precision format.
324*5f32b710SXin Li  *
325*5f32b710SXin Li  * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
326*5f32b710SXin Li  * floating-point operations and bitcasts between integer and floating-point variables.
327*5f32b710SXin Li  */
fp16_alt_to_fp32_value(uint16_t h)328*5f32b710SXin Li static inline float fp16_alt_to_fp32_value(uint16_t h) {
329*5f32b710SXin Li 	/*
330*5f32b710SXin Li 	 * Extend the half-precision floating-point number to 32 bits and shift to the upper part of the 32-bit word:
331*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
332*5f32b710SXin Li 	 *      | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
333*5f32b710SXin Li 	 *      +---+-----+------------+-------------------+
334*5f32b710SXin Li 	 * Bits  31  26-30    16-25            0-15
335*5f32b710SXin Li 	 *
336*5f32b710SXin Li 	 * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0 - zero bits.
337*5f32b710SXin Li 	 */
338*5f32b710SXin Li 	const uint32_t w = (uint32_t) h << 16;
339*5f32b710SXin Li 	/*
340*5f32b710SXin Li 	 * Extract the sign of the input number into the high bit of the 32-bit word:
341*5f32b710SXin Li 	 *
342*5f32b710SXin Li 	 *      +---+----------------------------------+
343*5f32b710SXin Li 	 *      | S |0000000 00000000 00000000 00000000|
344*5f32b710SXin Li 	 *      +---+----------------------------------+
345*5f32b710SXin Li 	 * Bits  31                 0-31
346*5f32b710SXin Li 	 */
347*5f32b710SXin Li 	const uint32_t sign = w & UINT32_C(0x80000000);
348*5f32b710SXin Li 	/*
349*5f32b710SXin Li 	 * Extract mantissa and biased exponent of the input number into the high bits of the 32-bit word:
350*5f32b710SXin Li 	 *
351*5f32b710SXin Li 	 *      +-----+------------+---------------------+
352*5f32b710SXin Li 	 *      |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
353*5f32b710SXin Li 	 *      +-----+------------+---------------------+
354*5f32b710SXin Li 	 * Bits  27-31    17-26            0-16
355*5f32b710SXin Li 	 */
356*5f32b710SXin Li 	const uint32_t two_w = w + w;
357*5f32b710SXin Li 
358*5f32b710SXin Li 	/*
359*5f32b710SXin Li 	 * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become mantissa and exponent
360*5f32b710SXin Li 	 * of a single-precision floating-point number:
361*5f32b710SXin Li 	 *
362*5f32b710SXin Li 	 *       S|Exponent |          Mantissa
363*5f32b710SXin Li 	 *      +-+---+-----+------------+----------------+
364*5f32b710SXin Li 	 *      |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
365*5f32b710SXin Li 	 *      +-+---+-----+------------+----------------+
366*5f32b710SXin Li 	 * Bits   | 23-31   |           0-22
367*5f32b710SXin Li 	 *
368*5f32b710SXin Li 	 * Next, the exponent is adjusted for the difference in exponent bias between single-precision and half-precision
369*5f32b710SXin Li 	 * formats (0x7F - 0xF = 0x70). This operation never overflows or generates non-finite values, as the largest
370*5f32b710SXin Li 	 * half-precision exponent is 0x1F and after the adjustment is can not exceed 0x8F < 0xFE (largest single-precision
371*5f32b710SXin Li 	 * exponent for non-finite values).
372*5f32b710SXin Li 	 *
373*5f32b710SXin Li 	 * Note that this operation does not handle denormal inputs (where biased exponent == 0). However, they also do not
374*5f32b710SXin Li 	 * operate on denormal inputs, and do not produce denormal results.
375*5f32b710SXin Li 	 */
376*5f32b710SXin Li 	const uint32_t exp_offset = UINT32_C(0x70) << 23;
377*5f32b710SXin Li 	const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset);
378*5f32b710SXin Li 
379*5f32b710SXin Li 	/*
380*5f32b710SXin Li 	 * Convert denormalized half-precision inputs into single-precision results (always normalized).
381*5f32b710SXin Li 	 * Zero inputs are also handled here.
382*5f32b710SXin Li 	 *
383*5f32b710SXin Li 	 * In a denormalized number the biased exponent is zero, and mantissa has on-zero bits.
384*5f32b710SXin Li 	 * First, we shift mantissa into bits 0-9 of the 32-bit word.
385*5f32b710SXin Li 	 *
386*5f32b710SXin Li 	 *                  zeros           |  mantissa
387*5f32b710SXin Li 	 *      +---------------------------+------------+
388*5f32b710SXin Li 	 *      |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
389*5f32b710SXin Li 	 *      +---------------------------+------------+
390*5f32b710SXin Li 	 * Bits             10-31                0-9
391*5f32b710SXin Li 	 *
392*5f32b710SXin Li 	 * Now, remember that denormalized half-precision numbers are represented as:
393*5f32b710SXin Li 	 *    FP16 = mantissa * 2**(-24).
394*5f32b710SXin Li 	 * The trick is to construct a normalized single-precision number with the same mantissa and thehalf-precision input
395*5f32b710SXin Li 	 * and with an exponent which would scale the corresponding mantissa bits to 2**(-24).
396*5f32b710SXin Li 	 * A normalized single-precision floating-point number is represented as:
397*5f32b710SXin Li 	 *    FP32 = (1 + mantissa * 2**(-23)) * 2**(exponent - 127)
398*5f32b710SXin Li 	 * Therefore, when the biased exponent is 126, a unit change in the mantissa of the input denormalized half-precision
399*5f32b710SXin Li 	 * number causes a change of the constructud single-precision number by 2**(-24), i.e. the same ammount.
400*5f32b710SXin Li 	 *
401*5f32b710SXin Li 	 * The last step is to adjust the bias of the constructed single-precision number. When the input half-precision number
402*5f32b710SXin Li 	 * is zero, the constructed single-precision number has the value of
403*5f32b710SXin Li 	 *    FP32 = 1 * 2**(126 - 127) = 2**(-1) = 0.5
404*5f32b710SXin Li 	 * Therefore, we need to subtract 0.5 from the constructed single-precision number to get the numerical equivalent of
405*5f32b710SXin Li 	 * the input half-precision number.
406*5f32b710SXin Li 	 */
407*5f32b710SXin Li 	const uint32_t magic_mask = UINT32_C(126) << 23;
408*5f32b710SXin Li 	const float magic_bias = 0.5f;
409*5f32b710SXin Li 	const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
410*5f32b710SXin Li 
411*5f32b710SXin Li 	/*
412*5f32b710SXin Li 	 * - Choose either results of conversion of input as a normalized number, or as a denormalized number, depending on the
413*5f32b710SXin Li 	 *   input exponent. The variable two_w contains input exponent in bits 27-31, therefore if its smaller than 2**27, the
414*5f32b710SXin Li 	 *   input is either a denormal number, or zero.
415*5f32b710SXin Li 	 * - Combine the result of conversion of exponent and mantissa with the sign of the input number.
416*5f32b710SXin Li 	 */
417*5f32b710SXin Li 	const uint32_t denormalized_cutoff = UINT32_C(1) << 27;
418*5f32b710SXin Li 	const uint32_t result = sign |
419*5f32b710SXin Li 		(two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));
420*5f32b710SXin Li 	return fp32_from_bits(result);
421*5f32b710SXin Li }
422*5f32b710SXin Li 
423*5f32b710SXin Li /*
424*5f32b710SXin Li  * Convert a 32-bit floating-point number in IEEE single-precision format to a 16-bit floating-point number in
425*5f32b710SXin Li  * ARM alternative half-precision format, in bit representation.
426*5f32b710SXin Li  *
427*5f32b710SXin Li  * @note The implementation relies on IEEE-like (no assumption about rounding mode and no operations on denormals)
428*5f32b710SXin Li  * floating-point operations and bitcasts between integer and floating-point variables.
429*5f32b710SXin Li  */
fp16_alt_from_fp32_value(float f)430*5f32b710SXin Li static inline uint16_t fp16_alt_from_fp32_value(float f) {
431*5f32b710SXin Li 	const uint32_t w = fp32_to_bits(f);
432*5f32b710SXin Li 	const uint32_t sign = w & UINT32_C(0x80000000);
433*5f32b710SXin Li 	const uint32_t shl1_w = w + w;
434*5f32b710SXin Li 
435*5f32b710SXin Li 	const uint32_t shl1_max_fp16_fp32 = UINT32_C(0x8FFFC000);
436*5f32b710SXin Li 	const uint32_t shl1_base = shl1_w > shl1_max_fp16_fp32 ? shl1_max_fp16_fp32 : shl1_w;
437*5f32b710SXin Li 	uint32_t shl1_bias = shl1_base & UINT32_C(0xFF000000);
438*5f32b710SXin Li 	const uint32_t exp_difference = 23 - 10;
439*5f32b710SXin Li 	const uint32_t shl1_bias_min = (127 - 1 - exp_difference) << 24;
440*5f32b710SXin Li 	if (shl1_bias < shl1_bias_min) {
441*5f32b710SXin Li 		shl1_bias = shl1_bias_min;
442*5f32b710SXin Li 	}
443*5f32b710SXin Li 
444*5f32b710SXin Li 	const float bias = fp32_from_bits((shl1_bias >> 1) + ((exp_difference + 2) << 23));
445*5f32b710SXin Li 	const float base = fp32_from_bits((shl1_base >> 1) + (2 << 23)) + bias;
446*5f32b710SXin Li 
447*5f32b710SXin Li 	const uint32_t exp_f = fp32_to_bits(base) >> 13;
448*5f32b710SXin Li 	return (sign >> 16) | ((exp_f & UINT32_C(0x00007C00)) + (fp32_to_bits(base) & UINT32_C(0x00000FFF)));
449*5f32b710SXin Li }
450*5f32b710SXin Li 
451*5f32b710SXin Li #endif /* FP16_FP16_H */
452