CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
Utilities.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2018 Mateusz Szpakowski
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
23 #ifndef __CLRX_UTILITIES_H__
24 #define __CLRX_UTILITIES_H__
25 
26 #include <CLRX/Config.h>
27 #include <exception>
28 #include <string>
29 #include <vector>
30 #include <cstdlib>
31 #include <cstring>
32 #include <cstdint>
33 #include <mutex>
34 #include <atomic>
35 #ifdef _MSC_VER
36 #include <intrin.h>
37 #endif
38 #include <CLRX/utils/Containers.h>
39 #include <CLRX/utils/CString.h>
40 
42 namespace CLRX
43 {
44 
47 {
58 };
59 
61 class Exception: public std::exception
62 {
63 protected:
64  std::string message;
65 public:
67  Exception() = default;
69  explicit Exception(const std::string& message);
71  virtual ~Exception() noexcept = default;
72 
74  const char* what() const noexcept;
75 };
76 
78 typedef uint64_t LineNo;
79 
81 typedef size_t ColNo;
82 
85 {
86 public:
88  ParseException() = default;
90  explicit ParseException(const std::string& message);
92  ParseException(LineNo lineNo, const std::string& message);
94  ParseException(LineNo lineNo, ColNo charNo, const std::string& message);
96  virtual ~ParseException() noexcept = default;
97 };
98 
100 typedef uint32_t Flags;
101 
102 enum: Flags {
103  FLAGS_ALL = 0xffffffffU
104 };
105 
106 enum: Flags {
110  DYNLIB_MODE1_MASK = 7,
112 };
113 
116 {
117 private:
118  void* handle;
119  static std::mutex mutex;
120 public:
121  DynLibrary();
126  DynLibrary(const char* filename, Flags flags = 0);
127  ~DynLibrary();
128 
133  void load(const char* filename, Flags flags = 0);
135  void unload();
136 
138  void* getSymbol(const char* symbolName);
139 
141  operator bool() const
142  { return handle!=nullptr; }
143 
145  bool operator!() const
146  { return handle==nullptr; }
147 
148  // return true if loaded
149  bool isLoaded() const
150  { return handle!=nullptr; }
151 };
152 
153 /* parse utilities */
154 
156 inline bool isSpace(unsigned char c);
157 
158 inline bool isSpace(unsigned char c)
159 { return (c == 32 || (c < 32 && (0x3e00U & (1U<<c)))); }
160 
162 inline bool isODigit(unsigned char c);
163 
164 inline bool isODigit(unsigned char c)
165 { return c>='0' && c<= '7'; }
166 
168 inline bool isDigit(unsigned char c);
169 
170 inline bool isDigit(unsigned char c)
171 { return c>='0' && c<= '9'; }
172 
174 inline bool isXDigit(unsigned char c);
175 
176 inline bool isXDigit(unsigned char c)
177 { return (c>='0' && c<= '9') || (c>='a' && c<='f') || (c>='A' && c<='F'); }
178 
180 inline bool isAlpha(unsigned char c);
181 
182 inline bool isAlpha(unsigned char c)
183 { return (c>='a' && c<='z') || (c>='A' && c<='Z'); }
184 
186 inline bool isAlnum(unsigned char c);
187 
188 inline bool isAlnum(unsigned char c)
189 { return (c>='0' && c<= '9') || (c>='a' && c<='z') || (c>='A' && c<='Z'); }
190 
192 inline const char* skipSpaces(const char* s);
193 
194 inline const char* skipSpaces(const char* s)
195 {
196  while (isSpace(*s)) s++;
197  return s;
198 }
199 
201 inline const char* skipSpacesAtEnd(const char* s, size_t length);
202 
203 inline const char* skipSpacesAtEnd(const char* s, size_t length)
204 {
205  const char* t = s+length;
206  if (t == s) return s;
207  for (t--; t != s-1 && isSpace(*t); t--);
208  return t+1;
209 }
210 
212 
226 template<typename T>
227 extern T cstrtovCStyle(const char* str, const char* inend, const char*& outend);
228 
230 
235 template<typename T>
236 T parseEnvVariable(const char* envVar, const T& defaultValue = T())
237 {
238  const char* var = getenv(envVar);
239  if (var == nullptr)
240  return defaultValue;
241  var = skipSpaces(var);
242  if (*var == 0)
243  return defaultValue;
244  const char* outend;
245  try
246  { return cstrtovCStyle<T>(var, nullptr, outend); }
247  catch(const ParseException& ex)
248  { return defaultValue; }
249 }
250 
252 extern template
253 cxuchar parseEnvVariable<cxuchar>(const char* envVar, const cxuchar& defaultValue);
254 
256 extern template
257 cxchar parseEnvVariable<cxchar>(const char* envVar, const cxchar& defaultValue);
258 
260 extern template
261 cxuint parseEnvVariable<cxuint>(const char* envVar, const cxuint& defaultValue);
262 
264 extern template
265 cxint parseEnvVariable<cxint>(const char* envVar, const cxint& defaultValue);
266 
268 extern template
269 cxushort parseEnvVariable<cxushort>(const char* envVar, const cxushort& defaultValue);
270 
272 extern template
273 cxshort parseEnvVariable<cxshort>(const char* envVar, const cxshort& defaultValue);
274 
276 extern template
277 cxulong parseEnvVariable<cxulong>(const char* envVar, const cxulong& defaultValue);
278 
280 extern template
281 cxlong parseEnvVariable<cxlong>(const char* envVar, const cxlong& defaultValue);
282 
284 extern template
285 cxullong parseEnvVariable<cxullong>(const char* envVar, const cxullong& defaultValue);
286 
288 extern template
289 cxllong parseEnvVariable<cxllong>(const char* envVar, const cxllong& defaultValue);
290 
292 extern template
293 float parseEnvVariable<float>(const char* envVar, const float& defaultValue);
294 
296 extern template
297 double parseEnvVariable<double>(const char* envVar, const double& defaultValue);
298 
299 #ifndef __UTILITIES_MODULE__
300 
302 extern template
303 std::string parseEnvVariable<std::string>(const char* envVar,
304  const std::string& defaultValue);
305 
307 extern template
308 bool parseEnvVariable<bool>(const char* envVar, const bool& defaultValue);
309 
310 #endif
311 
314 {
316  inline bool operator()(const char* c1, const char* c2) const
317  { return ::strcmp(c1, c2)<0; }
318 };
319 
322 {
324  inline bool operator()(const char* c1, const char* c2) const
325  { return ::strcasecmp(c1, c2)<0; }
326 };
327 
330 {
332  inline bool operator()(const char* c1, const char* c2) const
333  { return ::strcmp(c1, c2)==0; }
334 };
335 
338 {
340  size_t operator()(const char* c) const
341  {
342  if (c == nullptr)
343  return 0;
344  size_t hash = 0;
345 
346  for (const char* p = c; *p != 0; p++)
347  hash = ((hash<<8)^(cxbyte)*p)*size_t(0xbf146a3dU);
348  return hash;
349  }
350 };
351 
353 inline cxuint CLZ32(uint32_t v);
355 inline cxuint CLZ64(uint64_t v);
357 inline cxuint CTZ32(uint32_t v);
359 inline cxuint CTZ64(uint64_t v);
360 
361 inline cxuint CLZ32(uint32_t v)
362 {
363 #ifdef __GNUC__
364  return __builtin_clz(v);
365 #else
366 # ifdef _MSC_VER
367  unsigned long index;
368  _BitScanReverse(&index, v);
369  return 31-index;
370 # else
371  cxuint count = 0;
372  for (uint32_t t = 1U<<31; t > v; t>>=1, count++);
373  return count;
374 # endif
375 #endif
376 }
377 
378 inline cxuint CLZ64(uint64_t v)
379 {
380 #ifdef __GNUC__
381  return __builtin_clzll(v);
382 #else
383 # ifdef _MSC_VER
384 # ifdef HAVE_ARCH_X86
385  unsigned long indexlo, indexhi;
386  unsigned char nzhi;
387  _BitScanReverse(&indexlo, uint32_t(v));
388  nzhi = _BitScanReverse(&indexhi, uint32_t(v>>32));
389  // final index
390  indexlo = (nzhi ? indexhi+32 : indexlo);
391  return 63-indexlo;
392 # else
393  unsigned long index;
394  _BitScanReverse64(&index, v);
395  return 63-index;
396 # endif
397 # else
398  cxuint count = 0;
399  for (uint64_t t = 1ULL<<63; t > v; t>>=1, count++);
400  return count;
401 # endif
402 #endif
403 }
404 
405 inline cxuint CTZ32(uint32_t v)
406 {
407 #ifdef __GNUC__
408  return __builtin_ctz(v);
409 #else
410 # ifdef _MSC_VER
411  unsigned long index;
412  _BitScanForward(&index, v);
413  return index;
414 # else
415  cxuint count = 0;
416  for (uint32_t t = 1U; (t & v) == 0; t<<=1, count++);
417  return count;
418 # endif
419 #endif
420 }
421 
422 inline cxuint CTZ64(uint64_t v)
423 {
424 #ifdef __GNUC__
425  return __builtin_ctzll(v);
426 #else
427 # ifdef _MSC_VER
428 # ifdef HAVE_ARCH_X86
429  unsigned long indexlo, indexhi;
430  unsigned char nzlo;
431  nzlo = _BitScanForward(&indexlo, uint32_t(v));
432  _BitScanForward(&indexhi, uint32_t(v>>32));
433  // final index
434  indexlo = (nzlo ? indexlo : indexhi+32);
435  return indexlo;
436 # else
437  unsigned long index;
438  _BitScanForward64(&index, v);
439  return index;
440 # endif
441 # else
442  cxuint count = 0;
443  for (uint64_t t = 1ULL; (t & v) == 0; t<<=1, count++);
444  return count;
445 # endif
446 #endif
447 }
448 
449 
451 template<typename T, typename T2>
452 inline bool usumGt(T a, T b, T2 c)
453 { return ((a+b)>c) || ((a+b)<a); }
454 
456 template<typename T, typename T2>
457 inline bool usumGe(T a, T b, T2 c)
458 { return ((a+b)>=c) || ((a+b)<a); }
459 
461 extern std::string escapeStringCStyle(size_t strSize, const char* str);
462 
464 inline std::string escapeStringCStyle(const std::string& str)
465 { return escapeStringCStyle(str.size(), str.c_str()); }
466 
468 inline std::string escapeStringCStyle(const CString& str)
469 { return escapeStringCStyle(str.size(), str.c_str()); }
470 
471 
473 
481 extern size_t escapeStringCStyle(size_t strSize, const char* str,
482  size_t outMaxSize, char* outStr, size_t& outSize);
483 
485 
493 extern cxuint cstrtoui(const char* str, const char* inend, const char*& outend);
494 
496 extern int64_t cstrtoiXCStyle(const char* str, const char* inend,
497  const char*& outend, cxuint bits);
498 
500 extern uint64_t cstrtouXCStyle(const char* str, const char* inend,
501  const char*& outend, cxuint bits);
502 
504 struct UInt128
505 {
506  uint64_t lo;
507  uint64_t hi;
508 };
509 
511 extern uint64_t cstrtofXCStyle(const char* str, const char* inend,
512  const char*& outend, cxuint expBits, cxuint mantisaBits);
513 
515 extern UInt128 cstrtou128CStyle(const char* str, const char* inend, const char*& outend);
516 
517 /* cstrtovcstyle impls */
518 
520 template<> inline
521 cxuchar cstrtovCStyle<cxuchar>(const char* str, const char* inend, const char*& outend)
522 { return cstrtouXCStyle(str, inend, outend, sizeof(cxuchar)<<3); }
523 
525 template<> inline
526 cxchar cstrtovCStyle<cxchar>(const char* str, const char* inend, const char*& outend)
527 { return cstrtoiXCStyle(str, inend, outend, sizeof(cxchar)<<3); }
528 
530 template<> inline
531 cxuint cstrtovCStyle<cxuint>(const char* str, const char* inend, const char*& outend)
532 { return cstrtouXCStyle(str, inend, outend, sizeof(cxuint)<<3); }
533 
535 template<> inline
536 cxint cstrtovCStyle<cxint>(const char* str, const char* inend, const char*& outend)
537 { return cstrtoiXCStyle(str, inend, outend, sizeof(cxint)<<3); }
538 
540 template<> inline
541 cxushort cstrtovCStyle<cxushort>(const char* str, const char* inend, const char*& outend)
542 { return cstrtouXCStyle(str, inend, outend, sizeof(cxushort)<<3); }
543 
545 template<> inline
546 cxshort cstrtovCStyle<cxshort>(const char* str, const char* inend, const char*& outend)
547 { return cstrtoiXCStyle(str, inend, outend, sizeof(cxshort)<<3); }
548 
550 template<> inline
551 cxulong cstrtovCStyle<cxulong>(const char* str, const char* inend, const char*& outend)
552 { return cstrtouXCStyle(str, inend, outend, sizeof(cxulong)<<3); }
553 
555 template<> inline
556 cxlong cstrtovCStyle<cxlong>(const char* str, const char* inend, const char*& outend)
557 { return cstrtoiXCStyle(str, inend, outend, sizeof(cxlong)<<3); }
558 
560 template<> inline
561 cxullong cstrtovCStyle<cxullong>(const char* str, const char* inend, const char*& outend)
562 { return cstrtouXCStyle(str, inend, outend, sizeof(cxullong)<<3); }
563 
565 template<> inline
566 cxllong cstrtovCStyle<cxllong>(const char* str, const char* inend, const char*& outend)
567 { return cstrtoiXCStyle(str, inend, outend, sizeof(cxllong)<<3); }
568 
570 template<> inline
571 UInt128 cstrtovCStyle<UInt128>(const char* str, const char* inend, const char*& outend)
572 { return cstrtou128CStyle(str, inend, outend); }
573 
575 template<> inline
576 float cstrtovCStyle<float>(const char* str, const char* inend, const char*& outend)
577 {
578  union {
579  float f;
580  uint32_t u;
581  } v;
582  v.u = cstrtofXCStyle(str, inend, outend, 8, 23);
583  return v.f;
584 }
585 
587 template<> inline
588 double cstrtovCStyle<double>(const char* str, const char* inend, const char*& outend)
589 {
590  union {
591  double d;
592  uint64_t u;
593  } v;
594  v.u = cstrtofXCStyle(str, inend, outend, 11, 52);
595  return v.d;
596 }
597 
599 
610 cxushort cstrtohCStyle(const char* str, const char* inend, const char*& outend);
611 
613 inline cxushort cstrtohCStyle(const char* str, const char* inend, const char*& outend)
614 { return cstrtofXCStyle(str, inend, outend, 5, 10); }
615 
617 extern size_t uXtocstrCStyle(uint64_t value, char* str, size_t maxSize, cxuint radix,
618  cxuint width, bool prefix);
619 
621 extern size_t iXtocstrCStyle(int64_t value, char* str, size_t maxSize, cxuint radix,
622  cxuint width, bool prefix);
623 
625 
634 template<typename T>
635 extern size_t itocstrCStyle(T value, char* str, size_t maxSize, cxuint radix = 10,
636  cxuint width = 0, bool prefix = true);
637 
639 template<> inline
640 size_t itocstrCStyle<cxuchar>(cxuchar value, char* str, size_t maxSize, cxuint radix,
641  cxuint width, bool prefix)
642 { return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
643 
645 template<> inline
646 size_t itocstrCStyle<cxchar>(cxchar value, char* str, size_t maxSize, cxuint radix,
647  cxuint width, bool prefix)
648 { return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
649 
651 template<> inline
652 size_t itocstrCStyle<cxushort>(cxushort value, char* str, size_t maxSize, cxuint radix,
653  cxuint width, bool prefix)
654 { return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
655 
657 template<> inline
658 size_t itocstrCStyle<cxshort>(cxshort value, char* str, size_t maxSize, cxuint radix,
659  cxuint width, bool prefix)
660 { return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
661 
663 template<> inline
664 size_t itocstrCStyle<cxuint>(cxuint value, char* str, size_t maxSize, cxuint radix,
665  cxuint width, bool prefix)
666 { return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
667 
669 template<> inline
670 size_t itocstrCStyle<cxint>(cxint value, char* str, size_t maxSize, cxuint radix,
671  cxuint width, bool prefix)
672 { return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
673 
675 template<> inline
676 size_t itocstrCStyle<cxulong>(cxulong value, char* str, size_t maxSize, cxuint radix,
677  cxuint width, bool prefix)
678 { return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
679 
681 template<> inline
682 size_t itocstrCStyle<cxlong>(cxlong value, char* str, size_t maxSize, cxuint radix,
683  cxuint width, bool prefix)
684 { return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
685 
687 template<> inline
688 size_t itocstrCStyle<cxullong>(cxullong value, char* str, size_t maxSize, cxuint radix,
689  cxuint width , bool prefix)
690 { return uXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
691 
693 template<> inline
694 size_t itocstrCStyle<cxllong>(cxllong value, char* str, size_t maxSize, cxuint radix,
695  cxuint width, bool prefix)
696 { return iXtocstrCStyle(value, str, maxSize, radix, width, prefix); }
697 
699 extern size_t fXtocstrCStyle(uint64_t value, char* str, size_t maxSize,
700  bool scientific, cxuint expBits, cxuint mantisaBits);
701 
703 
713 size_t htocstrCStyle(cxushort value, char* str, size_t maxSize,
714  bool scientific = false);
715 
716 inline size_t htocstrCStyle(cxushort value, char* str, size_t maxSize, bool scientific)
717 { return fXtocstrCStyle(value, str, maxSize, scientific, 5, 10); }
718 
720 
730 size_t ftocstrCStyle(float value, char* str, size_t maxSize,
731  bool scientific = false);
732 
733 inline size_t ftocstrCStyle(float value, char* str, size_t maxSize, bool scientific)
734 {
735  union {
736  float f;
737  uint32_t u;
738  } v;
739  v.f = value;
740  return fXtocstrCStyle(v.u, str, maxSize, scientific, 8, 23);
741 }
742 
744 
754 size_t dtocstrCStyle(double value, char* str, size_t maxSize,
755  bool scientific = false);
756 
757 inline size_t dtocstrCStyle(double value, char* str, size_t maxSize, bool scientific)
758 {
759  union {
760  double d;
761  uint64_t u;
762  } v;
763  v.d = value;
764  return fXtocstrCStyle(v.u, str, maxSize, scientific, 11, 52);
765 }
766 
767 /* file system utilities */
768 
770 extern bool isDirectory(const char* path);
772 extern bool isFileExists(const char* path);
773 
775 
779 extern Array<cxbyte> loadDataFromFile(const char* filename);
780 
782 extern void filesystemPath(char* path);
784 extern void filesystemPath(std::string& path);
785 
787 extern std::string joinPaths(const std::string& path1, const std::string& path2);
788 
790 extern uint64_t getFileTimestamp(const char* filename);
791 
793 extern std::string getHomeDir();
795 extern void makeDir(const char* dirname);
797 extern Array<cxbyte> runExecWithOutput(const char* program, const char** argv);
798 
800 extern std::string findAmdOCL();
801 
803 extern std::string findMesaOCL();
804 
806 extern std::string findLLVMConfig();
807 
808 /*
809  * Reference support
810  */
811 
814 {
815 private:
816  mutable std::atomic<size_t> refCount;
817 public:
819  RefCountable() : refCount(1)
820  { }
821 
823  void reference() const
824  {
825  refCount.fetch_add(1);
826  }
827 
829  bool unreference() const
830  {
831  return (refCount.fetch_sub(1) == 1);
832  }
833 };
834 
837 {
838 private:
839  mutable size_t refCount;
840 public:
842  FastRefCountable() : refCount(1)
843  { }
844 
846  void reference() const
847  {
848  refCount++;
849  }
850 
852  bool unreference() const
853  {
854  return (--refCount == 0);
855  }
856 };
857 
859 template<typename T>
860 class RefPtr
861 {
862 private:
863  T* ptr;
864 public:
866  RefPtr(): ptr(nullptr)
867  { }
868 
870  explicit RefPtr(T* inputPtr) : ptr(inputPtr)
871  { }
872 
874  RefPtr(const RefPtr<T>& refPtr) : ptr(refPtr.ptr)
875  {
876  if (ptr != nullptr)
877  ptr->reference();
878  }
879 
881  RefPtr(RefPtr<T>&& refPtr) : ptr(refPtr.ptr)
882  { refPtr.ptr = nullptr; }
883 
886  {
887  if (ptr != nullptr)
888  if (ptr->unreference())
889  delete ptr;
890  }
891 
893  RefPtr<T>& operator=(const RefPtr<T>& refPtr)
894  {
895  if (ptr != nullptr)
896  if (ptr->unreference())
897  delete ptr;
898  if (refPtr.ptr != nullptr)
899  refPtr.ptr->reference();
900  ptr = refPtr.ptr;
901  return *this;
902  }
905  {
906  if (ptr != nullptr)
907  if (ptr->unreference())
908  delete ptr;
909  ptr = refPtr.ptr;
910  refPtr.ptr = nullptr;
911  return *this;
912  }
913 
915  bool operator==(const RefPtr<T>& refPtr) const
916  { return ptr == refPtr.ptr; }
918  bool operator!=(const RefPtr<T>& refPtr) const
919  { return ptr != refPtr.ptr; }
920 
922  operator bool() const
923  { return ptr!=nullptr; }
924 
926  bool operator!() const
927  { return ptr==nullptr; }
928 
930  T* operator->() const
931  { return ptr; }
932 
934  T* get() const
935  { return ptr; }
936 
938  void reset()
939  {
940  if (ptr != nullptr)
941  if (ptr->unreference())
942  delete ptr;
943  ptr = nullptr;
944  }
945 
947  void swap(RefPtr<T>& refPtr)
948  {
949  T* tmp = ptr;
950  ptr = refPtr.ptr;
951  refPtr.ptr = tmp;
952  }
953 
955  template<typename DestType>
957  {
958  DestType* p = const_cast<DestType*>(ptr);
959  if (p != nullptr)
960  p->reference();
961  return RefPtr<DestType>(p);
962  }
964  template<typename DestType>
966  {
967  DestType* p = static_cast<DestType*>(ptr);
968  if (p != nullptr)
969  p->reference();
970  return RefPtr<DestType>(p);
971  }
973  template<typename DestType>
975  {
976  DestType* p = dynamic_cast<DestType*>(ptr);
977  if (p != nullptr)
978  p->reference();
979  return RefPtr<DestType>(p);
980  }
981 };
982 
984 inline char toLower(char c);
985 
986 inline char toLower(char c)
987 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
988 
990 inline void toLowerString(std::string& string);
991 
992 inline void toLowerString(std::string& string)
993 { std::transform(string.begin(), string.end(), string.begin(), toLower); }
994 
996 inline void toLowerString(char* cstr);
997 
998 inline void toLowerString(char* cstr)
999 {
1000  for (; *cstr!=0; cstr++)
1001  *cstr = toLower(*cstr);
1002 }
1003 
1005 inline void toLowerString(CString& string);
1006 
1007 inline void toLowerString(CString& string)
1008 { if (!string.empty())
1009  toLowerString(string.begin()); }
1010 
1012 inline char toUpper(char c);
1013 
1014 inline char toUpper(char c)
1015 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
1016 
1018 inline void toUpperString(std::string& string);
1019 
1020 inline void toUpperString(std::string& string)
1021 { std::transform(string.begin(), string.end(), string.begin(), toUpper); }
1022 
1024 inline void toUpperString(char* cstr);
1025 
1026 inline void toUpperString(char* cstr)
1027 {
1028  for (; *cstr!=0; cstr++)
1029  *cstr = toUpper(*cstr);
1030 }
1031 
1033 inline void toUpperString(CString& string);
1034 
1035 inline void toUpperString(CString& string)
1036 { if (!string.empty())
1037  toUpperString(string.begin()); }
1038 
1039 /* CALL once */
1040 
1041 #ifdef HAVE_CALL_ONCE
1042 typedef std::once_flag OnceFlag;
1044 
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...); }
1049 #else
1050 struct OnceFlag: std::atomic<int>
1051 {
1052  // force zero initialization
1053  OnceFlag(): std::atomic<int>(0)
1054  { }
1055 };
1056 
1057 template<class Callable, class... Args>
1058 inline void callOnce(OnceFlag& flag, Callable&& f, Args&&... args)
1059 {
1060  if (flag.exchange(1) == 0)
1061  f(args...);
1062 }
1063 #endif
1064 
1065 };
1066 
1067 #endif
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
resolve symbols now
Definition: Utilities.h:109
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
treat symbols locally
Definition: Utilities.h:107
Configuration header.
bool operator==(const RefPtr< T > &refPtr) const
equality operator
Definition: Utilities.h:915
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
treats symbols globally
Definition: Utilities.h:111
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&#39;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
resolve symbols when is needed
Definition: Utilities.h:108
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
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
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
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
C-style string class.
void reference() const
reference object
Definition: Utilities.h:823
T * operator->() const
get elem from pointer
Definition: Utilities.h:930