Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/cpu.h"
      6 #include <intrin.h>
      7 #include <string>
      8 
      9 namespace base {
     10 
     11 CPU::CPU()
     12   : type_(0),
     13     family_(0),
     14     model_(0),
     15     stepping_(0),
     16     ext_model_(0),
     17     ext_family_(0),
     18     cpu_vendor_("unknown") {
     19   Initialize();
     20 }
     21 
     22 void CPU::Initialize() {
     23   int cpu_info[4] = {-1};
     24   char cpu_string[0x20];
     25 
     26   // __cpuid with an InfoType argument of 0 returns the number of
     27   // valid Ids in CPUInfo[0] and the CPU identification string in
     28   // the other three array elements. The CPU identification string is
     29   // not in linear order. The code below arranges the information
     30   // in a human readable form.
     31   //
     32   // More info can be found here:
     33   // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
     34   __cpuid(cpu_info, 0);
     35   int num_ids = cpu_info[0];
     36   memset(cpu_string, 0, sizeof(cpu_string));
     37   *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1];
     38   *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3];
     39   *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2];
     40 
     41   // Interpret CPU feature information.
     42   if (num_ids > 0) {
     43     __cpuid(cpu_info, 1);
     44     stepping_ = cpu_info[0] & 0xf;
     45     model_ = (cpu_info[0] >> 4) & 0xf;
     46     family_ = (cpu_info[0] >> 8) & 0xf;
     47     type_ = (cpu_info[0] >> 12) & 0x3;
     48     ext_model_ = (cpu_info[0] >> 16) & 0xf;
     49     ext_family_ = (cpu_info[0] >> 20) & 0xff;
     50     cpu_vendor_ = cpu_string;
     51   }
     52 }
     53 
     54 }  // namespace base
     55