compass.hpp 26.8 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
#ifndef COMPASS_RT_X86_IMPL_H_
#define COMPASS_RT_X86_IMPL_H_
363 364 365 366 367 368 369 370 371 372 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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
#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
steinbac's avatar
steinbac committed
431 432 433 434 435 436 437 438 439
#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

440
#ifndef COMPASS_CT_COMP_MSVC
steinbac's avatar
steinbac committed
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
#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;
    }

  };

};
479
#endif
steinbac's avatar
steinbac committed
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
#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
693 694
#ifndef COMPASS_RT_X86_META_H_
#define COMPASS_RT_X86_META_H_
steinbac's avatar
steinbac committed
695

696 697 698 699 700 701 702 703 704 705






#include <iostream>
#include <string>
#include <algorithm>
#include <thread>
steinbac's avatar
steinbac committed
706 707 708

namespace compass {

709
  namespace runtime {
steinbac's avatar
steinbac committed
710

711
    namespace detail {
steinbac's avatar
steinbac committed
712

713
      static std::string vendor(ct::x86_tag) {
steinbac's avatar
steinbac committed
714 715


716
        std::array<std::uint32_t,4> regs = rt::cpuid(0);
steinbac's avatar
steinbac committed
717

718
        std::string vendor_name = "";
steinbac's avatar
steinbac committed
719

720 721
        if(!regs.empty()){
          vendor_name.resize(3*4);
steinbac's avatar
steinbac committed
722

723 724 725 726 727 728 729
          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);
        }
steinbac's avatar
steinbac committed
730

731
        return vendor_name;
steinbac's avatar
steinbac committed
732 733


734
      }
steinbac's avatar
steinbac committed
735 736


737
      static std::string brand(ct::x86_tag) {
steinbac's avatar
steinbac committed
738

739 740 741 742
        std::string value = "";
        auto regs = rt::cpuid(0x80000000);
        if(regs[ct::eax] < 0x80000004)
          return value;
steinbac's avatar
steinbac committed
743

744 745 746 747
        value.resize(48);
        char* value_begin = &value[0];
        for(std::uint32_t i = 2; i<5;++i){
          auto ret = rt::cpuid(0x80000000 + i);
steinbac's avatar
steinbac committed
748

749 750 751 752
          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];
          }
steinbac's avatar
steinbac committed
753

754 755 756 757 758
        }

        return value;

      }
steinbac's avatar
steinbac committed
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
      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;

        bool is_intel = false;
        bool is_amd = false;


        if((find_pos = vendor.find("Genuine"))!=std::string::npos){
            vendor.erase(find_pos,7);
            is_intel = true;
        }


        if((find_pos = vendor.find("Authentic"))!=std::string::npos){
            vendor.erase(find_pos,9);
            is_amd = true;
        }

        std::string value = "";

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

          if(is_intel){

            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);
steinbac's avatar
steinbac committed
794 795 796
            }


797 798 799
            if((find_pos = value.find(" CPU "))!=std::string::npos){
              value.erase(find_pos,5);
            }
steinbac's avatar
steinbac committed
800

801 802
            value.erase(std::remove_if(value.begin(), value.end(), isspace), value.end());
          }
steinbac's avatar
steinbac committed
803

804
          if(is_amd){
steinbac's avatar
steinbac committed
805

806 807
            auto end_itr = brand_str.rfind("Processor");
            value = brand_str.substr(find_pos+4,end_itr-4);
steinbac's avatar
steinbac committed
808

809 810 811 812 813 814 815 816 817 818 819
          }
        }
        return value;

      }

    };

  };

};
steinbac's avatar
steinbac committed
820 821 822 823 824 825 826 827 828 829 830 831 832
#endif
#ifndef COMPASS_RT_X86_SIZES_H_
#define COMPASS_RT_X86_SIZES_H_










833

steinbac's avatar
steinbac committed
834 835 836 837 838 839 840 841 842 843 844 845
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

