compass.hpp 22.4 KB
Newer Older
steinbac's avatar
steinbac committed
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#ifndef _COMPASS_HPP_
#define _COMPASS_HPP_
#ifndef COMPASS_API_H
#define COMPASS_API_H
#ifndef COMPASS_DISPATCH_H_
#define COMPASS_DISPATCH_H_
#ifndef DETAIL_DETECT_OS_H
#define DETAIL_DETECT_OS_H




#if (defined(__linux) || defined(__linux__) || defined(__gnu_linux__))
#define COMPASS_CT_OS_LINUX
#endif

#if (defined(__APPLE__) || defined(__MACH__))
#define COMPASS_CT_OS_MACOS
#endif

#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64) || defined(__WIN32__))
#define COMPASS_CT_OS_WINDOWS
#endif
#ifndef COMPASS_DEFINITIONS_H
#define COMPASS_DEFINITIONS_H

namespace compass {

    namespace compiletime {


        static const int eax = 0;
        static const int ebx = 1;
        static const int ecx = 2;
        static const int edx = 3;

        struct unsupported_tag { static int dummy; };
        struct through_os_tag {};
    };

    namespace runtime {

    };

    namespace rt = runtime;
    namespace ct = compiletime;

};

#endif
namespace compass {

    namespace compiletime {

        struct linux_tag {};
        struct macos_tag {};
        struct windows_tag {};

        struct platform {

#ifdef COMPASS_CT_OS_LINUX
            typedef linux_tag type;
#endif

#ifdef COMPASS_CT_OS_MACOS
            typedef macos_tag type;
#endif

#ifdef COMPASS_CT_OS_WINDOWS
            typedef windows_tag type;
#endif

#if (!(defined(COMPASS_CT_OS_LINUX) || defined(COMPASS_CT_OS_MACOS) || defined(COMPASS_CT_OS_WINDOWS)))
            typedef unsupported_tag type;
            static_assert(true,
              "\ncompass is not aware of this operating system \nPlease create an issue under https://github.com/psteinb/compass\n\n");
#endif



        };

    }

}
#endif
#ifndef DETAIL_DETECT_COMPILER_H
#define DETAIL_DETECT_COMPILER_H



#ifdef __clang__
    #define COMPASS_CT_COMP_CLANG
#else

    #ifdef __INTEL_COMPILER
        #define COMPASS_CT_COMP_INTEL
    #else

      #ifdef __GNUC__
      #define COMPASS_CT_COMP_GCC
      #endif

   #endif

#endif

#if (defined(_MSC_VER) || defined(_MSC_FULL_VER) || defined(_MSC_BUILD))
#define COMPASS_CT_COMP_MSVC
#endif




namespace compass {

    namespace compiletime {

        struct gnu_tag {};
        struct llvm_tag {};
        struct msvc_tag {};
        struct intel_tag {};

        struct toolchain {

#ifdef COMPASS_CT_COMP_CLANG
            typedef llvm_tag type;
#endif

#ifdef COMPASS_CT_COMP_GCC
            typedef gnu_tag type;
#endif

#ifdef COMPASS_CT_COMP_MSVC
            typedef msvc_tag type;
#endif

#ifdef COMPASS_CT_COMP_INTEL
            typedef intel_tag type;
#endif

        };

        static const bool is_gnu(){

            using current_toolchain_t = toolchain::type;

            bool value = std::is_same<current_toolchain_t,gnu_tag>::value;
            return value;

        }

        static const bool is_llvm(){

            using current_toolchain_t = toolchain::type;
            bool value = std::is_same<current_toolchain_t,llvm_tag>::value;
            return value;

        }

        static const bool is_msvc(){

            using current_toolchain_t = toolchain::type;
            bool value = std::is_same<current_toolchain_t,msvc_tag>::value;
            return value;

        }

        static const bool is_intel(){

            using current_toolchain_t = toolchain::type;
            bool value = std::is_same<current_toolchain_t,intel_tag>::value;
            return value;

        }

    }

}

#endif
#ifndef DETAIL_DETECT_ARCHITECTURE_H
#define DETAIL_DETECT_ARCHITECTURE_H







