Coverage Report

Created: 2023-06-07 06:28

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