Skip to content

Commit 4241606

Browse files
authored
Merge pull request #47 from MichealReed/static_helpers
MSVC build fixes
2 parents e124bb1 + ea810b8 commit 4241606

2 files changed

Lines changed: 71 additions & 19 deletions

File tree

numeric_types/half.h

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,59 @@
77
#include <cstdint>
88
#include <cstdio>
99

10+
#ifdef _MSC_VER
11+
#include <intrin.h>
12+
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;
22+
}
23+
24+
static inline uint16_t __builtin_clz(uint16_t value)
25+
{
26+
return __builtin_clz(static_cast<uint32_t>(value)) - 16;
27+
}
28+
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+
}
36+
#if defined(_WIN64)
37+
_BitScanReverse64(&leading_zero, value);
38+
return 63 - leading_zero;
39+
#else
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+
}
50+
#endif
51+
}
52+
#endif
53+
1054
struct half;
11-
half halfFromFloat(float f);
12-
float halfToFloat(half h);
55+
static inline half halfFromFloat(float f);
56+
static inline float halfToFloat(half h);
1357

1458
/**
1559
* Experimental implementation of half-precision 16-bit floating point numbers.
1660
*/
17-
struct half {
61+
struct half
62+
{
1863
uint16_t data;
1964

2065
// Default constructor
@@ -32,19 +77,22 @@ struct half {
3277
operator uint16_t() const { return data; }
3378

3479
// Overload assignment operator from uint16_t
35-
half &operator=(uint16_t value) {
80+
half &operator=(uint16_t value)
81+
{
3682
data = value;
3783
return *this;
3884
}
3985

4086
// Overload assignment operator from another half
41-
half &operator=(const half &other) {
87+
half &operator=(const half &other)
88+
{
4289
data = other.data;
4390
return *this;
4491
}
4592

4693
// Overload assignment operator from float
47-
half &operator=(float value) {
94+
half &operator=(float value)
95+
{
4896
data = halfFromFloat(value);
4997
return *this;
5098
}
@@ -55,8 +103,10 @@ struct half {
55103
*
56104
* Based on Mike Acton's half.c implementation.
57105
*/
58-
half halfFromFloat(float f) {
59-
union {
106+
half halfFromFloat(float f)
107+
{
108+
union
109+
{
60110
float f;
61111
uint32_t u;
62112
} floatUnion = {f};
@@ -95,7 +145,8 @@ half halfFromFloat(float f) {
95145
const uint32_t floatMantissa = float32 & FLOAT_MANTISSA_MASK;
96146

97147
// Check for NaN
98-
if ((floatExpMasked == FLOAT_EXP_MASK) && (floatMantissa != 0)) {
148+
if ((floatExpMasked == FLOAT_EXP_MASK) && (floatMantissa != 0))
149+
{
99150
half result;
100151
result.data =
101152
HALF_EXP_MASK | (floatMantissa >> FLOAT_HALF_MANTISSA_POS_OFFSET);
@@ -175,7 +226,8 @@ half halfFromFloat(float f) {
175226
*
176227
* Based on Mike Acton's half.c implementation.
177228
*/
178-
float halfToFloat(half h) {
229+
float halfToFloat(half h)
230+
{
179231
// Constants for bit masks, shifts, and biases
180232
const uint16_t ONE = 0x0001;
181233
const uint16_t TWO = 0x0002;
@@ -256,7 +308,8 @@ float halfToFloat(half h) {
256308
const uint32_t result = checkNanResult;
257309

258310
// Reinterpret the uint32_t result as a float using a union
259-
union {
311+
union
312+
{
260313
uint32_t u;
261314
float f;
262315
} floatUnion;

utils/logging.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,13 @@ namespace gpu
9797
*/
9898
static Logger kDefLog = {std::cout, kInfo};
9999

100-
/**
101-
* @brief Set the log level of the default logger.
102-
* @param level The log level to set.
103-
*/
104-
static inline void setLogLevel(int level)
105-
{
106-
kDefLog.level = level;
107-
}
100+
/**
101+
* @brief Set the log level of the default logger.
102+
* @param level The log level to set.
103+
*/
104+
static inline void setLogLevel(int level) {
105+
kDefLog.level = level;
106+
}
108107

109108
} // namespace gpu
110109

0 commit comments

Comments
 (0)