#if (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64))
#define COMPASS_CT_ARCH_X86
#define COMPASS_CT_ARCH_64BITS

#undef COMPASS_CT_ARCH_UNSUPPORTED
#endif

#if (defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__M_I86) || defined(_M_IX86))
#define COMPASS_CT_ARCH_X86
#undef COMPASS_CT_ARCH_UNSUPPORTED
#endif

#if (defined(__powerpc64__) || defined(_ARCH_PPC64) || defined(__ppc64__))
#define COMPASS_CT_ARCH_POWER
#define COMPASS_CT_ARCH_64BITS
#define COMPASS_CT_ARCH_UNSUPPORTED
#endif

#if (defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) || defined(__PPC__) || defined(__ppc__))
#define COMPASS_CT_ARCH_POWER
#define COMPASS_CT_ARCH_UNSUPPORTED
#endif




#include <climits>

namespace compass {

    namespace compiletime {



        struct x86_tag {};
        struct arm_tag {};
        struct power_tag {};

        struct arch {

#ifdef COMPASS_CT_ARCH_X86
            typedef x86_tag type;
#endif

#ifdef COMPASS_CT_ARCH_UNSUPPORTED
            typedef unsupported_tag type;
            static_assert(std::is_same<compass::ct::arch::type,compass::ct::unsupported_tag>::value,
                          "\n\ncompass is not aware of this architecture \nPlease create an issue under https://github.com/psteinb/compass\n\n");
#endif

            const static int bitness = sizeof(void*)*CHAR_BIT;



        };

    }

}

#endif
#ifdef COMPASS_CT_ARCH_X86
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#ifndef COMPASS_CT_PREPROCESSOR_IMPL_H_
#define COMPASS_CT_PREPROCESSOR_IMPL_H_



#ifndef WIN32

#if defined(__SSE2__) || defined(__SSE2_MATH__)
    #define COMPASS_CT_HAS_SSE2 1
   #endif

#if defined(__SSE3__) && defined(__SSSE3__)
    #define COMPASS_CT_HAS_SSE3 1
   #endif

#if defined(__SSE4_2__) && defined(__SSE4_1__)
    #define COMPASS_CT_HAS_SSE4 1
   #endif

#else

Peter Steinbach's avatar
Peter Steinbach committed
274 275 276 277 278 279 280 281 282 283 284 285
    #ifdef _M_IX86_FP
		#if _M_IX86_FP >= 2
			#define COMPASS_CT_HAS_SSE2 1
			#define COMPASS_CT_HAS_SSE3 1
			#define COMPASS_CT_HAS_SSE4 1
		#endif
	#else
		#if defined(__AVX__) || defined(__AVX2__)
			#define COMPASS_CT_HAS_SSE2 1
			#define COMPASS_CT_HAS_SSE3 1
			#define COMPASS_CT_HAS_SSE4 1
		#endif
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    #endif
#endif
#ifndef COMPASS_TAGS_H_
#define COMPASS_TAGS_H_


namespace compass {


    namespace feature {




        struct sse {};
        struct sse2 {};
        struct sse3 {};
        struct sse4 {};


        struct avx {};
        struct avx2 {};


    };


};
#endif
namespace compass {

  namespace compiletime {

    template<typename feature_t>
    struct has{
      static const bool enabled = false;
    };

    template<>
    struct has<feature::sse2>{
      static const bool enabled=
#ifdef COMPASS_CT_HAS_SSE2
     true;
#else
     false;
#endif

    };

    template<>
    struct has<feature::sse3>{
      static const bool enabled=
#ifdef COMPASS_CT_HAS_SSE3
     true;
#else
     false;
#endif

    };

    template<>
    struct has<feature::sse4>{
      static const bool enabled=
#ifdef COMPASS_CT_HAS_SSE4
     true;
#else
     false;
#endif

    };

  };
};

#endif
steinbac's avatar
steinbac committed
361 362 363 364 365 366 367 368 369 370 371
#ifndef COMPASS_RT_X86_IMPL_H_
#define COMPASS_RT_X86_IMPL_H_
#ifndef COMPASS_RT_X86_CPUID_H



