23 #ifndef __CLRX_UTILITIES_H__ 24 #define __CLRX_UTILITIES_H__ 69 explicit Exception(
const std::string& message);
74 const
char* what() const noexcept;
94 ParseException(LineNo lineNo, ColNo charNo,
const std::string& message);
103 FLAGS_ALL = 0xffffffffU
110 DYNLIB_MODE1_MASK = 7,
119 static std::mutex mutex;
126 DynLibrary(
const char* filename, Flags flags = 0);
133 void load(
const char* filename, Flags flags = 0);
138 void* getSymbol(
const char* symbolName);
141 operator bool()
const 142 {
return handle!=
nullptr; }
146 {
return handle==
nullptr; }
149 bool isLoaded()
const 150 {
return handle!=
nullptr; }
156 inline bool isSpace(
unsigned char c);
159 {
return (c == 32 || (c < 32 && (0x3e00U & (1U<<c)))); }
162 inline bool isODigit(
unsigned char c);
165 {
return c>=
'0' && c<=
'7'; }
168 inline bool isDigit(
unsigned char c);
171 {
return c>=
'0' && c<=
'9'; }
174 inline bool isXDigit(
unsigned char c);
177 {
return (c>=
'0' && c<=
'9') || (c>=
'a' && c<='f') || (c>=
'A' && c<=
'F'); }
180 inline bool isAlpha(
unsigned char c);
183 {
return (c>=
'a' && c<=
'z') || (c>=
'A' && c<=
'Z'); }
186 inline bool isAlnum(
unsigned char c);
189 {
return (c>=
'0' && c<=
'9') || (c>=
'a' && c<='z') || (c>=
'A' && c<=
'Z'); }
205 const char* t = s+length;
206 if (t == s)
return s;
207 for (t--; t != s-1 &&
isSpace(*t); t--);
227 extern T
cstrtovCStyle(
const char* str,
const char* inend,
const char*& outend);
238 const char* var = getenv(envVar);
246 {
return cstrtovCStyle<T>(var,
nullptr, outend); }
248 {
return defaultValue; }
299 #ifndef __UTILITIES_MODULE__ 303 std::string parseEnvVariable<std::string>(
const char* envVar,
304 const std::string& defaultValue);
317 { return ::strcmp(c1, c2)<0; }
325 { return ::strcasecmp(c1, c2)<0; }
333 { return ::strcmp(c1, c2)==0; }
346 for (
const char* p = c; *p != 0; p++)
347 hash = ((hash<<8)^(
cxbyte)*p)*size_t(0xbf146a3dU);
364 return __builtin_clz(v);
368 _BitScanReverse(&index, v);
372 for (uint32_t t = 1U<<31; t > v; t>>=1, count++);
381 return __builtin_clzll(v);
384 # ifdef HAVE_ARCH_X86 385 unsigned long indexlo, indexhi;
387 _BitScanReverse(&indexlo, uint32_t(v));
388 nzhi = _BitScanReverse(&indexhi, uint32_t(v>>32));
390 indexlo = (nzhi ? indexhi+32 : indexlo);
394 _BitScanReverse64(&index, v);
399 for (uint64_t t = 1ULL<<63; t > v; t>>=1, count++);
408 return __builtin_ctz(v);
412 _BitScanForward(&index, v);
416 for (uint32_t t = 1U; (t & v) == 0; t<<=1, count++);
425 return __builtin_ctzll(v);
428 # ifdef HAVE_ARCH_X86 429 unsigned long indexlo, indexhi;
431 nzlo = _BitScanForward(&indexlo, uint32_t(v));
432 _BitScanForward(&indexhi, uint32_t(v>>32));
434 indexlo = (nzlo ? indexlo : indexhi+32);
438 _BitScanForward64(&index, v);
443 for (uint64_t t = 1ULL; (t & v) == 0; t<<=1, count++);
451 template<
typename T,
typename T2>
453 {
return ((a+b)>c) || ((a+b)<a); }
456 template<
typename T,
typename T2>
458 {
return ((a+b)>=c) || ((a+b)<a); }
482 size_t outMaxSize,
char* outStr,
size_t& outSize);
493 extern cxuint cstrtoui(
const char* str,
const char* inend,
const char*& outend);
497 const char*& outend,
cxuint bits);
500 extern uint64_t
cstrtouXCStyle(
const char* str,
const char* inend,
501 const char*& outend,
cxuint bits);
511 extern uint64_t
cstrtofXCStyle(
const char* str,
const char* inend,
512 const char*& outend,
cxuint expBits,
cxuint mantisaBits);
618 cxuint width,
bool prefix);
622 cxuint width,
bool prefix);
636 cxuint width = 0,
bool prefix =
true);
641 cxuint width,
bool prefix)
642 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
647 cxuint width,
bool prefix)
648 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
653 cxuint width,
bool prefix)
654 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
659 cxuint width,
bool prefix)
660 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
665 cxuint width,
bool prefix)
666 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
671 cxuint width,
bool prefix)
672 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
677 cxuint width,
bool prefix)
678 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
683 cxuint width,
bool prefix)
684 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
689 cxuint width ,
bool prefix)
690 {
return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
695 cxuint width,
bool prefix)
696 {
return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
699 extern size_t fXtocstrCStyle(uint64_t value,
char* str,
size_t maxSize,
714 bool scientific =
false);
717 {
return fXtocstrCStyle(value, str, maxSize, scientific, 5, 10); }
731 bool scientific =
false);
733 inline size_t ftocstrCStyle(
float value,
char* str,
size_t maxSize,
bool scientific)
754 size_t dtocstrCStyle(
double value,
char* str,
size_t maxSize,
755 bool scientific =
false);
757 inline size_t dtocstrCStyle(
double value,
char* str,
size_t maxSize,
bool scientific)
787 extern std::string
joinPaths(
const std::string& path1,
const std::string& path2);
795 extern void makeDir(
const char* dirname);
816 mutable std::atomic<size_t> refCount;
825 refCount.fetch_add(1);
831 return (refCount.fetch_sub(1) == 1);
839 mutable size_t refCount;
854 return (--refCount == 0);
870 explicit RefPtr(T* inputPtr) : ptr(inputPtr)
882 { refPtr.ptr =
nullptr; }
888 if (ptr->unreference())
896 if (ptr->unreference())
898 if (refPtr.ptr !=
nullptr)
899 refPtr.ptr->reference();
907 if (ptr->unreference())
910 refPtr.ptr =
nullptr;
916 {
return ptr == refPtr.ptr; }
919 {
return ptr != refPtr.ptr; }
922 operator bool()
const 923 {
return ptr!=
nullptr; }
927 {
return ptr==
nullptr; }
941 if (ptr->unreference())
955 template<
typename DestType>
958 DestType* p =
const_cast<DestType*
>(ptr);
964 template<
typename DestType>
967 DestType* p =
static_cast<DestType*
>(ptr);
973 template<
typename DestType>
976 DestType* p =
dynamic_cast<DestType*
>(ptr);
987 {
return (c >=
'A' && c <=
'Z') ? c -
'A' +
'a' : c; }
993 { std::transform(
string.begin(),
string.end(),
string.begin(), toLower); }
1000 for (; *cstr!=0; cstr++)
1008 {
if (!
string.empty())
1015 {
return (c >=
'a' && c <=
'z') ? c -
'a' +
'A' : c; }
1021 { std::transform(
string.begin(),
string.end(),
string.begin(), toUpper); }
1028 for (; *cstr!=0; cstr++)
1036 {
if (!
string.empty())
1041 #ifdef HAVE_CALL_ONCE 1046 template<
class Callable,
class... Args>
1047 inline void callOnce(std::once_flag& flag, Callable&& f, Args&&... args)
1048 { std::call_once(flag, f, args...); }
1050 struct OnceFlag: std::atomic<int>
1057 template<
class Callable,
class... Args>
1058 inline void callOnce(OnceFlag& flag, Callable&& f, Args&&... args)
1060 if (flag.exchange(1) == 0)
reference countable object
Definition: Utilities.h:813
template bool parseEnvVariable< bool >(const char *envVar, const bool &defaultValue)
parse environment variable of boolean type
bool operator!=(const RefPtr< T > &refPtr) const
unequality operator
Definition: Utilities.h:918
function class that returns true if C strings are equal
Definition: Utilities.h:329
function class that returns true if first C string is less than second (ignore case) ...
Definition: Utilities.h:321
int64_t cstrtoiXCStyle(const char *str, const char *inend, const char *&outend, cxuint bits)
parse 64-bit signed integer
cxlong cstrtovCStyle< cxlong >(const char *str, const char *inend, const char *&outend)
parse cxlong value from string
Definition: Utilities.h:556
bool isFileExists(const char *path)
returns true if file exists
std::string escapeStringCStyle(size_t strSize, const char *str)
escapes string into C-style string
non copyable and non movable base structure (class)
Definition: Utilities.h:46
bool operator!() const
return true if null
Definition: Utilities.h:926
uint64_t lo
low part
Definition: Utilities.h:506
bool unreference() const
unreference object (returns true if no reference count)
Definition: Utilities.h:829
template cxint parseEnvVariable< cxint >(const char *envVar, const cxint &defaultValue)
parse environment variable of cxint type
bool operator()(const char *c1, const char *c2) const
operator of call
Definition: Utilities.h:332
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:100
cxuint cstrtovCStyle< cxuint >(const char *str, const char *inend, const char *&outend)
parse cxuint value from string
Definition: Utilities.h:531
size_t itocstrCStyle< cxullong >(cxullong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxullong value to string
Definition: Utilities.h:688
template cxushort parseEnvVariable< cxushort >(const char *envVar, const cxushort &defaultValue)
parse environment variable of cxushort type
void makeDir(const char *dirname)
create directory
template cxuchar parseEnvVariable< cxuchar >(const char *envVar, const cxuchar &defaultValue)
parse environment variable of cxuchar type
template double parseEnvVariable< double >(const char *envVar, const double &defaultValue)
parse environment variable of double type
const char * c_str() const
return C-style string pointer
Definition: CString.h:254
size_t iXtocstrCStyle(int64_t value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
formast signed value to string
bool isDirectory(const char *path)
returns true if path refers to directory
reference pointer based on Glibmm refptr
Definition: Utilities.h:860
RefPtr< DestType > staticCast() const
static cast
Definition: Utilities.h:965
bool unreference() const
unreference object (returns true if no reference count)
Definition: Utilities.h:852
template cxullong parseEnvVariable< cxullong >(const char *envVar, const cxullong &defaultValue)
parse environment variable of cxullong type
signed short cxshort
signed short
Definition: Config.h:231
template cxchar parseEnvVariable< cxchar >(const char *envVar, const cxchar &defaultValue)
parse environment variable of cxchar type
cxullong cstrtovCStyle< cxullong >(const char *str, const char *inend, const char *&outend)
parse cxullong value from string
Definition: Utilities.h:561
size_t fXtocstrCStyle(uint64_t value, char *str, size_t maxSize, bool scientific, cxuint expBits, cxuint mantisaBits)
format float value to string
signed char cxchar
signed character (signed byte)
Definition: Config.h:225
Unsigned 128-bit integer.
Definition: Utilities.h:504
size_t itocstrCStyle< cxushort >(cxushort value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxushort value to string
Definition: Utilities.h:652
RefPtr< DestType > constCast() const
const cast
Definition: Utilities.h:956
bool operator()(const char *c1, const char *c2) const
operator of call
Definition: Utilities.h:316
std::string findAmdOCL()
find amdocl library, returns path if found, otherwise returns empty string
signed long cxlong
signed long
Definition: Config.h:239
RefCountable()
constructor
Definition: Utilities.h:819
RefPtr(T *inputPtr)
constructor from pointer
Definition: Utilities.h:870
an array class
Definition: Containers.h:41
RefPtr< DestType > dynamicCast() const
dynamic cast
Definition: Utilities.h:974
cxuint CLZ32(uint32_t v)
counts leading zeroes for 32-bit unsigned integer. For zero behavior is undefined ...
Definition: Utilities.h:361
template cxuint parseEnvVariable< cxuint >(const char *envVar, const cxuint &defaultValue)
parse environment variable of cxuint type
bool operator!() const
returns true if not loaded
Definition: Utilities.h:145
bool operator==(const RefPtr< T > &refPtr) const
equality operator
Definition: Utilities.h:915
resolve symbols when is needed
Definition: Utilities.h:108
cxuint CTZ64(uint64_t v)
counts trailing zeroes for 64-bit unsigned integer. For zero behavior is undefined ...
Definition: Utilities.h:422
bool isDigit(unsigned char c)
check whether character is digit
Definition: Utilities.h:170
std::string findMesaOCL()
find Mesa OpenCL library, returns path if found, otherwise returns empty string
size_t dtocstrCStyle(double value, char *str, size_t maxSize, bool scientific=false)
formats double float in C-style
Definition: Utilities.h:757
uint64_t getFileTimestamp(const char *filename)
get file timestamp in nanosecond since Unix epoch
~RefPtr()
destructor
Definition: Utilities.h:885
const char * skipSpaces(const char *s)
skip spaces from cString
Definition: Utilities.h:194
unsigned char cxuchar
unsigned character (unsigned byte)
Definition: Config.h:227
size_t itocstrCStyle< cxuchar >(cxuchar value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxuchar value to string
Definition: Utilities.h:640
RefPtr< T > & operator=(const RefPtr< T > &refPtr)
copy constructor
Definition: Utilities.h:893
template cxshort parseEnvVariable< cxshort >(const char *envVar, const cxshort &defaultValue)
parse environment variable of cxshort type
bool usumGt(T a, T b, T2 c)
safely compares sum of two unsigned integers with other unsigned integer
Definition: Utilities.h:452
UInt128 cstrtou128CStyle(const char *str, const char *inend, const char *&outend)
parse 128-bit unsigned integer
uint64_t cstrtouXCStyle(const char *str, const char *inend, const char *&outend, cxuint bits)
parse 64-bit unsigned integer
size_t ColNo
column number type
Definition: Utilities.h:81
template float parseEnvVariable< float >(const char *envVar, const float &defaultValue)
parse environment variable of float type
UInt128 cstrtovCStyle< UInt128 >(const char *str, const char *inend, const char *&outend)
parse UInt128 value from string
Definition: Utilities.h:571
size_t itocstrCStyle< cxulong >(cxulong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxulong value to string
Definition: Utilities.h:676
cxint cstrtovCStyle< cxint >(const char *str, const char *inend, const char *&outend)
parse cxint value from string
Definition: Utilities.h:536
unsigned long long cxullong
unsigned long long
Definition: Config.h:245
unsigned char cxbyte
unsigned byte
Definition: Config.h:229
size_t ftocstrCStyle(float value, char *str, size_t maxSize, bool scientific=false)
formats single float in C-style
Definition: Utilities.h:733
const char * skipSpacesAtEnd(const char *s, size_t length)
skip spaces from cString
Definition: Utilities.h:203
reference countable object (only for single threading usage)
Definition: Utilities.h:836
Array< cxbyte > loadDataFromFile(const char *filename)
load data from file (any regular or pipe or device)
bool isODigit(unsigned char c)
check whether character is digit
Definition: Utilities.h:164
template cxlong parseEnvVariable< cxlong >(const char *envVar, const cxlong &defaultValue)
parse environment variable of cxlong type
bool isSpace(unsigned char c)
check whether character is space
Definition: Utilities.h:158
void toUpperString(std::string &string)
convert string to uppercase
Definition: Utilities.h:1020
main namespace
Definition: AsmDefs.h:38
bool usumGe(T a, T b, T2 c)
safely compares sum of two unsigned integers with other unsigned integer
Definition: Utilities.h:457
T cstrtovCStyle(const char *str, const char *inend, const char *&outend)
parses integer or float point formatted looks like C-style
std::string findLLVMConfig()
find LLVM config, returns path if found, otherwise returns empty string
cxuint CTZ32(uint32_t v)
counts trailing zeroes for 32-bit unsigned integer. For zero behavior is undefined ...
Definition: Utilities.h:405
unsigned int cxuint
unsigned int
Definition: Config.h:237
void reset()
reset refpointer
Definition: Utilities.h:938
cxuint cstrtoui(const char *str, const char *inend, const char *&outend)
parses unsigned integer regardless locales
float cstrtovCStyle< float >(const char *str, const char *inend, const char *&outend)
parse float value from string
Definition: Utilities.h:576
std::string getHomeDir()
get user's home directory
bool isAlnum(unsigned char c)
check whether character is digit
Definition: Utilities.h:188
size_t uXtocstrCStyle(uint64_t value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format unsigned value to string
bool isAlpha(unsigned char c)
check whether character is digit
Definition: Utilities.h:182
size_t itocstrCStyle< cxchar >(cxchar value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxchar value to string
Definition: Utilities.h:646
treats symbols globally
Definition: Utilities.h:111
size_t itocstrCStyle< cxshort >(cxshort value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxshort value to string
Definition: Utilities.h:658
unsigned short cxushort
unsigned short
Definition: Config.h:233
void toLowerString(std::string &string)
convert string to lowercase
Definition: Utilities.h:992
T parseEnvVariable(const char *envVar, const T &defaultValue=T())
parse environment variable
Definition: Utilities.h:236
unsigned long cxulong
unsigned long
Definition: Config.h:241
size_t operator()(const char *c) const
operator of call
Definition: Utilities.h:340
size_t itocstrCStyle< cxint >(cxint value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxint value to string
Definition: Utilities.h:670
uint64_t cstrtofXCStyle(const char *str, const char *inend, const char *&outend, cxuint expBits, cxuint mantisaBits)
parse 64-bit float value
generate hash function for C string
Definition: Utilities.h:337
void swap(RefPtr< T > &refPtr)
swap between refpointers
Definition: Utilities.h:947
signed long long cxllong
signed long long
Definition: Config.h:243
cxllong cstrtovCStyle< cxllong >(const char *str, const char *inend, const char *&outend)
parse cxllong value from string
Definition: Utilities.h:566
bool isXDigit(unsigned char c)
check whether character is hexadecimal digit
Definition: Utilities.h:176
cxshort cstrtovCStyle< cxshort >(const char *str, const char *inend, const char *&outend)
parse cxshort value from string
Definition: Utilities.h:546
Array< cxbyte > runExecWithOutput(const char *program, const char **argv)
run executable with output, returns array of output
cxuchar cstrtovCStyle< cxuchar >(const char *str, const char *inend, const char *&outend)
parse cxuchar value from string
Definition: Utilities.h:521
char toLower(char c)
convert character to lowercase
Definition: Utilities.h:986
size_t itocstrCStyle< cxlong >(cxlong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxlong value to string
Definition: Utilities.h:682
NonCopyableAndNonMovable & operator=(const NonCopyableAndNonMovable &)=delete
copy-assignment
std::string message
message
Definition: Utilities.h:64
FastRefCountable()
constructor
Definition: Utilities.h:842
uint64_t hi
high part
Definition: Utilities.h:507
size_t htocstrCStyle(cxushort value, char *str, size_t maxSize, bool scientific=false)
formats half float in C-style
Definition: Utilities.h:716
exception class
Definition: Utilities.h:61
cxchar cstrtovCStyle< cxchar >(const char *str, const char *inend, const char *&outend)
parse cxchar value from string
Definition: Utilities.h:526
void reference() const
reference object
Definition: Utilities.h:846
parse exception class
Definition: Utilities.h:84
bool operator()(const char *c1, const char *c2) const
operator of call
Definition: Utilities.h:324
cxushort cstrtovCStyle< cxushort >(const char *str, const char *inend, const char *&outend)
parse cxushort value from string
Definition: Utilities.h:541
char toUpper(char c)
convert character to uppercase
Definition: Utilities.h:1014
std::string joinPaths(const std::string &path1, const std::string &path2)
join two paths
uint64_t LineNo
line number type
Definition: Utilities.h:78
signed int cxint
signed int
Definition: Config.h:235
treat symbols locally
Definition: Utilities.h:107
template cxllong parseEnvVariable< cxllong >(const char *envVar, const cxllong &defaultValue)
parse environment variable of cxllong type
RefPtr(const RefPtr< T > &refPtr)
copy constructor
Definition: Utilities.h:874
function class that returns true if first C string is less than second
Definition: Utilities.h:313
RefPtr(RefPtr< T > &&refPtr)
move constructor
Definition: Utilities.h:881
void callOnce(std::once_flag &flag, Callable &&f, Args &&...args)
callOnce - portable wrapper for std::call_once
Definition: Utilities.h:1047
cxushort cstrtohCStyle(const char *str, const char *inend, const char *&outend)
parses half float formatted looks like C-style
Definition: Utilities.h:613
cxuint CLZ64(uint64_t v)
counts leading zeroes for 64-bit unsigned integer. For zero behavior is undefined ...
Definition: Utilities.h:378
template cxulong parseEnvVariable< cxulong >(const char *envVar, const cxulong &defaultValue)
parse environment variable of cxulong type
NonCopyableAndNonMovable()
constructor
Definition: Utilities.h:49
double cstrtovCStyle< double >(const char *str, const char *inend, const char *&outend)
parse double value from string
Definition: Utilities.h:588
resolve symbols now
Definition: Utilities.h:109
std::once_flag OnceFlag
Once flag type (wrapper for std::once_flag)
Definition: Utilities.h:1043
RefPtr()
empty constructor
Definition: Utilities.h:866
size_t itocstrCStyle(T value, char *str, size_t maxSize, cxuint radix=10, cxuint width=0, bool prefix=true)
formats an integer
size_t size() const
compute size
Definition: CString.h:274
simple C-string container
Definition: CString.h:38
size_t itocstrCStyle< cxuint >(cxuint value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxuint value to string
Definition: Utilities.h:664
dynamic library class
Definition: Utilities.h:115
containers and other utils for other libraries and programs
cxulong cstrtovCStyle< cxulong >(const char *str, const char *inend, const char *&outend)
parse cxulong value from string
Definition: Utilities.h:551
void filesystemPath(char *path)
convert to filesystem from unified path (with slashes)
size_t itocstrCStyle< cxllong >(cxllong value, char *str, size_t maxSize, cxuint radix, cxuint width, bool prefix)
format cxllong value to string
Definition: Utilities.h:694
RefPtr< T > & operator=(RefPtr< T > &&refPtr)
move constructor
Definition: Utilities.h:904
void reference() const
reference object
Definition: Utilities.h:823
T * operator->() const
get elem from pointer
Definition: Utilities.h:930