namespace compass {

  namespace runtime {

    namespace detail {

      using bitview = compass::utility::bit_view<std::uint32_t>;
846
      using current_arch_t = ct::arch::type;
steinbac's avatar
steinbac committed
847 848 849 850 851 852 853

      namespace size{


        class cacheline
        {

854
          std::vector<std::uint32_t> sizes_in_bytes_;
steinbac's avatar
steinbac committed
855

856 857 858 859 860 861 862
          void on_intel(){

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

            for(std::uint32_t l = 0;l<maxlevel;++l)
steinbac's avatar
steinbac committed
863
            {
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
              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;


              std::uint32_t value = bitview(regs[ct::ebx]).range(0,11);
              sizes_in_bytes_.push_back(value+1);

            }

          }

          void on_amd(){
steinbac's avatar
steinbac committed
885

886
            sizes_in_bytes_.reserve(3);
steinbac's avatar
steinbac committed
887

888
            auto regs = cpuid(0x80000005);
steinbac's avatar
steinbac committed
889

890 891 892 893 894
            std::uint32_t ecx = regs[ct::ecx];
            auto bv = bitview(ecx);
            std::uint32_t linesize = bv.range(0,7);
            if(!linesize)
              return;
steinbac's avatar
steinbac committed
895

896
            sizes_in_bytes_.push_back(linesize);
steinbac's avatar
steinbac committed
897

898 899 900 901
            auto l23regs = cpuid(0x80000006);
            ecx = l23regs[ct::ecx];
            auto bv2 = bitview(ecx);
            linesize = bv2.range(0,7);
steinbac's avatar
steinbac committed
902

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
            sizes_in_bytes_.push_back(linesize);

            auto bv3 = bitview(l23regs[ct::edx]);
            linesize = bv3.range(0,7);

            sizes_in_bytes_.push_back(linesize);

          }

          cacheline():
            sizes_in_bytes_()
            {


              auto brand = compass::runtime::detail::vendor( current_arch_t() );

              if(brand.find("AMD") != std::string::npos){
                on_amd();
              }

              if(brand.find("Intel") != std::string::npos){
                on_intel();
steinbac's avatar
steinbac committed
925 926 927 928 929 930
              }

            }

        public:

931 932 933 934
          static const cacheline& get(){
            static cacheline instance;
            return instance;
          }
steinbac's avatar
steinbac committed
935

936
          static std::uint32_t levels_available(ct::x86_tag){
937
            return cacheline::get().sizes_in_bytes_.size();
938
          }
steinbac's avatar
steinbac committed
939 940 941

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

942
            auto value = cacheline::get().sizes_in_bytes_.at(_lvl-1);
steinbac's avatar
steinbac committed
943

944
            return value;
steinbac's avatar
steinbac committed
945 946 947 948 949 950
          }
        };

        class cache
        {

951
          std::vector<std::uint32_t> sizes_in_bytes_;
steinbac's avatar
steinbac committed
952

953 954 955 956 957 958 959 960