#ifdef COMPASS_CT_ARCH_X86
#define COMPASS_RT_X86_CPUID_H
#ifndef COMPASS_X86_CPUID_COMMON_H
#define COMPASS_X86_CPUID_COMMON_H

372
#ifndef COMPASS_CT_COMP_MSVC
steinbac's avatar
steinbac committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
#include "cpuid.h"

namespace compass {

  namespace runtime {

    static int extended_get_cpuid(std::uint32_t* regs,
                                  std::uint32_t in_eax = 0,
                                  std::uint32_t in_ecx = 0){

#ifdef __get_cpuid_count
      return __get_cpuid_count(in_eax,
                               in_ecx,
                               &regs[ct::eax],
                               &regs[ct::ebx],
                               &regs[ct::ecx],
                               &regs[ct::edx]);
#endif


      unsigned int clean_level = in_eax & 0x80000000;

      if (__get_cpuid_max (clean_level, 0) < in_eax)
        return 0;

      __cpuid_count(in_eax, in_ecx,
                    regs[ct::eax],
                    regs[ct::ebx],
                    regs[ct::ecx],
                    regs[ct::edx]
        );

      return 1;
    }

  };

};
411
#endif
steinbac's avatar
steinbac committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
#endif
#ifndef COMPASS_RT_X86_LLVM_CPUID_H_
#define COMPASS_RT_X86_LLVM_CPUID_H_



#ifdef COMPASS_CT_COMP_CLANG



#include <array>
#include <cstdint>

namespace compass {


  namespace runtime {

    static std::array<std::uint32_t,4> cpuid(std::uint32_t in_eax = 0,
                                             std::uint32_t in_ebx = 0,
                                             std::uint32_t in_ecx = 0,
                                             std::uint32_t in_edx = 0){

      std::array<std::uint32_t,4> regs = {in_eax,0,in_ecx,0};

      __cpuid_count(in_eax,
                    in_ecx,
                    regs[ct::eax],
                    regs[ct::ebx],
                    regs[ct::ecx],
                    regs[ct::edx]
        );

      if (in_eax >= 13)
        __cpuid_count (in_eax, 1, regs[ct::eax], regs[ct::ebx], regs[ct::ecx], regs[ct::edx]);
      else if (in_eax >= 7)
        __cpuid_count (in_eax, 0, regs[ct::eax], regs[ct::ebx], regs[ct::ecx], regs[ct::edx]);

      return regs;

    }



  };

};
#endif
#endif
#ifndef COMPASS_RT_X86_GNU_CPUID_H_



#ifdef COMPASS_CT_COMP_GCC

#define COMPASS_RT_X86_GNU_CPUID_H_




#include <array>
#include <cstdint>


namespace compass {

  namespace runtime {

    static std::array<std::uint32_t,4> cpuid(std::uint32_t in_eax = 0,
                                             std::uint32_t in_ebx = 0,
                                             std::uint32_t in_ecx = 0,
                                             std::uint32_t in_edx = 0){

      std::array<std::uint32_t,4> regs = {in_eax,0,in_ecx,0};







      int gnu_cpuid_rvalue = compass::runtime::extended_get_cpuid(regs.data(),
                                                              in_eax,
                                                              in_ecx);
      if(gnu_cpuid_rvalue < 1){
        return {0,0,0,0};
      }

      return regs;

    }

  };

};
#endif
#endif
#ifndef COMPASS_CT_MSVC_IMPL_H_



#ifdef COMPASS_CT_COMP_MSVC

#define COMPASS_CT_MSVC_IMPL_H_

#include "intrin.h"

#include <array>
#include <bitset>
#include <cstdint>



namespace compass {

  namespace runtime {



    static std::array<std::uint32_t, 4> cpuid(std::uint32_t in_eax = 0,
                                              std::uint32_t in_ebx = 0,
                                              std::uint32_t in_ecx = 0,
                                              std::uint32_t in_edx = 0) {

      std::array<std::uint32_t, 4> regs = { in_eax,in_ebx,in_ecx,in_edx };

      std::int32_t* regs_ptr = reinterpret_cast<std::int32_t*>(regs.data());
      __cpuidex(regs_ptr,
                (std::int32_t)in_eax,
          (std::int32_t)in_ecx
          );



      return regs;

    }


  };



};

