-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathPlatform.h
More file actions
116 lines (89 loc) · 3.03 KB
/
Copy pathPlatform.h
File metadata and controls
116 lines (89 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef _IN_CSP_CORE_PLATFORM_H
#define _IN_CSP_CORE_PLATFORM_H
#include <type_traits>
#include <stdint.h>
#include <time.h>
// cmake autogenerated export files
#include <csp/core/cspcore_export.h>
#include <csp/engine/cspengine_export.h>
#include <csp/engine/csptypes_export.h>
#include <csp/python/cspimpl_export.h>
#include <csp/python/csptypesimpl_export.h>
#include <csp/python/adapters/cspkafkaadapterimpl_export.h>
#include <csp/python/adapters/cspparquetadapterimpl_export.h>
#include <csp/python/adapters/cspwebsocketadapterimpl_export.h>
//TODO move Likely.h defines into Platform.h
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#include <assert.h>
#include <synchapi.h>
#undef ERROR
#undef GetMessage
#define CSP_PUBLIC __declspec(dllexport) // just for exceptions!
#define START_PACKED __pragma( pack(push, 1) )
#define END_PACKED __pragma( pack(pop))
#define NO_INLINE __declspec(noinline)
inline tm * localtime_r( const time_t * timep, tm * result )
{
tm * rv = localtime(timep);
if (rv)
*result = *rv;
return result;
}
#define timegm _mkgmtime
inline int nanosleep(const timespec* req, timespec* rem)
{
assert(rem == nullptr);
int64_t millis = req->tv_sec * 1000 + req->tv_nsec * 1000000;
Sleep(millis);
return 0;
}
inline uint8_t clz(uint64_t n)
{
unsigned long index = 0;
if (_BitScanReverse64(&index, n))
return 64 - index - 1;
return 0;
}
inline uint8_t clz(uint32_t n)
{
unsigned long index = 0;
if (_BitScanReverse(&index, n))
return 32 - index - 1;
return 0;
}
inline uint8_t clz(uint16_t n) { return clz(static_cast<uint32_t>(n)) - 16; }
inline uint8_t clz(uint8_t n) { return clz(static_cast<uint32_t>(n)) - 24; }
template<typename U, std::enable_if_t<std::is_unsigned<U>::value, bool> = true>
inline uint8_t ffs(U n)
{
unsigned long index = 0;
if (_BitScanForward(&index, n))
return index + 1;
return 0;
}
inline uint8_t ffs(uint64_t n)
{
unsigned long index = 0;
if (_BitScanForward64(&index, n))
return index + 1;
return 0;
}
#else
#define CSP_PUBLIC __attribute__ ((visibility ("default")))
#define START_PACKED
#define END_PACKED __attribute__((packed))
#define NO_INLINE __attribute__ ((noinline))
inline constexpr uint8_t clz(uint32_t n) { return __builtin_clz(n); }
inline constexpr uint8_t clz(uint64_t n) { return __builtin_clzl(n); }
// clz (count leading zeros) returns number of leading zeros before MSB (i.e. clz(00110..) = 2 )
// __builtin_clz auto-promotes to 32-bits: need to subtract off extra leading zeros
inline constexpr uint8_t clz(uint16_t n) { return clz(static_cast<uint32_t>(n)) - 16; }
inline constexpr uint8_t clz(uint8_t n) { return clz(static_cast<uint32_t>(n)) - 24; }
// ffs (find first set) returns offset of first set bit (i.e. ffs(..0110) = 2 ), with ffs(0) = 0
template<typename U, std::enable_if_t<std::is_unsigned<U>::value, bool> = true>
inline constexpr uint8_t ffs( U n ) { return __builtin_ffs(n); }
inline constexpr uint8_t ffs( uint64_t n ) { return __builtin_ffsl(n); }
#endif
#endif