ADS
Algorithms and Data Structures
Toggle main menu visibility
Loading...
Searching...
No Matches
Portable.h
Go to the documentation of this file.
1
/*--------------------------------------------------------------------------*/
2
/*---------------------------- File Portable.h -----------------------------*/
3
/*--------------------------------------------------------------------------*/
40
/*--------------------------------------------------------------------------*/
41
/*----------------------------- DEFINITIONS --------------------------------*/
42
/*--------------------------------------------------------------------------*/
43
44
#ifndef __ADS_Portable
45
#define __ADS_Portable
46
/* self-identification: #endif constant */
47
48
/*--------------------------------------------------------------------------*/
49
/*------------------------------ INCLUDES ----------------------------------*/
50
/*--------------------------------------------------------------------------*/
51
52
#include <bit>
53
#include <compare>
54
#include <concepts>
55
#include <cstdint>
56
#include <type_traits>
57
58
// the swap of bytes of Boost.Core, which knows the one of each compiler
59
// (boost::core::byteswap, taken from Boost 1.83 on)
60
#if __has_include( <boost/version.hpp> )
61
#include <boost/version.hpp>
62
#endif
63
#if defined( BOOST_VERSION ) && ( BOOST_VERSION >= 108300 )
64
#include <boost/core/bit.hpp>
65
#define ADS_BYTESWAP_BOOST 1
66
#else
67
#define ADS_BYTESWAP_BOOST 0
68
#endif
69
70
// The attributes below are chosen by the feature test of the standard
71
// (__has_cpp_attribute), not by the name of the compiler: an attribute of a
72
// namespace the compiler does not know is ignored, but GCC and Clang warn
73
// about it, so that writing both spellings unconditionally would warn in
74
// every translation unit that includes ADS.
75
78
#if __has_cpp_attribute( msvc::no_unique_address )
79
#define ADS_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
80
#else
81
#define ADS_NO_UNIQUE_ADDRESS [[no_unique_address]]
82
#endif
83
85
#if __has_cpp_attribute( gnu::always_inline )
86
#define ADS_ALWAYS_INLINE [[gnu::always_inline]] inline
87
#elif __has_cpp_attribute( msvc::forceinline )
88
#define ADS_ALWAYS_INLINE [[msvc::forceinline]] inline
89
#else
90
#define ADS_ALWAYS_INLINE inline
91
#endif
92
94
#if __has_cpp_attribute( gnu::noinline )
95
#define ADS_NOINLINE [[gnu::noinline]]
96
#elif __has_cpp_attribute( msvc::noinline )
97
#define ADS_NOINLINE [[msvc::noinline]]
98
#else
99
#define ADS_NOINLINE
100
#endif
101
104
#if __has_cpp_attribute( gnu::cold )
105
#define ADS_COLD_PATH [[gnu::cold]] ADS_NOINLINE
106
#else
107
#define ADS_COLD_PATH ADS_NOINLINE
108
#endif
109
114
#if __has_include( <sanitizer/asan_interface.h> )
115
#include <sanitizer/asan_interface.h>
116
#define ADS_POISON( p, n ) ASAN_POISON_MEMORY_REGION( ( p ), ( n ) )
117
#define ADS_UNPOISON( p, n ) ASAN_UNPOISON_MEMORY_REGION( ( p ), ( n ) )
118
#else
119
#define ADS_POISON( p, n ) ( ( void )( p ), ( void )( n ) )
120
#define ADS_UNPOISON( p, n ) ( ( void )( p ), ( void )( n ) )
121
#endif
122
123
/*--------------------------------------------------------------------------*/
124
/*--------------------------- NAMESPACE ------------------------------------*/
125
/*--------------------------------------------------------------------------*/
126
127
namespace
ADS
128
{
129
131
namespace
detail
132
{
133
134
/*--------------------------------------------------------------------------*/
135
137
struct
WordPair
138
{
139
std::uint64_t lo ,
140
hi;
141
};
142
164
[[nodiscard]]
ADS_ALWAYS_INLINE
constexpr
WordPair
165
wideProductHalves( std::uint64_t a , std::uint64_t b )
166
{
167
constexpr
std::uint64_t LOW = 0xFFFFFFFFULL;
168
const
std::uint64_t a0 = a & LOW , a1 = a >> 32 , b0 = b & LOW , b1 = b >> 32;
169
const
std::uint64_t p00 = a0 * b0 , p01 = a0 * b1 , p10 = a1 * b0 ,
170
p11 = a1 * b1;
171
const
std::uint64_t mid = ( p00 >> 32 ) + ( p01 & LOW ) + ( p10 & LOW );
172
return
( WordPair{ ( mid << 32 ) | ( p00 & LOW ) ,
173
p11 + ( p01 >> 32 ) + ( p10 >> 32 ) + ( mid >> 32 ) } );
174
}
175
176
/*--------------------------------------------------------------------------*/
177
182
constexpr
std::uint64_t byteSwap( std::uint64_t x )
183
{
184
#if defined( __cpp_lib_byteswap )
185
return
( std::byteswap( x ) );
186
#elif ADS_BYTESWAP_BOOST
187
return
( boost::core::byteswap( x ) );
188
#else
189
x = ( ( x >> 8 ) & 0x00FF00FF00FF00FFULL ) |
190
( ( x & 0x00FF00FF00FF00FFULL ) << 8 );
191
x = ( ( x >> 16 ) & 0x0000FFFF0000FFFFULL ) |
192
( ( x & 0x0000FFFF0000FFFFULL ) << 16 );
193
return
( ( x >> 32 ) | ( x << 32 ) );
194
#endif
195
}
196
197
/*--------------------------------------------------------------------------*/
198
199
}
// end( namespace detail )
201
202
/*--------------------------------------------------------------------------*/
203
204
}
// end( namespace ADS )
205
206
/*--------------------------------------------------------------------------*/
207
208
#endif
/* Portable.h included */
209
210
/*--------------------------------------------------------------------------*/
211
/*-------------------------- End File Portable.h ---------------------------*/
212
/*--------------------------------------------------------------------------*/
ADS_ALWAYS_INLINE
#define ADS_ALWAYS_INLINE
a function inlined even where the compiler would not
Definition
Portable.h:90
ADS
Definition
ADT.h:96
ADS
Portable.h
Generated by
1.17.0