#endif
#endif
#ifndef COMPASS_RT_X86_INTEL_CPUID_H_



#ifdef COMPASS_CT_COMP_INTEL

#define COMPASS_RT_X86_INTEL_CPUID_H_


#include <array>
#include <bitset>
#include <cstdint>



namespace compass {


  namespace runtime {




    static std::array<std::uint32_t,4> cpuid_to_int(std::uint32_t in_eax = 0,
                                                    std::uint32_t in_ebx = 0,
                                                    std::uint32_t in_ecx = 0,
                                                    std::uint32_t in_edx = 0){

      static std::array<std::uint32_t,4> value = {~0};

      int cpuid_rvalue = compass::runtime::extended_get_cpuid(&value[0],in_eax,in_ecx);

      if(cpuid_rvalue < 1){
        value = {0,0,0,0};
        return value;
      } else {
        return value;
      }

    }

    static std::array<std::bitset<32>, 4> cpuid(std::uint32_t in_eax = 0,
													std::uint32_t in_ebx = 0,
													std::uint32_t in_ecx = 0,
													std::uint32_t in_edx = 0) {

			auto regs = cpuid_to_int(in_eax, in_ebx, in_ecx, in_edx);

			static std::array<std::bitset<32>, 4> value;

			value[ct::eax] = regs[ct::eax];
			value[ct::ebx] = regs[ct::ebx];
			value[ct::ecx] = regs[ct::ecx];
			value[ct::edx] = regs[ct::edx];

			return value;

    }
  };

};
#endif
#endif
#endif

#endif
#ifndef COMPASS_BIT_VIEW_H
#define COMPASS_BIT_VIEW_H

#include <climits>
#include <type_traits>

namespace compass {

    namespace utility
    {

        template <typename T>
        struct bit_view{

            T& value_;

            static const int width = sizeof(T)*CHAR_BIT;
            static_assert(std::is_integral<T>::value, "compass bitview used with non-integral type (not supported)");

            bit_view(T& _val):
                value_(_val){}


            bool test(int offset) const {

                bool value = false;
                if(offset>(width-1))
                    return value;

                const int mask = 1 << offset;
                value = value_ & mask;

                return value;
            }

            void set(int offset) {

                if(offset>(width-1))
                    return ;

                const int mask = 1 << offset;
                value_ = value_ | mask;

                return;
            }

            T range(std::uint32_t _begin, std::uint32_t _end) const {

                T value = 0;
                if(_begin >= width || _end <= _begin)
                    return value;

                const T mask = ~(~0 << (_end - _begin));
                value = (value_ >> _begin) & mask;
                return value;


            }


        };

    }


}

#endif
#ifndef COMPASS_RT_X86_SIZES_H_
#define COMPASS_RT_X86_SIZES_H_










#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

namespace compass {

  namespace runtime {

    namespace detail {

      using bitview = compass::utility::bit_view<std::uint32_t>;

      namespace size{


        class cacheline
        {

          std::vector<std::uint32_t> ebx_data_;

          cacheline():
            ebx_data_()
            {

              ebx_data_.reserve(3);
              std::uint32_t maxlevel = 8;
              std::uint32_t eax = 0;

              for(std::uint32_t l = 0;l<maxlevel;++l)
              {
                auto regs = cpuid(0x04,0,l);

                eax = regs[ct::eax];
                auto bv = bitview(eax);

                if(!bv.test(1))
                  continue;

                auto truelevel = bv.range(5,8);
                if(truelevel != l)
                  continue;

                ebx_data_.push_back(regs[ct::ebx]);
              }

            }

        public:

            static const cacheline& get(){
              static cacheline instance;
              return instance;
            }



          static std::uint32_t level(int _lvl, ct::x86_tag){

            auto reg = cacheline::get().ebx_data_.at(_lvl-1);

            std::uint32_t value = bitview(reg).range(0,11);

            return value + 1;
          }
        };

