77#include < cstdint>
88#include < cstdio>
99
10- struct half ;
11- static half halfFromFloat (float f);
12- static float halfToFloat (half h);
13-
1410#ifdef _MSC_VER
1511#include < intrin.h>
1612
17- static inline uint32_t __builtin_clz (uint32_t value) {
18- unsigned long leading_zero = 0 ;
19- if (value == 0 ) {
20- return 32 ;
21- }
22- _BitScanReverse (&leading_zero, value);
23- return 31 - leading_zero;
13+ static inline uint32_t __builtin_clz (uint32_t value)
14+ {
15+ unsigned long leading_zero = 0 ;
16+ if (value == 0 )
17+ {
18+ return 32 ;
19+ }
20+ _BitScanReverse (&leading_zero, value);
21+ return 31 - leading_zero;
2422}
2523
26- static inline uint16_t __builtin_clz (uint16_t value) {
27- return __builtin_clz (static_cast <uint32_t >(value)) - 16 ;
24+ static inline uint16_t __builtin_clz (uint16_t value)
25+ {
26+ return __builtin_clz (static_cast <uint32_t >(value)) - 16 ;
2827}
2928
30- static inline uint64_t __builtin_clz (uint64_t value) {
31- unsigned long leading_zero = 0 ;
32- if (value == 0 ) {
33- return 64 ;
34- }
29+ static inline uint64_t __builtin_clz (uint64_t value)
30+ {
31+ unsigned long leading_zero = 0 ;
32+ if (value == 0 )
33+ {
34+ return 64 ;
35+ }
3536#if defined(_WIN64)
36- _BitScanReverse64 (&leading_zero, value);
37- return 63 - leading_zero;
37+ _BitScanReverse64 (&leading_zero, value);
38+ return 63 - leading_zero;
3839#else
39- uint32_t high = static_cast <uint32_t >(value >> 32 );
40- uint32_t low = static_cast <uint32_t >(value);
41- if (high != 0 ) {
42- return __builtin_clz (high);
43- } else {
44- return 32 + __builtin_clz (low);
45- }
40+ uint32_t high = static_cast <uint32_t >(value >> 32 );
41+ uint32_t low = static_cast <uint32_t >(value);
42+ if (high != 0 )
43+ {
44+ return __builtin_clz (high);
45+ }
46+ else
47+ {
48+ return 32 + __builtin_clz (low);
49+ }
4650#endif
4751}
4852#endif
4953
54+ struct half ;
55+ static inline half halfFromFloat (float f);
56+ static inline float halfToFloat (half h);
57+
5058/* *
5159 * Experimental implementation of half-precision 16-bit floating point numbers.
5260 */
53- struct half {
61+ struct half
62+ {
5463 uint16_t data;
5564
5665 // Default constructor
@@ -68,19 +77,22 @@ struct half {
6877 operator uint16_t () const { return data; }
6978
7079 // Overload assignment operator from uint16_t
71- half &operator =(uint16_t value) {
80+ half &operator =(uint16_t value)
81+ {
7282 data = value;
7383 return *this ;
7484 }
7585
7686 // Overload assignment operator from another half
77- half &operator =(const half &other) {
87+ half &operator =(const half &other)
88+ {
7889 data = other.data ;
7990 return *this ;
8091 }
8192
8293 // Overload assignment operator from float
83- half &operator =(float value) {
94+ half &operator =(float value)
95+ {
8496 data = halfFromFloat (value);
8597 return *this ;
8698 }
@@ -91,8 +103,10 @@ struct half {
91103 *
92104 * Based on Mike Acton's half.c implementation.
93105 */
94- half halfFromFloat (float f) {
95- union {
106+ half halfFromFloat (float f)
107+ {
108+ union
109+ {
96110 float f;
97111 uint32_t u;
98112 } floatUnion = {f};
@@ -131,7 +145,8 @@ half halfFromFloat(float f) {
131145 const uint32_t floatMantissa = float32 & FLOAT_MANTISSA_MASK;
132146
133147 // Check for NaN
134- if ((floatExpMasked == FLOAT_EXP_MASK) && (floatMantissa != 0 )) {
148+ if ((floatExpMasked == FLOAT_EXP_MASK) && (floatMantissa != 0 ))
149+ {
135150 half result;
136151 result.data =
137152 HALF_EXP_MASK | (floatMantissa >> FLOAT_HALF_MANTISSA_POS_OFFSET);
@@ -211,7 +226,8 @@ half halfFromFloat(float f) {
211226 *
212227 * Based on Mike Acton's half.c implementation.
213228 */
214- float halfToFloat (half h) {
229+ float halfToFloat (half h)
230+ {
215231 // Constants for bit masks, shifts, and biases
216232 const uint16_t ONE = 0x0001 ;
217233 const uint16_t TWO = 0x0002 ;
@@ -292,7 +308,8 @@ float halfToFloat(half h) {
292308 const uint32_t result = checkNanResult;
293309
294310 // Reinterpret the uint32_t result as a float using a union
295- union {
311+ union
312+ {
296313 uint32_t u;
297314 float f;
298315 } floatUnion;
0 commit comments