          void on_intel() {

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

            for(std::uint32_t l = 0;l<maxlevel;++l)
steinbac's avatar
steinbac committed
961
            {
962 963 964 965 966 967 968
              auto regs = cpuid(0x04,0,l);

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

              if(!bv.test(1))
                continue;
steinbac's avatar
steinbac committed
969

970 971 972
              auto truelevel = bv.range(5,8);
              if(truelevel != l)
                continue;
steinbac's avatar
steinbac committed
973

974 975 976
              std::uint32_t ebx = regs[ct::ebx];
              const bitview bv_ebx = bitview(ebx);
              const std::uint32_t ecx = regs[ct::ecx];
steinbac's avatar
steinbac committed
977

978 979 980 981
              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;
steinbac's avatar
steinbac committed
982

983
              std::uint32_t value = ways*partitions*line_size*sets;
steinbac's avatar
steinbac committed
984

985 986 987 988
              sizes_in_bytes_.push_back(value);
            }

          }
steinbac's avatar
steinbac committed
989

990
          void on_amd(){
steinbac's avatar
steinbac committed
991

992
            sizes_in_bytes_.reserve(3);
steinbac's avatar
steinbac committed
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
            auto regs = cpuid(0x80000005);

            std::uint32_t ecx = regs[ct::ecx];
            auto bv = bitview(ecx);
            std::uint32_t test_linesize = bv.range(0,7);
            if(!test_linesize)
              return;

            sizes_in_bytes_.push_back(bv.range(24,31)*1024);

            auto l23regs = cpuid(0x80000006);
            ecx = l23regs[ct::ecx];
            auto bv2 = bitview(ecx);
            auto l2size = bv2.range(16,31);
            l2size &= 0xffff;

            sizes_in_bytes_.push_back(l2size*1024);

            auto bv3 = bitview(l23regs[ct::edx]);
            auto l3size = bv3.range(19,31);
            l3size *= 512*1024;
            sizes_in_bytes_.push_back(l3size);

          }


          cache():
            sizes_in_bytes_()
            {


              auto brand = compass::runtime::detail::vendor( current_arch_t() );

              if(brand.find("AMD") != std::string::npos){
                on_amd();
              }

              if(brand.find("Intel") != std::string::npos){
                on_intel();
steinbac's avatar
steinbac committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
              }

            }


        public:

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

1045
          static std::uint32_t levels_available(ct::x86_tag){
1046
            return cache::get().sizes_in_bytes_.size();
1047 1048
          }

steinbac's avatar
steinbac committed
1049 1050 1051 1052
          static std::uint32_t level(int _lvl, ct::x86_tag){

            if(_lvl <= 0){
              std::cerr << "compass::size::cache requested invalid cache level (received: "<<
1053
                _lvl << ", found on this host: [1," << cache::get().sizes_in_bytes_.size() + 1 << "]\n";
steinbac's avatar
steinbac committed
1054 1055 1056 1057 1058
              return 0;
            }

            std::uint32_t index = _lvl - 1;

1059
            if(!(index < cache::get().sizes_in_bytes_.size())){
steinbac's avatar
steinbac committed
1060
              std::cerr << "compass::size::cache requested invalid cache level (received: "<<
1061
                _lvl << ", found on this host: [1," << cache::get().sizes_in_bytes_.size() + 1 << "]\n";
steinbac's avatar
steinbac committed
1062 1063 1064
              return 0;
            }

1065
            return cache::get().sizes_in_bytes_[index];
steinbac's avatar
steinbac committed
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 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
          }
        };



      };
    };

  };

};
#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 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());

        }

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

        namespace detail {

            static std::uint32_t accumulate(std::uint32_t value){
                return value;
            }

            template < typename T, typename... features_t >
            static std::uint32_t accumulate(std::uint32_t value,
                                        T head,
                                        features_t... tail)
            {

                std::uint32_t local = compass::runtime::has(head);
                value = (value <<= 1) | local;

                return accumulate(value,tail...);

            }

        };


        template <typename... feature_t>
        static std::uint32_t accumulate(feature_t... features) {

            static const int pack_size = sizeof...(features);
            static_assert(pack_size <= 32, "[compass::runtime::accumulate] unable to handle more than 32 features" );

            std::uint32_t value = compass::runtime::detail::accumulate( 0u, features...);
            return value;

        }

steinbac's avatar
steinbac committed
1293 1294 1295 1296
        namespace size {

            struct cacheline{

1297 1298 1299 1300 1301 1302 1303
                static std::uint32_t levels_available(){

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

                }

steinbac's avatar
steinbac committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
                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{

1314 1315 1316 1317 1318 1319 1320
                static std::uint32_t levels_available(){

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

                }

steinbac's avatar
steinbac committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
                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