        class cache
        {

          std::vector<std::uint32_t> ebx_data_;
          std::vector<std::uint32_t> ecx_data_;

          cache():
            ebx_data_(),
            ecx_data_()
            {

              ebx_data_.reserve(3);
              ecx_data_.reserve(3);

              std::uint32_t maxlevel = 8;
              std::uint32_t eax = 0;

              for(std::uint32_t l = 0;l<maxlevel;++l)
              {
                auto regs = cpuid(0x04,0,l);

                eax = regs[ct::eax];
                auto bv = bitview(eax);

                if(!bv.test(1))
                  continue;

                auto truelevel = bv.range(5,8);
                if(truelevel != l)
                  continue;

                ebx_data_.push_back(regs[ct::ebx]);
                ecx_data_.push_back(regs[ct::ecx]);

              }

            }


        public:

          static const cache& get(){
            static cache instance;
            return instance;
          }

          static std::uint32_t level(int _lvl, ct::x86_tag){

            if(_lvl <= 0){
              std::cerr << "compass::size::cache requested invalid cache level (received: "<<
                _lvl << ", found on this host: [1," << cache::get().ebx_data_.size() + 1 << "]\n";
              return 0;
            }

            std::uint32_t index = _lvl - 1;

            if(!(index < cache::get().ebx_data_.size())){
              std::cerr << "compass::size::cache requested invalid cache level (received: "<<
                _lvl << ", found on this host: [1," << cache::get().ebx_data_.size() + 1 << "]\n";
              return 0;
            }


            std::uint32_t ebx = cache::get().ebx_data_[index];
            const bitview bv_ebx = bitview(ebx);
            const std::uint32_t ecx = cache::get().ecx_data_[index];

            std::uint32_t ways = 1 + bv_ebx.range(22,31);
            std::uint32_t partitions = 1 + bv_ebx.range(12,21);
            std::uint32_t line_size = 1 + bv_ebx.range(0,11);
            std::uint32_t sets = 1 + ecx;

            std::uint32_t value = ways*partitions*line_size*sets;

            return value;
          }
        };



      };
    };

  };

};
#endif
#include <iostream>
#include <string>
#include <algorithm>
#include <thread>

namespace compass {

  namespace runtime {

    namespace detail {

      using bitview = compass::utility::bit_view<std::uint32_t>;

      static bool works(ct::x86_tag) {

        auto regs = rt::cpuid(0);

        if(regs.size())
          return true;
        else
          return false;

      }


      static std::string vendor(ct::x86_tag) {


        std::array<std::uint32_t,4> regs = rt::cpuid(0);

        std::string vendor_name = "";

        if(!regs.empty()){
          vendor_name.resize(3*4);

          std::copy(reinterpret_cast<char*>(&regs[ct::ebx]),reinterpret_cast<char*>(&regs[ct::ebx])+4,
                    vendor_name.begin());
          std::copy(reinterpret_cast<char*>(&regs[ct::edx]),reinterpret_cast<char*>(&regs[ct::edx])+4,
                    vendor_name.begin()+4);
          std::copy(reinterpret_cast<char*>(&regs[ct::ecx]),reinterpret_cast<char*>(&regs[ct::ecx])+4,
                    vendor_name.begin()+8);
        }

        return vendor_name;


      }


      static std::string brand(ct::x86_tag) {

        std::string value = "";
        auto regs = rt::cpuid(0x80000000);
        if(regs[ct::eax] < 0x80000004)
          return value;

        value.resize(48);
        char* value_begin = &value[0];
        for(std::uint32_t i = 2; i<5;++i){
          auto ret = rt::cpuid(0x80000000 + i);

          for(std::uint32_t r = 0; r<4;++r){
            std::uint32_t* tgt = reinterpret_cast<std::uint32_t*>(value_begin + (i-2)*16u + r*4u);
            *tgt = ret[r];
          }

        }

        return value;

      }


      static std::string device_name(ct::x86_tag) {

        std::string brand_str = compass::runtime::detail::brand(ct::x86_tag());
        std::string vendor = compass::runtime::detail::vendor(ct::x86_tag());
        std::size_t find_pos = 0;
        if((find_pos = vendor.find("Genuine"))!=std::string::npos){
            vendor.erase(find_pos,7);
        }

        std::string value = "";

        if(brand_str.find(vendor) != std::string::npos){

          auto second_bracket_itr = brand_str.rfind(")");
          auto last_at_itr = brand_str.rfind("@");
          value = brand_str.substr(second_bracket_itr+1,last_at_itr-(second_bracket_itr+1));

          if((find_pos = value.find(" CPU "))!=std::string::npos){
            value.erase(find_pos,5);
          }

          if((find_pos = value.find(" CPU "))!=std::string::npos){
            value.erase(find_pos,5);
          }

          value.erase(std::remove_if(value.begin(), value.end(), isspace), value.end());
        }
        return value;

      }


















      static bool has(feature::sse , ct::x86_tag){

        auto regs = rt::cpuid(1);

        if(std::count(regs.cbegin(), regs.cend(),0) == regs.size()){
          std::cerr << "unsupported cpuid level detected\n";
        }

        bool value = bitview(regs[ct::edx]).test(25);
        return value;
      }

      static bool has(feature::sse2 , ct::x86_tag){

        auto regs = rt::cpuid(1);

        if(regs.empty()){
          std::cerr << "unsupported cpuid level detected\n";
        }

        bool value = bitview(regs[ct::edx]).test(26);
        return value;
      }

      static bool has(feature::sse3 , ct::x86_tag){

        auto regs = rt::cpuid(1);

        if(regs.empty()){
          std::cerr << "unsupported cpuid level detected\n";
        }

        bool value = bitview(regs[ct::ecx]).test(9) |
          bitview(regs[ct::ecx]).test(0) |
          bitview(regs[ct::ecx]).test(3);
        return value;
      }


      static bool has(feature::sse4 , ct::x86_tag){

        auto regs = rt::cpuid(1);

        if(regs.empty()){
          std::cerr << "unsupported cpuid level detected\n";
        }

        bool value = bitview(regs[ct::ecx]).test(19) |
          bitview(regs[ct::ecx]).test(20);

        return value;
      }

      static bool has(feature::avx , ct::x86_tag){

        auto regs = rt::cpuid(1);

        if(regs.empty()){
          std::cerr << "unsupported cpuid level detected\n";
        }

        bool value = bitview(regs[ct::ecx]).test(28);

        return value;
      }

      static bool has(feature::avx2 , ct::x86_tag){

        auto regs = rt::cpuid(7,0,0,0);

        bool value = bitview(regs[ct::ebx]).test(5);

        return value;
      }

    };

  };

};
#endif
#endif


#endif
#include <type_traits>
#include <cstdint>
#include <string>
#include <thread>

namespace compass {

    namespace runtime {


        static bool works() {

            using current_arch_t = ct::arch::type;
            return detail::works(current_arch_t());

        }

        static std::string vendor() {

            using current_arch_t = ct::arch::type;
            return detail::vendor(current_arch_t());

        }

        static std::string brand() {

            using current_arch_t = ct::arch::type;
            return detail::brand(current_arch_t());

        }

        static std::string device_name() {

            using current_arch_t = ct::arch::type;
            return detail::device_name(current_arch_t());

        }


        static std::uint32_t threads() {

            return std::thread::hardware_concurrency();

        }


        template <typename feature_t>
        static bool has(feature_t) {

            using current_arch_t = ct::arch::type;
            return detail::has(feature_t(),current_arch_t());

        }

        namespace size {

            struct cacheline{

                static std::uint32_t level(int _lvl){

                    using current_arch_t = ct::arch::type;
                    return compass::runtime::detail::size::cacheline::level(_lvl,current_arch_t());

                }
            };

            struct cache{

                static std::uint32_t level(int _lvl){

                    using current_arch_t = ct::arch::type;
                    return compass::runtime::detail::size::cache::level(_lvl,current_arch_t());

                }
            };


        };

    };

}

#endif
#endif